Graphics Reference
In-Depth Information
3.
Finally, we add a new constant buffer for the material properties (note that the slot
number used is 2 ).
cbuffer PerMaterial : register (b2)
{
float4 MaterialAmbient;
float4 MaterialDiffuse;
float4 MaterialSpecular;
float MaterialSpecularPower;
bool HasTexture;
float4 MaterialEmissive;
float4 UVTransform;
};
A good practice would be to render objects sorted by their material,
saving on pipeline changes and constant buffer updates.
4.
Next, we will combine the vertex color and material diffuse and pass the result
to the pixel shader by modifying the vertex shader. Find the appropriate line in
Shaders\VS.hlsl and add the highlighted code:
result.Diffuse = vertex.Color * MaterialDiffuse ;
If we do not set a vertex color or material diffuse, the color will be
black as the colors are multiplied. Therefore, it is important that both
are provided with a value. If the diffused color should have no impact
upon the final color, for example, the texture sample provides all
the necessary surface colors, the vertex and material diffuse colors
should be set to white (1.0f, 1.0f, 1.0f, and 1.0f). Alternatively, if the
vertex color is to be ignored, provide the correct brightness, some
grayscale value, and vice versa.
5.
Also within the vertex shader, we apply the material's UVTransform matrix to
the UV coordinates. The following code shows how this is done:
/ Apply material UV transformation
result.TextureUV = mul(float4(vertex.TextureUV.x,
vertex.TextureUV.y, 0, 1), (float4x2)UVTransform).xy;
6.
Within ConstantBuffers.cs , we need to update the PerFrame structure to
also include the directional light and to create a new structure, PerMaterial ,
while keeping in mind the HLSL 16-byte alignment.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct DirectionalLight
{
 
Search WWH ::




Custom Search