Documentation

Unity Engine

Download Unity

Unity Engine

SetPass(int)

Activate the given pass for rendering.
Read time 2 minutesLast updated 2 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule
public bool SetPass(int pass)

Parameters

pass

Shader pass number to setup.

Returns

Type

Description

boolIf false is returned, no rendering should be done.

Remarks

Pass indices start from zero and go up to (but not including) Material.passCount.
Use this method in direct drawing code, for example, to draw meshes with Graphics.DrawMeshNow.
SetPass
returns false if the pass index is out of range, or if Unity can't set up the pass for rendering. If
SetPass
returns false, don't render anything.
The following example draws the GameObject's mesh again with a material that inverts the colors behind it. The mesh is visible even when other objects are in front of it. Attach the script to a GameObject that has a MeshFilter, for example a cube, and enter Play mode.
using UnityEngine;using UnityEngine.Rendering;[RequireComponent(typeof(MeshFilter))]public class ExampleClass : MonoBehaviour{ Material m_Material; void OnEnable() { // Hidden/Internal-Colored is a built-in shader with properties that control blending, culling, and depth. m_Material = new Material(Shader.Find("Hidden/Internal-Colored")); m_Material.hideFlags = HideFlags.HideAndDontSave; // Invert the colors behind the mesh. m_Material.SetFloat("_SrcBlend", (float)BlendMode.OneMinusDstColor); m_Material.SetFloat("_DstBlend", (float)BlendMode.Zero); // Draw only the front faces, so Unity inverts each pixel once. m_Material.SetFloat("_Cull", (float)CullMode.Back); // Draw the mesh even when other objects are in front of it. m_Material.SetFloat("_ZWrite", 0); m_Material.SetFloat("_ZTest", (float)CompareFunction.Always); } void OnDisable() { DestroyImmediate(m_Material); } // Unity calls this after each camera renders the scene. void OnRenderObject() { // Activate the first shader pass. If Unity can't set up the pass, don't draw anything. if (!m_Material.SetPass(0)) return; Graphics.DrawMeshNow(GetComponent<MeshFilter>().sharedMesh, transform.localToWorldMatrix); }}
Additional Resources: Material.passCount property, GL class, ShaderLab documentation.