- Home /
Default Uniy Shader variables are not working.
Hey, i am trying to work on Unity shaders through script. However in order to achieve what i need i have to use the built in Unity shader variables such as _Time which represents the time since the level is loaded. And this default shader variable is not working. cause when i try to peek the definition from the MVS it says not defined.
Shader "Custom/ScrollingShaders" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _ScrollXSeed ("X Scroll Speed", Range(0,10)) = 2 _ScrollYSeed("Y Scroll Speed", Range(0,10)) = 2
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#include "Assets/CGIncludes/UnityShaderVariables.cginc"
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
fixed4 _Color;
fixed _ScrollXSpeed;
fixed _ScrollYSpeed;
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
//create a separate variable to store our UV's
//before we pass them to the text2D() function
fixed2 scrolledUV = IN.uv_MainTex;
//create a variable that stores individual X and Y
//components for the uv scaled by time
fixed xScrollValue = _ScrollXSpeed * _Time;
fixed yScrollValue = _ScrollYSpeed * _Time;
//Apply final UV offset where scrolls are merged with UV
scrolledUV += fixed2(xScrollValue, yScrollValue);
// Albedo comes from a texture tinted by color
half4 c = tex2D (_MainTex, scrolledUV);
o.Albedo = c.rgb * _Color;
// Metallic and smoothness come from slider variables
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
[1]: /storage/temp/143682--time.png
Answer by Deathdefy · Jul 29, 2019 at 02:02 PM
_Time is stored as a float4. You need to access one of the floats within it to actually use it.
https://answers.unity.com/questions/266584/shaders-why-is-time-stored-in-a-vector4.html
so does this mean this is how i should rewrite the code as @Deathdefy
fixed xScrollValue = _ScrollXSpeed mul _Time.x;
. fixed yScrollValue = _ScrollYSpeed mul _Time.y;
or may be change the variables from fixed to float as mentioned in the document in that case this is how it should be.
float xScrollValue = _ScrollXSpeed mul _Time.x;
. float yScrollValue = _ScrollYSpeed mul _Time.y;