0
\$\begingroup\$

I am trying to write a shader for Unity that replicates Splatoon's painting system, but I cannot get the shader to compile. It has an error on line 41 stating invalid subscript 'worldPos' at line 41 (on d3d11). I'm unsure what this means, and my research came up short. I tried a few different things but nothing worked. I switched my shader to unlit which solved another problem with the v2f not being recognized, but now there's this problem. Below is the code.

Shader "Unlit/TexturePaintingMechanicShader2"
{
    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;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

                v2f vert(appdata v)
            {
            v2f o;
            o.worldPos = mul(unity_ObjectToWorld,v.vertex);
            o.uv = v.uv;
            float4 uv = float(0,0,0,,1);
            uv.xy = (v.uv.xy * 2-1) * float2(1,_ProjectionParams.x);
            o.vertex = uv;
            return o;
             }

        float mask(float3 position, float3 center, float radius, float hardness)
        {
            float m = distance(center,position);
            return 1- smoothstep(radius * hardness,radius,m);
        }

        fixed4 frag(v2f i) : SV_Target{
            float m= mask(i.worldPos, _PainterPosition,_Radius, _Hardness);
            float edge = m * _Strength;
            return lerp(float4(0,0,0,0), float4(1,0,0,1),edge);
        }

            ENDCG
        }
    }
}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

The error you are getting is that the variable worldPos is not defined in your v2f struct. You will need to include it inside the struct if you want to use it.

I can't say for sure but since your v.vertex is a float4, your type of worldPos should be float4.

\$\endgroup\$
1
  • \$\begingroup\$ We can generally store a world position as a float3, since it doesn't need any further transformations that rely on w. But due to padding, you don't necessarily gain anything from this unless you have a single float to pack in the space. \$\endgroup\$
    – DMGregory
    Commented Jun 23 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .