【问题标题】:Unity shader: syntax error: unexpected token '(' at line 70 (on d3d11) help!!!! shader noobUnity 着色器:语法错误:第 70 行的意外标记“(”(在 d3d11 上)帮助!!!着色器新手
【发布时间】:2020-03-12 14:22:55
【问题描述】:

第 82 行未声明的标识符“sphereHit”(在 d3d11 上)

(shader noob sorry)我需要一些帮助来解决这些错误,谢谢...

Shader "Unlit/VolSDFOne"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}

    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100



        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

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

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

                float4 pos : SV_POSITION; // Clip space
                float3 wPos : TEXCOORD1; // World position
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.wPos = mul(unity_ObjectToWorld, v.vertex).xyz; 
                return o;
            }

            float3 _Centre;
            float _Radius;

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;

                float3 worldPosition = i.wPos;
                float3 viewDirection = normalize(i.wPos - _WorldSpaceCameraPos);


                bool sphereHit (float3 p)
                {
                    return distance(p,_Centre) < _Radius;
                }

                #define STEP_SIZE 0.01
                #define STEPS 35

                bool raymarchHit (float3 position, float3 direction)
                {
                    for (int i = 0; i < STEPS; i++)
                    {
                        if ( sphereHit(position) )
                            return true;
                            position += direction * STEP_SIZE;
                    }

                    return false;
                }

                if ( raymarchHit(worldPosition, viewDirection) )
                    return fixed4(1,0,0,1); // Red if hit the ball
                else
                    return fixed4(1,1,1,1); // White otherwise
            }
            ENDCG
        }
    }
}

【问题讨论】:

  • 尝试在你的问题中多加一点努力。不要在标题中添加不必要的信息,例如您的经历或寻求帮助的信息,也不要在您的问题中填写很多 a 以达到问题的最少字符数。您本可以利用该空间告诉我们您到目前为止所做的尝试。
  • 从例如开始至少告诉我们哪一行是 82 .. 没有人想数他们...
  • 对不起,我很沮丧

标签: unity3d shader fragment-shader


【解决方案1】:

您正在线性执行的着色器中定义函数。将函数定义移到 fixed4 frag 函数的范围之外,它应该可以工作。

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



    Pass
    {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        // make fog work
        #pragma multi_compile_fog

        #include "UnityCG.cginc"

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

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

            float4 pos : SV_POSITION; // Clip space
            float3 wPos : TEXCOORD1; // World position
        };

        sampler2D _MainTex;
        float4 _MainTex_ST;

        v2f vert (appdata v)
        {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            o.wPos = mul(unity_ObjectToWorld, v.vertex).xyz; 
            return o;
        }

        float3 _Centre;
        float _Radius;

        float3 worldPosition = i.wPos;
        float3 viewDirection = normalize(i.wPos - _WorldSpaceCameraPos);


        bool sphereHit (float3 p)
        {
            return distance(p,_Centre) < _Radius;
        }

        #define STEP_SIZE 0.01
        #define STEPS 35

        bool raymarchHit (float3 position, float3 direction)
        {
            for (int i = 0; i < STEPS; i++)
            {
                if ( sphereHit(position) )
                    return true;
                    position += direction * STEP_SIZE;
            }
            return false;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            // sample the texture
            fixed4 col = tex2D(_MainTex, i.uv);
            // apply fog
            UNITY_APPLY_FOG(i.fogCoord, col);
            //return col;

            if ( raymarchHit(worldPosition, viewDirection) )
                return fixed4(1,0,0,1); // Red if hit the ball
            else
                return fixed4(1,1,1,1); // White otherwise
        }
        ENDCG
    }
}
}

现在这段代码可能仍然有一些错误,但应该可以帮助您朝着正确的方向前进

【讨论】:

  • 非常感谢,现在我才知道...解析错误:语法错误,第 88 行出现意外 $end 你知道是什么原因造成的吗?
  • 这可能是您根本不需要 ENDCG 行,但我不确定。尝试将其移除或将其降低 1 } 以检查其是否在正确的范围内
  • 如果您这样做,请编辑我的答案以反映是什么使它起作用。这样其他可能偶然发现这个问题的人也可以尝试使用答案。如果它不起作用,请给我一些关于给出错误的行的信息,这样我就不必将所有内容复制粘贴到编辑器中来帮助计算行数。 @derHugo 所说的:没有人喜欢计算线条并猜测上面是否有任何白线导致计数出现偏差
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多