Unity Shaders and Effects Cookbook (3

编程入门 行业动态 更新时间:2024-10-24 15:19:08

Unity <a href=https://www.elefans.com/category/jswz/34/1668456.html style=Shaders and Effects Cookbook (3"/>

Unity Shaders and Effects Cookbook (3

上一节学习了如何使用 Unity 自带的 BlinnPhong 高光光照模型,这一节手动创建一个 高光光照模型。


首先创建 Shader 与 Material ,命名 为 BasicPhongSpecular 。


在场景中添加一个 Sphere ,一个 Direction Light 。


然后来修改 Shader 。


在之前的 3-1 创建基本的Diffuse 光照模型的时候,介绍了 按照需求的不同,有三种不同的光照模型函数格式:

//不需要视角方向的前向着色
inline half4 LightingBasicDiffuse(SurfaceOutput so,half3 lightDir,half atten)
{half4 color=(0,0,0,0);return color;
}//需要视角方向的前向着色
inline half4 LightingBasicDiffuseWithViewDir(SurfaceOutput so,half3 lightDir,half3 viewDir,half atten)
{half4 color=(0,0,0,0);return color;
}//需要使用延迟着色
inline half4 LightingBasicDiffuse_PrePass(SurfaceOutput so,half4 light)
{half4 color=(0,0,0,0);return color;
}

按照日常生活中的经验,当人眼睛对着镜子,如果正好对着光源反射方向,那么眼睛看的最亮,如果偏离光源反射方向,那么就会变暗。

所以高光是和 眼睛方向有关的,也就是说,高光需要的是 第二种,需要视角方向的前向着色。转自



如上图中,N是法线,R 是反射光,左边灯泡是光源,右边大眼睛是相机眼睛。

眼睛 和 反射光 越靠近,夹角越小,眼睛看的越亮,这就是高光模型的核心。



上图中介绍了 Phong 光照模型的算法。

反射向量 R = 2N(L · N ) - L

高光强度 Spec = (R · Eye )


按照上面的算法,在Shader 中进行编程。至于计算公式为什么这么写,暂时我也不懂,后面了解了再补上!


下面是 BasicPhongSpecular.shader  具体内容

Shader "CookBookShaders/BasicPhongSpecular" 
{Properties {_MainTint("Diffuse Tint",Color) =(1,1,1,1)_MainTex ("Base (RGB)", 2D) = "white" {}_SpecularColor("Specular Color",Color)=(1,1,1,1)_SpecularPower("Specular Power",Range(1,30))=1}SubShader {Tags { "RenderType"="Opaque" }LOD 200CGPROGRAM#pragma surface surf BasicPhongSpecularfloat4 _MainTint;sampler2D _MainTex;float4 _SpecularColor;float _SpecularPower;struct Input {float2 uv_MainTex;};void surf (Input IN, inout SurfaceOutput o) {half4 c = tex2D (_MainTex, IN.uv_MainTex) * _MainTint;o.Albedo = c.rgb;o.Alpha = c.a;}inline fixed4 LightingBasicPhongSpecular(SurfaceOutput s,fixed3 lightDir,half3 viewDir,fixed atten){//计算漫反射float diffuse=dot(s.Normal,lightDir);//计算反射光方向float3 reflectionVector=normalize( ( 2.0 * s.Normal * diffuse ) - lightDir); //首先dot()求反射光与眼睛位置夹角cos值,眼睛位置越接近反射光,夹角越小,值越大,眼睛看到的光越亮。float specularLightPower=pow( max( 0,dot(reflectionVector,viewDir ) ) ,_SpecularPower );float3 specularColorFinal=_SpecularColor.rgb * specularLightPower;fixed4 c;c.rgb=( s.Albedo * _LightColor0.rgb * diffuse ) + ( _LightColor0.rgb * specularColorFinal ) ;//c.rgb=( s.Albedo * _LightColor0.rgb * diffuse );c.a=1.0;return c;}ENDCG} FallBack "Diffuse"
}

这里计算出来的 diffuse ,如果 ==1,说明物体是正对着光源方向的,如果==-1,说明是背对着光源方向的。


然后计算反射向量,首先对顶点Normal 乘以2 再乘以 diffuse。 得到的值再减去 光照方向向量。这样实现法线朝向 光源弯曲的效果。。所以如果是远离光源的法线向量,会被强制朝向光源方向。。   --书上这么写,我没明白是什么意思。

转自

测试效果图



示例工程下载:


更多推荐

Unity Shaders and Effects Cookbook (3

本文发布于:2024-02-25 22:03:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Shaders   Unity   Cookbook   Effects

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!