2

I'm writing one of my first shaders. I'm editing a metaball shader I found. I'm new to shaders and I'd just like to set up some variables.

I want these variables to be calculated when the shader initialises

Heres my whole shader.. You can see what I am trying to do setting up a "_radius" in between the "PROBLEM CODE" comments

    //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" { } 

        _bottomthresh ("bottom cutoff", Range(0,1)) = 0.1   
        _topthresh ("top cutoff", Range(0,1)) = 0.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 _bottomthresh;
        float _topthresh;
        //============PROBLEM CODE START===================
        float _radius = _topthresh - _bottomthresh;
        if(_radius < 0)
        {
            _radius = 0;
        }
        //============PROBLEM CODE END=====================
        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<_bottomthresh)
            {
                finalColor.a= 0;  
            }
            else if((texcol.a>_topthresh))
            {
                finalColor.a= 0;
            }
            else
            {
                finalColor.a= 1;
            }           
            return finalColor;
        }
        ENDCG

        }
    }
    Fallback "VertexLit"

I'm really confused.. In some cases it seems you can write normal c# code in a shaderlab shader and in other parts you cant.

Guye Incognito
  • 2,726
  • 6
  • 38
  • 72

1 Answers1

2

You can't write C# code anywhere in a shaderlab file. The syntax is special shaderlab syntax that can contain other shader languages like HLSL.

In this case the problem is that you are trying to do a calculation in the top level subshader which isn't allowed - any calculations must be done in a function. You could add that code to the vert function or the frag function. Like this:

     v2f vert (appdata_base v){
        float _radius = _topthresh - _bottomthresh;
        if(_radius < 0)
        {
            _radius = 0;
        }

        v2f o;
        o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
        o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
        return o;
    }   
CiscoIPPhone
  • 9,457
  • 3
  • 38
  • 42