Possible to update textures used in shader?

I’ve discovered that working on pixel’s alpha channel is pretty clunky and doesn’t look great. Long story short in order to get the desired effect it’s going to take a rediculous amount of time.

In short i’ve decided to try a new method, I have a shader that allows me to use one texture as a clipping mask for another, the mask is a white and black texture, and the main texture is only showed in area’s where the mask is white…

Now i need to make it so that the clipping mask texture can be changed at real time, essentially i will have a sprite sheet of clipping mask textures making more and more of the main texture visible over time. However i don’t understand shader code in the slightest, is it possible to update a texture used in the shader at real time? If so could some one point me in the direction of how i could do that? (if there’s a tutorial or something on this that you know of i’d be very appreciative).

edit - Also as a side note, i will need this animation to only play after a certain point, can this be done in shader code? or alternatively can i make my code communicate with the shader to tell it when to begin?
Thanks for your time community.

Sure thing, you can change a texture in a shader at any time through the object’s renderer’s material property. This, for example, changes the main texture:

    Texture2D someTexture = new Texture2D(128,128);
    renderer.material.mainTexture = someTexture;

Note that assigning a texture to material.mainTexture is actually shorthand for this:

    renderer.material.SetTexture("_MainTex", someTexture);

Where the property (and variable) that defines the texture described as e.g. “Base (RGB)” in a diffuse shader is actually called “_MainTex” in the shadercode. That’s the tricky part about this: You don’t have to write any shadercode yourself, but you do have to know what the texture you want to set is called in the shadercode, or else, you won’t know what to put in your call to Material.SetTexture. It MUST be the texture’s actual variable name in the shader.

To help you out, you can download the source code for all of Unity’s builtin shaders here:

http://unity3d.com/unity/download/archive

The source code for the builtin shaders reveal what the variable name of the textures are.

The code for the standard Transparent/Cutout/Bumped Diffuse looks like this, for example:

Shader "Transparent/Cutout/Bumped Diffuse" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	_BumpMap ("Normalmap", 2D) = "bump" {}
	_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}

SubShader {
	Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
	LOD 300
	
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;

struct Input {
	float2 uv_MainTex;
	float2 uv_BumpMap;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	o.Albedo = c.rgb;
	o.Alpha = c.a;
	o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}

FallBack "Transparent/Cutout/Diffuse"
}

Which reveals that the bumpmap texture is called “_BumpMap”. So, to set that programmatically in script, you’d write this:

    renderer.material.SetTexture("_BumpMap", someTexture);

Hope this helps. :slight_smile:

Edit: Sorry, changed example shader source to Bumped version because the first was unsuitable for the description here. :slight_smile: