Unity Shader标准光照模型的实现

复制代码
 1 Shader "Custom/P_Illumination"
 2 {
 3     Properties{
 4         _BasicColor("基础颜色",Color)=(1.0,1.0,1.0,1.0)
 5         _SpecularColor("高光颜色",Color)=(0,0,0,0)
 6         _SpecularPow("高光范围",Range(1,256))=50
 7         _DiffuseChoice("兰伯特<——>半兰伯特",Range(0,1))=0
 8         _SpecularChoice("Phone<——>Blinn",Range(0,1))=0
 9     }
10     SubShader
11     {
12         Pass
13         {
14             tags{
15                 "LightMode"="ForwardBase"
16             }
17             CGPROGRAM
18             #pragma vertex vert
19             #pragma fragment frag
20             #pragma target 3.0
21             #include "UnityCG.cginc"
22             #include "Lighting.cginc"
23             fixed4 _BasicColor;
24             float _SpecularPow;
25             fixed _SpecularChoice;
26             fixed _DiffuseChoice;
27             fixed4 _SpecularColor;
28             struct a2v{
29                 float4 position :position;
30                 float3 normal :normal;
31             };
32             struct v2f{
33                 float4 sv_position :sv_position;
34                 float3 texcoord :texcoord0;
35                 float3 wc_position :texcoord1;
36             };
37             v2f vert(a2v a){
38                 v2f v;
39                 v.sv_position=UnityObjectToClipPos(a.position);
40                 v.texcoord=UnityObjectToWorldNormal(a.normal);
41                 v.wc_position=mul(unity_ObjectToWorld,a.position).xyz;
42                 return v;
43             }
44             float4 frag(v2f v) :SV_Target{
45                 fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;
46                 fixed3 lDir=normalize(_WorldSpaceLightPos0.xyz);
47                 fixed3 nDir=normalize(v.texcoord);
48                 fixed3 rDir=normalize(reflect(-lDir,nDir));
49                 fixed3 vDir=normalize(_WorldSpaceCameraPos.xyz-v.wc_position);
50                 fixed3 hDir=normalize(lDir+vDir);
51                 fixed Lambert=dot(nDir,lDir);
52                 fixed HalfLambert=0.5+0.5*Lambert;
53                 fixed3 diffuse=_BasicColor.rgb*saturate(Lambert*(1.0-_DiffuseChoice)+HalfLambert*_DiffuseChoice);
54                 fixed3 Phone=saturate(dot(rDir,vDir));
55                 fixed3 Blinn=saturate(dot(nDir,hDir));
56                 fixed3 Specular=_SpecularColor.rgb*pow((Phone*(1.0-_SpecularChoice)+Blinn*_SpecularChoice),_SpecularPow);
57                 return fixed4(ambient+_LightColor0.rgb*(diffuse+Specular),1.0);
58             }
59             ENDCG
60         }
61     }
62 }
复制代码

实现环境光+漫反射+高光反射的标准光照模型

实现兰伯特,半兰伯特、Phone,Blinn-Phone之间线性变化的逐像素方法Shader

学习资料:Unity Shader 入门精要+庄懂的技术美术入门课

 

posted @   非遂爻辞  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示