半透明物体边缘透射/散射这样的效果应该用SSS(Sub-Surface Scatter)的技术来模拟,通俗点说就是玉的效果.但是本人数学不过关,看不懂SSS :-( ,.
今天早上在想,如果考虑到物体的在沿着视线方向的厚度,以及正对光源的程度,是不是也可以出来点半透明的效果呢?
来不及写一个程序来计算物体表面的厚度信息, 所以假设,越是边缘的地方,越薄.
shader很简单. 用RenderMonkey写的。
http://xreal.51.net/Game/fakess.rfx
//Vertex Shader use GLSL
uniform vec3 fvLightPosition;
uniform vec3 fvEyePosition;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying vec3 Normal;
void main( void )
{
gl_Position = ftransform();
Texcoord = gl_MultiTexCoord0.xy;
vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
ViewDirection = fvEyePosition - fvObjectPosition.xyz;
LightDirection = fvLightPosition - fvObjectPosition.xyz;
Normal = gl_NormalMatrix * gl_Normal;
}
//Pixel Shader use GLSL
uniform vec4 fvAmbient;
uniform vec4 fvSpecular;
uniform vec4 fvDiffuse;
uniform vec4 fvScatter;
uniform float fSpecularPower;
uniform float fLightPower;
uniform float fScatterPower;
uniform sampler2D baseMap;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying vec3 Normal;
void main( void )
{
vec3 fvLightDirection = normalize( LightDirection );
vec3 fvNormal = normalize( Normal );
float fNDotL = dot( fvNormal, fvLightDirection );
vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );
vec3 fvViewDirection = normalize( ViewDirection );
float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) );
vec4 fvBaseColor = texture2D( baseMap, Texcoord );
vec4 fvTotalAmbient = fvAmbient;
vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;
vec4 fvTotalSpecular = fvSpecular * ( pow( fRDotV, fSpecularPower ) );
float thickness = 1.0 - dot(fvViewDirection, fvNormal);
float viewness = fLightPower - fNDotL;
vec4 vTrCl = fScatterPower * viewness * thickness * fvScatter;
gl_FragColor = vTrCl + ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );
}