Parallax Mapping Shader 凸凹感【轉】

來源:互聯網
上載者:User

標籤:style   class   blog   http   tar   ext   

原文 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;
      
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.