• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
relaxok

HLSL - Billboarding a mesh

14 posts in this topic

Guys:

 

I have a vertex shader that I use for all my non-terrain models.  My per-mesh structure has a flags uint where i can set things like billboard, unlit, etc.  Some models have single meshes that need to be billboarded, like a light halo that looks the same from any angle.  However, I can't figure out how to successfully billboard the mesh.

 

Below is my vertex shader.. in the billboard section I've tried various combinations of matrices as you can see.. based on MJP's response in this topic:  

 

http://www.gamedev.net/topic/507808-hlsl-billboard/


PixelInputType VS(VertexInputType input, uint InstanceID : SV_InstanceID )
{	
	PixelInputType output;
    
	// Fix w
        input.position.w = 1.0f;

	// Calculate animation matrices
	matrix bonematrix = ( float( input.boneweight0 ) / 255.0f ) *  BoneMatrices[ input.boneindex0 ];
	bonematrix += ( float( input.boneweight1 ) / 255.0f ) *  BoneMatrices[ input.boneindex1 ];
	bonematrix += ( float( input.boneweight2 ) / 255.0f ) *  BoneMatrices[ input.boneindex2 ];
	bonematrix += ( float( input.boneweight3 ) / 255.0f ) *  BoneMatrices[ input.boneindex3 ];

	// World matrix from instance buffer
	matrix worldMatrixInstance = InstanceLocations[ InstanceID ];

	// Transform via combined bones + world matrix
	output.position = mul(input.position, bonematrix);
	output.position = mul(output.position, worldMatrixInstance );
	
	// Animate normal the same way
	output.normal = mul(input.normal, bonematrix);
	output.normal = mul(output.normal, worldMatrixInstance );

	// Camera view
        output.position = mul(output.position, viewMatrix);
	float4 cameraPosition = output.position;

	// vertex color
	output.vertex_color = input.vertex_color;

	// billboard?
	if( flags & PER_MESH_FLAG_BILLBOARD )
	{
		matrix boneWorldMat = mul( bonematrix, worldMatrixInstance );
		matrix boneWorldViewMat = mul( boneWorldMat, viewMatrix );
		//output.position = input.position + float4( boneWorldMat._41, boneWorldMat._42, boneWorldMat._43, 1.0f );
		output.position = input.position + float4( boneWorldViewMat._41, boneWorldViewMat._42, boneWorldViewMat._43, 1.0f );
		//output.position = input.position + float4( viewMatrix._41, viewMatrix._42, viewMatrix._43, 1.0f );
	}
		
	output.position = mul(output.position, projectionMatrix);
	 
	output.tex = input.tex;

	// Fog calc
	output.fogFactor = saturate((fogEnd - cameraPosition.z) / (fogEnd - fogStart));

	output.instance = InstanceID;
    
        return output;
}

I am using a y-up system, if it matters.

 

Please note there are no issues with the shader outside of the billboarding so please no recommendations on other things I can do differently.  I just want to know how to billboard within the context of what you see here.

 

Thanks..

Edited by relaxok
0

Share this post


Link to post
Share on other sites

Out.Pos = mul(float4(J + mul(Wm,Vm)[3], 1.0f), Pm);

 

float4 Out.Pos - result

float3 J - triangle in object space what must to bee bilboarded

float4x4 Wm - object to world space matrix

float4x4 Vm - world to view space matrix

float4x4 Pm - view to screen space matrix

 

And this tringle will always be aligned to screen

 

But triangel must be aligned to screen.

Screen coordinates is:

X wide

Y heght

Z deep

 

----------------------------

| Y                             |

| ^                              |

| |                               |
| | Z                            |

| |------------> X           |

----------------------------

 

Culling may to make your triangle invizible

Edited by 8up tuotlo
0

Share this post


Link to post
Share on other sites

Out.Pos = mul(float4(J + mul(Wm,Vm)[3], 1.0f), Pm);

 

float4 Out.Pos - result

float3 J - triangle in object space what must to bee bilboarded

float4x4 Wm - object to world space matrix

float4x4 Vm - world to view space matrix

float4x4 Pm - view to screen space matrix

 

And this tringle will always be aligned to screen

 

But triangel must be aligned to screen.

Screen coordinates is:

X wide

Y heght

Z deep

 

----------------------------

| Y                             |

| ^                              |

| |                               |
| | Z                            |

| |------------> X           |

----------------------------

 

Culling may to make your triangle invizible

 

It seems like that's what I'm doing?

 

Latest attempt:

// billboard?
	if( flags & PER_MESH_FLAG_BILLBOARD )
	{
		matrix boneWorldMatrix = mul( bonematrix, worldMatrixInstance );
		matrix boneWorldViewMatrix = mul( boneWorldMatrix, viewMatrix );
		
		float3 position = input.position + float3( boneWorldViewMatrix._41, boneWorldViewMatrix._42, boneWorldViewMatrix._43 );
		output.position = mul( float4( position, 1.0f ), projectionMatrix );
	}
	else
	{	
		output.position = mul(output.position, projectionMatrix);
	}
