版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35759688/article/details/77926246
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
}
}
}