原文 http://www.azure.com.cn/article.asp?id=231
用深度值近似模拟物体的厚度,
厚度越小处透光越多。
varying vec4 position;
varying vec4 world_pos;
varying vec2 texcoord0;
varying vec3 normal;
void main(void)
{
gl_Position = ftransform();
world_pos = gl_Position;
position = gl_Vertex;
texcoord0 = gl_MultiTexCoord0.st;
normal = gl_NormalMatrix * gl_Normal;
}
varying vec4 position;
varying vec4 world_pos;
varying vec2 texcoord0;
varying vec3 normal;
uniform vec4 camera_pos;
uniform float near_clip;
uniform float ampify;
uniform vec3 singlelight;
uniform sampler2D base;
uniform vec3 lightcolor;
uniform float ks;
void main(void)
{
float distance =( distance(camera_pos, position) - near_clip )/200.0 * ampify;
distance = log2(distance);
//gl_FragColor = vec4(distance, distance, distance, 1);
vec4 lightpos = vec4(singlelight.x,
singlelight.y,
singlelight.z,
1);
vec4 lightvec = lightpos - position;
normalize(lightvec);
normalize(normal);
float diffuse = max(0.0, dot(normal, lightvec.xyz));
vec4 diffusecolor = vec4(diffuse*lightcolor.r, diffuse*lightcolor.g, diffuse*lightcolor.b, 1.0);
vec3 V = normalize(camera_pos - position).xyz;
vec3 H = normalize(lightvec.xyz + V);
float speclight = pow(max(dot(normal.xyz, H), 0.0), ks);
vec4 specularcolor = vec4(speclight, speclight, speclight, 1.0);
gl_FragColor = texture2D(base, texcoord0) * distance + diffusecolor + specularcolor;
//gl_FragColor.r = distance;
//gl_FragColor = diffusecolor;
}