Search Unity

Error Variable 'o' used without having been completely initialized

Discussion in 'Shaders' started by Isaac15, Jun 21, 2017.

  1. Isaac15

    Isaac15

    Joined:
    Sep 12, 2014
    Posts:
    2
    Hi everyone, I'm working with a low poly water shader but it gives me an error that I dont understand and I dont know how to fix it. The code is:

    Code (CSharp):
    1.  
    2. Shader "FX/FlatShadedWaterWithEdgeBlend" {
    3. Properties {
    4.  
    5.     _BaseColor ("Base color", COLOR)  = ( .54, .95, .99, 0.5)
    6.     _SpecColor ("Specular Material Color", Color) = (1,1,1,1)
    7.     _Shininess ("Shininess", Float) = 10
    8.     _ShoreTex ("Shore & Foam texture ", 2D) = "black" {}
    9.      
    10.     _InvFadeParemeter ("Auto blend parameter (Edge, Shore, Distance scale)", Vector) = (0.2 ,0.39, 0.5, 1.0)
    11.  
    12.     _BumpTiling ("Foam Tiling", Vector) = (1.0 ,1.0, -2.0, 3.0)
    13.     _BumpDirection ("Foam movement", Vector) = (1.0 ,1.0, -1.0, 1.0)
    14.  
    15.     _Foam ("Foam (intensity, cutoff)", Vector) = (0.1, 0.375, 0.0, 0.0)
    16.     [MaterialToggle] _isInnerAlphaBlendOrColor("Fade inner to color or alpha?", Float) = 0
    17. }
    18.  
    19.  
    20. CGINCLUDE
    21.  
    22.  
    23.     #include "UnityCG.cginc"
    24.     #include "UnityLightingCommon.cginc" // for _LightColor0
    25.  
    26.  
    27.     sampler2D _ShoreTex;
    28.     sampler2D_float _CameraDepthTexture;
    29.  
    30.     uniform float4 _BaseColor;  
    31.     uniform float _Shininess;
    32.      
    33.     uniform float4 _InvFadeParemeter;
    34.    
    35.     uniform float4 _BumpTiling;
    36.     uniform float4 _BumpDirection;
    37.  
    38.     uniform float4 _Foam;
    39.       float _isInnerAlphaBlendOrColor;
    40.     #define VERTEX_WORLD_NORMAL i.normalInterpolator.xyz
    41.  
    42.  
    43.     struct appdata
    44.     {
    45.         float4 vertex : POSITION;
    46.         float3 normal : NORMAL;
    47.     };
    48.  
    49.    
    50.     struct v2f
    51.     {
    52.         float4 pos : SV_POSITION;
    53.         float4 normalInterpolator : TEXCOORD0;
    54.         float4 viewInterpolator : TEXCOORD1;
    55.         float4 bumpCoords : TEXCOORD2;
    56.         float4 screenPos : TEXCOORD3;
    57.         float4 grabPassPos : TEXCOORD4;
    58.         half3 worldRefl : TEXCOORD6;
    59.         float4 posWorld : TEXCOORD7;
    60.         float3 normalDir : TEXCOORD8;
    61.  
    62.         UNITY_FOG_COORDS(5)
    63.     };
    64.  
    65.  
    66.  
    67.     inline half4 Foam(sampler2D shoreTex, half4 coords)
    68.     {
    69.         half4 foam = (tex2D(shoreTex, coords.xy) * tex2D(shoreTex,coords.zw)) - 0.125;
    70.         return foam;
    71.     }
    72.  
    73.     v2f vert(appdata_full v)
    74.     {
    75.         v2f o;
    76.        
    77.         half3 worldSpaceVertex = mul(unity_ObjectToWorld,(v.vertex)).xyz;
    78.         half3 vtxForAni = (worldSpaceVertex).xzz;
    79.  
    80.         half3    offsets = half3(0,0,0);
    81.         half3    nrml = half3(0,1,0);
    82.        
    83.         v.vertex.xyz += offsets;
    84.          
    85.         half2 tileableUv = mul(unity_ObjectToWorld,(v.vertex)).xz;
    86.        
    87.         o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
    88.  
    89.         o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
    90.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    91.         o.screenPos=ComputeScreenPos(o.pos);
    92.         o.normalInterpolator.xyz = nrml;
    93.         o.viewInterpolator.w = saturate(offsets.y);
    94.         o.normalInterpolator.w = 1;
    95.        
    96.         UNITY_TRANSFER_FOG(o,o.pos);
    97.         half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    98.           float4x4 modelMatrix = unity_ObjectToWorld;
    99.         float4x4 modelMatrixInverse = unity_WorldToObject;
    100.         o.posWorld = mul(modelMatrix, v.vertex);
    101.         o.normalDir = normalize( mul(float4(v.normal, 0.0), modelMatrixInverse).xyz);
    102.  
    103.         float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    104.         float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
    105.         o.worldRefl = reflect(-worldViewDir, worldNormal);
    106.  
    107.         return o;
    108.     }
    109.  
    110.  
    111.     half4 calculateBaseColor(v2f input)  
    112.          {
    113.             float3 normalDirection = normalize(input.normalDir);
    114.  
    115.             float3 viewDirection = normalize(
    116.                _WorldSpaceCameraPos - input.posWorld.xyz);
    117.             float3 lightDirection;
    118.             float attenuation;
    119.  
    120.             if (0.0 == _WorldSpaceLightPos0.w) // directional light?
    121.             {
    122.                attenuation = 1.0; // no attenuation
    123.                lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    124.             }
    125.             else // point or spot light
    126.             {
    127.                float3 vertexToLightSource =
    128.                   _WorldSpaceLightPos0.xyz - input.posWorld.xyz;
    129.                float distance = length(vertexToLightSource);
    130.                attenuation = 1.0 / distance; // linear attenuation
    131.                lightDirection = normalize(vertexToLightSource);
    132.             }
    133.  
    134.             float3 ambientLighting =
    135.                UNITY_LIGHTMODEL_AMBIENT.rgb * _BaseColor.rgb;
    136.  
    137.             float3 diffuseReflection =
    138.                attenuation * _LightColor0.rgb * _BaseColor.rgb
    139.                * max(0.0, dot(normalDirection, lightDirection));
    140.  
    141.             float3 specularReflection;
    142.             if (dot(normalDirection, lightDirection) < 0.0)
    143.                // light source on the wrong side?
    144.             {
    145.                specularReflection = float3(0.0, 0.0, 0.0);
    146.                   // no specular reflection
    147.             }
    148.             else  
    149.             {
    150.                specularReflection = attenuation * _LightColor0.rgb  * _SpecColor.rgb * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), _Shininess);
    151.             }
    152.  
    153.             return half4(ambientLighting + diffuseReflection  + specularReflection, 1.0);
    154.          }
    155.  
    156.     half4 frag( v2f i ) : SV_Target
    157.     {
    158.  
    159.         half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
    160.        
    161.         #ifdef WATER_EDGEBLEND_ON
    162.             half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
    163.             depth = LinearEyeDepth(depth);
    164.             edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.w));
    165.             edgeBlendFactors.y = 1.0-edgeBlendFactors.y;
    166.         #endif
    167.        
    168.  
    169.         half4 baseColor = calculateBaseColor(i);
    170.        
    171.  
    172.         half4 foam = Foam(_ShoreTex, i.bumpCoords * 2.0);
    173.         baseColor.rgb += foam.rgb * _Foam.x * (edgeBlendFactors.y + saturate(i.viewInterpolator.w - _Foam.y));
    174.         if( _isInnerAlphaBlendOrColor==0)
    175.             baseColor.rgb += 1.0-edgeBlendFactors.x;
    176.         if(  _isInnerAlphaBlendOrColor==1.0)
    177.             baseColor.a  =  edgeBlendFactors.x;
    178.         UNITY_APPLY_FOG(i.fogCoord, baseColor);
    179.         return baseColor;
    180.     }
    181.    
    182. ENDCG
    183.  
    184. Subshader
    185. {
    186.     Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    187.    
    188.     Lod 500
    189.     ColorMask RGB
    190.    
    191.     GrabPass { "_RefractionTex" }
    192.    
    193.     Pass {
    194.             Blend SrcAlpha OneMinusSrcAlpha
    195.             ZTest LEqual
    196.             ZWrite Off
    197.             Cull Off
    198.        
    199.             CGPROGRAM
    200.        
    201.             #pragma target 3.0
    202.        
    203.             #pragma vertex vert
    204.             #pragma fragment frag
    205.             #pragma multi_compile_fog
    206.        
    207.             #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
    208.        
    209.             ENDCG
    210.     }
    211. }
    212.  
    213.  
    214. Fallback "Transparent/Diffuse"
    215. }
    216. }
    I work with version 5.5.1 and the error it gives me is: variable 'o' used without having been completely initialized at line 102 (on d3d9). I hope you can help me.

    Thank you very much
     
    Last edited: Jun 21, 2017
  2. Isaac15

    Isaac15

    Joined:
    Sep 12, 2014
    Posts:
    2
    It's on line 108 when have the proble, not on 102 but it will not let me edit it, sorry for the confusion
     
  3. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    That means your vert program isn't returning everything in your v2f struct.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You never set grabPassPos.