我正在尝试使用几何着色器渲染广告牌,该几何着色器将点作为输入并输出三角形流(使用 DirectX11)。然而,目前的结果并不是我所期望的。
为了比较,这里有两张截图,渲染了相同的一组粒子,从相同的方向(或多或少,我不得不将相机旋转约 90°)和距离,一次渲染为点,一次用我的广告牌渲染着色器:
正如您在第二张图片中看到的那样,当渲染为点时,可以清楚地看到粒子正在远离中心移动,几乎覆盖了整个屏幕,而当使用广告牌着色器渲染时,它们的比例略有变化,但始终保持静止。
不幸的是,我不知道是什么原因造成的。我一直在关注诸如this之类的教程,它解释了应该如何设置矩阵,但是结果表明我的期望或实现是错误的。
下面是广告牌着色器的代码,它构建矩阵来描述粒子的方向,然后为广告牌发射两个三角形:
#include <Materials/SceneConstants.hlsl>
#include <Materials/ModelConstants.hlsl>
#include <Particles/Particle.hlsl>
Texture2D diffuseTexture : register(ps, t[0]);
SamplerState diffuseSampler : register(ps, s[0]);
struct PS_IN
{
float4 Position : SV_POSITION;
float4 Color : COLOR;
float2 TexCoord : TEXCOORD;
};
Particle vs(Particle input)
{
return input;
}
[maxvertexcount(4)]
void gs(point Particle particles[1], inout TriangleStream<PS_IN> triStream)
{
// We need to create a matrix for the local coordinate system for the billboard of the given particle.
// One axis points from the particle to the camera, one axis is the camera's side axis (for example to
// the left) and the third one is perpendicular to both.
Particle particle = particles[0];
float3 zAxis = normalize(CameraPosition - particle.Position);
float3 xAxis = normalize(cross(float3(0, 1, 0), zAxis));
float3 yAxis = cross(zAxis, xAxis);
// The matrix to describe the local coordinate system is easily constructed:
float4x4 localToWorld;
localToWorld._11 = xAxis.x;
localToWorld._21 = xAxis.y;
localToWorld._31 = xAxis.z;
localToWorld._12 = yAxis.x;
localToWorld._22 = yAxis.y;
localToWorld._32 = yAxis.z;
localToWorld._13 = zAxis.x;
localToWorld._23 = zAxis.y;
localToWorld._33 = zAxis.z;
localToWorld._41 = particle.Position.x;
localToWorld._42 = particle.Position.y;
localToWorld._43 = particle.Position.z;
localToWorld._14 = 0;
localToWorld._24 = 0;
localToWorld._34 = 0;
localToWorld._44 = 1;
// And the matrix to transform from local to screen space...
float4x4 transform = localToWorld * World * ViewProjection;
// The positions of that quad is easily described in the local coordinate system:
// -z points towards the camera, y points upwards and x towards the right.
// The position marks the center of the quad, hence (0, 0, 0) is the center of the quad in
// local coordinates and the quad has an edge-length of particle.Size to either side.
PS_IN v1, v2, v3, v4;
//float size = particle.Size / 2;
float size = 0.5f;
v1.Position = mul(float4(-size, size, 0, 1), transform);
v1.TexCoord = float2(0, 0);
v1.Color = particle.Color;
v2.Position = mul(float4(size, size, 0, 1), transform);
v2.TexCoord = float2(1, 0);
v2.Color = particle.Color;
v3.Position = mul(float4(-size,-size, 0, 1), transform);
v3.TexCoord = float2(0, 1);
v3.Color = particle.Color;
v4.Position = mul(float4(size, -size, 0, 1), transform);
v4.TexCoord = float2(1, 1);
v4.Color = particle.Color;
triStream.Append(v1);
triStream.Append(v2);
triStream.Append(v3);
triStream.Append(v4);
}
float4 ps(PS_IN input) : SV_TARGET0
{
/*float4 texel = diffuseTexture.Sample(diffuseSampler, input.TexCoord);
return input.Color * texel;*/
return float4(1, 1, 1, 1);
}
作为参考,这里是将粒子渲染为简单点的着色器代码:
#include <Materials/SceneConstants.hlsl>
#include <Materials/ModelConstants.hlsl>
#include <Particles/Particle.hlsl>
struct PS_IN
{
float4 Position : SV_POSITION;
float4 Color : COLOR;
};
PS_IN vs(Particle input)
{
PS_IN output;
float4 posWorld = mul(float4(input.Position, 1), World);
output.Position = mul(posWorld, ViewProjection);
output.Color = input.Color;
return output;
}
float4 ps(PS_IN input) : SV_TARGET0
{
//return input.Color;
return float4(1, 1, 1, 1);
}
我注意到的另一个奇怪之处是我的广告牌没有面向相机,至少并非总是如此。但是,从我设置矩阵的方式来看,我希望他们能够这样做。相反,它们仅在从两个相反方向观察时才面向相机,然后在我开始旋转相机时宽度会减小。
这使我相信我在构建矩阵时犯了一个错误,但是我无法发现它。
希望大家能帮我找到问题。在此先感谢您的帮助!
编辑
我似乎已经找到了解决这个问题的方法,但是我不明白为什么会这样。出于某种奇怪的原因,我不能将 localToWorld 矩阵与 ViewProjection 矩阵相乘。相反,我必须将其分为两个步骤,如下所示:
v1.Position = mul(float4(-size, size, 0, 1), localToWorld);
v1.Position = mul(v1.Position, ViewProjection);
我不明白为什么会这样,也许它与使用 row_major 矩阵而不是默认的 column_major 有关。但就像现在一样,这种行为对我来说根本没有任何意义:矩阵乘法应该是关联的,因此上面的代码片段应该产生与原始代码相同的结果,但显然不是。也许你们中的一些人可以对这里发生的事情有所了解。