Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, Quad, M_D), 1,217 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
billboard model lod shader (for vegetation) #456935
12/16/15 08:48
12/16/15 08:48
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline OP
Expert
sivan  Offline OP
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
Hi,

the following little contribution provides an effective solution for vegetation rendering by minimizing performance cost of far away models, because only a sprite-like 4-vertex model is rotated to face the camera. It requires Commercial or Pro edition.



Special thanks to txesmi who really made the calculations.

First you need to store sin and cos of camera pan angle in a material skill, as it is needed later in the vertex shader. Just call this function once when your material is created, or put the 2 lines into your main loop:
Code:
void Materials_ShadersLoop()
{
	while(1)
		{
			// slightly faster to set here material skills than by material events
			
			mtl_tree->skill1 = floatv( sinv(camera->pan) );
			mtl_tree->skill2 = floatv( cosv(camera->pan) );			
			
			wait(1);
		}
}



Alternatively you can make a material event to do it (the performance won't be much worse, even if you have 1000 trees), like this:

Code:
MATERIAL* mtl_tree =
{
	effect= "obj_Tree_MB.fx";
	ambient_blue  = 50;  	ambient_green  = 50;  	ambient_red  = 50;
	diffuse_blue  = 225;  	diffuse_green  = 225;  	diffuse_red  = 225;
	specular_blue = 0;  	specular_green = 0;  	specular_red = 0;
	emissive_blue = 0;  	emissive_green = 0;  	emissive_red = 0;
	power = 5;
	albedo = 100;
	
	event = sc_tree_billboard_event;
}

// intstead of event, it can be set in main loop of your game
function sc_tree_billboard_event()
{
	// feed vecSkill1 used in vs shader lod						
	mtl.skill1 = floatv( sinv(camera->pan) );
	mtl.skill2 = floatv( cosv(camera->pan) );
}



Then you need to add one line to the beginning of your vertex shader to do the actual rotation, and of course to define the material skill as vecSkill1:

Code:
float4 vecSkill1;

outModelVS2 ModelVS2B
		(float4 InPos		: POSITION, 
   		float3 InNormal		: NORMAL, 
   		float2 InTex		: TEXCOORD0) 
{ 
	//----------------------------------------------------------------
	// billboard
	
	InPos.xz = float2 ( InPos.x * vecSkill1.y - InPos.z * vecSkill1.x, InPos.x * vecSkill1.x + InPos.z * vecSkill1.y );
	
	//----------------------------------------------------------------



Of course, you need to set this shader to be used at the proper lod stage:

Code:
technique animtree1
{ 

   pass p0
   {
   	ZWriteEnable 		= True;		
	AlphaTestEnable 	= True;
	AlphaBlendEnable 	= False;
   	
	VertexShader = compile vs_3_0 AnimTree1VS();	// wind animation
     	PixelShader  = compile ps_3_0 ModelPS();	
   }

}

technique animtree1_lod1
{ 

   pass p0
   {
   	ZWriteEnable 		= True;		
	AlphaTestEnable 	= True;
	AlphaBlendEnable 	= False;
   	
	VertexShader = compile vs_3_0 ModelVS();	// no animation
     	PixelShader  = compile ps_3_0 ModelPS();
   }

}

technique animtree1_lod2
{ 

   pass p0
   {
   	ZWriteEnable 		= True;		
	AlphaTestEnable 	= True;
	AlphaBlendEnable 	= False;
   	
	VertexShader = compile vs_3_0 ModelVS2B();	// billboard lod, simplified lighting
     	PixelShader  = compile ps_3_0 ModelPS2();	// simplified lighting
   }

}

technique animtree1_lod3
{ 

   pass p0
   {
   	ZWriteEnable 		= True;		
	AlphaTestEnable 	= True;
	AlphaBlendEnable 	= False;
   	
	VertexShader = compile vs_3_0 ModelVS2B();	// billboard lod, simplified lighting
	PixelShader  = compile ps_3_0 ModelPS2();	// simplified lighting
   }

}

//////////////////////////////////////////

technique fallback 
{ 
	pass 
	{									
		ZWriteEnable 		= True;		
		AlphaTestEnable 	= True;
	   	AlphaBlendEnable 	= False; 	
	} 
}



And finally, you have to make the proper lod model. It should be a rectangle consisting of 2 triangles, facing in the opposite direction of X axis (i.e. should look towards -X). And its texture should be the image of the 3d model. And you have to play a bit with proper scaling. You can do it with the free tool TreeIt http://www.evolved-software.com/treeit/treeit

(Later I will upload a sample)

I wanted to prepare a little demo for this contribution, maybe I do it later, but it is so simple to implement. This feature will be available in the next minor update of MapBuilder.


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: billboard model lod shader (for vegetation) [Re: sivan] #457097
12/25/15 12:59
12/25/15 12:59
Joined: Dec 2003
Posts: 1,097
Maryland, USA
Steempipe Offline
Serious User
Steempipe  Offline
Serious User

Joined: Dec 2003
Posts: 1,097
Maryland, USA
Thanks for the contribution. Will be handy to have around.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1