Graphics Reference
In-Depth Information
// Choose the most detailed mipmap level
const float mipLevel = 1.0f;
// Sample height map - using R channel
float height = DisplacementMap.SampleLevel(Sampler, UV,
mipLevel).r;
// remap height from 0 to 1, to -1 to 1
height = (2 * height) - 1;
// Return offset along normal.
return height * DisplaceScale * normal;
}
5.
Within each domain shader, just prior to applying the
ViewProjection
matrix
to the new vertex position, we can apply the displacement function.
... existing domain shader code
// Perform displacement
position += CalculateDisplacement(UV, normal);
// Transform world position to view-projection
result.Position = mul(float4(position,1), ViewProjection );
... existing domain shader code
6.
To use our models with displacement mapping, we need to create a copy
of
TessellatedMeshRenderer.cs
from the previous chapter and call it
DisplacedMeshRenderer.cs
. Then, we need to incorporate the changes
from the previous recipe
Adding surface detail with normal mapping
.
7.
Add a new property
DisplacementScale
to
DisplacedMeshRenderer
.
public float DisplacementScale { get; set; }
8.
Initialize the default value for
DisplacementScale
within the constructor.
this.DisplacementScale = 1.0f;
9.
We now update the
DisplacedMeshRenderer.DoRender
function; so that when
applying the mesh's materials to the per material constant buffer, we are also binding
the displacement map and updating the
PerMaterial.DisplacementScale
property. We will assume that the third texture is the displacement map.
int texIndxOffset = mIndx * Common.Mesh.MaxTextures;
...
material.HasNormalMap = (uint)(EnableNormalMap &&
textureViews[texIndxOffset+1] != null ? 1 : 0);
material.DisplaceScale = (textureViews[texIndxOffset+2] !=
null ? DisplacementScale : 0);