Search Unity

Web player shaders not working!

Discussion in 'Shaders' started by hyp78, Jan 26, 2010.

  1. hyp78

    hyp78

    Joined:
    Jan 26, 2010
    Posts:
    10
    Hi all,

    Long time reader, first time poster.

    I hacked together a simple falloff shader using the shield example from the wiki. Basically creates a halo behind/around an object.

    Code (csharp):
    1.  
    2. Shader "HaloShader"
    3. {
    4.     Properties
    5.     {
    6.         _Color ("Tint (RGB)", Color) = (1,1,1,1)
    7.     }
    8.     SubShader
    9.     {
    10.         ZWrite Off
    11.         Tags { "Queue" = "Transparent" }
    12.        
    13.         Pass
    14.         {
    15.             Cull Front
    16.             Blend One One
    17.            
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #pragma fragmentoption ARB_fog_exp2
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct v2f
    25.             {
    26.                 V2F_POS_FOG;
    27.                 float2 uv : TEXCOORD0;
    28.                 float2 uv2 : TEXCOORD1;
    29.                 float3 normal : TEXCOORD2;
    30.             };
    31.            
    32.             uniform float _Offset;
    33.            
    34.             v2f vert (appdata_base v)
    35.             {
    36.                 v2f o;
    37.                 PositionFog( v.vertex, o.pos, o.fog );
    38.                
    39.                 float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    40.                 o.uv = TRANSFORM_UV (0);
    41.                 o.uv2 = float2( abs (dot (viewDir, v.normal)), 0.5);
    42.                 o.normal = v.normal;
    43.                 return o;
    44.             }
    45.            
    46.             uniform float4 _Color;
    47.  
    48.             half4 frag (v2f f) : COLOR
    49.             {
    50.                 float ramp = f.uv2.x * _Color.a;
    51.                 half4 outColour = ramp * _Color;
    52.                
    53.                 return half4 (outColour.r, outColour.g, outColour.b, ramp.r);
    54.             }
    55.            
    56.             ENDCG
    57.  
    58.         }
    59.     }
    60.    
    61.     Fallback "Transparent/Diffuse"
    62. }
    63.  
    Simple enough. Works perfectly in the editor however when I build to webplayer, all I get is opaque black! Any suggestions as to what I'm doing wrong?

    Many thanks :)
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    Just to check... you are testing the webplayer on the same computer, right?
     
  3. hyp78

    hyp78

    Joined:
    Jan 26, 2010
    Posts:
    10
    Aye, same machine. Couldnt find anything in docs or forums about similar quirks or issues with webplayer. The shader compiles to 1.x and 2.0 also so hardly taxing on the card or system. None of the standard shaders have issues. As far as I can tell a shader that runs in the editor should run on the same machine in a browser plugin. Therefore it indicates issue between monitor and chair! (me :D)

    Nothing special I have to do to include custom shaders in a web build, etc?
     
  4. hyp78

    hyp78

    Joined:
    Jan 26, 2010
    Posts:
    10
    I should add that there's no errors in the web player logs either.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    are you sure the object that uses this shader is present in the build at all? check the editor log (log -> editor log button) after building to see if there is potentially a problem with it on build
     
  6. hyp78

    hyp78

    Joined:
    Jan 26, 2010
    Posts:
    10
    Aha I think you've cracked it. Not knowing what the editor log contained was my failing. Thanks for the pointer.

    In the 'Player size statistics' section, was this telling lil line..

    Code (csharp):
    1. Shaders       0.0 kb     0.0%
    No shaders appear in the compiled assets list either.

    I was using foo.renderer.material.shader = Shader.Find("HaloShader"); and, tho this worked fine in editor, it worked much better on web when it was placed in a Resources folder! :D

    I am referencing them in code and most are used in nearly every scene so Resources would seem the place to put them. However is there a folder I should be putting my shaders into for the build to function normally?

    Thanks again :)
     
  7. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Unity checks all references when it decides what to put in a build. Using Shader.Find() means Unity won't see know you're trying to use the shader, and won't include it in the build. As you have discovered, putting assets in the Resources folder causes Unity to include them in builds no matter what.

    Unless it simplifies your design to use Resources, I recommend writing your scripts such that all used assets get real references. That way, Unity will always automatically make builds that include exactly the assets you need.