Possible to pause a shader?

I’m playing with this shader from the AngryBots project, which animates rain splashing onto the ground by moving an animated texture. My game pauses independently from Time.timeScale as I use the timeScale in the menus, so I instead set animated objects to stop. But this is animating inside of a shader — how can I get this to stop?

Shader "AngryBots/FX/RainSplash" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Intensity ("Intensity", Range (0.5, 4.0)) = 1.5
	}
	
	CGINCLUDE

		#include "UnityCG.cginc"

		sampler2D _MainTex;
		
		uniform half4 _MainTex_ST;
		uniform half4 _Color;
		uniform half4 _CamUp;
		
		uniform fixed _Intensity;
		
		struct v2f {
			half4 pos : SV_POSITION;
			half2 uv : TEXCOORD0;	
			fixed4 color : TEXCOORD1;
		};

		v2f vert(appdata_full v)
		{
			v2f o;
			
			half timeVal = frac(_Time.z * 0.5 + v.texcoord1.x) * 2.0;
			
			o.uv.xy = v.texcoord.xy;
			
			// animation of 6 frames:
			o.uv.x = o.uv.x / 6 + floor(timeVal * 6) / 6;
			
			o.pos = mul (UNITY_MATRIX_MVP, v.vertex);	
			
			o.color = saturate(1.0 - timeVal) * _Intensity;		
			
			return o; 
		}
		
		fixed4 frag( v2f i ) : COLOR
		{	
			fixed4 outColor = tex2D(_MainTex, i.uv) * i.color;
			return outColor;
		}
	
	ENDCG
	
	SubShader {
		Tags { "Queue" = "Transparent" }
		Cull Off
		ZWrite Off
		Blend One OneMinusSrcColor

	Pass {
	
		CGPROGRAM
		
		#pragma vertex vert
		#pragma fragment frag
		#pragma fragmentoption ARB_precision_hint_fastest 
		
		ENDCG
		 
		}
				
	} 
	FallBack Off
}

1 Answer

1

On obvious idea would be to pass in a shader param instead of using _Time. Drive the animated shader value with your param and then you can set the param externally from regular code as needed.

That sounds like a good approach. I'm not too well versed in Shader lingo though. Would I change the following line… half timeVal = frac(_Time.z * 0.5 + v.texcoord1.x) * 2.0; to something like this?... half timeVal = _MyNewTimeValue;

Yep. Define a property up at the top and a matching uniform in the body of the shader. Use Material.SetFloat("_MyNewTimeValue", ) to update the time as needed from game code. Setting params every frame is definitely more expensive than just running off the builtin time value. Its unlikely to be disastrous if you are only doing it for a couple of objects.

Working great, thanks! :) Now I just wish there was a way to print info from a shader to the console so I could see how it's all working…

You can't do that but you can come up with a debug visualization. You can use #defines to enable or disable parts of a shader, so you could show the real deal or something that was useful for debugging by setting a shader keyword. See the last few posts in [this thread][1] [1]: http://forum.unity3d.com/threads/100231-Conditional-Shader-Compilation