Built-in转URP Chapter10(2)-Refraction 折射

这篇博客对比了两种Unity中实现折射效果的Shader编写方式:Built-CG写法和URP(Universal Render Pipeline)结合HLSL的写法。内容包括Shader的属性定义、结构体、顶点和片段着色器的实现,以及在不同渲染管线下的适配。通过对代码的比较,帮助读者理解Shader在不同渲染环境下的差异和调整技巧。
摘要由CSDN通过智能技术生成

话不多说,直接上代码,具体区别我后边会补充上,

暂时委屈各位,先锻炼下自己的“找不同”的能力,哈哈:

Built-CG写法:

Shader "Unity Shaders Book/Chapter 10/Refraction" {
	Properties {
		_Color("Color Tint", Color) = (1,1,1,1)
		_RefractColor("Refraction Color", Color) = (1,1,1,1)
		_RefractAmount("Refraction Amount", Range(0, 1)) = 0.5
		_RefractRatio("Refraction Ration", Range(0, 1)) = 0.5
		_Cubemap("Cube Map", Cube) = "_Skybox" {}
	}

	SubShader {
		Tags {"Queue"="Geometry" "RenderType"="Opaque"}

		Pass {
			Tags {"LightMode"="ForwardBase"}

			CGPROGRAM
			#include "Lighting.cginc"
			#include "AutoLight.cginc"

			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fwdbase

			fixed4 _Color;
			fixed4 _RefractColor;
			fixed _RefractAmount;
			fixed _RefractRatio;
			samplerCUBE _Cubemap;

			struct a2v {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float3 worldPos : TEXCOORD0;
				float3 worldNormal : TEXCOORD1;
				fixed3 worldViewDir : TEXCOORD2;
				fixed3 worldRefr : TEXCOORD3;
				SHADOW_COORDS(4)
			};

			v2f vert(a2v v) {
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex);
				o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
				o.worldRefr = refract(-normalize(o.worldViewDir), normalize(o.worldNormal), _RefractRatio);

				TRANSFER_SHADOW(o);
				return o;
			}

			fixed4 frag(v2f i) : SV_Target {
				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 viewDir = normalize(i.worldViewDir);
				fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));

				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				fixed3 refraction = texCUBE(_Cubemap, i.worldRefr).rgb * _RefractColor.rgb;
				fixed3 diffuse = _LightColor0.rgb * _Color.rgb * saturate(dot(i.worldNormal, lightDir));

				UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);

				fixed3 color = ambient + lerp(diffuse, refraction, _RefractAmount) * atten;
				return fixed4(color, 1);
			}

			ENDCG
		}
	}
	FallBack "Reflective/VertexLit"
	// FallBack "Refraction/vertexLit"
}

URP+HLSL写法:

Shader "Unity Shaders Book/Chapter 10/Refraction" {
	Properties {
		_Color("Color Tint", Color) = (1,1,1,1)
		_RefractColor("Refraction Color", Color) = (1,1,1,1)
		_RefractAmount("Refraction Amount", Range(0, 1)) = 0.5
		_RefractRatio("Refraction Ration", Range(0, 1)) = 0.5
		_Cubemap("Cube Map", Cube) = "_Skybox" {}
	}

	SubShader {
		Tags {
			"RenderPipeline"="UniversalPipeline"
			"Queue"="Geometry" "RenderType"="Opaque"
		}

		Pass {
			Tags {"LightMode"="UniversalForward"}

			HLSLPROGRAM
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

			#pragma vertex vert
			#pragma fragment frag
			// #pragma multi_compile_fwdbase

CBUFFER_START(UnityPerMaterial)
			half4 _Color;
			half4 _RefractColor;
			half _RefractAmount;
			half _RefractRatio;
CBUFFER_END

			TEXTURECUBE(_Cubemap);
			SAMPLER(sampler_Cubemap);

			struct a2v {
				float4 vertex : POSITION;
				half3 normal : NORMAL;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				half3 worldPos : TEXCOORD0;
				half3 worldNormal : TEXCOORD1;
				half3 worldViewDir : TEXCOORD2;
				half3 worldRefr : TEXCOORD3;
				// SHADOW_COORDS(4)
			};

			v2f vert(a2v v) {
				v2f o;
				o.pos = TransformObjectToHClip(v.vertex.xyz);
				o.worldNormal = TransformObjectToWorldNormal(v.normal).xyz;
				o.worldPos = mul(UNITY_MATRIX_M, v.vertex).xyz;
				o.worldViewDir = _WorldSpaceCameraPos.xyz - o.worldPos;
				o.worldRefr = refract(-normalize(o.worldViewDir), normalize(o.worldNormal), _RefractRatio);	// 折射率

				// TRANSFER_SHADOW(o);
				return o;
			}

			half4 frag(v2f i) : SV_Target {
				half3 worldNormal = normalize(i.worldNormal);
				// half3 viewDir = normalize(i.worldViewDir);
				// half3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
				float4 shadowCoord = TransformWorldToShadowCoord(i.worldPos);
				Light mainLight = GetMainLight(shadowCoord);
				half3 lightDir = normalize(mainLight.direction);

				half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
				half3 refraction = SAMPLE_TEXTURECUBE(_Cubemap, sampler_Cubemap, i.worldRefr).rgb * _RefractColor.rgb;
				half3 diffuse = mainLight.color.rgb * _Color.rgb * saturate(dot(i.worldNormal, lightDir));
				// UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
				half3 color = ambient + lerp(diffuse, refraction, _RefractAmount);
				return half4(color, 1);
			}

			ENDHLSL
		}
	}

	// FallBack "Reflective/VertexLit"
	// 不知道URP用什么fallback代替
	FallBack "Universal Render Pipeline/Simple Lit"
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值