Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Material.SetPass

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public function SetPass(pass: int): bool;
public bool SetPass(int pass);

Parameters

pass Shader pass number to setup.

Returns

bool If false is returned, no rendering should be done.

Description

Activate the given pass for rendering.

Pass indices start from zero and go up to (but not including) passCount.

This is mostly used in direct drawing code using GL class. For example, Image Effects use materials for implementing screen post-processing. For each pass in the material they activate the pass and draw a fullscreen quad.

If SetPass returns false, you should not render anything. This is typically the case for special pass types that aren't meant for rendering, like GrabPass.

#pragma strict
// A script that when attached to the camera, makes the resulting
// colors inverted. See its effect in play mode.
private var mat: Material;
// Will be called from camera after regular rendering is done.
public function OnPostRender() {
	if (!mat) {
		// a blend mode that inverts destination colors.			
		var shader: Shader = Shader.Find("Hidden/Internal-Colored");
		mat = new Material(shader);
		mat.hideFlags = HideFlags.HideAndDontSave;
		// Set blend mode to invert destination colors.
		mat.SetInt("_SrcBlend", intUnityEngine.Rendering.BlendMode.OneMinusDstColor);
		mat.SetInt("_DstBlend", intUnityEngine.Rendering.BlendMode.Zero);
		// Turn off backface culling, depth writes, depth test.
		mat.SetInt("_Cull", intUnityEngine.Rendering.CullMode.Off);
		mat.SetInt("_ZWrite", 0);
		mat.SetInt("_ZTest", intUnityEngine.Rendering.CompareFunction.Always);
	}
	GL.PushMatrix();
	GL.LoadOrtho();
	// activate the first shader pass (in this case we know it is the only pass)
	mat.SetPass(0);
	// draw a quad over whole screen
	GL.Begin(GL.QUADS);
	GL.Vertex3(0, 0, 0);
	GL.Vertex3(1, 0, 0);
	GL.Vertex3(1, 1, 0);
	GL.Vertex3(0, 1, 0);
	GL.End();
	GL.PopMatrix();
}
using UnityEngine;

// A script that when attached to the camera, makes the resulting // colors inverted. See its effect in play mode. public class ExampleClass : MonoBehaviour { private Material mat;

// Will be called from camera after regular rendering is done. public void OnPostRender () { if (!mat) { // Unity has a built-in shader that is useful for drawing // simple colored things. In this case, we just want to use // a blend mode that inverts destination colors. Shader shader = Shader.Find ("Hidden/Internal-Colored"); mat = new Material (shader); mat.hideFlags = HideFlags.HideAndDontSave; // Set blend mode to invert destination colors. mat.SetInt ("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusDstColor); mat.SetInt ("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); // Turn off backface culling, depth writes, depth test. mat.SetInt ("_Cull", (int)UnityEngine.Rendering.CullMode.Off); mat.SetInt ("_ZWrite", 0); mat.SetInt ("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always); }

GL.PushMatrix (); GL.LoadOrtho ();

// activate the first shader pass (in this case we know it is the only pass) mat.SetPass (0); // draw a quad over whole screen GL.Begin (GL.QUADS); GL.Vertex3 (0, 0, 0); GL.Vertex3 (1, 0, 0); GL.Vertex3 (1, 1, 0); GL.Vertex3 (0, 1, 0); GL.End ();

GL.PopMatrix (); } }

See Also: passCount property, GL class, ShaderLab documentation.