1. 程式人生 > >Unity 漫反射

Unity 漫反射

diffuse=Kd*_LightColor0*max(N·L,0)
Kd–環境光顏色、頂點顏色、點光源的距離衰減
_LightColor0–光源顏色
N·L–入射光向量 · 頂點法向量


Shader "Custom/MyDiffuse_Vertex"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _Bright("Bright",Float)=2
    }

    SubShader
    {
        Tags{ "RenderType" = "Opaque"
} Pass { Tags{ "LightMode" = "ForwardBase" } //處理平行光 CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "Lighting.cginc" uniform sampler2D _MainTex; uniform float
_Bright; struct appdata { float4 vertex:POSITION; float2 texcoord:TEXCOORD0; float3 normal:NORMAL; }; struct v2f { float4 pos:SV_POSITION; float2 uv:TEXCOORD0; fixed4 color
: COLOR; }; v2f vert(in appdata v) { //_LightColor0 :光的顏色 v2f o; o.pos = UnityObjectToClipPos(v.vertex); // 頂點變換到裁剪空間 o.uv = v.texcoord; float3 N = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));//法線 float3 L;//入射光方向 float atten;//點光源的距離衰減係數 if (_WorldSpaceLightPos0.w == 0)//_WorldSpaceLightPos0:平行光: (world space direction, 0); { L = normalize(_WorldSpaceLightPos0); atten = 1; } else //_WorldSpaceLightPos0: 其他光: (world space position, 1). { float3 light2Vertex = _WorldSpaceLightPos0 - mul(unity_ObjectToWorld, v.vertex); L = normalize(light2Vertex); float Length = length(light2Vertex); atten = 1 / Length; } float Kd = UNITY_LIGHTMODEL_AMBIENT*atten*_Bright; //計算入射光和法線的夾角的餘弦值,並剔除小於零的值(小於0的值相當於從物體的背面向物體表面照射,沒有物理意義) float cos = max(dot(N, L),0); fixed4 difColor = Kd*_LightColor0 *cos;//漫反射光的顏色 o.color = difColor; return o; } fixed4 frag(v2f i) :SV_Target { fixed4 col; col = tex2D(_MainTex, i.uv); col *= i.color; return col; } ENDCG } Pass { Tags{ "LightMode" = "ForwardAdd" } //處理點光源 Blend one one //**著重注意 //與前一個Pass 的顏色1:1混合 CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "Lighting.cginc" uniform sampler2D _MainTex; uniform float _Bright; struct appdata { float4 vertex:POSITION; float2 texcoord:TEXCOORD0; float3 normal:NORMAL; }; struct v2f { float4 pos:SV_POSITION; float2 uv:TEXCOORD0; fixed4 color : COLOR; }; v2f vert(in appdata v) { //_LightColor0 :光的顏色 v2f o; o.pos = UnityObjectToClipPos(v.vertex); // 頂點變換到裁剪空間 o.uv = v.texcoord; float3 N = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));//法線 float3 L;//入射光方向 float atten;//點光源的距離衰減係數 if (_WorldSpaceLightPos0.w == 0)//_WorldSpaceLightPos0:平行光: (world space direction, 0); { L = normalize(_WorldSpaceLightPos0); atten = 1; } else //_WorldSpaceLightPos0: 其他光: (world space position, 1). { float3 light2Vertex = _WorldSpaceLightPos0 - mul(unity_ObjectToWorld, v.vertex); L = normalize(light2Vertex); float Length = length(light2Vertex); atten = 1 / Length; } float Kd = UNITY_LIGHTMODEL_AMBIENT*atten*_Bright; //計算入射光和法線的夾角的餘弦值,並剔除小於零的值(小於0的值相當於從物體的背面向物體表面照射,沒有物理意義) float cos = max(dot(N, L),0); fixed4 difColor = Kd*_LightColor0 *cos;//漫反射光的顏色 o.color = difColor; return o; } fixed4 frag(v2f i) :SV_Target { fixed4 col; col = tex2D(_MainTex, i.uv); col *= i.color; return col; } ENDCG } } }