- Home /
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.
Well I have accidentally linked the commented code... But it doesnt matter if you remove those comments. It still can't read any values... I have added the script to a game object with camera attached by the way...
have you tried using the plain-text names of your params in the .set* functions (e.g. .SetColor("Base Color",basecolor))? I'd imagine those names in the shader get used for something, and a compiler pattern-matching script strings to shader handles would be unusual if not per se unprecedented. Also, are there any separators (",", ";", etc.) needed between the Properties in the shader?
Yes I have tried using the plane text names in the set functions. In fact I do not think that those set functions are actually necessary. I have written them as it didnt work without them. But it does not work with them either. Are seperators necessary for shader properties? I havent seen any examples of them anywhere. I think it would be inappropriate syntax for shader definition. Thanks for help Sean. I really appreciate it.
Answer by Edy · Jan 30, 2011 at 08:35 PM
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.
Answer by schetty · Apr 11, 2014 at 12:21 PM
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.