Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Meshes Invisible in Build

Discussion in 'Shaders' started by Foam, Aug 16, 2013.

  1. Foam

    Foam

    Joined:
    Jun 13, 2012
    Posts:
    322
    Hello.

    I have a custom shader that I wrote that works fine in the Editor. When I make a build for my platform (OSX) all of the materials with that shader are invisible. The material is being assigned dynamically but the material and shader are in the proper Resources folder. I have also checked the Quality settings and those in the build match what I have in the editor.

    Any suggestions how to fix this? Basically it is vertex colors + shadows + a single directional light color (commented out for now).

    Code (csharp):
    1. Shader "Custom/VertexColorPlusDiffusePlusShadows" {
    2.     Properties {
    3.     _Color ("Diffuse Color", Color) = (1.0, 1.0, 1.0, 1.0)
    4.     _MainTexture ("Texture (RGB)", 2D) = "white" {}
    5.     _UseTexture ("Use Texture?", float) = 1
    6.     }
    7.     SubShader {
    8.     Tags { "RenderType" = "Opaque" }
    9.     Pass {
    10.         Tags { "LightMode" = "ForwardBase" }
    11.         ZWrite On
    12.  
    13.         CGPROGRAM
    14.         #pragma vertex vert
    15.         #pragma fragment frag
    16.         //#pragma multi_compile_fwdbase
    17.             #pragma multi_compile_fwdadd_fullshadows
    18.         //#pragma fragmentoption ARB_precision_hint_fastest
    19.  
    20.  
    21.         #include "UnityCG.cginc"
    22.         #include "Lighting.cginc"
    23.         #include "AutoLight.cginc"
    24.         //#include <cstdlib>
    25.  
    26.         uniform float4 _Color;
    27.         uniform sampler2D _MainTexture;
    28.         uniform float _UseTexture;
    29.  
    30.         struct vertex_input {
    31.         float4 vertex : POSITION;
    32.         float4 color : COLOR;
    33.         float3 normal : NORMAL;
    34.         float2 uv : TEXCOORD0;
    35.         };
    36.  
    37.         struct vertex_output {
    38.         float4 pos : POSITION;
    39.         float4 color : COLOR;
    40.         float2 uv : TEXCOORD0;
    41.         //float4 _ShadowCoord : TEXCOORD3;
    42.         LIGHTING_COORDS(3, 4)
    43.         };
    44.  
    45.         struct fragment_output {
    46.         float4 color : COLOR;
    47.         };
    48.  
    49.         vertex_output vert(vertex_input v) {
    50.         vertex_output o;
    51.         // convert the local position to world position
    52.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    53.  
    54.         float4x4 model_matrix = _Object2World;
    55.         float4x4 model_matrix_inverse = _World2Object;
    56.         // calculate the diffuse lighting
    57.         float3 normal_dir = normalize(float3(mul(float4(v.normal, 0.0), model_matrix_inverse)));
    58.         float3 light_dir = normalize(float3(_WorldSpaceLightPos0));
    59.         float3 diffuse_reflection = float3(_LightColor0) * float3(_Color) * max(0.0, dot(normal_dir, light_dir));
    60.  
    61.         // combine the diffuse lighting from the light source
    62.                 // with the vertex color passed in by the mesh generator
    63.         /* OUT.color = (IN.color + float4(diffuse_reflection, 1.0)) * 0.5; */
    64.  
    65.         /* o.color = lerp(tex2D(_MainTexture, float2(v.uv)).rgba, float4(diffuse_reflection, 1.0), 0.5); */
    66.  
    67.         /* o.color = lerp(v.color, float4(diffuse_reflection, 1.0), 0.5); */
    68.         o.color = v.color;
    69.            
    70.  
    71.         o.uv = v.uv;
    72.         TRANSFER_VERTEX_TO_FRAGMENT(o);
    73.  
    74.         return o;
    75.         };
    76.  
    77.  
    78.         fragment_output frag(vertex_output IN) {
    79.         fragment_output OUT;
    80.         float atten = LIGHT_ATTENUATION(IN);
    81.         if (_UseTexture > 0) {
    82.             float4 tex_color = tex2D(_MainTexture, IN.uv);
    83.             OUT.color = IN.color * atten * tex_color;
    84.         } else {
    85.  
    86.             OUT.color = IN.color * atten;
    87.         }
    88.  
    89.  
    90.  
    91.         return OUT;
    92.         };
    93.         ENDCG
    94.  
    95.     }
    96.     }
    97.  
    98.     Fallback "Diffuse"//, 2
    99. }
    100.  
     
  2. Foam

    Foam

    Joined:
    Jun 13, 2012
    Posts:
    322
    Anyone? In general how would this happen? It doesn't have to be specific to this shader, I don't think?

    All I can find online is Resources and Quality settings.
     
  3. Justafin

    Justafin

    Joined:
    Aug 6, 2012
    Posts:
    30
    Are the meshes which are disappearing set to batch static? If so this may be the problem since Unity can't apply vertex colors once the meshes are combined.
     
  4. Foam

    Foam

    Joined:
    Jun 13, 2012
    Posts:
    322
    Nope, I tried with static batch disabled and that does nothing. The meshes are procedurally generated anyway. I also fiddled with most of the settings (dynamic batching, shadow quality, rendering path, etc.) and haven't had any luck yet either.
     
    Last edited: Aug 18, 2013
  5. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    Try if the meshes disappear even with another shader. If not, try to pinpoint which part exactly causes problems... I'd start with "#pragma multi_compile_fwdadd_fullshadows" and the branching at the end.
     
  6. Foam

    Foam

    Joined:
    Jun 13, 2012
    Posts:
    322
    I'm sorry, this is really embarrassing, but, it looks like the issue was unrelated to the shader. Had a dumbass bug elsewhere related to using a StreamReader to access a path that was no longer correct in the build. Didn't notice it until I actually ran a full debug build with all the bells and whistles. :(

    I had some debug output before that told me that all the meshes were generated properly but the debuging output itself was bugged, which is why I thought the issue was the shader.
     
    Last edited: Aug 18, 2013