Xavyiy
06-10-2007 02:30:01
Hi people,
I've done a little Texture mode with support for Parallax mapping:
- 2 Coverage maps
- 5 Splatting textures, one of them with parallax mapping.
- 1 pass
There's a shot:
SplattingShaderParallax_1_4.hlsl
SplattingShaderParallax_1_4.material
.
I've done a little Texture mode with support for Parallax mapping:
- 2 Coverage maps
- 5 Splatting textures, one of them with parallax mapping.
- 1 pass
There's a shot:
SplattingShaderParallax_1_4.hlsl
//
// SplattingShaderParallax_1_4.hlsl for PLSM2
// Xavier VerguÃn González
// October 2007
//
// - 2 Coverage maps
// - 1 Parallax splatting texture / 4 diffuse splatting textures
// - 1 pass
//
void main_vp(// IN
float4 iPosition : POSITION,
float3 iNormal : NORMAL,
float2 iUv : TEXCOORD0,
// OUT
out float4 oPosition : POSITION,
out float2 oUv : TEXCOORD0,
out float3 oLightDir : TEXCOORD1,
out float3 oEyeDir : TEXCOORD2,
out float3 oHalfAngle : TEXCOORD3,
// UNIFORM
uniform float4 uLightPosition,
uniform float3 uEyePosition,
uniform float4x4 uWorldViewProj)
{
oPosition = mul(uWorldViewProj, iPosition);
oUv = iUv;
float3 lightDir = normalize(uLightPosition.xyz - (iPosition * uLightPosition.w));
float3 eyeDir = uEyePosition - iPosition.xyz;
float3 binormal = cross(float3(1,0,0), iNormal);
float3 tangent = cross(iNormal, binormal);
float3x3 rotation = float3x3(tangent, binormal, iNormal);
lightDir = normalize(mul(rotation, lightDir));
eyeDir = normalize(mul(rotation, eyeDir));
oLightDir = lightDir;
oEyeDir = eyeDir;
oHalfAngle = normalize(eyeDir + lightDir);
}
// Expand a range-compressed vector
float3 expand(float3 v)
{
return (v - 0.5) * 2;
}
void main_fp(// IN
float2 iUv : TEXCOORD0,
float3 iLightDir : TEXCOORD1,
float3 iEyeDir : TEXCOORD2,
float3 iHalfAngle : TEXCOORD3,
// OUT
out float4 oColor : COLOR,
// UNIFORM
uniform float3 uLightDiffuse,
uniform float3 uLightSpecular,
uniform float4 uScaleBias,
uniform float3 uAmbient,
uniform sampler2D uCoverage1 : register(s0),
uniform sampler2D uCoverage2 : register(s1),
uniform sampler2D uSplatting0 : register(s2),
uniform sampler2D uSplatting1 : register(s3),
uniform sampler2D uSplatting2 : register(s4),
uniform sampler2D uSplatting3 : register(s5),
uniform sampler2D uNormalHeightMap : register(s6),
uniform sampler2D uDiffuseMap : register(s7))
{
// Get coverage values
float4 Coverage1 = tex2D(uCoverage1, iUv);
float Coverage2 = tex2D(uCoverage2, iUv).y;
// Scale UV coord
float2 texscale = float2(80,80);
iUv *= texscale;
float4 col = float4(0,0,0,0);
if(Coverage1.x>0)
{
// Get the height from the alpha channel of normal-height tex.
float height = tex2D(uNormalHeightMap, iUv).a;
// Scale and bias factors
float scale = uScaleBias.x;
float bias = uScaleBias.y;
// Calculate displacement
float displacement = (height * scale) + bias;
float3 uv2 = float3(iUv, 1);
// Calculate the new tex coord to use for normal and diffuse
float2 newTexCoord = ((iEyeDir * displacement) + uv2).xy;
// Get the new normal and diffuse values
float3 normal = expand(tex2D(uNormalHeightMap, newTexCoord).xyz);
float3 diffuse = tex2D(uDiffuseMap, newTexCoord).xyz;
// Calculate specular and final pixel colour
float3 specular = pow(saturate(dot(normal, iHalfAngle)), 32) * uLightSpecular;
col = float4(diffuse * saturate(dot(normal, iLightDir)) * uLightDiffuse + specular, 1);
// Adjust the ambient value
float3 diffuse2 = diffuse*uAmbient;
if(col.x < diffuse2.x)
col.x += (diffuse2.x-col.x);
if(col.y < diffuse2.y)
col.y += (diffuse2.y-col.y);
if(col.z < diffuse2.z)
col.z += (diffuse2.z-col.z);
}
// Final pixel colour
oColor = col*Coverage1.x
+ (tex2D(uSplatting0, iUv) * Coverage1.y
+ tex2D(uSplatting1, iUv) * Coverage1.z
+ tex2D(uSplatting2, iUv) * Coverage1.w
+ tex2D(uSplatting3, iUv) * Coverage2) * float4(uAmbient, 1);
}
SplattingShaderParallax_1_4.material
//
// SplattingShaderParallax_1_4.material for PLSM2
// Xavier VerguÃn González
// October 2007
//
// - 2 Coverage maps
// - 1 Parallax splatting texture / 4 diffuse splatting textures
// - 1 pass
//
vertex_program SplattingShaderParallax_1_4/VP hlsl
{
source SplattingShaderParallax_1_4.hlsl
entry_point main_vp
target vs_1_1
}
fragment_program SplattingShaderParallax_1_4/FP hlsl
{
source SplattingShaderParallax_1_4.hlsl
entry_point main_fp
target ps_2_0
}
material SplattingShaderParallax_1_4
{
technique
{
pass
{
vertex_program_ref SplattingShaderParallax_1_4/VP
{
param_named_auto uLightPosition light_position_object_space 0
param_named_auto uEyePosition camera_position_object_space
param_named_auto uWorldViewProj worldviewproj_matrix
}
fragment_program_ref SplattingShaderParallax_1_4/FP
{
param_named uAmbient float3 0.7 0.7 0.7
param_named uLightDiffuse float3 0.95 1 1
param_named uLightSpecular float3 0.6 0.6 0.6
// Parallax Height scale and bias
param_named uScaleBias float4 0.05 -0.02 1 0
}
texture_unit
{
texture Coverage
}
texture_unit
{
texture Coverage
}
texture_unit
{
texture Splatting
}
texture_unit
{
texture Splatting
}
texture_unit
{
texture Splatting
}
texture_unit
{
texture Splatting
}
// Normal + height(alpha) map
texture_unit
{
texture Splatting
}
// Base diffuse texture map
texture_unit
{
texture Splatting
}
}
}
}
.