Search Unity

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

Discussion in 'Shaders' started by falldeaf_unity, Jul 21, 2017.

  1. falldeaf_unity

    falldeaf_unity

    Joined:
    Jul 27, 2016
    Posts:
    35
    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! o_O

    Code (CSharp):
    1. Shader "Unlit/Triplanar"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Tiling ("Tiling", Float) = 1.0
    7.         _offsetX ("offset_x", Float) = 1.0
    8.         _offsetY ("offset_y", Float) = 1.0
    9.         _debugRows ("debug row", Int) = 20
    10.         _debugColumns ("debug col", Int) = 20
    11.     }
    12.     SubShader
    13.     {
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct v2f
    22.             {
    23.                 half3 objNormal : TEXCOORD0;
    24.                 float3 coords : TEXCOORD1;
    25.                 float2 uv : TEXCOORD2;
    26.                 float4 pos : SV_POSITION;
    27.             };
    28.  
    29.             float _Tiling;
    30.             float _offsetX;
    31.             float _offsetY;
    32.             float _debugRows;
    33.             float _debugColumns;
    34.  
    35.             v2f vert (float4 pos : POSITION, float3 normal : NORMAL, float2 uv : TEXCOORD0)
    36.             {
    37.                 v2f o;
    38.                 o.pos = UnityObjectToClipPos(pos);
    39.                 o.coords = pos.xyz * _Tiling;
    40.                 o.objNormal = normal;
    41.                 o.uv = uv;
    42.                 return o;
    43.             }
    44.  
    45.             sampler2D _MainTex;
    46.             sampler2D _OcclusionMap;
    47.  
    48.             float4 circle(float2 uv, float2 pos, float rad, float4 ccol) {
    49.                 float d = length(pos - uv) - rad;
    50.                 float t = clamp(d, 0.0, 1.0);
    51.                 if(t == 1.0) return float4(1.0, 1.0, 1.0, 1.0);
    52.                 //return float4(1.0, 0.0, 0.0, 1.0);
    53.                 return ccol;
    54.             }
    55.  
    56.             fixed4 frag (v2f i) : SV_Target
    57.             {
    58.                 float2 uv = i.coords.xz + -85.;
    59.  
    60.                 //float2 uv2 = i.coords.yx + -85.;
    61.  
    62.                 float WHATTHEHELL= 2;
    63.                 float spacing = 8. + 2.;
    64.  
    65.                 uv.x += _offsetX;
    66.                 uv.y += _offsetY;
    67.  
    68.                 float4 col = float4(1.0, 1.0, 1.0, 1.0);
    69.                 float4 ccol = float4(1.0, 1.0, 1.0, 1.0);
    70.                 for(int n = 0; n<_debugRows; n++) {
    71.                   for(int k = 0; k<_debugColumns; k++) {
    72.                     ccol = float4(1.0, 0.0, 0.0, 1.0);
    73.                     if(k==0) ccol = float4(0., 1., 0., 1.);
    74.                     if(n==0) ccol = float4(0., 0., 1., 1.);
    75.                     col = col * circle(uv, float2( (float(k)*spacing)-100., (float(n)*spacing)-100. ), 3., ccol);
    76.                   }
    77.                 }
    78.                 //col = col * circle(uv, float2( 30., 30. ), 44.);
    79.  
    80.                   float4 fragColor = col;
    81.  
    82.                 // use absolute value of normal as texture weights
    83.                 half3 blend = 1.;//abs(i.objNormal);
    84.                 // make sure the weights sum up to 1 (divide by sum of x+y+z)
    85.                 blend /= dot(blend,1.);
    86.                 // read the three texture projections, for x,y,z axes
    87.  
    88.                                 //fixed4 cx = tex2D(_OcclusionMap, i.coords.yz);
    89.                 fixed4 cx =  fragColor;
    90.                 //fixed4 cy = tex2D(_OcclusionMap, i.coords.yz);
    91.                 //fixed4 cz = tex2D(_OcclusionMap, i.coords.yz);
    92.                 // blend the textures based on weights
    93.                 fixed4 c = (cx * blend.x + cx * blend.y + cx * blend.z);
    94.  
    95.  
    96.  
    97.                                 /*fixed4 cx = tex2D(_MainTex, i.coords.yz);
    98.                 fixed4 cy = tex2D(_MainTex, i.coords.xz);
    99.                 fixed4 cz = tex2D(_MainTex, i.coords.xy);
    100.                 // blend the textures based on weights
    101.                 fixed4 c = cx * blend.x + cy * blend.y + cz * blend.z;*/
    102.                 // modulate by regular occlusion map
    103.                 c *= tex2D(_MainTex, i.uv);
    104.                 return c;
    105.             }
    106.             ENDCG
    107.         }
    108.     }
    109. }
    110.  
     
  2. falldeaf_unity

    falldeaf_unity

    Joined:
    Jul 27, 2016
    Posts:
    35
    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.
     
    neoshaman likes this.