08_Ambient(环境光)
根据法线方向,将环境光分为三个部分,顶、低、侧面。
vs:
varying vec3 nDir;
void main() {
nDir = normalize( normalMatrix * normal.xyz );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
fs:
// 顶点的法线
varying vec3 nDir;
uniform vec3 upAmbientColor;
uniform vec3 downAmbientColor;
uniform vec3 sideAmbientColor;
void main() {
float upMask = max(0.0, nDir.y);
float downMask = max(0.0, -nDir.y);
float sideMask = 1.0 - upMask - downMask;
vec3 col = upAmbientColor * upMask +
downAmbientColor * downMask +
sideAmbientColor * sideMask;
gl_FragColor = vec4(col, 1.);
}