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