我正在尝试使用几何着色器渲染广告牌,该着色器将点作为输入并输出三角形流(使用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有关。但就像现在一样,这种行为对我来说根本没有任何意义:矩阵乘法应该是关联的,因此上面的片段应该产生与原始代码相同的结果,但它显然不会。也许你们中的一些人可以对这里发生的事情有所了解。
答案 0 :(得分:1)
我认为您的翻译向量位于矩阵的错误部分,应该读取
localToWorld._14 = particle.Position.x;
localToWorld._24 = particle.Position.y;
localToWorld._34 = particle.Position.z;
localToWorld._41 = 0;
localToWorld._42 = 0;
localToWorld._43 = 0;