unity3d Camera.SetReplacementShader的用法.

来源:互联网 发布:atmega128p单片机 编辑:程序博客网 时间:2024/05/19 17:50

unity3d Camera.SetReplacementShader的用法.

用此方法可以让全局使用一个shader来处理全部的vertex和fragment. 

第一个参数是一个shader. 第二个参数replacementTag(看文档无解了半天还以为函数有bug,结果是没说清楚,这个Tag是填Tag的名字.具体对应的就会被保留,比方说第一个参数的Shader里面Tags {"RenderType" = "Timeshift"}. 那么这个第二个参数就要填"RenderType", 然后场景里面的材质.只要用了这个RenderType=Timeshift的都会被保留下来.然后使用第一个参数的Shader处理).


将该脚本扔到随便什么物体上.

using UnityEngine;using System.Collections;public class Test2 : MonoBehaviour {// Use this for initializationvoid Start () {Camera.main.SetReplacementShader(Shader.Find("Custom/Test6"), "RenderType");}// Update is called once per framevoid Update () {}}

shader代码:

Shader "Custom/Test6" {Properties {_MainTex ("Base (RGB)", 2D) = "white" {}_Float1("Float1", Float) = 0.5}SubShader {Tags {"RenderType" = "Timeshift"}PASS {CGPROGRAM#pragma vertex vert#pragma fragment fragsampler2D _MainTex;uniform float _Float1;struct Input {float4 pos : POSITION;float4 normal : NORMAL;float4 color : COLOR;};struct InputFrag {float4 pos : SV_POSITION;float4 color : COLOR;}; InputFrag vert (Input i) {InputFrag o;o.pos = mul (UNITY_MATRIX_MVP, i.pos);o.color = i.normal * -0.5 + 0.5;return o;}float4 frag(InputFrag i) : COLOR{return i.color;}ENDCG}} }
由于Plane和外星人都使用了 Timeshift RenderType的Shader所以保留了下来.但是内容却被参数1的shader内容替代了.



0 0