通过使用Unity Shader实现基础光照效果

Posted CaiLin907

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过使用Unity Shader实现基础光照效果相关的知识,希望对你有一定的参考价值。

目录

 

标准光照模型

漫反射和高光反射的计算公式

Blinn-Phong光照模型

逐顶点漫反射光照模型

逐像素漫反射光照模型

逐顶点高光反射光照模型

逐像素高光反射光照模型

Blinn-Phong光照模型


标准光照模型

自发光部分,描述当给定一个方向时,一个表面本身会向该方向发射多少辐射量;

高光反射部分,用于描述当光线从光源照射到模型表面时,该表面会在完全镜面反射方向散射多少辐射量;

漫反射部分,当光线从光源照射到环境表面时,该表面会向每个方向散射多少辐射量;

环境光部分,用于描述其他所有的间接光照。

 

 

漫反射和高光反射的计算公式

漫反射:符合兰伯特定律C diffuse=(c light* m diffuse)max(0,n * I)

       n是表面法线,I是指向光源的单位矢量,m diffuse是材质的漫反射颜色,c light是光源颜色

高光反射:需要知道表面法线,视角方向,光源方向,反射方向,

r = 2(n^ * 1)n^ -1,Cspscular = (clight * mspecular)max(0,v^ * r)

 

Blinn-Phong光照模型

并不完全符合世界中的光照现象,使用漫反射和高光反射的和来对反射光找进行建模的基本思想,并且提出了基于经验的计算高光反射的方法;

此模型是各向同性的,当我们固定视角和房源方向旋转这个表面时,反射不会发生任何改变。


逐顶点漫反射光照模型

Shader "Unity Shaders Book/Chapter 6/Diffuse Vertex-Level" 
	Properties
		_Diffuse("Diffuse", Color) = (1, 1, 1, 1)
	
	SubShader
		Pass 
			Tags  "LightMode" = "ForwardBase" 

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#include "Lighting.cginc"

			fixed4 _Diffuse;

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

			struct v2f 
				float4 pos : SV_POSITION;
				fixed3 color : COLOR;
			;

			v2f vert(a2v v) 
				v2f o;
				// Transform the vertex from object space to projection space
				o.pos = UnityObjectToClipPos(v.vertex);

				// Get ambient term
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

				// Transform the normal from object space to world space
				fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
				// Get the light direction in world space
				fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
				// Compute diffuse term
				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));

				o.color = ambient + diffuse;

				return o;
			

			fixed4 frag(v2f i) : SV_Target 
				return fixed4(i.color, 1.0);
			

			ENDCG
		
	
	FallBack "Diffuse"

逐像素漫反射光照模型

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 "Lighting.cginc"

			fixed4 _Diffuse;

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

			struct v2f 
				float4 pos : SV_POSITION;
				float3 worldNormal : TEXCOORD0;
			;

			v2f vert(a2v v) 
				v2f o;
				// Transform the vertex from object space to projection space
				o.pos = UnityObjectToClipPos(v.vertex);

				// Transform the normal from object space to world space
				o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);

				return o;
			

			fixed4 frag(v2f i) : SV_Target 
				// Get ambient term
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

				// Get the normal in world space
				fixed3 worldNormal = normalize(i.worldNormal);
				// Get the light direction in world space
				fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

				// Compute diffuse term
				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));

				fixed3 color = ambient + diffuse;

				return fixed4(color, 1.0);
			

		ENDCG
	
	
	FallBack "Diffuse"

逐顶点高光反射光照模型

Shader "Unity Shaders Book/Chapter 6/Specular Vertex-Level" 
	Properties
		_Diffuse("Diffuse", Color) = (1, 1, 1, 1)
		_Specular("Specular", Color) = (1, 1, 1, 1)
		_Gloss("Gloss", Range(8.0, 256)) = 20
	
	SubShader
		Pass 
			Tags  "LightMode" = "ForwardBase" 

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#include "Lighting.cginc"

			fixed4 _Diffuse;
			fixed4 _Specular;
			float _Gloss;

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

			struct v2f 
				float4 pos : SV_POSITION;
				fixed3 color : COLOR;
			;

			v2f vert(a2v v) 
				v2f o;
				// Transform the vertex from object space to projection space
				o.pos = UnityObjectToClipPos(v.vertex);

				// Get ambient term
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

				// Transform the normal from object space to world space
				fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
				// Get the light direction in world space
				fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

				// Compute diffuse term
				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));

				// Get the reflect direction in world space
				fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
				// Get the view direction in world space
				fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, v.vertex).xyz);

				// Compute specular term
				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);

				o.color = ambient + diffuse + specular;

				return o;
			

			fixed4 frag(v2f i) : SV_Target 
				return fixed4(i.color, 1.0);
			

			ENDCG
		
	
	FallBack "Specular"

逐像素高光反射光照模型

Shader "Unity Shaders Book/Chapter 6/Specular Pixel-Level" 
	Properties
		_Diffuse("Diffuse", Color) = (1, 1, 1, 1)
		_Specular("Specular", Color) = (1, 1, 1, 1)
		_Gloss("Gloss", Range(8.0, 256)) = 20
	
	SubShader
		Pass 
			Tags  "LightMode" = "ForwardBase" 

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#include "Lighting.cginc"

			fixed4 _Diffuse;
			fixed4 _Specular;
			float _Gloss;

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

			struct v2f 
				float4 pos : SV_POSITION;
				float3 worldNormal : TEXCOORD0;
				float3 worldPos : TEXCOORD1;
			;

			v2f vert(a2v v) 
				v2f o;
				// Transform the vertex from object space to projection space
				o.pos = UnityObjectToClipPos(v.vertex);

				// Transform the normal from object space to world space
				o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
				// Transform the vertex from object spacet to world space
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

				return o;
			

			fixed4 frag(v2f i) : SV_Target 
				// Get ambient term
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

				// Compute diffuse term
				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));

				// Get the reflect direction in world space
				fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
				// Get the view direction in world space
				fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
				// Compute specular term
				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);

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

			ENDCG
		
	
	FallBack "Specular"

Blinn-Phong光照模型

Shader "Unity Shaders Book/Chapter 6/Blinn-Phong" 
	Properties
		_Diffuse("Diffuse", Color) = (1, 1, 1, 1)
		_Specular("Specular", Color) = (1, 1, 1, 1)
		_Gloss("Gloss", Range(8.0, 256)) = 20
	
		SubShader
			Pass 
				Tags  "LightMode" = "ForwardBase" 

				CGPROGRAM

				#pragma vertex vert
				#pragma fragment frag

				#include "Lighting.cginc"

				fixed4 _Diffuse;
				fixed4 _Specular;
				float _Gloss;

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

				struct v2f 
					float4 pos : SV_POSITION;
					float3 worldNormal : TEXCOORD0;
					float3 worldPos : TEXCOORD1;
				;

				v2f vert(a2v v) 
					v2f o;
					// Transform the vertex from object space to projection space
					o.pos = UnityObjectToClipPos(v.vertex);

					// Transform the normal from object space to world space
					o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);

					// Transform the vertex from object spacet to world space
					o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

					return o;
				

				fixed4 frag(v2f i) : SV_Target 
					// Get ambient term
					fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

					fixed3 worldNormal = normalize(i.worldNormal);
					fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

					// Compute diffuse term
					fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLightDir));

					// Get the view direction in world space
					fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
					// Get the half direction in world space
					fixed3 halfDir = normalize(worldLightDir + viewDir);
					// Compute specular term
					fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss);

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

				ENDCG
			
	
		FallBack "Specular"

 

 

 

 

 

 

以上是关于通过使用Unity Shader实现基础光照效果的主要内容,如果未能解决你的问题,请参考以下文章

Unity Shader 光照模型(基础公式和代码实现)

学unityshader之前学unity吗

Unity无光照假阴影Shader实现及常见问题总结

Unity Shader ------ 透明效果之半透明效果的实现及原理

优化实现Mobile Diffuse实时光照shader

着色器编程_unity中的基础纹理,使用Unity Shader实现基础纹理的渲染效果