Built-in转URP Chapter7(6)-MaskTexture 遮罩纹理

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

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

Built-CG写法:

Shader "Unity Shaders Book/Chapter 7/Mask Texture" {
	Properties {
		_Color("Color", Color) = (1, 1, 1, 1)
		_MainTex("MainTex", 2D) = "white"{}
		_BumpMap("BumpTex", 2D) = "white"{}
		_BumpScale("BumpScale", Range(-2, 2)) = 1.0
		_SpecularMask("SpecularMask", 2D) = "white"{}
		_SpecularScale("SpecularScale", Range(-2, 2)) = 1.0
		_Specular("Specular", Color) = (1, 1, 1, 1)
		_Gloss("Gloss", Range(10, 256)) = 20
	}

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


			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			fixed4 _Color;
			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _BumpMap;
			float _BumpScale;
			sampler2D _SpecularMask;
			float _SpecularScale;
			fixed4 _Specular;
			float _Gloss;

			struct a2v {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float4 tangent : TANGENT;
				float4 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				float3 lightDir : TEXCOORD1;
				float3 viewDir : TEXCOORD2;
			};

			v2f vert(a2v v) {
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
				// float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w;
				// float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
				TANGENT_SPACE_ROTATION;
				o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
				o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
				return o;

				// v2f o;
				// o.pos = UnityObjectToClipPos(v.vertex);
				
				// o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
				
				// TANGENT_SPACE_ROTATION;
				// o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
				// o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
				
				// return o;
			}

			fixed4 frag(v2f v) : SV_Target {
				fixed3 tangentLightDir = normalize(v.lightDir);
				fixed3 tangentViewDir = normalize(v.viewDir);

				fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, v.uv));
				tangentNormal.xy *= _BumpScale;
				tangentNormal.z = sqrt(1.0 - max(0, dot(tangentNormal.xy, tangentNormal.xy)));

				fixed3 albedo = tex2D(_MainTex, v.uv).rgb * _Color.rgb;

				// 环境光
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

				// 漫反射
				fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentViewDir));

				// 高光
				fixed3 halfDir = normalize(tangentViewDir + tangentLightDir);
				fixed specularMask = tex2D(_SpecularMask, v.uv).r * _SpecularScale;
				fixed3 specular = _LightColor0.rgb * _Specular.rbg * pow(max(0, dot(halfDir, tangentNormal)), _Gloss) * specularMask;

				return fixed4(ambient + diffuse + specular, 1.0);
			}

			ENDCG
		}
	}
}

URP+HLSL写法:

Shader "Unity Shaders Book/Chapter 7/Mask Texture" {
	Properties {
		_Color("Color", Color) = (1, 1, 1, 1)
		_MainTex("MainTex", 2D) = "white"{}
		_BumpMap("BumpTex", 2D) = "white"{}
		_BumpScale("BumpScale", Range(-2, 2)) = 1.0
		_SpecularMask("SpecularMask", 2D) = "white"{}
		_SpecularScale("SpecularScale", Range(-2, 2)) = 1.0
		_Specular("Specular", Color) = (1, 1, 1, 1)
		_Gloss("Gloss", Range(10, 256)) = 20
	}

	SubShader {
		Tags {
			"RenderPipeline"="UniversalPipeline"
		}
		Pass {
			Tags {
				"LightMode"="UniversalForward"
			}

			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag

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

CBUFFER_START(UnityPerMaterial)
			half4 _Color;
			float4 _MainTex_ST;
			float _BumpScale;
			float _SpecularScale;
			half4 _Specular;
			float _Gloss;
CBUFFER_END

			TEXTURE2D(_MainTex);
			SAMPLER(sampler_MainTex);

			TEXTURE2D(_BumpMap);
			SAMPLER(sampler_BumpMap);

			TEXTURE2D(_SpecularMask);
			SAMPLER(sampler_SpecularMask);

			struct a2v {
				float4 vertex : POSITION;
				half3 normal : NORMAL;
				float4 tangent : TANGENT;
				float4 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				float3 lightDir : TEXCOORD1;
				float3 viewDir : TEXCOORD2;
			};

			v2f vert(a2v v) {
				v2f o;
				o.pos = TransformObjectToHClip(v.vertex.xyz);
				o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

				float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w;
				float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
				// TANGENT_SPACE_ROTATION;

				half3 objectSpaceLightDir = TransformWorldToObjectDir(_MainLightPosition.xyz);
				o.lightDir = mul(rotation, objectSpaceLightDir).xyz;

				half3 objectSpaceViewDir = TransformWorldToObject(_WorldSpaceCameraPos.xyz - v.vertex.xyz);
				o.viewDir = mul(rotation, objectSpaceViewDir).xyz;
				return o;

				// v2f o;
				// o.pos = UnityObjectToClipPos(v.vertex);
				
				// o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
				
				// TANGENT_SPACE_ROTATION;
				// o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
				// o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
				
				// return o;
			}

			half4 frag(v2f v) : SV_Target {
				half3 tangentLightDir = normalize(v.lightDir);
				half3 tangentViewDir = normalize(v.viewDir);

				half3 tangentNormal = UnpackNormal(SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, v.uv));
				tangentNormal.xy *= _BumpScale;
				tangentNormal.z = sqrt(1.0 - max(0, dot(tangentNormal.xy, tangentNormal.xy)));

				half3 albedo = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, v.uv).rgb * _Color.rgb;

				// 环境光
				half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w) * albedo;

				// 漫反射
				half3 diffuse = _MainLightColor.rgb * albedo * max(0, dot(tangentNormal, tangentViewDir));

				// 高光
				half3 halfDir = normalize(tangentViewDir + tangentLightDir);
				half specularMask = SAMPLE_TEXTURE2D(_SpecularMask, sampler_SpecularMask, v.uv).r * _SpecularScale;
				half3 specular = _MainLightColor.rgb * _Specular.rbg * pow(max(0, dot(halfDir, tangentNormal)), _Gloss) * specularMask;

				return half4(ambient + diffuse + specular, 1.0);
			}

			ENDHLSL
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值