Insane bug, texture is black unless there's a useless variable declaration!?

I’m creating a dynamic tri-planar texture overlay on an unlit texture. Everythings going great except that without the following line:
float WHATTHEHELL= 2;

The texture goes all black! The variable type can be any type, the variable name doesn’t matter (it’s never used!), but the number at the end has to be greater than 1.0.?! And I’ve moved it all throughout the function and it still works. But when I comment it out or remove it. Black texture. I’m at a complete loss.

This one definitely feels like I’m losing my mind! :eyes:

Shader "Unlit/Triplanar"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Tiling ("Tiling", Float) = 1.0
        _offsetX ("offset_x", Float) = 1.0
        _offsetY ("offset_y", Float) = 1.0
        _debugRows ("debug row", Int) = 20
        _debugColumns ("debug col", Int) = 20
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct v2f
            {
                half3 objNormal : TEXCOORD0;
                float3 coords : TEXCOORD1;
                float2 uv : TEXCOORD2;
                float4 pos : SV_POSITION;
            };

            float _Tiling;
            float _offsetX;
            float _offsetY;
            float _debugRows;
            float _debugColumns;

            v2f vert (float4 pos : POSITION, float3 normal : NORMAL, float2 uv : TEXCOORD0)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(pos);
                o.coords = pos.xyz * _Tiling;
                o.objNormal = normal;
                o.uv = uv;
                return o;
            }

            sampler2D _MainTex;
            sampler2D _OcclusionMap;

            float4 circle(float2 uv, float2 pos, float rad, float4 ccol) {
                float d = length(pos - uv) - rad;
                float t = clamp(d, 0.0, 1.0);
                if(t == 1.0) return float4(1.0, 1.0, 1.0, 1.0);
                //return float4(1.0, 0.0, 0.0, 1.0);
                return ccol;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float2 uv = i.coords.xz + -85.;

                //float2 uv2 = i.coords.yx + -85.;

                float WHATTHEHELL= 2;
                float spacing = 8. + 2.;

                uv.x += _offsetX;
                uv.y += _offsetY;

                float4 col = float4(1.0, 1.0, 1.0, 1.0);
                float4 ccol = float4(1.0, 1.0, 1.0, 1.0);
                for(int n = 0; n<_debugRows; n++) {
                  for(int k = 0; k<_debugColumns; k++) {
                    ccol = float4(1.0, 0.0, 0.0, 1.0);
                    if(k==0) ccol = float4(0., 1., 0., 1.);
                    if(n==0) ccol = float4(0., 0., 1., 1.);
                    col = col * circle(uv, float2( (float(k)*spacing)-100., (float(n)*spacing)-100. ), 3., ccol);
                  }
                }
                //col = col * circle(uv, float2( 30., 30. ), 44.);

                  float4 fragColor = col;

                // use absolute value of normal as texture weights
                half3 blend = 1.;//abs(i.objNormal);
                // make sure the weights sum up to 1 (divide by sum of x+y+z)
                blend /= dot(blend,1.);
                // read the three texture projections, for x,y,z axes

                                //fixed4 cx = tex2D(_OcclusionMap, i.coords.yz);
                fixed4 cx =  fragColor;
                //fixed4 cy = tex2D(_OcclusionMap, i.coords.yz);
                //fixed4 cz = tex2D(_OcclusionMap, i.coords.yz);
                // blend the textures based on weights
                fixed4 c = (cx * blend.x + cx * blend.y + cx * blend.z);



                                /*fixed4 cx = tex2D(_MainTex, i.coords.yz);
                fixed4 cy = tex2D(_MainTex, i.coords.xz);
                fixed4 cz = tex2D(_MainTex, i.coords.xy);
                // blend the textures based on weights
                fixed4 c = cx * blend.x + cy * blend.y + cz * blend.z;*/
                // modulate by regular occlusion map
                c *= tex2D(_MainTex, i.uv);
                return c;
            }
            ENDCG
        }
    }
}

Update, I’ve fixed the issue. I changed the line:
col = col * circle(uv, float2( (float(k)*spacing)-100., (float(n)*spacing)-100. ), 3., ccol);
to
col *= circle(uv, float2( (float(k)*spacing)-100., (float(n)*spacing)-100. ), 3., ccol);

I have no idea why this fixed it, though. I narrowed down the issue to the circle algorithm returning all black, so just tried some different things.

1 Like