跳转到内容

Cg 编程/边缘轮廓切口

来自维基教科书,面向开放世界的开放书籍

边缘轮廓切口版本。

Shader "Outlined/Cutout" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_Cutout ("Alpha Cutout", Range (0,1)) = .5
		_MainTex ("Base (RGB)", 2D) = "white" { }
		_OutlineColor ("Outline Color", Color) = (0,0,0,1)	//改变这个能改变轮廓边的颜色
		_OutlineWidth ("Outline width", Range (0.0, 0.03)) = 0.008	//改变这个能改变轮廓边的粗细
		_OutlineAlpha ("Outline Alpha", Range (0.0, 1)) = 1	//改变这个能改变轮廓边的透明度
	}
 
CGINCLUDE
#include "UnityCG.cginc"
	uniform float _OutlineWidth;
	uniform float4 _OutlineColor;
	uniform sampler2D _MainTex;
	uniform float4 _MainTex_ST;
	uniform float _OutlineAlpha;
	float _Cutout;
	float4 _Color;
	struct v2f {
		float4 pos : POSITION;
		float4 color : COLOR;
		float2 texcoord : TEXCOORD1;
	};
ENDCG 
	SubShader {
		Pass {
			Name "CUTOUT"
			Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }			
			ZWrite On
			ZTest LEqual
			Blend SrcAlpha OneMinusSrcAlpha
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag	
			
			v2f vert(appdata_base v) {
				// just make a copy of incoming vertex data but scaled according to normal direction
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
				o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
				return o;
			}
			half4 frag(v2f i) :COLOR {
				half4 col= tex2D(_MainTex,i.texcoord)*_Color;
				clip(col.a - _Cutout);
				return col;
			}
			ENDCG
		}
		
		Pass {
			Name "OUTLINE"
			Tags { "LightMode" = "Always" "RenderType"="Transparent"}
			Cull Front
			ZWrite On
			ColorMask RGB
			Blend SrcAlpha OneMinusSrcAlpha
			//Offset 50,50
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			v2f vert(appdata_base v) {
				// just make a copy of incoming vertex data but scaled according to normal direction
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);			 
				float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
				float2 offset = normalize(TransformViewToProjection(norm.xy));
				o.pos.xy += offset * o.pos.z * _OutlineWidth;
				o.color = _OutlineColor;
				return o;
			}			
			half4 frag(v2f i) :COLOR { 
				half4 col = i.color;
				col.a *=_OutlineAlpha;
				return col; 
			}
			ENDCG
		}
	} 
	Fallback "Unlit/Transparent Cutout"
}
华夏公益教科书