Here's the shader.
Code: Select all
/*********************************************************************NVMH3****
*******************************************************************************
$Revision: #4 $
Copyright NVIDIA Corporation 2008
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
% This material shows and compares results from four popular and
% advanced schemes for emulating displaement mapping. They are:
% Relief Mapping, Parallax Mapping, Normal Mapping, and Relief
% Mapping with Shadows. Original File by Fabio Policarpo.
keywords: material bumpmap
date: 071005
Note: Strong discontinuties in the model geometric normal (e.g., very sharp
differences from the normals in two or more parts of the same triangle)
can cause unusual overall light-attentuation errors. Re-normalizing the
rasterized normals in the fragment shader can correct this, but the case
was considered rare enough that these extra steps were eliminated for
code efficiency. If you see off lighting near sharp model edges, just
normalize "IN.normal" in the calculation of the varible "att" (shared
by all techniques).
To learn more about shading, shaders, and to bounce ideas off other shader
authors and users, visit the NVIDIA Shader Library Forums at:
http://developer.nvidia.com/forums/
*******************************************************************************
******************************************************************************/
// Modified for OGRE by mkultra333, 090529
// Got rid of BINORMAL input, now computed in vertex program.
// Changed "- tNorm.y*IN.binormal" to "+ tNorm.y*IN.binormal" in fragment program.
// Removed WorldITXf, WorldXf and ViewIXf 4x4 matrixes that weren't used.
/********** CONNECTOR STRUCTURES *****************/
struct AppVertexData {
float4 pos : POSITION;
float4 color : COLOR0;
float3 normal : NORMAL; // expected to be normalized
float2 txcoord : TEXCOORD0;
float3 tangent : TANGENT0; // pre-normalized
};
struct VertexOutput {
float4 hpos : POSITION;
float2 UV : TEXCOORD0;
float3 vpos : TEXCOORD1;
float3 tangent : TEXCOORD2;
float3 binormal : TEXCOORD3;
float3 normal : TEXCOORD4;
float4 lightpos : TEXCOORD5;
float4 color : COLOR0;
};
/*** SHADER FUNCTIONS **********************************************/
VertexOutput view_spaceVS(AppVertexData IN,
uniform float4x4 WvpXf,
uniform float4x4 ViewXf,
uniform float4x4 WorldViewXf,
uniform float TileCount,
uniform float3 LampPos
) {
VertexOutput OUT = (VertexOutput)0;
// isolate WorldViewXf rotation-only part
float3x3 modelViewRotXf;
modelViewRotXf[0] = WorldViewXf[0].xyz;
modelViewRotXf[1] = WorldViewXf[1].xyz;
modelViewRotXf[2] = WorldViewXf[2].xyz;
float4 Po = float4(IN.pos.xyz,1.0);
OUT.hpos = mul(WvpXf,Po);
// vertex position in view space (with model transformations)
OUT.vpos = mul(WorldViewXf,Po).xyz;
// light position in view space
float4 Lw = float4(LampPos.xyz,1); // this point in world space
OUT.lightpos = mul(ViewXf,Lw); // this point in view space
// Calculate the binormal (NB we assume both normal and tangent are
// already normalised)
// NB looks like nvidia cross params are BACKWARDS to what you'd expect
// this equates to NxT, not TxN
float3 binormal = cross(IN.tangent, IN.normal);
// tangent space vectors in view space (with model transformations)
OUT.tangent = mul(modelViewRotXf,IN.tangent);
OUT.binormal = mul(modelViewRotXf,binormal);
OUT.normal = mul(modelViewRotXf,IN.normal);
// copy color and texture coordinates
OUT.color = IN.color; // currently ignored by all techniques
OUT.UV = TileCount * IN.txcoord.xy;
return OUT;
}
//// ray-intersect functions for relief mapping //////////
float ray_intersect_rm( // use linear and binary search
in sampler2D reliefmap,
in float2 dp,
in float2 ds)
{
const int linear_search_steps=15;
// current size of search window
float size = 1.0/linear_search_steps;
// current depth position
float depth = 0.0;
// search front to back for first point inside object
for( int i=0;i<linear_search_steps-1;i++ ) {
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += size;
}
const int binary_search_steps=5;
// recurse around first point (depth) for closest match
for( int ii=0;ii<binary_search_steps;ii++ ) {
size*=0.5;
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += (2*size);
depth -= size;
}
return depth;
}
float ray_intersect_rm_lin( // only linear search for shadows
in sampler2D reliefmap,
in float2 dp,
in float2 ds)
{
const int linear_search_steps=15;
// current size of search window
float size = 1.0/linear_search_steps;
// current depth position
float depth = 0.0;
// search front to back for first point inside object
for( int i=0;i<linear_search_steps-1;i++ ) {
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += size;
}
return depth;
}
float4 relief_mapPS(VertexOutput IN,
uniform float Depth,
uniform float3 SurfaceColor,
uniform sampler2D ColorSampler : register(s1),
uniform sampler2D ReliefSampler : register(s0),
uniform float PhongExp,
uniform float3 SpecColor,
uniform float3 AmbiColor
) : COLOR
{
// ray intersect in view direction
float3 p = IN.vpos;
float3 Vn = normalize(p);
float a = dot(IN.normal,-Vn);
float3 s = float3(dot(Vn,IN.tangent.xyz), dot(Vn,IN.binormal.xyz), a);
s *= Depth/a;
float2 ds = s.xy;
float2 dp = IN.UV;
float d = ray_intersect_rm(ReliefSampler,dp,ds);
// get rm and color texture points
float2 uv = dp+ds*d;
float3 texCol = tex2D(ColorSampler,uv).xyz;
float3 tNorm = tex2D(ReliefSampler,uv).xyz - float3(0.5,0.5,0.5);
tNorm = normalize(tNorm.x*IN.tangent +
tNorm.y*IN.binormal +
tNorm.z*IN.normal);
// compute light direction
p += Vn*d/(a*Depth);
float3 Ln = normalize(p-IN.lightpos.xyz);
// compute diffuse and specular terms
float att = saturate(dot(-Ln,IN.normal));
float diff = saturate(dot(-Ln,tNorm));
float spec = saturate(dot(normalize(-Ln-Vn),tNorm));
spec = pow(spec,PhongExp);
// compute final color
float3 finalcolor = AmbiColor*texCol +
att*(texCol*SurfaceColor*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}
float4 relief_map_shadowsPS(VertexOutput IN,
uniform float Depth,
uniform float3 SurfaceColor,
uniform sampler2D ColorSampler : register(s1),
uniform sampler2D ReliefSampler : register(s0),
uniform float PhongExp,
uniform float3 SpecColor,
uniform float3 AmbiColor
) : COLOR
{
// ray intersect in view direction
float3 p = IN.vpos;
float3 Vn = normalize(p);
float a = dot(IN.normal,-Vn);
float3 s = float3(dot(Vn,IN.tangent.xyz), dot(Vn,IN.binormal.xyz), a);
s *= Depth/a;
float2 ds = s.xy;
float2 dp = IN.UV;
float d = ray_intersect_rm(ReliefSampler,dp,ds);
// get rm and color texture points
float2 uv = dp+ds*d;
float3 texCol = tex2D(ColorSampler,uv).xyz;
float3 tNorm = tex2D(ReliefSampler,uv).xyz - float3(0.5,0.5,0.5);
tNorm = normalize(tNorm.x*IN.tangent +
tNorm.y*IN.binormal +
tNorm.z*IN.normal);
// compute light direction
p += Vn*d/(a*Depth);
float3 Ln = normalize(p-IN.lightpos.xyz);
// compute diffuse and specular terms
float att = saturate(dot(-Ln,IN.normal));
float diff = saturate(dot(-Ln,tNorm));
float spec = saturate(dot(normalize(-Ln-Vn),tNorm));
// ray intersect in light direction
dp+= ds*d;
a = dot(IN.normal,-Ln);
s = float3(dot(Ln,IN.tangent.xyz),dot(Ln,IN.binormal.xyz),a);
s *= Depth/a;
ds = s.xy;
dp -= ds*d;
float dl = ray_intersect_rm_lin(ReliefSampler,dp,s.xy);
if (dl<d-0.05) { // if pixel in shadow
diff *= dot(AmbiColor.xyz,float3(1.0,1.0,1.0))*0.333333;
spec = 0;
}
spec = pow(spec,PhongExp);
// compute final color
float3 finalcolor = AmbiColor*texCol +
att*(texCol*SurfaceColor*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}
And here's a pic of the effect, sorry it was a bit dark so I brightened it externally.
I only got this running the other night, and there may be bugs. There are two version of the shader, one with shadows and one without.
. As for the license, it says on that page "All the shaders in this library are provided free of charge for use in derivative works, whether academic, commercial, or personal" and has more in depth license info here,
.