0

Share this post


Link to post
Share on other sites

Yes, it is.

BUT your triangle must already be placed in CMERA coordinates in vertex buffer(your mesh).

it means thad what up side of your triangle is y+;

front side z+( or z-) i forget ( try two variants; 

and right side is x+;

 

THAN THIS works so

 

if( flags & PER_MESH_FLAG_BILLBOARD )
    {
        matrix boneWorldMatrix = mul( bonematrix, worldMatrixInstance );    
        matrix boneWorldViewMatrix = mul( boneWorldMatrix, viewMatrix );  
        
        float3 position
= input.position + float3( boneWorldViewMatrix._41, boneWorldViewMatrix._42, boneWorldViewMatrix._43 );

        now we TRANSLATE our triangle from Object space to World space ONLY translate no rotations (and this is the bilboarding)
        output.position = mul( float4( position, 1.0f ), projectionMatrix ); standart projection

    }
    else
    {    
        output
.position = mul(output.position, projectionMatrix);
    }    

 

we do NOT make rotations

thet means thad what if our triangle were ben rotatet from the CMERA we do not see it

screen tays on z+ (or z-) and takes look an another side (this is CMERA SPACE)

 

Wm takes our verties from ObjectSpce to WorldSpace

Vm takes our vertices from WorldSpace to ViewSpace - transfer vertices without rotation its (BILLBOARDING) but triangle must be already placed correctly

Pm takes our vertices from ViewSpace to ScreenSpace

0

Share this post


Link to post
Share on other sites

        now we TRANSLATE our triangle from Object space to World space ONLY translate no rotations (and this is the bilboarding)

 

 

Isn't the line:

 

float3 position = input.position + float3( boneWorldViewMatrix._41, boneWorldViewMatrix._42, boneWorldViewMatrix._43 );

 

supposed to do exactly that?  adjust by the translation only..

0

Share this post


Link to post
Share on other sites

To billboard an object you are supposed to multiply it by the camera matrix, not the view matrix.  The view matrix is the inverse of the camera matrix and it is what you send in an MVP (model-view-projection) matrix.

You should not be manually adjusting positions etc.  It’s nothing more than a single matrix multiply.

 

 

L. Spiro

0

Share this post


Link to post
Share on other sites

To billboard an object you are supposed to multiply it by the camera matrix, not the view matrix.  The view matrix is the inverse of the camera matrix and it is what you send in an MVP (model-view-projection) matrix.

You should not be manually adjusting positions etc.  It’s nothing more than a single matrix multiply.

 

Why would that accomplish not rotating it?

0

Share this post


Link to post
Share on other sites

My new idea was to try making a billboarding view matrix which does the translation based on the real camera rotations, but for the rotation part of the view matrix, uses the inverse matrices.

 

This results in ALMOST the correct result.. the mesh now stays 'camera facing' no matter what the camera is doing, however the translation seems to put it all over the place.  I'm wondering how this can be when the translation is the same as when not billboarding:

 

http://bh.polpo.org/quad_no_rot1.avi

 

What adjustment can I make to keep the translation correct?

void CCamera::transform( float x, float y, float z, float fOrientation )
{
	XMMATRIX matRotationX = XMMatrixRotationX( m_Pitch );
	XMMATRIX matRotationY = XMMatrixRotationY( XMConvertToRadians( fOrientation  * 360.0f ) + m_Yaw );
	XMVECTOR unitVec = XMVectorSet( 0.0f, m_Height, -m_Distance, 0.0f );
	XMVECTOR transVec = XMVector3TransformCoord( unitVec, matRotationX * matRotationY );

	m_posX = x + XMVectorGetX( transVec );
	m_posY = y + XMVectorGetY( transVec );
	m_posZ = z + XMVectorGetZ( transVec );

	XMMATRIX matTranslation = XMMatrixTranslation( m_posX, m_posY, m_posZ );
	XMVECTOR matInvDeter;
	XMMATRIX matInverse = XMMatrixInverse( &matInvDeter, matRotationX * matRotationY );
	XMMATRIX matTransInverse = XMMatrixInverse( &matInvDeter, matTranslation );

	// Billboard version
	XMVECTOR matRotBillboardDeter;
	XMMATRIX matRotationXBillboard = XMMatrixInverse( &matRotBillboardDeter, matRotationX );
	XMMATRIX matRotationYBillboard = XMMatrixInverse( &matRotBillboardDeter, matRotationY );
	XMMATRIX matInverseBillboard = XMMatrixInverse( &matInvDeterBillboard, matRotationXBillboard * matRotationYBillboard );
	
	XMStoreFloat4x4( &m_matView, matTransInverse * matInverse );
	XMStoreFloat4x4( &m_matBillboard, matTransInverse * matInverseBillboard );
	XMStoreFloat4x4( &m_matRotationX, matRotationX );
	XMStoreFloat4x4( &m_matRotationY, matRotationY );
	XMStoreFloat4x4( &m_matTranslation, matTranslation );

	calculateViewFrustum();
}
Edited by relaxok
0

Share this post


Link to post
Share on other sites

Why would that accomplish not rotating it?

Because it undoes the rotation applied by the camera…

 

My new idea was to try making a billboarding view matrix which does the translation based on the real camera rotations, but for the rotation part of the view matrix, uses the inverse matrices.

Why don’t you just quickly make the WBP (world-billboard-projection) matrix and be done with all that ridiculous math?
 
XMMATRIX mBillboard;
XMMatrixInverse( &mBillboard, m_matView ); // m_matView is the inverse of another matrix, so instead of inverting it again you could just copy that matrix.
// Only keep the upper-left 3×3.
mBillboard._41 = mBillboard._42 = mBillboard._43 = 0.0f;  // Billboard matrix complete.


XMMATRIX mBillboardView = mBillboard * m_matView; // Can be used instead of the view matrix.
XMMATRIX mBillboardViewProjection = mBillboardView * m_matProj; // Can be used instead of the view-projection matrix.
Why make it so complicated?


L. Spiro Edited by L. Spiro
2

Share this post


Link to post
Share on other sites

on my mind billboarding its placing triangle fefore screen without rotations(always to us)

 

just make WorldViewMatrix

Add to triangle WorldViewMatrix[3] - translation

and multiple it on projection

 

all

 

that what you doing is too much

Edited by 8up tuotlo
0

Share this post


Link to post
Share on other sites

 

Why would that accomplish not rotating it?

Because it undoes the rotation applied by the camera…

 

My new idea was to try making a billboarding view matrix which does the translation based on the real camera rotations, but for the rotation part of the view matrix, uses the inverse matrices.

Why don’t you just quickly make the WBP (world-billboard-projection) matrix and be done with all that ridiculous math?
 
XMMATRIX mBillboard;
XMMatrixInverse( &mBillboard, m_matView ); // m_matView is the inverse of another matrix, so instead of inverting it again you could just copy that matrix.
// Only keep the upper-left 3×3.
mBillboard._41 = mBillboard._42 = mBillboard._43 = 0.0f;  // Billboard matrix complete.


XMMATRIX mBillboardView = mBillboard * m_matView; // Can be used instead of the view matrix.
XMMATRIX mBillboardViewProjection = mBillboardView * m_matProj; // Can be used instead of the view-projection matrix.
Why make it so complicated?


L. Spiro

 

 

The reason it's all spread out is that it's not working yet, and this way I can experiment with changing individual elements.

 

I have tried everything suggested in this thread and it's still not working.  Using your suggested method in the above quote, the mesh simply doesn't appear at all, the same as most other attempts..

 

Still the closest I've gotten is using the inverses of the rotation matrices, but the vertices translate all over the place in this case.. as shown in the video i linked.

0

Share this post


Link to post
Share on other sites

Try to do so:

 

Get the transpose(float3x3(your WorldViewMatrix));

Get your mesh in local space, multiple him on this ^

And than multiple him as simple, no billboarded mesh;

 

and try to set culling mode off

0

Share this post


Link to post
Share on other sites

Try to do so:

 

Get the transpose(float3x3(your WorldViewMatrix));

Get your mesh in local space, multiple him on this ^

And than multiple him as simple, no billboarded mesh;

 

and try to set culling mode off

 

I'm not quite following your description, sorry... but maybe this is going back to what you're referring to.. this and showing both sides of the mesh, I do get fairly close..

	// billboard?
	if( flags & PER_MESH_FLAG_BILLBOARD )
	{
		matrix worldViewMat = mul( worldMatrixInstance, viewMatrix );
		output.position = skinnedPos + float4( worldViewMat._41, worldViewMat._42, worldViewMat._43, 1.0f );
	}
	
	output.position = mul( output.position, projectionMatrix ); 

But as you can see, while the distance of the camera is accounted for fine.. the proper translation doesn't happen when the camera rotates on axes..

 

http://bh.polpo.org/billboard_rotate_issue.wmv

 

What's the reason for this?

Edited by relaxok
0

Share this post


Link to post
Share on other sites

Try to do so:

 

matrix worldViewMat = mul( worldMatrixInstance, viewMatrix );
output.position = mul(skinnedPos, worldViewMat );

output.position = mul(output.position, transpose(float3x3(worldViewMat)) );

 

or this:

 

matrix worldViewMat = mul( worldMatrixInstance, viewMatrix );       

output.position = skinnedPos + mul(float4( worldViewMat._41, worldViewMat._42, worldViewMat._43, 1.0f ), worldViewMat );

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0