• Create Account

## Particle geometry shader bill boarding problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1TaintedGear  Members

146
Like
1Likes
Like

Posted 06 May 2014 - 09:35 PM

Hi GameDev.

Im currently having a problem with my geometry shader in which i want to billboard my particles to face the camera. Currently the bill boarding seems to work from most angles, however when i go to close to the particles they warp, and when at a certain position in which i look at the particles the bill board effect stops entirely until i have moved again.

Pictures to show, because my explanation skills are atrocious:

In my eyes the calculation of the vectors for bill boarding is correct. However im skeptical about the need to multiply the new position of each vertex by the world matrix, as that had already been done in the vertex shader and been assigned to each new vertex generated.

Here is both vertex shader and geometry shader, hopefully its something stupid that i have missed or not understood correctly.

Any help is greatly appreciated.

cbuffer cbNeverChanges : register( b0 )
{
matrix g_view;
};

cbuffer cbChangeOnResize : register( b1 )
{
matrix g_proj;
};

//Camera position to work out billboarding
cbuffer cbCameraPos : register( b2 )
{
float4 g_camPos;
};

struct VS_Input
{
float4 pos : POSITION;
float2 tex0 : TEXCOORD0;
float4x4 InstWorld : WORLD;
float Size : SIZE;
uint InstID : SV_InstanceID;
};

struct GS_Input
{
float4 posL : POSITION1;
float4 posH : POSITION2;
float2 tex0 : TEXCOORD0;
float4x4 InstWorld : WORLD;
float Size : SIZE;
};

struct GS_Output
{
float4 Pos : SV_POSITION;
float2 TexCoords : TEXCOORD0;
};

GS_Input VS( VS_Input vertex )
{
GS_Input vsOut = ( GS_Input )0;

//Transform the vertex position
vsOut.posL = mul( vertex.pos, vertex.InstWorld );
vsOut.posH = mul( vsOut.posL, g_view );
vsOut.posH = mul( vsOut.posH, g_proj );

//Assign size so that it can be used in the gs
vsOut.Size = vertex.Size;
vsOut.InstWorld = vertex.InstWorld;
vsOut.tex0 = vertex.tex0;

return vsOut;
}

[maxvertexcount(4)]
void GS(point GS_Input input[1], inout TriangleStream<GS_Output> outputStream)
{
//calculate the look at vector
float3 lookVec = (float3)g_camPos - (float3)input[0].posL;
lookVec = normalize(lookVec);

//assign the up vec
float3 upVec = float3(0.0f, 1.0f, 0.0f);

//Calculate the right vector
float3 rightVec = normalize(cross(upVec, lookVec));

// To use so that the particle will rotate around its centre
float halfSize = input[0].Size / 2.0f;

//Get the up vector of the quad

//Times it by  half the size to build quad from around the center
rightVec = rightVec * halfSize;

// Create the positions of the quad
float3 position[4];
//Top Left
position[0] = (float3)input[0].posL - rightVec + quadUpVec;
//Top Right
position[1] = (float3)input[0].posL + rightVec + quadUpVec;
//Bottom Left
position[2] = (float3)input[0].posL - rightVec - quadUpVec;
//Bottom Right
position[3] = (float3)input[0].posL + rightVec - quadUpVec;

float2 texcoords[4];
texcoords[0] = float2(1.0f, 0.0f);
texcoords[1] = float2(1.0f, 1.0f);
texcoords[2] = float2(0.0f, 0.0f);
texcoords[3] = float2(0.0f, 1.0f);

GS_Output newVertex;
for(int k = 0; k < 4; k++)
{
//Assign position
newVertex.Pos = mul(float4(position[k], 1.0f), input[0].InstWorld);
newVertex.Pos = mul(newVertex.Pos, g_view);
newVertex.Pos = mul(newVertex.Pos, g_proj);
newVertex.TexCoords = texcoords[k];

outputStream.Append(newVertex);
}
}


Thanks

TaintedGear

### #2TaintedGear  Members

146
Like
2Likes
Like

Posted 07 May 2014 - 01:37 AM

UPDATE: I fixed the problem!

The warping was due to the fact that i was using a free look camera however setting an arbitrary up value with 1 in the Y. So it was not creating the correct right vector for the quad. And the other problem was because i was basically doubling up on its world matrix multiplication, as i was assigning the value already multiplied in the vertex shader to the new points of the quads and then multiplying it again before i append the new vertex.

Silly me

### #3SIC Games  Members

805
Like
0Likes
Like

Posted 10 May 2014 - 11:41 AM

Hi GameDev.

Im currently having a problem with my geometry shader in which i want to billboard my particles to face the camera. Currently the bill boarding seems to work from most angles, however when i go to close to the particles they warp, and when at a certain position in which i look at the particles the bill board effect stops entirely until i have moved again.

