UnityShader——Phong光照模型 = Diffuse + Specular + Ambient

一.图形学颜色叠加乘法(各分量相乘)
 light(1, 1, 1) * stuff(0.2f, 0.3f, 0.5f) = (0.2f, 0.3f, 0.5f)

二.经验模型Phong

Phong = Ambient + Diffuse +Specular

1.Diffuse漫反射

Diffuse

Diffuse = dot ( -lightDir , N) * lightColor
这里点乘: dot( A , B) =|A| |B| Cos<A,B> ,所以夹角越小,diffuse越大;
Diffuse 兰伯特光照模型:
Shader "Unlit/Lambert"
{
	SubShader
	{
		Tags { "RenderType" = "Opaque" }
		LOD 100

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

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

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

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				//法线N
				fixed3 worldNormal = normalize(i.worldNormal);
				//光源方向LightDir
				fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
				//diffuse分量计算
				fixed3 diffuse = max(0.0, dot(worldNormal, -worldLightDir));
				//叠加光源色 
				fixed3 diffuseColor = diffuse * _LightColor0.xyz;

				return fixed4(diffuseColor, 0);
			}
			ENDCG
		}
	}
}

unity中diffuse漫反射效果

2.Specular镜面反射

Unity中加入Specular镜面反射

Specular = pow (max( 0 , dot(ReflectDir, ViewDir) ) , Glass) * lightColor;
反射方向用CG函数 reflect(L , N)计算;
ViewDir = normalize( _WorldSpaceCameraPos.xyz - i.worldPos); //i为v2f

加入Specular之后的Shader:

Shader "Unlit/Phong"
{
	Properties{
		_SpecularGlass("SpecularGlassStrength", Range(0,64))=32
	}
	SubShader
	{
		Tags { "RenderType" = "Opaque" }
		LOD 100

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

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

			struct v2f
			{
				float3 worldNormal:NORMAL;
				float4 vertex : SV_POSITION;
				float3 worldPos : TEXCOORD1;
			};	
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				return o;
			}
			
			float _SpecularGlass;

			fixed4 frag (v2f i) : SV_Target
			{
				fixed3 worldNormal = normalize(i.worldNormal);
				
				fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

				fixed3 diffuse = max(0.0, dot(worldNormal, -worldLightDir));

				fixed3 diffuseColor = diffuse  * _LightColor0.xyz;

				//计算reflect方向
				fixed3 reflectDir = normalize(reflect(worldLightDir, worldNormal));
				//计算view方向
				fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos);
				//specular分量
				fixed3 specular = pow(max(0.0, dot(reflectDir, viewDir)), _SpecularGlass);

				fixed3 specularColor = specular * _LightColor0.xyz;

				return fixed4(diffuseColor + specularColor, 0);
			}
			ENDCG
		}
	}
}

3.Ambient环境光

环境光之间获取 乘上叠加物体颜色;

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

给一个Obj颜色之后:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Phong光照模型是一种广泛应用于计算机图形学中的光照计算方法,它描述了光线如何与物体表面交互,产生反射和折射效果。以下是一个简单的Phong光照模型的伪代码概述: ```cpp // 假设我们有一个顶点着色器(Vertex Shader)和片段着色器(Fragment Shader)的例子 // 在顶点着色器中(Vertex Shader) float3 calculateNormal(in float3 position, in float3 texCoord, in float3 normal) { // 计算法线方向 } float3 calculateLightDirection(in float3 lightPosition, in float3 viewPosition) { // 计算光源方向 } float3 calculateReflectance(in float3 ambient, in float3 diffuse, in float3 specular, in float shininess) { // 计算反射颜色 return ambient + diffuse + specular; } void main() { float3 normal = calculateNormal(...); float3 lightDirection = calculateLightDirection(...); float3 viewDirection = normalize(viewPosition - position); float3 diffuse = max(dot(normal, lightDirection), 0.0); // 反射系数 float specular = pow(max(dot(normal, viewDirection), 0.0), shininess); // 镜面反射 float3 color = calculateReflectance(ambient, diffuse, specular, ...); outColor = color; // 输出到片段着色器 } // 在片段着色器中(Fragment Shader) void main() { in vec3 outColor; // 从顶点着色器传入的颜色 gl_FragColor = outColor; // 设置最终的颜色 } ``` 在这个例子中,我们首先计算每个像素的法线、光源方向和视线方向。然后使用这些值来计算反射(diffuse)和镜面反射(specular)部分,最后根据环境光(ambient)、直接光照(diffuse)和镜面反射(specular)混合出最终的颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值