【UnityShader自学日志】透明着色器(透明头盔效果)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_24994943/article/details/82813323

新建一个Standard Surface Shader,将其命名为Glass

1、打开Glass,将其名称改为"PACKT/Glass"

2、在Subshader下面,找到Tags一行,将Opaque改为Transparent(即从“不透明”改为“透明”)

3、找到着色器的编译指令代码#pragma surface surf Standard fullforwardshadows,并在其后面添加关键字alpha,此关键字使得着色器支持透明,透明度由着色器的颜色属性的alpha通道决定

此时的着色器可以使得外表面有高光效果

出于对运行效率的考虑,Unity默认忽略物体的背面,只渲染正面

可以在Subshader代码块中,在LOD一行的下面添加Cull off,此时正面和背面均会被渲染,但因Cull off指令使得正面和背面在同一道绘制调用里被渲染,因此偶尔会出现由于顺序错误而导致的渲染错误。

因此可以通过用两块Cg代码分两次渲染正面和背面来提高画面质量,即多遍渲染

1、首先在Properties代码块中添加_Thickness ("Thickness", Range(-1,1)) = 0.5

     _Thickness 表示厚度,用来控制在其中一遍渲染中,模型被放大或缩小的程度

2、将Cull off改为Cull Back(即剔除背面,只渲染正面)

3、往下找到ENDCG一行,在该行下添加

Cull Front//剔除正面(即只渲染背面)
		CGPROGRAM
		//指令vertex:vert使得我们可以向着色器插入自定义的顶点处理代码
		#pragma surface surf Standard fullforwardshadows alpha vertex:vert
		struct Input {
			float2 uv_MainTex;
		};
		float _Thickness;
		//顶点处理代码
		//以_Thickness属性的值为移动距离,将模型的每一个顶点沿着表面的法线移动,从而便可实现模型的缩放
		void vert(inout appdata_full v) {
			v.vertex.xyz += v.normal * _Thickness;
		}

		sampler2D _MainTex;
		half _Glossiness;
		half _Metallic;
		fixed4 _Color;

		void surf(Input IN, inout SurfaceOutputStandard o) {
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG

此部分为渲染背面的代码

综上,该shader文件的完整代码如下

Shader "PACKT/Glass" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
		_Thickness ("Thickness", Range(-1,1)) = 0.5//厚度,用来控制在其中一遍渲染中,模型被放大或缩小的程度
	}
	SubShader {
		Tags { "RenderType"="Transparent" }
		LOD 200
		//Cull off//关闭剔除,使得正面和背面在同一道绘制调用里被渲染,可能会导致渲染错误
		
		Cull Back//剔除背面(即只渲染正面)		
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows alpha

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		half _Glossiness;
		half _Metallic;
		fixed4 _Color;

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG


		Cull Front//剔除正面(即只渲染背面)
		CGPROGRAM
		//指令vertex:vert使得我们可以向着色器插入自定义的顶点处理代码
		#pragma surface surf Standard fullforwardshadows alpha vertex:vert
		struct Input {
			float2 uv_MainTex;
		};
		float _Thickness;
		//顶点处理代码
		//以_Thickness属性的值为移动距离,将模型的每一个顶点沿着表面的法线移动,从而便可实现模型的缩放
		void vert(inout appdata_full v) {
			v.vertex.xyz += v.normal * _Thickness;
		}

		sampler2D _MainTex;
		half _Glossiness;
		half _Metallic;
		fixed4 _Color;

		void surf(Input IN, inout SurfaceOutputStandard o) {
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

猜你喜欢

转载自blog.csdn.net/sinat_24994943/article/details/82813323