Graphics Reference
In-Depth Information
How to do it...
As we have often done in previous recipes, we will begin by updating our constant buffers,
and then move on to updating our shaders and C# rendering code:
1.
We need to modify our per material constant buffer to indicate which displacement
scale to apply (if any), zero being the equivalent of no displacement. The updated
PerMaterial HLSL structure in Shaders\Common.hlsl is as follows:
cbuffer PerMaterial : register (b2)
{
...
bool HasNormalMap;
float DisplaceScale;
float4 MaterialEmissive;
float4x4 UVTransform;
};
2.
And the updated ConstantBuffers.PerMaterial structure within ConstantBuffers.cs is
as follows (note that we replaced the padding property):
public struct PerMaterial
{
...
public uint HasNormalMap; // (0 false, 1 true)
public float DisplaceScale;// displacement scale
public Color4 Emissive;
public Matrix UVTransform;
}
3.
At the start of Shaders\CommonTess.hlsl , add our input displacement texture.
Texture2D DisplacementMap : register(t0);
SamplerState Sampler : register(s0);
4.
We will add our CalculateDisplacement HLSL function to the end of this file.
This will be used by each of the domain shaders to apply the vertex displacement.
// Simple displacement calculation from displacement map
// If DisplaceScale is 0 no displacement takes place
// The float3 result should be added to the vertex position
float3 CalculateDisplacement(float2 UV, float3 normal)
{
// Skip displacement sampling if 0 multiplier
if (DisplaceScale == 0)
return 0;
 
Search WWH ::




Custom Search