Pictures to show, because my explanation skills are atrocious:

In my eyes the calculation of the vectors for bill boarding is correct. However im skeptical about the need to multiply the new position of each vertex by the world matrix, as that had already been done in the vertex shader and been assigned to each new vertex generated.

Here is both vertex shader and geometry shader, hopefully its something stupid that i have missed or not understood correctly.

Any help is greatly appreciated.

cbuffer cbNeverChanges : register( b0 )
{
matrix g_view;
};

cbuffer cbChangeOnResize : register( b1 )
{
matrix g_proj;
};

//Camera position to work out billboarding
cbuffer cbCameraPos : register( b2 )
{
float4 g_camPos;
};

struct VS_Input
{
float4 pos : POSITION;
float2 tex0 : TEXCOORD0;
float4x4 InstWorld : WORLD;
float Size : SIZE;
uint InstID : SV_InstanceID;
};

struct GS_Input
{
float4 posL : POSITION1;
float4 posH : POSITION2;
float2 tex0 : TEXCOORD0;
float4x4 InstWorld : WORLD;
float Size : SIZE;
};

struct GS_Output
{
float4 Pos : SV_POSITION;
float2 TexCoords : TEXCOORD0;
};

GS_Input VS( VS_Input vertex )
{
GS_Input vsOut = ( GS_Input )0;

//Transform the vertex position
vsOut.posL = mul( vertex.pos, vertex.InstWorld );
vsOut.posH = mul( vsOut.posL, g_view );
vsOut.posH = mul( vsOut.posH, g_proj );

//Assign size so that it can be used in the gs
vsOut.Size = vertex.Size;
vsOut.InstWorld = vertex.InstWorld;
vsOut.tex0 = vertex.tex0;

return vsOut;
}

[maxvertexcount(4)]
void GS(point GS_Input input[1], inout TriangleStream<GS_Output> outputStream)
{
//calculate the look at vector
float3 lookVec = (float3)g_camPos - (float3)input[0].posL;
lookVec = normalize(lookVec);

//assign the up vec
float3 upVec = float3(0.0f, 1.0f, 0.0f);

//Calculate the right vector
float3 rightVec = normalize(cross(upVec, lookVec));

// To use so that the particle will rotate around its centre
float halfSize = input[0].Size / 2.0f;

//Get the up vector of the quad

//Times it by  half the size to build quad from around the center
rightVec = rightVec * halfSize;

// Create the positions of the quad
float3 position[4];
//Top Left
position[0] = (float3)input[0].posL - rightVec + quadUpVec;
//Top Right
position[1] = (float3)input[0].posL + rightVec + quadUpVec;
//Bottom Left
position[2] = (float3)input[0].posL - rightVec - quadUpVec;
//Bottom Right
position[3] = (float3)input[0].posL + rightVec - quadUpVec;

float2 texcoords[4];
texcoords[0] = float2(1.0f, 0.0f);
texcoords[1] = float2(1.0f, 1.0f);
texcoords[2] = float2(0.0f, 0.0f);
texcoords[3] = float2(0.0f, 1.0f);

GS_Output newVertex;
for(int k = 0; k < 4; k++)
{
//Assign position
newVertex.Pos = mul(float4(position[k], 1.0f), input[0].InstWorld);
newVertex.Pos = mul(newVertex.Pos, g_view);
newVertex.Pos = mul(newVertex.Pos, g_proj);
newVertex.TexCoords = texcoords[k];

outputStream.Append(newVertex);
}
}


Thanks

TaintedGear

Inside my editor I have the default editor camera set to free camera - so it's not a real issue. You fixed it and that's awesome. You wanted the particle camera to always face the camera like a world of billboard trees or anywhere the camera is facing? Try this but if you decided not to it's okay.

//-- Calculate look at vector
float3 planeNormal = input[0].posL.xyz - g_camPos.xyz;
planeNormal.y = 0.0f; //-- This allows the particle to be aligned the Y Axis when moving around with free camera.
planeNormal = normalize(planeNormal);
float3 upVector = float3(0,1,0);
float3 rightVector = normalize(cross(planeNormal, upVector);
rightVector = rightVector * 2.0f;

//-- Carry on with the rest of your geometry shader code when you append to output.


In your case the rightVector = rightVector * 2.0f will be just what you have in your shader code. The only thing mine will do is make sure it's always facing the camera but aligned on the Y axis. So, if I look go up or down then look at it - it will stay won't rotate on the Y Axis. You're may be different though because the effect you want. I got the billboard shader code from BrayzenSoft and thought you did too as well. Other than that if it works it works! I don't see how the free camera would mess up the calculation but more likely the doubling of the matrix when you output the geometry. I use to have my follow the camera and stretch in all weird funny positions. I'm glad you got it fixed though!

Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.