課時72 Fragment shader 7 - 實現半透明着色 2

Shader "Custom/Transparent_Object" {

	SubShader{
		pass {

		blend srcalpha  oneminussrcalpha
		ztest greater
		zwrite on

		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"

			struct v2f
		{
			float4 pos:POSITION;
		};

		v2f vert(appdata_base v)
		{
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
			return o;
		}

		fixed4 frag(v2f IN) :COLOR
		{
			fixed4 color = fixed4(1,1,0,0.5);
		return color;
		}
        ENDCG
	    }

	pass {

		//blend srcalpha  oneminussrcalpha
		ztest less
	    zwrite on


		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"

			struct v2f
		{
			float4 pos:POSITION;
		};

		v2f vert(appdata_base v)
		{
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
			return o;
		}

		fixed4 frag(v2f IN) :COLOR
		{
			fixed4 color = fixed4(1,1,0,0.5);
		return color;
		}
			ENDCG
	}
	}
}
Shader "Custom/Transparent_Replace" {


<span style="white-space:pre">	</span>SubShader{
<span style="white-space:pre">		</span>tags{"rendertype"="transparent"}
<span style="white-space:pre">		</span>pass {


<span style="white-space:pre">		</span>CGPROGRAM
<span style="white-space:pre">		</span>#pragma vertex vert
<span style="white-space:pre">		</span>#pragma fragment frag
<span style="white-space:pre">		</span>#include "unitycg.cginc"


<span style="white-space:pre">	</span>    struct v2f
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>float4 pos:POSITION;
<span style="white-space:pre">			</span>float2 depth:TEXCOORD0;
<span style="white-space:pre">		</span>};


<span style="white-space:pre">		</span>v2f vert(appdata_base v)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>v2f o;
<span style="white-space:pre">			</span>o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
<span style="white-space:pre">			</span>o.depth = o.pos.zw;
<span style="white-space:pre">			</span>return o;
<span style="white-space:pre">		</span>}


<span style="white-space:pre">		</span>fixed4 frag(v2f IN) :COLOR
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>float depth =Linear01Depth( IN.depth.x / IN.depth.y);
<span style="white-space:pre">			</span>fixed4 color = fixed4(depth,0,0,1);
<span style="white-space:pre">		</span>    return color;
<span style="white-space:pre">		</span>}
        ENDCG
<span style="white-space:pre">	</span>    }
<span style="white-space:pre">	</span>}
}
using UnityEngine;
using System.Collections;


public class Replace : MonoBehaviour {


<span style="white-space:pre">	</span>// Use this for initialization
<span style="white-space:pre">	</span>void Start () {
        Camera.main.SetReplacementShader(Shader.Find("Custom / Transparent_2"),"rendertype");
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>// Update is called once per frame
<span style="white-space:pre">	</span>void Update () {
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章