Game Development Community

RenderMonkey Water Shader to T3D help and workflow advice requested

by Jack Stone · in Torque 3D Professional · 09/28/2013 (8:55 am) · 16 replies

Hello,

I am trying to port a fairly simple shader from RenderMonkey into T3D (Latest Release).

I hae done extensive research, and I am confident that I understand the theory behind samplers, linking up inputs, etc, and I have played around with simple sample shaders before successfully. For some reason however all of the more complex shaders I have tried to import have looked like this:

phoenixgamedevelopment.com/blogs/media/blogs/pgd/Project%20130%20Fluid%20Dynamic...

This is my custom metaball liquid mesh, but I have confirmed that this occurs with all objects I apply the custom material too, and that other materials work perfectly on my metablob mesh.
This effect seems to happen with all of the shaders I try to use.

If I could figure out how to get shaders from RenderMonkey reliably, it would be a major step forward.

This is my shader definition:
singleton ShaderData( FluidDynamicsWaterShader )  
{  
   DXVertexShaderFile   = "shaders/common/FluidDynamicsWaterV.hlsl";  
   DXPixelShaderFile    = "shaders/common/FluidDynamicsWaterP.hlsl";  
   pixVersion = 2.0;  
   samplerNames[0] = "$Noise";
   samplerNames[1] = "$skyBox";
};

and my CustomMaterial:

singleton CustomMaterial(FluidDynamics)
{
	mapTo = "FluidDynamicsTex";
	sampler["Noise"] =  "art/shapes/FluidDynamics/NoiseVolumeMOD"; 
	sampler["skyBox"] = "art/shapes/FluidDynamics/Snow";

	texture[0] = "./NoiseVolume.png";
	texture[1] = "./Snow.png";

	shader = FluidDynamicsWaterShader;
	version = 2.0;
};

I had to convert the noise volume and snow skybox texture to PNG format from DDS, because I couldn't get the DDS files to work.


#1
09/28/2013 (8:56 am)
Finally, my vertex and pixel shaders. There were several variables being sent in on registers, which I changed to definitions within the shader itself for simplicity.I also had to hard-code a "time_0_x" variables to 1.0. I think I can use PC_ACCUM_TIME for this?

I can post the original shader code from render monkey too if neccessary, but I changed very little.

Vertex first:

#define IN_HLSL  
    #include "shdrConsts.h"  
      
float4x4 view_proj_matrix: register(VC_WORLD_PROJ); 


float4 view_position : register(VC_EYE_POS);

struct VS_OUTPUT {
   float4 Pos:    POSITION;
   float3 pos:    TEXCOORD0;
   float3 normal: TEXCOORD1;
   float3 vVec:   TEXCOORD2;
};

VS_OUTPUT main(float4 Pos: POSITION, float3 normal: NORMAL){
   VS_OUTPUT Out;

float4 scale = float4(0.01001,0.00554,0.01000,1.00000);

   // Get some size on the water
   Pos.xy *= 1000;
   Pos.z = -30;

   Out.Pos = mul(view_proj_matrix, Pos);
   Out.pos = Pos.xyz * scale;
   Out.vVec = Pos - view_position;
   Out.normal = normal;

   return Out;
}

and finally Pixel:
sampler Noise: register(s0);
sampler skyBox: register(s1);

float4 main(float3 pos: TEXCOORD0, float3 normal: TEXCOORD1, float3 vVec: TEXCOORD2) : COLOR {

//These were being send in on registers:
float waveSpeed = 0.34;
float noiseSpeed = 0.18;
float fadeBias = 0.3;
float fadeExp = 6.08;

float4 waterColor = float4(0.196078,0.345865,0.684210,1.00000);

float time_0_X = 1.0; //This was a "time" variable, I have hard coded it here for testing

   pos.x += waveSpeed  * time_0_X;
   pos.z += noiseSpeed * time_0_X;

   float4 noisy = tex3D(Noise, pos);

   // Signed noise 
   float3 bump = 2 * noisy - 1;
   bump.xy *= 0.15;
   // Make sure the normal always points upwards
   bump.z = 0.8 * abs(bump.z) + 0.2;
   // Offset the surface normal with the bump
   bump = normalize(normal + bump);

   // Find the reflection vector
   float3 reflVec = reflect(vVec, bump);
   float4 refl = texCUBE(skyBox, reflVec.yzx);

   float lrp = 1 - dot(-normalize(vVec), bump);

   // Interpolate between the water color and reflection
   return lerp(waterColor, refl, saturate(fadeBias + pow(lrp, fadeExp)));
   
}

I know that's a lot of code to look through, but I can't see where I am going wrong. I matched the inputs, and I don't see anything obviously wrong...

Thanks for any help!
#2
09/28/2013 (8:23 pm)
do not know much about shader.
but i shave seen people to use render monkey to develop shader
and then port it to t3d.

if u do a search on forum u will find old threads on it.
may be u can ask there to get attraction on this post.
#3
09/29/2013 (11:17 am)
Thank you for your reply!

I have read quite a few tutorials and looked at example shaders, I know it's possible to export from RenderMonkey to T3d with some work, but I can't figure out where I am going wrong.

