Camera.main.SetReplacementShader()

作者:追风剑情 发布于:2017-8-5 16:39 分类:Shader

替换前

22222.png11111.png

替换后

3333.png

示例: Unity5.5


using System.Collections.Generic;
using UnityEngine;

public class TestShader : MonoBehaviour {

	void Start () {
        //SetReplacementShader(): 永久替换
        //RenderWithShader(): 仅替换当前帧渲染
        Camera.main.SetReplacementShader(Shader.Find("Custom/ReplacementShader3"), "RenderType");
	}
}


替换前使用的Shader


Shader "Custom/ReplacementShader1" {
	Properties {
        _Color ("Main Color", Color) = (1,1,1,0)
        _SpecColor ("Spec Color", Color) = (1,1,1,1)
        _Emission ("Emmisive Color", Color) = (0,0,0,0)
        _Shininess ("Shininess", Range (0.01, 1)) = 0.7
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {
		Tags {"Queue"="Geometry" "RenderType"="MyTag" "PreviewType"="Plane"}
	
        Pass {
            Material {
            	//漫反射颜色
                Diffuse [_Color]
                //环境光颜色
                Ambient [_Color]
                //亮度
                Shininess [_Shininess]
                //高光颜色
                Specular [_SpecColor]
                //自发光颜色
                Emission [_Emission]
            }
            //开启光照
            Lighting On
            //开启高光
            SeparateSpecular On
            //混合纹理
            SetTexture [_MainTex] {
            	//纹理混合模式参见: https://docs.unity3d.com/Manual/SL-SetTexture.html
            	//主纹理rgb * 光照颜色rgb 2倍亮度, 主纹理alpha * 光照颜色的alpha
                Combine texture * primary DOUBLE, texture * primary

                //Quad: 4倍亮度
                //Combine texture * primary Quad, texture * primary
            }
        }
    }
}


替换后使用的Shader


Shader "Custom/ReplacementShader3" {
	Properties
    {
        
    }
    SubShader
    {
		Tags {"RenderType" = "MyTag"}
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            float4 vert (float4 vertex : POSITION) : SV_POSITION
            {
                return mul(UNITY_MATRIX_MVP, vertex);
            }

            fixed4 frag () : SV_Target
            {
                return fixed4(0,1,0,1);//返回绿色
            }
            ENDCG
        }
    }
}


注意:"RenderType"="MyTag"匹配上才会替换,否则物体将不被Camera渲染。

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号