Home | History | Annotate | Download | only in Terrain
      1 uniform sampler2D m_Alpha;
      2 uniform sampler2D m_Tex1;
      3 uniform sampler2D m_Tex2;
      4 uniform sampler2D m_Tex3;
      5 uniform float m_Tex1Scale;
      6 uniform float m_Tex2Scale;
      7 uniform float m_Tex3Scale;
      8 
      9 varying vec2 texCoord;
     10 
     11 #ifdef TRI_PLANAR_MAPPING
     12   varying vec4 vVertex;
     13   varying vec3 vNormal;
     14 #endif
     15 
     16 void main(void)
     17 {
     18 
     19     // get the alpha value at this 2D texture coord
     20     vec4 alpha   = texture2D( m_Alpha, texCoord.xy );
     21 
     22 #ifdef TRI_PLANAR_MAPPING
     23     // tri-planar texture bending factor for this fragment's normal
     24     vec3 blending = abs( vNormal );
     25     blending = (blending -0.2) * 0.7;
     26     blending = normalize(max(blending, 0.00001));      // Force weights to sum to 1.0 (very important!)
     27     float b = (blending.x + blending.y + blending.z);
     28     blending /= vec3(b, b, b);
     29 
     30     // texture coords
     31     vec4 coords = vVertex;
     32 
     33     vec4 col1 = texture2D( m_Tex1, coords.yz * m_Tex1Scale );
     34     vec4 col2 = texture2D( m_Tex1, coords.xz * m_Tex1Scale );
     35     vec4 col3 = texture2D( m_Tex1, coords.xy * m_Tex1Scale );
     36     // blend the results of the 3 planar projections.
     37     vec4 tex1 = col1 * blending.x + col2 * blending.y + col3 * blending.z;
     38 
     39     col1 = texture2D( m_Tex2, coords.yz * m_Tex2Scale );
     40     col2 = texture2D( m_Tex2, coords.xz * m_Tex2Scale );
     41     col3 = texture2D( m_Tex2, coords.xy * m_Tex2Scale );
     42     // blend the results of the 3 planar projections.
     43     vec4 tex2 = col1 * blending.x + col2 * blending.y + col3 * blending.z;
     44 
     45     col1 = texture2D( m_Tex3, coords.yz * m_Tex3Scale );
     46     col2 = texture2D( m_Tex3, coords.xz * m_Tex3Scale );
     47     col3 = texture2D( m_Tex3, coords.xy * m_Tex3Scale );
     48     // blend the results of the 3 planar projections.
     49     vec4 tex3 = col1 * blending.x + col2 * blending.y + col3 * blending.z;
     50 
     51 #else
     52 	vec4 tex1    = texture2D( m_Tex1, texCoord.xy * m_Tex1Scale ); // Tile
     53 	vec4 tex2    = texture2D( m_Tex2, texCoord.xy * m_Tex2Scale ); // Tile
     54 	vec4 tex3    = texture2D( m_Tex3, texCoord.xy * m_Tex3Scale ); // Tile
     55 	
     56 #endif
     57 
     58     vec4 outColor = tex1 * alpha.r; // Red channel
     59 	outColor = mix( outColor, tex2, alpha.g ); // Green channel
     60 	outColor = mix( outColor, tex3, alpha.b ); // Blue channel
     61 	gl_FragColor = outColor;
     62 }
     63 
     64