先上图:左边是背面,右边是正面
然后是代码:
Shader "Custom/ShaderExample9"
{
Properties
{
_Color ("Front Material Diffuse Color", Color) = (1,1,1,1)
_SpecColor ("Front Material Specular Color", Color) = (1,1,1,1)
_Shininess("Front Material Shininess", Float) = 10
_BackColor ("Back Material Diffuse Color", Color) = (1,1,1,1)
_BackSpecColor ("Back Material Specular Color", Color) = (1,1,1,1)
_BackShininess ("Back Material Shininess", Float) = 10
}
SubShader
{
Pass
{
Tags { "LightMode" = "ForwardBase" }
Cull Off
GLSLPROGRAM
uniform vec4 _Color;
uniform vec4 _SpecColor;
uniform float _Shininess;
uniform vec4 _BackColor;
uniform vec4 _BackSpecColor;
uniform float _BackShininess;
uniform vec3 _WorldSpaceCameraPos;
uniform mat4 _Object2World;
uniform mat4 _World2Object;
uniform vec4 _WorldSpaceLightPos0;
uniform vec4 _LightColor0;
#ifdef VERTEX
//顶点着色器
out vec4 frontColor;
out vec4 backColor;
void main()
{
mat4 modelMatrix = _Object2World;
mat4 modelMatrixInverse = _World2Object;
vec3 normalDirection = normalize(vec3(vec4(gl_Normal, 0.0) * modelMatrixInverse));
vec3 viewDirection = normalize(vec3(
vec4(_WorldSpaceCameraPos, 1.0) - modelMatrix * gl_Vertex));
vec3 lightDirection;
float attenuation;
if (0.0 == _WorldSpaceLightPos0.w) //方向光
{
attenuation = 1.0;
lightDirection = normalize(vec3(_WorldSpaceLightPos0));
}
else //点光源或聚光灯
{
vec3 vertexToLightSource = vec3(_WorldSpaceLightPos0 - modelMatrix * gl_Vertex);
float distance = length(vertexToLightSource);
attenuation = 1.0/ distance;
lightDirection = normalize(vertexToLightSource);
}
//正面的光照计算-------------------------------------------------------
vec3 ambientLighting = vec3(gl_LightModel.ambient) * vec3(_Color);
vec3 diffuseReflection = attenuation * vec3(_LightColor0) * vec3(_Color)
* max(0.0, dot(normalDirection, lightDirection));
vec3 specularReflection;
if (dot(normalDirection, lightDirection) < 0.0)
{
specularReflection = vec3(0.0, 0.0, 0.0);
}
else
{
specularReflection = attenuation * vec3(_LightColor0) * vec3(_SpecColor)
* pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), _Shininess);
}
frontColor = vec4(ambientLighting + diffuseReflection + specularReflection, 1.0);
//--------------------------------------------------------------------
//背面的光照计算---------------------------------------------------------
vec3 backAmbientLighting = vec3(gl_LightModel.ambient) * vec3(_BackColor);
vec3 backDiffuseReflection = attenuation * vec3(_LightColor0) * vec3(_BackColor)
* max(0.0, dot(-normalDirection, lightDirection));
vec3 backSpecularReflection;
if (dot(-normalDirection, lightDirection) < 0.0)
{
backSpecularReflection = vec3(0.0, 0.0, 0.0);
}
else
{
backSpecularReflection = attenuation * vec3(_LightColor0) * vec3(_BackSpecColor)
* pow(max(0.0, dot(reflect(-lightDirection, -normalDirection), viewDirection)),
_BackShininess);
}
backColor = vec4(backAmbientLighting + backDiffuseReflection + backSpecularReflection, 1.0);
//----------------------------------------------------------------------
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif
#ifdef FRAGMENT
//片元着色器
in vec4 frontColor;
in vec4 backColor;
void main()
{
if (gl_FrontFacing)
{
gl_FragColor = frontColor;
}
else
{
gl_FragColor = backColor;
}
}
#endif
ENDGLSL
}
}
}