1、添加点光源后,右侧的系统内嵌的小球被点光源照亮了,左侧的自定义的shader小球没有被照亮
2、辅助光照函数ShadeVertexLightsFull只能使用在pass通道的Vertext中,tags标签必须修改成下面的值
3、添加代码o.color.rgb = ShadeVertexLights(v.vertex,v.normal);
Shader "Custom/s_diffuse" {
SubShader{
pass {
tags{"LightMode" = "Vertex"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
#include "lighting.cginc"
struct v2f {
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
float3 N = normalize(v.normal);
float3 L = normalize(_WorldSpaceLightPos0);
N = mul(float4(N,0), _World2Object).xyz;
N = normalize(N);
float ndotl = saturate(dot(N, L));
o.color = _LightColor0*ndotl;
o.color.rgb = ShadeVertexLights(v.vertex,v.normal);
return o;
}
fixed4 frag(v2f IN) :COLOR{
//环境光照
return IN.color+UNITY_LIGHTMODEL_AMBIENT;
}
ENDCG
}
}
}
点光源对物体起了作用
4、但是着色与左侧系统默认的不一样,使用Shade4PointLights函数
Shader "Custom/s_diffuse" {
SubShader{
pass {
tags{"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
#include "lighting.cginc"
struct v2f {
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
float3 N = normalize(v.normal);
float3 L = normalize(_WorldSpaceLightPos0);
N = mul(float4(N,0), _World2Object).xyz;
N = normalize(N);
float ndotl = saturate(dot(N, L));
o.color = _LightColor0*ndotl;
float3 wpos = mul(_Object2World,v.vertex).xyz;
o.color.rgb = Shade4PointLights(unity_4LightPosX0,unity_4LightPosY0,unity_4LightPosZ0,
unity_LightColor[0].rgb,unity_LightColor[1].rgb,
unity_LightColor[2].rgb,unity_LightColor[3].rgb,
unity_4LightAtten0,
wpos,N);
return o;
}
fixed4 frag(v2f IN) :COLOR{
//环境光照
return IN.color+UNITY_LIGHTMODEL_AMBIENT;
}
ENDCG
}
}
}
球体已经被点光源照亮
5、但是平行光没有添加上去,因为只赋了rgb的颜色
改为+=就可以与平行光进行合并
Shader "Custom/s_diffuse" {
SubShader{
pass {
tags{"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
#include "lighting.cginc"
struct v2f {
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
float3 N = normalize(v.normal);
float3 L = normalize(_WorldSpaceLightPos0);
N = mul(float4(N,0), _World2Object).xyz;
N = normalize(N);
float ndotl = saturate(dot(N, L));
o.color = _LightColor0*ndotl;
float3 wpos = mul(_Object2World,v.vertex).xyz;
o.color.rgb += Shade4PointLights(unity_4LightPosX0,unity_4LightPosY0,unity_4LightPosZ0,
unity_LightColor[0].rgb,unity_LightColor[1].rgb,
unity_LightColor[2].rgb,unity_LightColor[3].rgb,
unity_4LightAtten0,
wpos,N);
return o;
}
fixed4 frag(v2f IN) :COLOR{
//环境光照
return IN.color+UNITY_LIGHTMODEL_AMBIENT;
}
ENDCG
}
}
}