为什么我在 Unity RayMarching 着色器中出现 for-loop 错误?

Why am I getting a for-loop error in Unity RayMarching shader?

这是我的第一个着色器,所以它可能是一些愚蠢的小错误。 我正在关注 YouTube 上的教程: https://www.youtube.com/watch?v=S8AWd66hoCo&t=83s&pbjreload=10

我被距离函数(对于一个球体)困住了,如果我的 for 循环没有出现一些奇怪的错误,我就无法让它工作。之前的一切都完美无缺。我重写了 for 循环,但一遍又一遍地出现此错误。

提前致谢!

(Unity 2019.2.19f1)

错误:

+Shader error in 'Unlit/RayMarchOne': syntax error: unexpected token ')' at line 58 (on d3d11)
+Shader error in 'Unlit/RayMarchOne': syntax error: unexpected token '=' at line 58 (on d3d11)
+Shader error in 'Unlit/RayMarchOne': 'GetDist': no matching 1 parameter function at line 60 (on d3d11)
+Shader error in 'Unlit/RayMarchOne': undeclared identifier 'p' at line 60 (on d3d11)

代码:

Shader "Unlit/RayMarchOne"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            #define MAX_STEPS = 100
            #define MIN_DIST = .001
            #define MAX_DIST = 100.

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            float GetDist(float3 p)
            {
                float d = length(p) - .5;
                return d;
            }

            float RayMarch(float3 ro, float3 rd)
            {
                float dO = 0;
                float dS;
                for (int i = 0; i < MAX_STEPS; i++) {
                    float3 p = ro + dO * rd;
                    dS = GetDist(p);
                    dO += dS;
                    if (dS<MIN_DIST || dO>MAX_DIST) break;
                }

                return dO;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float2 uv = i.uv-.5;
                float3 ro = float3(0,0,-3);
                float3 rd = normalize(float3(uv.x, uv.y, 1));

                float d = RayMarch(ro, rd);
                fixed4 col = 0;

                if (d < maxDist)
                {
                    col.r = 1;
                }
                return col;
            }
            ENDCG
        }
    }
}

从您的定义中删除 = 符号。

#define MAX_STEPS = 100
#define MIN_DIST = .001
#define MAX_DIST = 100.

应该是

#define MAX_STEPS 100
#define MIN_DIST .001
#define MAX_DIST 100.

还有一个 maxDist 而不是 MAX_DIST,它可以编译。

(有时我的定义中也有 =。:-) )