Search code examples
c#for-loopunity-game-engineshaderraymarching

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


This is my first shader so it's probably some dumb tiny error. I'm following a tutorial on YouTube: https://www.youtube.com/watch?v=S8AWd66hoCo&t=83s&pbjreload=10

I'm stuck with the distance function (for a sphere) and simply can't get it to work without some weird error going with my for loop. Everything before that worked perfectly. I rewrote the for-loop but get this error over and over.

Thank you in advance!

(Unity 2019.2.19f1)

ERRORS:

+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)

CODE:

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
        }
    }
}

Solution

  • Remove the = signes from your defines.

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

    should be

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

    And one more maxDist instead of MAX_DIST, and it compiles.

    ( I have that = in my defines also sometimes. :-) )