I'm nearly sure the problem is in my vertex shader, where the data isn't being sent from T3D properly. For example:

float4x4 view_proj_matrix: register(VC_WORLD_PROJ);  
    float3 eye_pos: register(VC_EYE_POS);  
    float3 inLightDir : register(VC_LIGHT_DIR1);

This doesn't seem to work...
#4
09/29/2013 (4:14 pm)
If we could @mention people I'd try to pull Ron Kapaun over here. Failing that you might like to shoot him an email; he's done some great shader work. Also the liman3d guys (Ivan Mandzhukov comes to mind).
#5
09/29/2013 (10:08 pm)
Thanks for the suggestion Daniel, I might send him an email if I still can't get this figured out.

I have come across a lot of posts by the liman3D people, they certainly seem to know a lot about this kind of thing!

Just to clarify, it seems now that to use things like $refractbuff and $lightdir, I need to do some C++ coding, is this right? It seems like when I use script alone they are not being passed in...
#6
09/30/2013 (12:58 pm)
I was under the impression a volumetric texture has to be in dds format? In which case your tex3D instruction wouldn't be valid with a png. I could be totally off though. But that's the first thing that strikes me a bit curious.

And how were you unable to get the dds to work? In my experience with Torque and dds, you have to make sure to add the ".dds" extension to the file in the material script. Were you sure to include that?
#7
09/30/2013 (5:31 pm)
" how were you unable to get the dds to work? "

do not know what was his problem with dds.
but in case if anybody else have noticed that there are certain dds formats that make t3d to crash.
specially those which came up with dexsoft and arteria 3d's content packs.
i have tested them with unity and other engines.
works fine on them.
but not in t3d.
#8
09/30/2013 (8:49 pm)
Quote:" how were you unable to get the dds to work? "

Not every DDS compression is supported in T3D (certainly not the new 64 and 128 bpp) Try to stick to DXT1, DXT3 or DXT5 if you're having issues.
#9
09/30/2013 (10:32 pm)
" Try to stick to DXT1, DXT3 or DXT5 if you're having issues."

hard when u have lots of texture and each of them in separate folder.
a batch convertion can be done.
but in that case all textures are converted and started to make problem with texture entry in material defination having no extension specified in it.
how hard it is to make t3d support all dds version?
or integrating nvidea dds tool in t3d?
#10
09/30/2013 (11:08 pm)
Quote:a batch convertion can be done

You have to handle DDS like a compressed JPG file. Once you start with compression you'll produce artefacts. Ideally you generate the DDS file from the source file (models bought online often include TGA or PNG in 24 or 32 bit). Each DDS file should be exported manually to ensure a maximum quality / compression ratio. NVIDIA's DDS tool is highly recommended for this, as you can control all the things that happen when converting to a DDS format (changing of colours, sharpness etc.) It's a lot of work, I know, but it's worth the effort IMO.

In case of crashing in T3D, one could try to import the models with one at a time in an empty scene until the problems occur?!
#11
10/01/2013 (6:42 am)
I didn't realise DDS files had different formatting options, this could certainly be the cause of the issue. I tried with DDS files, and I believe, as ahsan said, I was getting crashes.

I'll bet it was because of a file compression issue. I am downloading Nvidia's DDS tool now, and convert the noise volume, and see if that helps, thanks a lot!
#12
10/01/2013 (6:28 pm)
It seems the noise volume is definitely the problem. Or a problem, at least. I have tried converting the PNG file to a dds, and it loads but doesn't display correctly, I have tried the PNG file by iself, and I have tried creating custom noise volumes. All of these produced bad-looking results.
Using the orignal DDS file by itself triggered an assert in gfxD3D9TextureManager.cpp:

AssertFatal( dds->mSurfaces.size() > 0, "Assumption failed. DDSFile has no surfaces." );

Is there something different about a DDS file that a regular image file doesn't have?
#13
10/01/2013 (9:00 pm)
DDS files are different from bitmap files like PNG. DDS is a format that can be read directly in to VRAM, bitmap files need to be 'converted' upfront. Beside that you have 'attributes' that are DDS specific, like MIP maps for example.

Textures for terrain and props will perform better in DDS with the right properties. GUI and FX don't profit that much from the DDS format. I think you should be able to use a PNG file. You can send your file to me: info[a]studiodimsum_com , and I'll convert it for you into a PNG -and DDS- if you want.

Else, if it's used in the same way, you can try out the noise file from T3D: coreartfizz_noise.dds
#14
10/04/2013 (6:08 pm)
Hello,

Thank you for your advice, I was able to use torques noise dds, that worked fine!

I think I need to do some research into DDS files, before I can create my own, I assumed they were just like JPG's or PNG's.

How do you correctly convert a file to DDS?

Thanks again!

#15
10/04/2013 (6:18 pm)
"How do you correctly convert a file to DDS?"

there r some old threads on this topic.so some search.
here is the newest:
http://www.garagegames.com/community/forums/viewthread/135221
#16
10/14/2013 (8:55 am)
Thank you ahsan, that's very helpful!