Game Development Community

FoliageReplicator Upgrades For TSE

by J.C. Smith · in Torque Game Engine Advanced · 01/18/2007 (10:27 pm) · 64 replies

Hello all. I was looking at this resource the other day from Jeff Loveless:

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=11963

which implemented clustering and multiple foliage textures for TGE. So I decided to port that resource to TSE. The clustering works in the same way though needed some code changes for the TSE version. Along the way though I decided to do the multi foliage types using a single texture. It works by using a flag to denote if your foliage is normal or a QuadTexture (the UseQuadTexture flag in Mission Editor). So you'd just make a PNG or DDS file and split it into 4 imaginary quadrants, and paste a foliage texture in each of the 4 quadrants. If you leave the quadtexture flag unchecked then it works as normal.

As another bonus I added in some simple color variations. This will allow you to take you texture and blend some different colors with it to give some variation so all your blades of grass aren't the same color for example. That works by setting three four fields: GradientTexture, UseBlend, BlendStart and BlendEnd. If you check the UseBlend flag then it will activate this effect. BlendStart and BlendEnd are a float value from 0.0 to 1.0, which reflects which parts of the gradient texture range you want to use. The GradientTexture is a texture file that you will need to create in photoshop or whatever that stores the color range vertically. It only uses the Y pixels, and is always using the first pixel X wise. So for example if you made a 64x64 texture, and the first line started with a green color, and moving down the Y axis by the time you reached the bottom line it was yellow, and you set the BlendStart to 0 and the BlendEnd to 1 then your foliage would be colored a random shade of green to yellow. If you set the blendEnd to 0.5 then it would start at green and end at a greenishyellow variant. If your using a grayscale image, this allows you to make some nice variations on your foliage colors.

I'm pasting the source code into here in it's entirety for the four files which require changes:

shaders/FxFoliageReplicatorP.hlsl

#include "shdrConsts.h"

//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
struct ConnectData
{
   float2 texCoord        : TEXCOORD0;
   float4 lum		  : COLOR0;
   float4 groundAlphaCoeff : COLOR1;
   float2 alphaLookup	  : TEXCOORD1;
   float2 blendColor      : TEXCOORD2;
};

struct Fragout
{
   float4 col : COLOR0;
};

//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform sampler2D diffuseMap      : register(S0),
              uniform sampler2D alphaMap		: register(S1),
              uniform sampler2D blendMap        : register(S2),
              uniform float4    groundAlpha     : register(C1)       
)
{
   Fragout OUT;

   float4 alpha = tex2D(alphaMap, IN.alphaLookup);
   OUT.col = IN.lum * (tex2D(diffuseMap, IN.texCoord) * tex2D(blendMap, IN.blendColor));
   OUT.col.a = OUT.col.a * min(alpha, groundAlpha + IN.groundAlphaCoeff.x);
   
   return OUT;
}
Page«First 1 2 3 4 Next»
#61
10/29/2007 (6:18 pm)
Thomas: Delete the offending variables.

The warnings are also not normal, you can resolve them easily by using the correct random functions (RandI not RandF) and casting the Integers to Floats. Not sure that was the intent.

FYI, i use Warnings As Errors for most of my projects to stop others from leaving junk behind, this forces them to use datatypes and initialize variables properly. It also keeps my output windows nice and clean during compiles for more serious issues. :)
#62
02/23/2008 (9:58 am)
Great work on this resource. I have encountered two issues with it that perhaps someone else has run into and fixed.

The first is that random foliage replicators end up acting as if lighting and / or lightSync are on, even if they are set to off. I checked and mLightOn and mLightSync are set to false by default in the tagFieldData class, but just to make sure I also added:
mFieldData.mLightOn = false;
	mFieldData.mLightSync = false;
to the fxFoliageReplicator constructor. Unfortuantely this did not help. If I load up the mission editor and switch the LightOn checkbox on and off, it fixes it most of the time.

The second issue I've encountered has been with certain foliage replicators generating extremely dark foliage when using the gradiant option. I only use two different gradiants between about 20 different foliage replicators, but some replicators end up producing foliage that is very very dark for some reason.

I will look into these when I have more time, and if / when I find a fix I'll post it here, but if anyone else has found and fixed these issues I'd also appreciate any information you have. Thanks!
#63
02/23/2008 (10:21 am)
Sometimes just typing things out helps a lot when trying to solve a problem.

I'm fairly sure I solved my problem already.

Some of these are likely redundant or unncessary, but just to make sure that certain variables were initialized correctly I added the following code to the foliage replicator constructor:
fxFoliageReplicator::fxFoliageReplicator()
{
...
	mFieldData.mLightOn = false;
	mFieldData.mLightSync = false;
	mGlobalLightPhase = 0.0f;
	mGlobalSwayPhase = 0.0f;
	mGlobalLightTimeRatio = 0.0f;
	mGlobalSwayTimeRatio = 0.0f;
}

Initializing GlobalLightPhase and GlobalLightTimeRatio to 0.0 seems to have solved both of my problems (unusual, often super fast, lighting even on replicators with lighting turned off, and patches of extremely dark foliage).

I made sure the comparable sway variables are set to 0 by default as well, just for good measure.
#64
06/05/2008 (11:11 am)
Just a quick note..

Point2F texCoords[4];
	texCoords[0] = Point2F(0.0, 0.0);
	texCoords[1] = Point2F(0.0, 0.5);
	texCoords[2] = Point2F(0.5, 0.5);
	texCoords[3] = Point2F(0.5, 0.0);

makes the foliage take only the top left quad of the texture regardless of the state of the mQuadFoliage variable, so you might want to change this to:

Point2F texCoords[4];
	if (mFieldData.mQuadFoliage) {
		texCoords[0] = Point2F(0.0f, 0.0f);
		texCoords[1] = Point2F(0.0f, 0.5f);
		texCoords[2] = Point2F(0.5f, 0.5f);
		texCoords[3] = Point2F(0.5f, 0.0f);
	} else {
		texCoords[0] = Point2F(0.0f, 0.0f);
		texCoords[1] = Point2F(0.0f, 1.0f);
		texCoords[2] = Point2F(1.0f, 1.0f);
		texCoords[3] = Point2F(1.0f, 0.0f);
	}
Page«First 1 2 3 4 Next»