首页 > 代码库 > Parallax Mapping Shader 凸凹感【转】
Parallax Mapping Shader 凸凹感【转】
原文 http://www.azure.com.cn/default.asp?cat=11&page=2
Parallax Mapping 就是通过高度图中的高度,对纹理坐标进行偏移,
来视觉上欺骗观察者,产生很有凸凹感个幻觉。
uniform vec3 fvLightPosition;
uniform vec3 fvEyePosition;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
attribute vec3 rm_Binormal;
attribute vec3 rm_Tangent;
void main( void )
{
gl_Position = ftransform();
Texcoord = gl_MultiTexCoord0.xy;
vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
vec3 fvViewDirection = fvEyePosition - fvObjectPosition.xyz;
vec3 fvLightDirection = fvLightPosition - fvObjectPosition.xyz;
vec3 fvNormal = gl_NormalMatrix * gl_Normal;
vec3 fvBinormal = gl_NormalMatrix * rm_Binormal;
vec3 fvTangent = gl_NormalMatrix * rm_Tangent;
ViewDirection.x = dot( fvTangent, fvViewDirection );
ViewDirection.y = dot( fvBinormal, fvViewDirection );
ViewDirection.z = dot( fvNormal, fvViewDirection );
LightDirection.x = dot( fvTangent, fvLightDirection.xyz );
LightDirection.y = dot( fvBinormal, fvLightDirection.xyz );
LightDirection.z = dot( fvNormal, fvLightDirection.xyz );
}
uniform vec4 fvAmbient;
uniform vec4 fvSpecular;
uniform vec4 fvDiffuse;
uniform float fSpecularPower;
uniform sampler2D baseMap;
uniform sampler2D bumpMap;
uniform sampler2D Parallax;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
void main( void )
{
vec3 fvViewDirection = normalize( ViewDirection );
vec4 height = texture2D( Parallax, Texcoord);
float uoffset = (height.x-0.5)*0.11*fvViewDirection.x;
float voffset = (height.x-0.5)*0.11*fvViewDirection.y;
vec2 modifyTexcoord = vec2(Texcoord.x+uoffset, Texcoord.y+voffset);
vec3 fvLightDirection = normalize( LightDirection );
vec4 avent = texture2D( bumpMap, modifyTexcoord );
float d = dot(avent.xyz, fvLightDirection);
float shadow = clamp((d - avent.w) * 1.8 + 0.5, 0.0, 1.0);
shadow = shadow * shadow * (3.0 - 2.0 * shadow);
vec3 fvNormal = normalize( avent.xyz * 2.0 - 1.0 );
float fNDotL = dot( fvNormal, fvLightDirection );
vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );
float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) );
vec4 fvBaseColor = texture2D( baseMap, modifyTexcoord );
vec4 fvTotalAmbient = fvAmbient * fvBaseColor;
vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;
vec4 fvTotalSpecular = fvSpecular * ( pow( fRDotV, fSpecularPower ) );
gl_FragColor = fvTotalAmbient + (fvTotalDiffuse + fvTotalSpecular) * shadow;
}