逐顶点渲染效果:
逐像素渲染效果
逐顶点渲染顶点shader
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
uniform mat4 NM;
uniform vec3 U_LightPos;
uniform vec3 U_EyePos;
uniform vec4 U_DiffuseLightColor;
uniform vec4 U_DiffuseMaterial;
uniform vec4 U_SpecularLightColor;
uniform vec4 U_SpecularMaterial;
varying vec4 V_Color;
void main()
{
//光线方向
vec3 L=U_LightPos;
L=normalize(L);
//法向量转换到世界空间
vec3 n=normalize(mat3(NM)*normal);
//计算光照强度
float intensity=max(0.0,dot(L,n));
//根据不同的光照强度,计算出不同的颜色
if (intensity > 0.95)
V_Color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
V_Color = vec4(0.6,0.3,0.3,1.0);
else if (intensity > 0.25)
V_Color = vec4(0.4,0.2,0.2,1.0);
else
V_Color = vec4(0.2,0.1,0.1,1.0);
gl_Position=P*V*M*vec4(pos,1.0);
}
uniform vec4 U_AmbientLightColor;
uniform vec4 U_AmbientMaterial;
varying vec4 V_Color;
void main()
{
vec4 ambientColor=U_AmbientLightColor*U_AmbientMaterial;
gl_FragColor=ambientColor+V_Color;
}
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
uniform mat4 NM;
varying vec3 V_Normal;
varying vec3 V_WorldPos;
void main()
{
V_Normal=mat3(NM)*normal;
vec4 worldPos=M*vec4(pos,1.0);
V_WorldPos=worldPos.xyz;
gl_Position=P*V*worldPos;
}
uniform vec3 U_LightPos;
uniform vec3 U_EyePos;
uniform vec4 U_AmbientLightColor;
uniform vec4 U_AmbientMaterial;
uniform vec4 U_DiffuseLightColor;
uniform vec4 U_DiffuseMaterial;
uniform vec4 U_SpecularLightColor;
uniform vec4 U_SpecularMaterial;
varying vec3 V_Normal;
varying vec3 V_WorldPos;
void main()
{
//计算环境光
vec4 ambientColor=U_AmbientLightColor*U_AmbientMaterial;
//光线方向
vec3 L=U_LightPos;
L=normalize(L);
//法向量转换到世界空间
vec3 n=normalize(V_Normal);
//根据不同的光照强度给出不同颜色值
float diffuseIntensity=max(0.0,dot(L,n));
vec4 diffuseColor;
if (diffuseIntensity > 0.95)
diffuseColor = vec4(1.0,0.5,0.5,1.0);
else if (diffuseIntensity > 0.5)
diffuseColor = vec4(0.6,0.3,0.3,1.0);
else if (diffuseIntensity > 0.25)
diffuseColor = vec4(0.4,0.2,0.2,1.0);
else
diffuseColor = vec4(0.2,0.1,0.1,1.0);
gl_FragColor=ambientColor+diffuseColor;
}