所以我试图为我的统一游戏制作一个计算着色器,但我不断收到这个奇怪的错误:
Shader error in 'PathTracing': undeclared identifier 'intersectionPos' at kernel CSMain at PathTracing.compute(76) (on d3d11)
我正在使用光标编辑器进行编码。它与 Visual Studio 代码相同,但集成了 GPT-4。
这是着色器:
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;
float4x4 _CameraToWorld;
float4x4 _CameraInverseProjection;
struct Ray
{
float3 origin;
float3 direction;
};
Ray CreateRay(float3 origin, float3 direction)
{
Ray ray;
ray.origin = origin;
ray.direction = direction;
return ray;
}
struct Triangle {
float3 v0;
float3 v1;
float3 v2;
};
Triangle CreateTriangle(float3 Vert1, float3 Vert2, float3 Vert3)
{
Triangle triangle;
triangle.v0 = Vert1;
triangle.v1 = Vert2;
triangle.v2 = Vert3;
return triangle;
}
bool IntersectRayTriangle(Ray ray, Triangle triangle, out float3 intersectionPos) {
// Compute vectors for two edges of the triangle
float3 edge1 = triangle.v1 - triangle.v0;
float3 edge2 = triangle.v2 - triangle.v0;
// Compute determinant to check for back-face culling
float3 pvec = cross(ray.direction, edge2);
float det = dot(edge1, pvec);
// If determinant is near zero, the ray and triangle are parallel
if (abs(det) < 0.00001f)
return false;
float invDet = 1.0f / det;
// Compute distance from triangle origin to ray origin
float3 tvec = ray.origin - triangle.v0;
// Compute u parameter and test bounds
float u = dot(tvec, pvec) * invDet;
if (u < 0.0f || u > 1.0f)
return false;
// Compute v parameter and test bounds
float3 qvec = cross(tvec, edge1);
float v = dot(ray.direction, qvec) * invDet;
if (v < 0.0f || u + v > 1.0f)
return false;
// Compute distance along ray to intersection point
float t = dot(edge2, qvec) * invDet;
// Ensure intersection is in front of ray origin
if (t > 0.00001f) {
intersectionPos = ray.origin + ray.direction * t;
return true;
}
return false;
}
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
uint width, height;
Result.GetDimensions(width, height);
float2 uv = float2((id.xy + float2(0.5f, 0.5f)) / float2(width, height) * 0.2f - 1.0f);
float3 RayOrigin = float3(0.0f, 2.5f, -2.0f); //mul(_CameraToWorld, float4(0, 0, 0, 1)).xyz;
float3 RayDir = normalize(float3(0.0f, 2.5f, 20.0f) - RayOrigin); //mul(_CameraInverseProjection, float4(uv, 0, 1)).xyz;
Ray ray = CreateRay(RayOrigin, RayDir);
Triangle triangle = CreateTriangle(float3(-2.0f, 1.0f, 3.0f), float3(2.0f, 1.0f, 3.0f), float3(0.0f, 4.0f, 3.0f));
float4 Col = float4(0.0f, 0.0f, 0.0f, 0.0f);
bool RayHit = IntersectRayTriangle(ray, triangle, out float3 RayHitPos);
if(RayHit == true)
{
Col = float4(1.0f, 1.0f, 1.0f, 0.0f);
}
Result[id.xy] = Col;
//Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0);
}
我在其他网站上搜索了解决方案,但似乎大多数人不使用HLSL。我向 GPT-4 询问了这个问题,没有发现任何错误。
所以我尝试将“三角形”重命名为“网格三角形”,它成功了。看来“triangle”这个词是HLSL中的保留关键字。您可以在这里找到它们:
https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-appendix-keywords