I cannot pass parameters to my custom shaders... help please...

Hello There,

I have been trying to pass parameters from unity to my custom depth/normal rendering shader. The code is as follows:

Shader "MIHAS/SAR" {
Properties {
    _NormalContribution( "Normal Contribution", Float ) = 0.999
    _DepthContribution( "Depth Contribution", Float ) = 0.001
    _NormalPower( "Normal Contribution", Float ) = 8
    _DepthPower( "Depth Contribution", Float ) = 2  
    _BaseColor("Base Color", Color) = (1,1,1,0)
}
SubShader {
    Tags { "RenderType"="Opaque" }
    Pass {
        Fog { Mode Off }

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct v2f {
    float4 pos : POSITION;
    float4 color: COLOR;
};

uniform float _NormalContribution;
uniform float _DepthContribution;
uniform float _NormalPower;
uniform float _DepthPower;
uniform float4 _BaseColor;

v2f vert( appdata_base v ) {

    v2f o;

    o.pos = mul(glstate.matrix.mvp, v.vertex);

    //float3 eNormal = mul((float3x3)glstate.matrix.mvp, v.normal );    

    //float depth;
    //TRANSFER_EYEDEPTH(depth);
    //depth = pow(1.0 - depth,_DepthPower);

    //float dotP = pow(abs(dot( (float3)normalize(o.pos), eNormal )), _NormalPower);

    float4 baseColor = { 0.0, 1.0, 0.0, 0.0 };

    //Calculate vertex color
    //o.color = ( (_NormalContribution * dotP)+(_DepthContribution * depth ) ) * _BaseColor;
    o.color = _BaseColor;

    return o;
}

float4 frag(v2f i) : COLOR {

    return i.color;
}

ENDCG

    }
}

Fallback Off

}

However the values of all of my custom variables(like _BaseColor) are read as zero on the shader side. Removing "uniform" specifier does not change anything. And if I initialize these values to local variables or if I provide a constant instead of them at necessary positions it works without a problem.

Although not necessary I have even written the below script to force parameter setting(in case it is initialized wrong) but this doesnt make a difference either:

using UnityEngine;
using System.Collections;

    public class SarShader : MonoBehaviour {

        public float normalContribution = 0.999f;
        public float depthContribution = 0.001f;
        public float normalPower = 8f;
        public float depthPower = 2f;
        public Color baseColor = new Color(1.0f,1.0f,1.0f,0.0f);

        public Material sarMaterial;

        // Use this for initialization
        void Start () {

            if( sarMaterial != null ){

                setParameters();

                Shader sarShader = sarMaterial.shader;
                //!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth)
                if( sarShader != null && sarShader.isSupported && camera != null ){
                        camera.SetReplacementShader( sarShader, "" );
                }       
            }

        }

        void setParameters(){

            sarMaterial.SetColor("_BaseColor", baseColor );
            sarMaterial.SetFloat("_NormalContribution", normalContribution );
            sarMaterial.SetFloat("_DepthContribution", depthContribution );
            sarMaterial.SetFloat("_NormalPower", normalPower );
            sarMaterial.SetFloat("_DepthPower", depthPower );       
        }

}

Do anyone have any idea on what might be the problem??? All of these 5 parameters are always read as zero... Without removing these if I only copy constant in their place in the shader code it works perfectly without a problem.

it works for me.

I did the same thing what you have explained here.

Actually i changed the value of my shader slide value using my GUI.HorizontalSlider
it working fine.
The thing is i call every thing in Update function.

Use the function Shader.SetGlobalFloat in the SetParameters funcion, then you can discard all the code related to the material.

Also, include the SetReplacementShader call at the end of the SetParameters function. Call SetParameters once after each render, for instance at the PreCull function.