how to create camera facing model shader

Posted By: sivan

how to create camera facing model shader - 09/08/15 07:27

hi,
I just saw Superku's great forest shader, and it reminded me that a while ago I wanted to create a camera facing lod model shader to apply sprite-like 4 vertex models, instead of the current ugly cross-shaped double-faced models I use in MapBuilder.
(they are static models, and I don't want to adjust their rotation from a lite-c loop, to save performance.)
any help welcome laugh
thanks.
Posted By: txesmi

Re: how to create camera facing model shader - 09/08/15 11:24

I never faced this issue but I can imagine a path.

Compute on the cpu and save on mateial skills a 2d rotation matrix related to camera inverse pan and apply to every tree sprite xz members before converting to clip space into the vertex shader.

EDITED
I just realized this method will not work with trees merged in a single entity, as they should be if you look for performance optimitation. The solution could go around saving the rotation axe in object space for each vertex into the buffer.

EDITED 2
Since I am interested in these things I did a test workaround.

I created a model procedurally of 126^2 face trees. I added the center of rotation for each vertex to the vertex buffer. Secondary uv coords or x3-y3. Both worked.

I compute the sine and cosine of the camera pan in the main loop and save into material skills. The faces all look to the -X axe so rotating them by the camera pan makes them visible parallel to the projection plane.

Code:
mtlTreeSprites->skill1 = floatv ( sinv ( camera->pan ) );
mtlTreeSprites->skill2 = floatv ( cosv ( camera->pan ) );



Code:
void TreesVS (
	in float4 InPos	: POSITION,
	in float2 InTex	: TEXCOORD0,
	in float2 InOffset: TEXCOORD1,
	out float4 OutPos	: POSITION,
	out float2 OutTex	: TEXCOORD0 )
	{
		float2 pos = InPos.xz - InOffset.xy;
		InPos.xz = float2 ( pos.x * vecSkill1.y - pos.y * vecSkill1.x, pos.x * vecSkill1.x + pos.y * vecSkill1.y ) + InOffset.xy;
		OutPos = mul ( InPos, matViewProj );
		OutTex = InTex.xy;
	}



It works well.



Salud!
Posted By: txesmi

Re: how to create camera facing model shader - 09/08/15 14:06

Maybe a reply would be better than editing...
bump!
Posted By: sivan

Re: how to create camera facing model shader - 09/08/15 19:32

hey, you made a crazy fast good job grin
I don't fully know yet how to store the centre of rotation of model vertices... by the way currently I use no merged models for trees as I keep them destructible.
Posted By: txesmi

Re: how to create camera facing model shader - 09/08/15 23:43

Faster cause no lighting grin

Using single entities for each tree lets you rotate around their origin so no extra data is needed.

Code:
InPos.xz = float2 ( InPos.x * vecSkill1.y - InPos.z * vecSkill1.x, InPos.x * vecSkill1.x + InPos.z * vecSkill1.y );



Salud!
Posted By: sivan

Re: how to create camera facing model shader - 09/09/15 06:51

thanks!
Posted By: sivan

Re: how to create camera facing model shader - 09/09/15 09:32

I think matWorldViewProj is needed for OutPos, and entity rotation must be 0. probably entity.pan could be added by an entity skill to handle it too...
Posted By: txesmi

Re: how to create camera facing model shader - 09/09/15 10:34

Yes, I used matViewProj because world and object spaces were coincident in the test I built (0,0,0 origin plus no rotation). I can see the inconvenience of sharing as it was crazy
Posted By: sivan

Re: how to create camera facing model shader - 09/29/15 09:54

one more question:
is there a way to compensate entity pan within the vertex shader? maybe by storing their pan is an entity skill...
because trees look better when they are randomly rotated, but their billboard lod has an angle offset currently.
© 2024 lite-C Forums