How to access to a variable in shader from a script ?
Hi, I have a variable to control a value of a something in my shader. I need to control and change that value from a script.
The variable is "fovSize". Bellow my shader :
 Pass
             {
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
 
                 #include "UnityCG.cginc"
 
                 static const float fovSize = 0.1; 
And
 fragmentOutput frag(v2f i)
                 {
                     fragmentOutput o;
                     o.zvalue = 0.0;
                     o.color = (1, 1, 1, 1);
 
                     if ((i.posInObjectCoords.y < -fovSize || i.posInObjectCoords.y > fovSize)   ||
                         (i.posInObjectCoords.x < -fovSize || i.posInObjectCoords.x > fovSize))
                     {
                        o.zvalue = 1.0;
                     }
                     return o;
                 }
But it looks like I can't use that kind of code in my script because its not in the Properties :
 rend.material.SetFloat("_FovSize", fovSize);
I tried to declare in the Properties : _FovSize("fovSize", Float) = 1
And to use : if (i.posInObjectCoords.y < -_FovSize)
But doesn't work.
Any idea ?
Answer by MaT227 · May 11, 2017 at 08:55 AM
If you want to control a shader value from a script you don't need to declare it in the properties. Just declare it like this.
 // Shader
 uniform float _myValue;
In your script use Shader.SetGlobalFloat or Material.SetFloat depending on you use case.
 // Script
 public float myValue = 1.0f;
 Shader.SetGlobalFloat("_myValue", myValue);
I would also suggest you to take a look at those chapters Accessing shader properties in Cg/HLSL and MaterialPropertyBlock.
Thank you,
Why don't you declare it like this :
 _FovSize("fovSize", Float) = 1.0
What is the difference ? And why do you use uniform ?
The declaration you showed is used to expose the properties in the inspector.
 // Visible in the material inspector
  _FovSize("fovSize", Float) = 1.0
 float _FovSize;
 // Not visible in the material inspector
 float _FovSize;
the uniform  attribute is used when you want to control the value from the application or if you prefer from "outside" the shader.
Ok, So first I tried to do every thing in the shader but I have two new errors :
Shader error in 'FadeBlack': Parse error: syntax error, unexpected TO$$anonymous$$_FLOAT, expecting '(' at line 6
Shader warning in 'FadeBlack': Shader is not supported on this GPU (none of subshaders/fallbacks are suitable)
I declare _fovSize in the Properties, define it in the Pass and affect a value in fragmentOutput to use it.
I post my complete shader bellow :
 Shader "Custom/FadeBlack"
 {        Properties 
         {
             uniform float _fovSize;
         }
         
         SubShader
         {
             Tags{ "Queue" = "Geometry+5" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
 
             ZTest Always
             ZWrite On
             Color$$anonymous$$ask 0
 
             Pass
             {
                 CGPROGRA$$anonymous$$
                 #pragma vertex vert
                 #pragma fragment frag
 
                 #include "UnityCG.cginc"
 
                 float _fovSize;
 
                 struct appdata
                 {
                     float4 vertex : POSITION;
                 };
 
                 struct v2f
                 {
                     float4 vertex : SV_POSITION;
                     float4 posInObjectCoords : TEXCOORD0;
                 };
     
                 v2f vert(appdata v)
                 {
                     v2f o;
                     o.vertex = UnityObjectToClipPos(v.vertex);
                     o.posInObjectCoords = v.vertex;
                     return o;
                 }
 
                 struct fragmentOutput 
                 {
                     float zvalue : SV_Depth;
                     fixed4 color : SV_Target;
                 };
 
                 fragmentOutput frag(v2f i)
                 {
                     fragmentOutput o;
                     o.zvalue = 0.0;
                     o.color = (1, 1, 1, 1);
                     _fovSize = 0.1;
 
                     if ((i.posInObjectCoords.y <(-_fovSize) || i.posInObjectCoords.y >(_fovSize))   ||
                         (i.posInObjectCoords.x <(-_fovSize) || i.posInObjectCoords.x >(_fovSize)))
                     {
                        o.zvalue = 1.0;
                     }
                     return o;
                 }
                 ENDCG
             }
         }
 }
I'm really novice with shader and i read pages you have suggested.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                