高光反射

本文深入探讨了Phong和BlinnPhong光照模型在计算机图形学中的应用,详细解析了顶点高光和片元高光反射Shader的实现原理。通过对比Phong与BlinnPhong模型,阐述了两者在高光计算上的差异,为读者提供了全面的光照模型理解和实践指导。

在这里插入图片描述
Phong顶点高光反射Shader

Shader "Unlit/01"
{
	Properties
	{
		_Diffuse("Diffuse", Color) = (1,1,1,1)
		_Specular("Specular", Color) = (1,1,1,1)
		_Gloss("Gloss", Range(1,256)) = 5
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			fixed4 _Diffuse;
			fixed4 _Specular;
			float _Gloss;

			struct v2f
			{
				float4 vertex : SV_POSITION;
				fixed3 color: Color;
			};

			v2f vert (appdata_base v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				//取得物体顶点的世界位置信息
				fixed3 worldPos = mul(unity_ObjectToWorld, v.vertex);

				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				//世界空间下的法线方向
				fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
				//fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
				//光的方向
				fixed3 worldLight = UnityWorldSpaceLightDir(worldPos);
				//先取得漫反射信息 入射光线颜色和强度*材质漫反射系数*max(0, dot(worldNormal,worldLight))
				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal,worldLight));
				//reflect(I, N)	根据光源方向和表面法向量N计算反射向量,仅对三元向量有效
				fixed3 reflectDir = normalize(reflect(-worldLight,worldNormal));
				//世界空间坐标下  相机位置-物体顶点位置
				//fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - UnityObjectToWorldDir(v.vertex));
				fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
				//fixed3 viewDir = normalize(WorldSpaceViewDir(v.vertex));
				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(reflectDir,viewDir)),_Gloss);
				//漫反射加环境光加高光
				o.color = diffuse + ambient + specular;
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				return fixed4(i.color,1);
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}

Phong片元高光反射Shader

Shader "Unlit/02"
{
   Properties
	{
		_Diffuse("Diffuse", Color) = (1,1,1,1)
		_Specular("Specular", Color) = (1,1,1,1)
		_Gloss("Gloss", Range(1,256)) = 5
	}

	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			fixed4 _Diffuse;
			fixed4 _Specular;
			float _Gloss;

			struct v2f
			{
				float4 vertex : SV_POSITION;
				fixed3 worldNormal: TEXCOORD0;
				float3 worldPos: TEXCOORD1;
			};

			v2f vert (appdata_base v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				fixed3 worldNormal = UnityObjectToWorldNormal( v.normal);
				o.worldNormal = worldNormal;
				o.worldPos =  mul(unity_ObjectToWorld, v.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				//漫反射
				fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
				//fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0,dot(worldLightDir,i.worldNormal));

				//高光反射
				fixed3 reflectDir = normalize(reflect(-worldLightDir,i.worldNormal));
				//fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
				fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);
				
				fixed3 color = ambient + diffuse + specular;
				return fixed4(color,1);
			}
			ENDCG
		}
	}
}

BlinnPhong高光反射

Shader "Unlit/03"
{
   Properties
	{
		_Diffuse("Diffuse", Color) = (1,1,1,1)
		_Specular("Specular", Color) = (1,1,1,1)
		_Gloss("Gloss", Range(1,256)) = 5
	}

	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			fixed4 _Diffuse;
			fixed4 _Specular;
			float _Gloss;

			struct v2f
			{
				float4 vertex : SV_POSITION;
				fixed3 worldNormal: TEXCOORD0;
				float3 worldPos: TEXCOORD1;
			};

			v2f vert (appdata_base v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				fixed3 worldNormal = UnityObjectToWorldNormal( v.normal);
				o.worldNormal = worldNormal;
				o.worldPos = mul(unity_ObjectToWorld, v.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				//漫反射
				fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
				//fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0,dot(worldLightDir,i.worldNormal));

				//高光反射
				//fixed3 reflectDir = normalize(reflect(-worldLightDir,i.worldNormal));
				fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
				//fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
				fixed3 halfDir = normalize(worldLightDir + viewDir);
				//去Phong光照的区别 
				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(i.worldNormal,halfDir)),_Gloss);
				
				fixed3 color = ambient + diffuse + specular;
				return fixed4(color,1);
			}
			ENDCG
		}
	}
}

### 高光反射效果的实现 在 Unity 中,可以通过编写自定义 Shader 来实现高光反射效果。这种效果通常基于 Blinn-Phong 或 Phong 反射模型来计算光线与表面法线之间的交互关系。 #### 使用 Blinn-Phong 模型的高光反射 Blinn-Phong 是一种改进版的 Phong 模型,它通过引入半角向量(Half Vector)优化了高光计算效率。以下是其实现的核心逻辑: 1. **输入参数** 定义材质的颜色属性以及光源的相关数据。 2. **顶点着色器 (Vertex Shader)** 计算世界空间中的位置和法线,并传递给片段着色器。 3. **片段着色器 (Fragment Shader)** 利用 Blinn-Phong 公式计算最终颜色,包括环境光、漫反射和镜面反射分量。 下面是具体的代码示例: ```csharp Shader "Custom/SpecularReflection" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _SpecColor ("Specular Color", Color) = (1,1,1,1) _Shininess ("Shininess", Float) = 10 } SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float3 normal : NORMAL; }; struct v2f { float4 pos : SV_POSITION; float3 worldNormal : TEXCOORD0; float3 viewDir : TEXCOORD1; }; fixed4 _Color; fixed4 _SpecColor; float _Shininess; v2f vert(appdata_t v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal)); o.viewDir = normalize(ObjSpaceViewDir(v.vertex)); // 获取视图方向 return o; } fixed4 frag(v2f i) : SV_Target { float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz); // 主光源方向 // Diffuse Term float NdotL = max(0, dot(i.worldNormal, lightDirection)); // Specular Term float3 halfVector = normalize(lightDirection + i.viewDir); // 半角向量 float NdotH = max(0, dot(i.worldNormal, halfVector)); float specularIntensity = pow(NdotH, _Shininess); // Combine terms fixed4 diffuseTerm = _Color * NdotL; // 漫反射项 fixed4 specularTerm = _SpecColor * specularIntensity; // 镜面反射项 return diffuseTerm + specularTerm; } ENDCG } } } ``` 此代码实现了基本的 Blinn-Phong 高光反射效果[^1]。其中 `_Shininess` 控制高光区域的大小,而 `_SpecColor` 定义了高光的颜色。 --- #### 关于 Lightmap 和 Directional Lighting 的兼容性 如果希望该 Shader 能够兼容 baked lighting,则可以加入以下宏指令以支持方向贴图: ```csharp #pragma multi_compile _ DIRLIGHTMAP_COMBINED ``` 这使得 Shader 在运行时能够自动检测是否存在方向贴图并适配相应的光照条件[^2]。 --- #### 设置透明队列标签 为了确保 Shader 正确处理透明对象,在 `SubShader` 中设置合适的渲染队列非常重要。例如: ```csharp Tags { "Queue" = "Transparent" } ``` 这样可以让物体按照正确的顺序绘制到屏幕上[^3]。 --- #### 进一步扩展功能 对于更复杂的场景需求,还可以考虑集成其他特性,比如: - 法线映射(Normal Mapping) - 环境遮挡(Ambient Occlusion) 这些都可以通过增加纹理采样操作完成。具体实现方式可以在基础代码之上逐步叠加新功能[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值