Built-in转URP Chapter6(2)-片元级别漫反射

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

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

Built-CG写法:

Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level" {
	Properties {
		_Diffuse("Diffuse", Color) = (1, 1, 1, 1)
	}

	SubShader {
		Pass {

			Tags {
				"LightMode"="ForwardBase"
			}
			
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

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

			fixed4 _Diffuse;

			struct v2f {
				float4 pos : SV_POSITION;
				float3 worldNor : TEXCOORD0;
			};

			v2f vert(appdata_full data) {
				v2f v;
				v.pos = UnityObjectToClipPos(data.vertex);
				v.worldNor = mul(data.normal, (float3x3)unity_WorldToObject);
				return v;
			}

			fixed4 frag(v2f v) : SV_Target {
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				fixed3 worldVetNormal = normalize(v.worldNor);
				fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldVetNormal, worldLightDir));
				fixed3 color = diffuse + ambient;
				return fixed4(color, 1);
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}

URP+HLSL写法:

Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level" {
	Properties {
		_Diffuse("Diffuse", Color) = (1, 1, 1, 1)
	}

	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 _Diffuse;
CBUFFER_END

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

			struct v2f {
				float4 pos : SV_POSITION;
				half3 worldNormal : TEXCOORD0;
			};

			v2f vert(a2v data) {
				v2f v;
				v.pos = TransformObjectToHClip(data.vertex.xyz);
				v.worldNormal = mul(data.normal, (float3x3)UNITY_MATRIX_I_M);
				return v;
			}

			half4 frag(v2f v) : SV_Target {
				half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
				half3 worldNormal = normalize(v.worldNormal);
				half3 worldLightDir = normalize(_MainLightPosition.xyz);
				half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));
				half3 color = diffuse + ambient;
				return half4(color, 1);
			}
			ENDHLSL
		}
	}
	FallBack "Universal Render Pipeline/Simple Lit"
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值