我正在学习有关使用C ++,HLSL和DirectX 11的图形管道的课程。我目前正在使用已应用的高度贴图对立方球体进行镶嵌。
我的问题是弄清楚在应用镶嵌高度图后如何重新计算法线。我正在尝试像这样在域着色器中重新计算它们:
// Tessellation domain shader
// After tessellation the domain shader processes the all the vertices
Texture2D HightMapTexture : register(t0);
SamplerState Sampler : register(s0);
cbuffer MatrixBuffer : register(b0)
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
cbuffer TessellationBuffer : register(b1)
{
float4 TessellationFactor;
float4 CameraPosition;
float4 padding;
};
struct ConstantOutputType
{
float edges[4] : SV_TessFactor;
float inside[2] : SV_InsideTessFactor;
};
struct InputType
{
float3 position : POSITION;
float4 colour : COLOUR;
float3 normal : NORMAL;
float2 tex : TEXCOORD0;
};
struct OutputType
{
float4 position : SV_POSITION;
float4 colour : COLOUR;
float3 normal : NORMAL;
float2 tex : TEXCOORD0;
};
[domain("quad")]
OutputType main(ConstantOutputType input, float2 uvwCoord : SV_DomainLocation, const OutputPatch<InputType, 4> patch)
{
float3 vertexPosition;
float3 normalPosition;
float2 UV;
OutputType output;
float3 v1 = lerp(patch[0].position, patch[1].position, uvwCoord.y);
float3 v2 = lerp(patch[3].position, patch[2].position, uvwCoord.y);
vertexPosition = lerp(v1, v2, uvwCoord.x);
float3 np1 = lerp(patch[0].normal, patch[1].normal, uvwCoord.y);
float3 np2 = lerp(patch[3].normal, patch[2].normal, uvwCoord.y);
normalPosition = lerp(np1, np2, uvwCoord.x);
// Send the input normalPosition into the pixel shader.
output.normal = normalPosition;
float2 uv1 = lerp(patch[0].tex, patch[1].tex, uvwCoord.y);
float2 uv2 = lerp(patch[3].tex, patch[2].tex, uvwCoord.y);
UV = lerp(uv1, uv2, uvwCoord.x);
output.tex = UV;
float hightColour = HightMapTexture.SampleLevel(Sampler, output.tex, 0).r;
vertexPosition += (2 * hightColour) * float4(output.normal, 0);
//vertexPosition.z = sin((vertexPosition.y *2) + (padding.x * 2) *2);
//vertexPosition.z += cos((vertexPosition.x *2) + (padding.x * 2)*2);
// Calculate the position of the new vertex against the world, view, and projection matrices.
output.position = mul(float4(vertexPosition, 1.0f), worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
//output.worldPosition = float4(vertexPosition, 1.0f);
// Send the input color into the pixel shader.
output.colour = patch[0].colour;
output.normal = mul(output.normal, (float3x3)worldMatrix);
output.normal = normalize(output.normal);
return output;
}
`