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

本文详细介绍了如何在Unity中创建并调整玻璃材质的Shader。通过修改StandardSurfaceShader模板,实现透明效果,支持双面渲染,并引入多遍渲染技术提高画面质量。教程包括更改着色器属性、添加透明关键字、调整Cull指令以及实现模型缩放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新建一个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"
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值