Hello, I have a rather weird problem when passing a shader value from script
The value appears to change when running the program, but when I use it in the vertex shader, there is no change
The field that I modify is _CurrentAngle and it changes correctly in the Update() method from the c# script
BUT when I want to use it in the vertex shader( float angle = _CurrentAngle), it appears to have reseted.
When I change it like this
float angle = _Time * 30
it works as intended. But I need to be able to pass values from outside the shader
Please help.
Thank you
My shader code:
Shader “DisplacementShader”
{
Properties
{
_AppliedColor(“Applied Color”, Color) = (0, 0, 1, 0.5)
_Texture(“Texture”, 2D) = “blue” {}
_CurrentAngle(“_CurrentAngle”, Float) = 0
}
SubShader
{
//ZWrite Off
Tags {“Queue”=“Transparent” “IgnoreProjector”=“True” “RenderType”=“Transparent”}
LOD 300
Alphatest Greater 0.05
ColorMask RGBA
Pass
{
CGPROGRAM
#pragma vertex VertexShaderFunction
#pragma fragment PixelShaderFunction
#include “UnityCG.cginc”
sampler2D _Texture;
float4 _AppliedColor;
float _CurrentAngle;
float4 _Texture_ST;
struct vertexShaderInput
{
float4 Vertex : POSITION;
float3 Normal : NORMAL;
float2 TexCoord : TEXCOORD0;
};
struct vertexShaderOutput
{
float4 Position : SV_POSITION;
float2 UV: TEXCOORD0;
};
vertexShaderOutput VertexShaderFunction(vertexShaderInput input)
{
vertexShaderOutput output;
float x = input.Vertex.x;
float y = input.Vertex.y;
float z = input.Vertex.z;
float angle = _CurrentAngle;
// float angle = _Time * 30;
y = sin( x + angle) + cos(z + angle);
y *= x * 0.06f;
float4 deformedPosition = float4(x, y, z, 1);
output.Position = mul(UNITY_MATRIX_MVP, deformedPosition);
output.UV = TRANSFORM_TEX(input.TexCoord, _Texture);
return output;
}
fixed4 PixelShaderFunction(vertexShaderOutput input) : COLOR
{
fixed4 outputColor = tex2D(_Texture, input.UV) ;
return outputColor;
}
ENDCG
}
}
Fallback “VertexLit”
}
the script code
public class super_Script : MonoBehaviour
{
public float currentAngle;
public float rotationSpeed;
void Start ()
{
rotationSpeed = 30;
currentAngle = rotationSpeed * Time.time;
}
void Update ()
{
currentAngle = rotationSpeed * Time.time;
renderer.material.SetFloat(“_CurrentAngle”, currentAngle);
}
}