1
votes

I'm attempting to build a webiste made in unity to WebGL using the unity 5 beta. A custom shader I wrote (or more accurately edited from an existing one) no longer works in Unity 5. Heres what the shader is supposed to do. Create a metaball effect where the alpha ramps up in a circular curve. Shader turns this.. enter image description here into this.. (via a render texture) enter image description here

Heres the whole thing..

//Water Metaball Shader effect by Rodrigo Fernandez Diaz-2013
//Visit http://codeartist.info/ for more!!
Shader "Custom/Metaballs" {
Properties { 


    _MyColor ("Some Color", Color) = (1,1,1,1)  
    _MainTex ("Texture", 2D) = "white" { } 

    _botmcut ("bottom cutoff", Range(0,1)) = 0.1   
    _topcut ("top cutoff", Range(0,4)) = 0.8   
    _constant ("curvature constant", Range(0,5)) = 1  
}
SubShader {
    Tags {"Queue" = "Transparent" }
    Pass {
    Blend SrcAlpha OneMinusSrcAlpha     
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag   
    #include "UnityCG.cginc"    
    float4 _MyColor;
    float4 _Color;
    sampler2D _MainTex; 
    float _botmcut,_topcut,_constant;

    struct v2f {
        float4  pos : SV_POSITION;
        float2  uv : TEXCOORD0;
    };  
    float4 _MainTex_ST; 

    v2f vert (appdata_base v){
        v2f o;
        o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
        o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
        return o;
    }   

    half4 frag (v2f i) : COLOR{     
        half4 texcol,finalColor;
        texcol = tex2D (_MainTex, i.uv);        
        //finalColor=_Color*texcol;
        finalColor=_MyColor;
        if(texcol.a<_botmcut)
        {
            finalColor.a= 0;  
        }
        else if((texcol.a>_topcut))
        {
            finalColor.a= 0;
        }
        else
        {
            float r = _topcut-_botmcut; 
            float xpos = _topcut - texcol.a;

            finalColor.a= 1-(_botmcut + sqrt((xpos*xpos)-(r*r)))/_constant;
        }           
        return finalColor;
    }
    ENDCG

    }
}
Fallback "VertexLit"
} 

The problem I am having in Unity 5 is that the resulting texture is blank. ie. 0 alpha. The bit that seems to be causing the problem is this one.

    else
    {
        float r = _topcut-_botmcut; 
        float xpos = _topcut - texcol.a;

        finalColor.a= 1-(_botmcut + sqrt((xpos*xpos)-(r*r)))/_constant;
    }   

If I comment out the last line of this "finalCOlor...etc etc" then I see something This is the line that normally creates that circular alpha curve, but in unity 5 it is always resolving to 0 it seems. Has there been some API change? because the math should work out identically to how it worked in unity 4. Ps. I dont know much about shaders!

2

2 Answers

2
votes

A few things that I normally do when tracking down shader issues.

Option 1 Try using PIX or some other standard program to debug the shader. You just need to capture the frame and right click on the pixel and hit debug. I'd pay close attention to what each value is, make sure none are set to 0 that shouldn't be. Also verify in this tool the right textures are being used.

Options 2 If you set finalColor.a to 0.5 does this do anything? If this does you know the issue is in one of your variables being 0. Should _constant even allow the range of 0? I think that should be from >0 to 5 honestly. Also verify you haven't overriden any of the constants or variables on the material, make sure they are still all set to the default. You might even want to just hard set them in the shader to see if that fixes the problem.

Finally, solving shader problems are not easy, but the fact that it worked in Unity 4 and doesn't in 5 tells me that you are probably just resolving something to 0, so I would check that first.

0
votes

I have no idea why..But changing this line..

 finalColor.a= 1-(_botmcut + sqrt((xpos*xpos)-(r*r)))/_constant;

to this..

 finalColor.a= 1-(_botmcut + sqrt((r*r)-(xpos*xpos)))/_constant;

Worked. It doesnt make sense!