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
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;
}
#22
Be great if it could be adjusted to use 2 planes in a cross shape instead of a single plane.
01/29/2007 (9:54 am)
Can the changed files be uploaded somewhere, or emailed directly to me? We are in the very early stages of development and learning (slowly) the systems, and this seems to be an ideal improvement.Be great if it could be adjusted to use 2 planes in a cross shape instead of a single plane.
#23
Anyhow, with the shape replicator you can have as many planes as you like intersecting.
01/29/2007 (2:03 pm)
Theres the shape replicator for that addiktive, if you want it to cluster like this check my orginal resource, it might need some of the same changes that the foliage did to get it working in TGEA. Anyhow, with the shape replicator you can have as many planes as you like intersecting.
#24
01/29/2007 (5:01 pm)
FxShapeReplicator isn't ported for TGEA GFX calls, so thats out :) And to do that with the shape replicator would require making a dts with planes, etc. The simple solution is to add in functionality to rotate the plane that is drawn by 90 degrees and draw both planes double-sided.
#25
01/29/2007 (9:36 pm)
There is actually an FxShapeReplicator port from a few months ago that does work with some modifications. I posted some fixes for getting it working in a thread after MS4 released, which updated it to work with the batched rendering changes. It was a quick hack, not sure if it was the best solution or not, but it got the TSE port of it working. Hellaciously busy at the moment though you'll have to look it up on your own. It's out there though.
#26
01/29/2007 (10:14 pm)
Interesting, as I try to keep up with all of the resources or forum threads about things like that hah.. I'll take a look
#27
You definately need to Resource these changes, and IMO worthy of a HEAD merge... if we still did that with TGEA. :)
Brilliant job, J.C.
01/30/2007 (4:59 pm)
The difference in realism is striking.You definately need to Resource these changes, and IMO worthy of a HEAD merge... if we still did that with TGEA. :)
Brilliant job, J.C.
#28
02/04/2007 (7:22 pm)
J.C. could you please maybe edit your first post to properly give me credit (ie spell my name right possibly)
#29
02/05/2007 (2:56 am)
Oops sorry bruhda. Doing that now, don't know where I got John from hehe.
#30
02/05/2007 (8:25 pm)
Thanks man :) I feel loved now
#31
http://i141.photobucket.com/albums/r75/piratelordx/Perdy.jpg
It only seems to be that single quad grass texture, but the image itself doesn't have that line, and it seems to be distance from camera related. Up close, it's perfect.
02/07/2007 (10:23 am)
Does anyone know why I'm getting these strange lines above the foliage?http://i141.photobucket.com/albums/r75/piratelordx/Perdy.jpg
It only seems to be that single quad grass texture, but the image itself doesn't have that line, and it seems to be distance from camera related. Up close, it's perfect.
#32
02/07/2007 (10:46 am)
@Addikive - Most likely it's a filtering issue with it pulling the last pixel from the piece of foliage above that one in the map (or if it's the top wrapping around to the bottom of the texture). Check your layout within the texture and maybe move that row down.
#33
Dark green lines up close to the camera, white lines at the distance.
02/07/2007 (12:08 pm)
Sorry, it's not the texture. I've also developed now dark green lines close to the ground, and neither the white or the dark green lines are in the texture file.Dark green lines up close to the camera, white lines at the distance.
#34
It compiles, and the foliage replicator appears to work, but im not sure if theres some sort of performance issue or not..
Cheers,
addikt
03/06/2007 (2:34 am)
Hey JC, I just wanted to check if this warning I get when compiling is anything to worry about?It compiles, and the foliage replicator appears to work, but im not sure if theres some sort of performance issue or not..
Cheers,
addikt
fxFoliageReplicator.cpp ..\engine\game\fx\fxFoliageReplicator.cpp(655) : warning C4244: '=' : conversion from 'float' to 'U32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(656) : warning C4244: '=' : conversion from 'float' to 'U32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(701) : warning C4244: 'argument' : conversion from 'U32' to 'F32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(701) : warning C4244: 'argument' : conversion from 'U32' to 'F32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(701) : warning C4244: '=' : conversion from 'F32' to 'U32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(702) : warning C4244: 'argument' : conversion from 'U32' to 'F32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(702) : warning C4244: 'argument' : conversion from 'U32' to 'F32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(702) : warning C4244: '=' : conversion from 'F32' to 'U32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(711) : warning C4244: 'argument' : conversion from 'U32' to 'F32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(711) : warning C4244: 'argument' : conversion from 'U32' to 'F32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(712) : warning C4244: 'argument' : conversion from 'U32' to 'F32', possible loss of data ..\engine\game\fx\fxFoliageReplicator.cpp(712) : warning C4244: 'argument' : conversion from 'U32' to 'F32', possible loss of data
#35
03/06/2007 (2:58 am)
The warnings are normal it shouldn't affect anything.
#37
1. We use the foliage replicator to place seaweed under water which works when the player is underwater, but when you stand out of the water, the waterblock doesnt let you see the replicator underwater. Is there some kind of tweak we can do for this?
2. We would like to have the option of 1 or 3 planes in the foliage replicator instead of just 1 in a crosshatch fashion, is this possible, or easy to modify?
3. The foliage replicator has a maximum angle setting, how easy is it to have a minimum angle setting, so we can have foliage that only shows on cliff walls or rocks etc?
03/08/2007 (1:19 pm)
Hi JC, sorry to bother you mate, I have a couple of questions..1. We use the foliage replicator to place seaweed under water which works when the player is underwater, but when you stand out of the water, the waterblock doesnt let you see the replicator underwater. Is there some kind of tweak we can do for this?
2. We would like to have the option of 1 or 3 planes in the foliage replicator instead of just 1 in a crosshatch fashion, is this possible, or easy to modify?
3. The foliage replicator has a maximum angle setting, how easy is it to have a minimum angle setting, so we can have foliage that only shows on cliff walls or rocks etc?
#38
2) Should be... see #3 below...
3) We've been posting in the same threads for a while now so I know you're a little savvy at engine mods so I'll give you a starting point and a few tips. Adding new functionality to existing features is really easy.
Step 1 - Declare your new variable in the .h file. If you're extending an existing feature, base your declaration on the existing bits...
So now you need to first convert all instances of "mAllowedTerrainSlope" to "mMaxAllowedTerrainSlope" in both the .h and .cpp files... remember to update your scripting... and then at least that bit is done.
Now throughout the .h and .cpp files, use all the "mMaxAllowedTerrainSlope" lines and duplicate them for "mMinAllowedTerrainSlope".
Now you'll have both of your new parameters available.
Good thing about most TGEA code is that it's well (if not pithily) documented.
Find this line (as it should read after you made your substitutions):
Add this line beneath it:
Now I can't say that this is gonna work off the bat... but it serves as a good example for a 5 minute tutorial on extending TGEA functionality. :)
... and it's certainly a good pointer in the right direction for getting it working.
EDIT: mah awshum speeling
03/08/2007 (7:40 pm)
1) There's a general problem with viewing alpha transparencies through waterblocks. I think it's a flaw in the one of the shaders.2) Should be... see #3 below...
3) We've been posting in the same threads for a while now so I know you're a little savvy at engine mods so I'll give you a starting point and a few tips. Adding new functionality to existing features is really easy.
Step 1 - Declare your new variable in the .h file. If you're extending an existing feature, base your declaration on the existing bits...
// S32 mAllowedTerrainSlope;
S32 mMaxAllowedTerrainSlope;
S32 mMinAllowedTerrainSlope;So now you need to first convert all instances of "mAllowedTerrainSlope" to "mMaxAllowedTerrainSlope" in both the .h and .cpp files... remember to update your scripting... and then at least that bit is done.
Now throughout the .h and .cpp files, use all the "mMaxAllowedTerrainSlope" lines and duplicate them for "mMinAllowedTerrainSlope".
Now you'll have both of your new parameters available.
Good thing about most TGEA code is that it's well (if not pithily) documented.
Find this line (as it should read after you made your substitutions):
// Invalidate if we are below Allowed Terrain Angle. if (RayEvent.normal.z < mSin(mDegToRad(90.0f-mFieldData.mMaxAllowedTerrainSlope))) CollisionResult = false;
Add this line beneath it:
// Invalidate if we are below Allowed Terrain Angle. if (RayEvent.normal.z > mSin(mDegToRad(90.0f-mFieldData.mMinAllowedTerrainSlope))) CollisionResult = false;
Now I can't say that this is gonna work off the bat... but it serves as a good example for a 5 minute tutorial on extending TGEA functionality. :)
... and it's certainly a good pointer in the right direction for getting it working.
EDIT: mah awshum speeling
#39
addikt
03/08/2007 (8:58 pm)
Awesome as always Bryce, Ill get onto trying this out asap and let you know how I get on ;)addikt
#40
cheers
addikt
03/11/2007 (5:27 pm)
Would anyone have any idea how to stop the foliage billboard from rotating and how to add 2 additional planes in a crosshatch fashion?cheers
addikt
Torque Owner Nick Wagner