Realtime water reflections shader

Posted By: XNASorcerer

Realtime water reflections shader - 02/03/05 10:32

How can I do that?
Posted By: ISG

Re: Realtime water reflections shader - 02/03/05 11:40

You should really get use to using the 'Search' feature with these forums. Steempipe has recently released his source of his Water Shader you can find here:
http://www.conitecserver.com/ubbthreads/showflat.php?Cat=&Board=SHADER&Number=446910&page=0&view=collapsed&sb=5&o=&fpart=1
And I am sure there have been at least one or two other releases in the past, but you shouldn't post without looking.

Hope that helps though.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/03/05 11:51

Thanks, but this is not a Realtime water reflection shader.
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/03/05 11:59

To do realtime reflections, you must render the scence to a texture using a clipping plane at the surface of the water, this must be done in a DLL i think.
Then you map the result projectively on the water.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/04/05 03:17

Why in a dll?
Posted By: oliver2s

Re: Realtime water reflections shader - 02/04/05 03:30

In Antwort auf:

using a clipping plane at the surface of the water, this must be done in a DLL i think.




This can be done in the pixelshader (2.0) with the "clip()" instruction. I use this in my contest entry "Water Shader Demo".
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/04/05 04:01

Could please, post the code?
Posted By: oliver2s

Re: Realtime water reflections shader - 02/04/05 04:49

The code is unpacked in the demo: water_shader.zip.
I will post the code with a "how to implement" text soon. But at the moment I have no time.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/04/05 07:06

Quote:

I will post the code with a "how to implement" text soon.




Thank you. I will wait eagerly!
Posted By: Matt_Coles

Re: Realtime water reflections shader - 02/04/05 08:48

You'll have to use Render To Texture only available in the pro demo to get realtime reflection in the water in your open source demo. Right Oliver?

Or have you got an dll like the zeraphine x3 engine plugin to do this?
Posted By: Christoph_B

Re: Realtime water reflections shader - 02/04/05 17:13

In Antwort auf:

Or have you got an dll like the zeraphine x3 engine plugin to do this?





i think it's the only option to do mirrors and reflections ind versions lower than pro, zeraphine x3 also supports stencil mirrors.
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/04/05 17:28

too bad it its not available ..
Posted By: oliver2s

Re: Realtime water reflections shader - 02/05/05 00:02

In Antwort auf:

You'll have to use Render To Texture only available in the pro demo to get realtime reflection in the water in your open source demo. Right Oliver?




Yes this water shader is open source. And you need the Pro-Render-To-Texture feature for realtime reflections. In my demo is the reflection texture done by a screenshot because I have only commercial edition. The code is also written for Pro-Users. You must comment the screenshot function and uncomment the render-to-texture part (its documented in the code).
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/05/05 01:42

You can of course write a dll to render the terrain.. maybe i will do it
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/05/05 02:01

"Parameter unknow: refrview..."

???
Posted By: oliver2s

Re: Realtime water reflections shader - 02/05/05 02:04

You can delete this line (it was original for a refraction effect underwater, but then I haven't implemented this).
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/05/05 02:16

This is what I am getting:

Posted By: oliver2s

Re: Realtime water reflections shader - 02/05/05 02:46

You must comment out the "make_mirror();" line. Then it should work.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/05/05 03:10

I did that and it is not working.
Posted By: oliver2s

Re: Realtime water reflections shader - 02/05/05 03:47

Have you called the "loadfx()" function after starting?
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/05/05 04:04

That is it! It is working now!
Thank you.

Now... How can I move the camera inside your level?
Posted By: oliver2s

Re: Realtime water reflections shader - 02/05/05 05:41

By pressing "0" in development version (like in other 3DGS levels).
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/05/05 07:57

Oliver-- great shader.. I have written a DLL that renders the terrain for me.. but since it renders it form the camera view, obviously the reflection wrong.. it should be upside down.. how can you make it so the reflection is upside down..just inverting the mid.xy coords flips it but also makes it move the wrong way left and right when panning the camera..
Posted By: Steempipe

Re: Realtime water reflections shader - 02/05/05 08:21

Yes.. This is a great shader. Thanks for making the code available!

Matt: Maybe you can get some ideas from this place:
http://www.gamedev.net/community/forums/topic.asp?topic_id=253039

This Yann L. guy, says:
Quote:

Assuming your water plane is at y = 0, and y is the up axis, then the easiest method is to simply multiply the view matrix with a mirror matrix like this one:

1 0 0 0
0 -1 0 0
0 0 1 0
0 0 0 1


That will flip the geometry (or the camera, it's the same, just seen from a different coordinate frame) around the Y axis.

If your plane is not exactly at y=0, then you need to adjust the translation part accordingly.






I have never worked on implementing it, however.
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/05/05 08:24

looks like it would work.. also i need to make a user clip plane, but frankl;y i dont get how the plane is setup.. you have any idea how to setup a clip plane in D3D?
Posted By: Drew

Re: Realtime water reflections shader - 02/05/05 08:31

sweet, will this be anohter "ultimate" shader?
or "ultimate" dll?


Posted By: Steempipe

Re: Realtime water reflections shader - 02/05/05 08:44

Quote:

looks like it would work.. also i need to make a user clip plane, but frankl;y i dont get how the plane is setup.. you have any idea how to setup a clip plane in D3D?




I have no words of wisdom on doing that. But, I was reading up on it and had found this link to some code.

Maybe this will help in showing a D3D way to deal with clipping.
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/05/05 09:08

Quote:

sweet, will this be anohter "ultimate" shader?
or "ultimate" dll?






dunno.. I'll see how it goes, i'm also currently working on an improved soft shadow method that uses a dll to render projected mesh textures, looking good so far. What i will do eventually ,if i can get it working right, i will release the dll with both the shadowing and reflections and do some example scripts.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/05/05 10:11

I can't use it in my level. Wed crashs!It only works with your level.
Can you help me? Please!

Another thing... How can I set the alpha tranparency?
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/05/05 23:00

Does anyone knows why I cannot use this with other level?
Posted By: oliver2s

Re: Realtime water reflections shader - 02/05/05 23:19

You can set transparency if the skin texture of the water model has a alpha channel (in which the alpha is set).

Why does WED crashs?
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/06/05 00:02

If I use your level, the code works, but if I try to use your code in any other level, Wed crashs and it doesn't give any information.
The steps that I did to use your code:
* Make a new level.
* Open Map Properties and set the script to water_shader.wdl.
* Put water model inside and give it the water action.
* Run the level and Wed crashs.


OBS.: The engine is crashing, not Wed. Sorry!
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/06/05 22:30

Yes, finally. Just when I was going to my bed, I decide to give it just one more try. It is working now. But it gives me some limitation. For instance, you use a called model floor.mdl as water. He only has 4 vertexes and two faces. And I need to use a terrain with 2025 vertexes. With my terrain, the effect is realy terrible.

This is with your floor.mdl:


This is with my terrain:

Posted By: oliver2s

Re: Realtime water reflections shader - 02/06/05 22:51

This happens if your terrain has no 3rd skin.
1# skin: render-to-texture texture
2# skin: not used
3# skin: normalmap
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/06/05 23:21

Thank you. That's the reason, because it works now.
And the reason that I coudn't use it before is: This shader needs blocks in the level. And all my blocks is map entities. So, with a big hollowed block with the sky flag on in my level, the shader started to work.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/06/05 23:27

The skin texture of the water model has a alpha channel. Now how can make it look a litle bit transparent?
Posted By: oliver2s

Re: Realtime water reflections shader - 02/06/05 23:35

I thought with a alphachannel it would work, but it doesn't. There must be something changed in the shader.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/06/05 23:48

With the other shader that Iwas using, the one that only reflects the sky cubes(Steempipe's), I could set alpha transparency with this:

Code:
 
AlphaBlendEnable = True;
ZwriteEnable = True;
ZEnable = True;

// Main point is that pixelShaderConstant 'W' will control the alpha tranparency. The XYZ will adjust some coloring.

PixelShaderConstant[0] = {0.80,0.80,0.92,0.88}; // x, y, z, w



,before "VertexShader = compile vs_1_1 WaterBump_VS();"
Posted By: key_46

Re: Realtime water reflections shader - 02/07/05 01:25

This Water Reflection impressed me, can you release the code?
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/07/05 02:58

to adjust the transparency try something like this fake fresnel term i devised:
put this in your vertex shader output struct:
float4 fresnel: COLOR0;

put this in the vertex shader:
float3 PositionWorld = mul(Pos, matWorld);
float dist=distance(PositionWorld, vecViewPos);

if (dist<=300)
{
Out.fresnel.xyzw=dist*0.005;
}

if (dist>300)
{
Out.fresnel.xyzw=1.0;
}

put this in the pixel shader input
float4 fresnel:COLOR0

then in the pixel shader at the end:

float4 final=reflection;

float3 watercolor=(0.58,0.65,0.63); //adjust this change water coloring

final.rgb*=watercolor;

final.w=(1*fresnel)*0.8;

this adjusts transparency based on view distance-but your water terrain needs a lot of faces. You said yours did so it should work. There are better ways to do this, like a real fresnel term, but this works for my purposes.

also, in the technique part of the fx file put these lines before you compile the shaders:
zwriteenable=false;
zenable=true;
alphablendenable=true;

Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/07/05 06:33

Quote:

... put this in the pixel shader input
float4 fresnel:COLOR0 ...




I have no pixel shader input.
Also matWorld and vecViewPos are not declared.

Here is the code:
Code:
 
float4x4 matWorldViewProj: register(c0);

texture entSkin1;
texture entSkin2;
texture entSkin3;

vector vecSkill41;

float1 mtlSkill1;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 texCoord: TEXCOORD0;
float3 eyeLinear: TEXCOORD1;
};

VS_OUTPUT VS_p0(float4 Pos: POSITION)
{
VS_OUTPUT Out;

float4 pos = float4(1 * Pos.x, 1 * Pos.y - 1, 1 * Pos.z, 1);
float4 pPos = mul(float4(pos.xyz,1), matWorldViewProj);

Out.Pos = pPos;

Out.texCoord.x = Pos.x * 0.5;
Out.texCoord.y = Pos.z * 0.5;

Out.eyeLinear.x = 0.5 * (pPos.z + pPos.x);
Out.eyeLinear.y = 0.5 * (pPos.z - pPos.y);
Out.eyeLinear.z = pPos.z * 1;

return Out;
}

sampler BumpMap = sampler_state
{
texture=(entSkin3);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler RefractionMap = sampler_state
{
texture=(entSkin2);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler ReflectionMap = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

float4 PS_p0( float2 texCoord: TEXCOORD0,float3 eyeLinear: TEXCOORD1) : COLOR
{

// addiere die Geschindigkeit von vecSkill41 hinzu für Wellenbewegung
float2 waterCoord = texCoord.xy * 0.001 * vecSkill41;

// die Bumpmap mit der waterCoord Koordinaten
float4 bump = tex2D(BumpMap, waterCoord) * 2 - 1;
bump = normalize(bump);

float2 mid = eyeLinear.xy / eyeLinear.z + bump.xy * 0.1f ;

// Reflektierte Wasseroberfläche
float4 reflection = tex2D(ReflectionMap, mid);

float4 final=reflection;


// Output
return reflection;
}
technique water
{
pass p1
{

VertexShader = compile vs_1_1 VS_p0();
PixelShader = compile ps_2_0 PS_p0();

}
}


Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/07/05 08:10

well simply declare the vectors at the beginning
and the pixel shader input goes in this line:
PS_p0( float2 texCoord: TEXCOORD0,float3 eyeLinear: TEXCOORD1, float4 fresnel:COLOR0)
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/07/05 08:44

Matt_Aufderheide,

I must be making something wrong, because it doesn't work. Will it be that you can help me and to alter the code?
Thanks.
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/07/05 09:35

what errors do you get?
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/07/05 11:21

Nothing is happenig!
Please change the code for me.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/08/05 06:05

Any would be apreciated.
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/08/05 17:50

post the code you made with my chnages so i can see what is wrong
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/09/05 02:39

Ok. Here it is:
Code:
  
float4x4 matWorldViewProj: register(c0);
///////////////////////////////////////////////////
float4 matWorld;
float4 vecViewPos;
///////////////////////////////////////////////////

texture entSkin1;
texture entSkin2;
texture entSkin3;

vector vecSkill41;

float1 mtlSkill1;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 texCoord: TEXCOORD0;
float3 eyeLinear: TEXCOORD1;
//////////////////////////////////////////////////////////
float4 fresnel: COLOR0;
//////////////////////////////////////////////////////////
};

VS_OUTPUT VS_p0(float4 Pos: POSITION)
{
VS_OUTPUT Out;

float4 pos = float4(1 * Pos.x, 1 * Pos.y - 1, 1 * Pos.z, 1);
float4 pPos = mul(float4(pos.xyz,1), matWorldViewProj);

Out.Pos = pPos;

Out.texCoord.x = Pos.x * 0.5;
Out.texCoord.y = Pos.z * 0.5;

Out.eyeLinear.x = 0.5 * (pPos.z + pPos.x);
Out.eyeLinear.y = 0.5 * (pPos.z - pPos.y);
Out.eyeLinear.z = pPos.z * 1;
///////////////////////////////////////////////////////
float3 PositionWorld = mul(Pos, matWorld);
float dist=distance(PositionWorld, vecViewPos);

if (dist<=300)
{
Out.fresnel.xyzw=dist*0.005;
}

if (dist>300)
{
Out.fresnel.xyzw=1.0;
}
/////////////////////////////////////////////////////////

return Out;
}

sampler BumpMap = sampler_state
{
texture=(entSkin3);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler RefractionMap = sampler_state
{
texture=(entSkin2);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler ReflectionMap = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

float4 PS_p0( float2 texCoord: TEXCOORD0,float3 eyeLinear: TEXCOORD1, float4 fresnel: COLOR0) : COLOR //////////////
{

// addiere die Geschindigkeit von vecSkill41 hinzu für Wellenbewegung
float2 waterCoord = texCoord.xy * 0.001 * vecSkill41;

// die Bumpmap mit der waterCoord Koordinaten
float4 bump = tex2D(BumpMap, waterCoord) * 2 - 1;
bump = normalize(bump);

float2 mid = eyeLinear.xy / eyeLinear.z + bump.xy * 0.1f ;

// Reflektierte Wasseroberfläche
float4 reflection = tex2D(ReflectionMap, mid);

float4 final=reflection;

///////////////////////////////////////////////////////
float3 watercolor=(0.58,0.65,0.63); //adjust this change water coloring

final.rgb*=watercolor;

final.w=(1*fresnel)*0.8;
////////////////////////////////////////////////////////


// Output
return reflection;
}
technique water
{
pass p1
{
/////////////////////////////////////////////////////////
zwriteenable=false;
zenable=true;
alphablendenable=true;
/////////////////////////////////////////////////////////
VertexShader = compile vs_1_1 VS_p0();
PixelShader = compile ps_2_0 PS_p0();

}
}


Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/09/05 03:47

change this line:
// Output return reflection

to:
return final;
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/09/05 06:58

Thank you Matt_Aufderheide. The transparency is working now.

But now, I found a bug. The objective of the transparency is to show a little of what is underneath of the water. Therefore I put a block underneath of the water, because before I didn't have anything there. Only that now it appears another reflection layer above the water.

In this pic, you can see the bug layer. It only appears in areas that have something under.


Here I am under that layer.


And here, I am above that layer.

Posted By: Pappenheimer

Re: Realtime water reflections shader - 02/09/05 07:10

This is done with a level block or map entity!

What does it do with a terrain underneath, and fish models etc.?

Terrain is IMO the more reasonable combination with this water shader!
Posted By: Nadester

Re: Realtime water reflections shader - 02/09/05 07:56

@Oliver and everyone else,
Replace the lines:

Code:
sampler BumpMap = sampler_state 
{
texture=(entSkin3);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler RefractionMap = sampler_state
{
texture=(entSkin2);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler ReflectionMap = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};



with:

Code:
sampler BumpMap = sampler_state 
{
texture=(entSkin3);
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

sampler ReflectionMap = sampler_state
{
texture=(entSkin1);
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
};



That will get rid of the glitches that occur around the edge of the camera when it's over the water. Awesome job though.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/10/05 02:46

With terrain... Sometimes I can see the terrain through the water's transparency, and sometimes I can't.

Here I can see the terrain:


Here I cannot:

Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/10/05 02:56

You must make sure the terrain entity has an action that sets:
my.transparent=off;
my.flare=off;

and your water entity must be set:
my.transparent=on;
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/10/05 03:22

Thanks. It works!!!
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/10/05 07:26

Now... How can I change the ripple direction and velocity in this shader?
Posted By: Nadester

Re: Realtime water reflections shader - 02/10/05 11:45

@Oliver,
Why are you using PS2.0 for the water effect? You could pull it off with 1.1, if all you need is enviromental bumpmapping. (or FFP envbumpmap, although that crashes on nvidia cards).
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/10/05 16:29

He is using HLSL... HLSL doesnt usually like shader versions below 2.0
Posted By: oliver2s

Re: Realtime water reflections shader - 02/10/05 22:35

Yes, I've tried 1.4 but there are some errors with instruction that I need. And I can only HLSL, no ASM. Why using environment mapping? Then there isn't a realtime reflection anymore.
Posted By: Nadester

Re: Realtime water reflections shader - 02/11/05 09:44

Well, it's not "enviromental mapping", but is it not the same type of shader? - just instead you're applying a rendered view rather than a cube map. Like I said, if that being the case - You can do it in HLSL with ps1.1 and in just plain FFP (though like I said, ffp envbumpmap has compatability issues on nvidia cards). If I have some time, I'll try and fiddle with it.
Posted By: Nadester

Re: Realtime water reflections shader - 02/11/05 09:46

You can also code just the pixel shader in ASM, and code the rest in HLSL - and it will work fine.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/11/05 10:28

There is still something strange with this shader and terrains.
Here you can see the effect working ok:

And if you walk just two steps back with the cam you can see the bad one:


Here without the transparency code:


Posted By: oliver2s

Re: Realtime water reflections shader - 02/12/05 05:54

Have you applied the "terrainobject" action to the terrain? All objects that are in water must have this action for clipping underwater in the mirror view.

[EDIT]: use this new action:
Code:
MATERIAL terrain_clipping //for clipping the terrain underwater in the mirror view

{
event = render_event();

flags = enable_view;

effect=
"

float4x4 matWorldViewProj: register(c0);

texture entSkin1;

vector vecSkill41;

float1 mtlSkill1;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 texCoord: TEXCOORD0;
float3 eyeLinear: TEXCOORD1;
};

VS_OUTPUT VS_p0(float4 Pos1: POSITION, float2 basemap: TEXCOORD0)
{
VS_OUTPUT Out;

Out.Pos = mul(Pos1, matWorldViewProj);

Out.texCoord = basemap;

Out.eyeLinear.x = Pos1.x;
Out.eyeLinear.y = Pos1.y;
Out.eyeLinear.z = Pos1.z;

return Out;
}

sampler BaseMap = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

float4 PS_p0( float2 texCoord: TEXCOORD0,
float3 eyeLinear: TEXCOORD1) : COLOR
{
float4 base = tex2D(BaseMap, texCoord);

clip((eyeLinear.y-vecSkill41)*mtlSkill1);

// Output

return base * 1.2;
}
technique water
{
pass p1
{
VertexShader = compile vs_1_1 VS_p0();
PixelShader = compile ps_2_0 PS_p0();
}
}
";
}

ACTION terrainobject
{
var hoch1;
my.material = terrain_clipping;
my.nofog = on;
hoch1 = water_ent.z*0.049;
while(1)
{
my.skill41 = float(hoch1);
wait(1);
}
}


Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/12/05 07:32

Yes, I have. But the problem still persist. If you want, I could send this level for you to see it.
Posted By: oliver2s

Re: Realtime water reflections shader - 02/12/05 08:24

Sorry, but I haven't enough time at the moment to debug the level.
Posted By: XNASorcerer

Re: Realtime water reflections shader - 02/12/05 13:19

Maybe, I could send you a video.
Posted By: sriram

Re: Realtime water reflections shader - 02/12/05 22:53

Hey this code is awesome...but unfortunately,its not working on my level
I've created a new map with just your floor_mdl and a terrain object.I've assigned the 'ater' action to the floor and 'terrainobject' action to the terrain and also uncommented the pro code...but I'm getting these Compilation errors :

<mirror.nocull=on>
Error(0): parameter unknown mirror parameter

<loadfx();>
Error(29): keyword unknown loadfx

<d3d_shaderversion<2020>
Error(63): parameter unknown d3d_shaderversion

<my.material=terrain_clipping;>
Error(29): keyword unknown my.material

My modified Code is :
Code:
 
// shader_test.wdl

var video_mode = 7;
var video_depth = 32;
var d3d_mipmapping = 3;
var d3d_anisotropy = 16;
var clip_range = 200000;
var clip_factor = 1;
var clip_size = 0;
var clip_factor= 1;
var fps_max = 70;
var fps_min = 35;
var max_entities = 1000;
var d3d_transform = on;
var move_friction = 0;
var d3d_antialias = 6;
var logo = 0;
var video_screen = 1;

var bumpness = 100;
var speed = 1;

bmap slider_tga = "slider.tga";
bmap mauszeiger_bmp = "mauszeiger.bmp";
bmap slider_box_tga = "slider_box.tga";

PANEL pan_sliders
{
bmap = slider_box_tga;
hslider = 492, 4, 610, slider_tga, 0, 500, bumpness;
hslider = 492, 30, 610, slider_tga, 0, 10, speed;
}

FUNCTION loadfx();

function main
{
d3d_autotransparency = on;
level_load("water_shader.wmb");
}

SKY himmel1
{
type = <himmel3+6.tga>;
layer = 3;
flags = cube,visible;
z = 0;
}

VIEW mirror
{
layer = 10;
}

entity* water_ent;

function render_event();

MATERIAL waterfx
{
}

var hoch;
var hoch1 = 3.2;

ACTION water
{
var count;
var count1;
var a = 180;
var b;
water_ent = me;
my.nofog = on;
// my.invisible = on;
my.material = waterfx;
my.scale_x = 40;
my.scale_y = 40;
my.z += 20;
hoch = my.z;
while(1)
{
my.z = hoch;
my.skill41=float(count);
my.skill42=float(count1);
my.skill43=float(0);
my.skill44=float(0);
a += speed*time;
a %= 360;
b += speed*time;
b %= 360;
count = sin(a)+bumpness;
count1 = cos(b)+bumpness;
hoch1 = my.z*0.049;
wait(1);
}
}

STARTER init_water
{
wait(2);

camera.x = -1552;
camera.y = -935;
camera.z = 98;
camera.pan = 43;
camera.tilt = -4;

mirror.size_x = camera.size_x;
mirror.size_y = camera.size_y;

d3d_fogcolor1.red = 128;
d3d_fogcolor1.green = 128;
d3d_fogcolor1.blue = 150;

fog_color = 1;

camera.fog_start = 20;
camera.fog_end = 5000;
mirror.fog_start = 20;
mirror.fog_end = 5000;

camera.clip_near = 0;
mirror.clip_near = 0;
mirror.nocull = on;

/*If you have A6 Pro Version*/
mirror.bmap = bmap_for_entity(water_ent,1);
// refrview.bmap = bmap_for_entity(water_ent,2);
mirror.aspect = -1;
mirror.visible = on;
// refrview.visible = on;

/*If you have A6 Com Version

IF(d3d_shaderversion < 2020)
{
pan_sliders.visible = off;
while(1)
{
draw_text("You need a video card with Pixelshader 2.0",screen_size.x-520,screen_size.y-280,vector(0,0,0));
wait(1);
}
}

make_mirror();
mirror.aspect = -1;*/

mouse_map = mauszeiger_bmp;

proc_late();
while(1)
{
mirror.x = camera.x;
mirror.y = camera.y;
mirror.z = -camera.z+ (2*water_ent.z);
mirror.pan = camera.pan;
mirror.tilt = -camera.tilt;

mouse_pos.x = pointer.x;
mouse_pos.y = pointer.y;

pan_sliders.pos_x = (screen_size.x/2)-800;
pan_sliders.pos_y = screen_size.y-52;

wait(1);
}
}

/*FUNCTION make_mirror
{
mirror.visible = on;
pan_sliders.visible = off;
mouse_mode = 0;
mirror.size_x = camera.size_x;
mirror.size_y = camera.size_y;
water_ent.visible = off;
wait(1);
bmap_for_screen(bmap_for_entity(water_ent,1),1,0);
mirror.visible = off;
water_ent.visible = on;
pan_sliders.visible = on;
mouse_mode = 2;*/

loadfx();
}

MATERIAL terrain_clipping //for clipping the terrain underwater in the mirror view
{
event = render_event();

flags = enable_view;

effect=
"

float4x4 matWorldViewProj: register(c0);

texture entSkin1;

vector vecSkill41;

float1 mtlSkill1;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 texCoord: TEXCOORD0;
float3 eyeLinear: TEXCOORD1;
};

VS_OUTPUT VS_p0(float4 Pos1: POSITION, float2 basemap: TEXCOORD0)
{
VS_OUTPUT Out;

Out.Pos = mul(Pos1, matWorldViewProj);

Out.texCoord = basemap;

Out.eyeLinear.x = Pos1.x;
Out.eyeLinear.y = Pos1.y;
Out.eyeLinear.z = Pos1.z;

return Out;
}

sampler BaseMap = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

float4 PS_p0( float2 texCoord: TEXCOORD0,
float3 eyeLinear: TEXCOORD1) : COLOR
{
float4 base = tex2D(BaseMap, texCoord);

clip((eyeLinear.y-vecSkill41)*mtlSkill1);

// Output

return base * 1.2;
}
technique water
{
pass p1
{
VertexShader = compile vs_1_1 VS_p0();
PixelShader = compile ps_2_0 PS_p0();
}
}
";
}

FUNCTION loadfx
{
effect_load(terrain_clipping,"clip.fx");
effect_load(waterfx,"water.fx");
}

ACTION terrainobject
{
IF(d3d_shaderversion < 2020) { return; }
my.material = terrain_clipping;
my.nofog = on;
while(1)
{
my.skill41 = float(hoch1);
wait(1);
}

}

function render_event()
{
if(render_view == camera)
{
mtl.skill1 = float(0);
}
if(render_view == mirror)
{
mtl.skill1 = float(1);
}
}



Any help is appericiated..BTW,I have A6 Pro..and I have a GeForceFX 5200,so the problem is with the code
Posted By: oliver2s

Re: Realtime water reflections shader - 02/12/05 22:58

I have not tested it, but I think my the code is only working with A6.31
Posted By: sriram

Re: Realtime water reflections shader - 02/13/05 01:18

Thanks for replyig oliver...I'm using 3DGS A6 Pro v6.22.I reverted from 6.31 as I've started making a game in 6.22...so,could someone please post a code with the necessary changes so that it would work in 6.22 ?

Thanks
Posted By: Matt_Aufderheide

Re: Realtime water reflections shader - 02/13/05 01:42

because 6.22 is directx8 not 9 this shader will not work(because it's HLSL). A new shader would have to be written to use ps 1.1-1.4 in assembler. It probably wouldnt work though, as there is no clip function in the lower shader versions..you need to render the terrain manually in a dll with a clip plane.

Your best solution is to go to 6.31.. you won't regret it.
Posted By: sriram

Re: Realtime water reflections shader - 02/13/05 22:01

Thanks Matt! Got it working with 6.31
Posted By: MASTERJUNOJET

Re: Realtime water reflections shader - 02/14/05 12:38

I need some help here, i see that the water effect in the demo is very nice, how can i impelement it into my own application? Ive tried to mess around with the scripts and all that jazz, and splice them into my own, no success however, can someone tell me how to get this great water effect in my project please?
Posted By: XNASorcerer

Implementing the Realtime water reflections shader - 02/21/05 06:37

///////////////////////////////////////////////////////////
Shader Code from Oliver2s and tranparency code from Matt_Aufderheide.
///////////////////////////////////////////////////////////


In Wed, add a big Hollowed cubes and increase it so that it involves your level and activate the flag Sky of Hollowed cube.
The water terrain needs to have a 3rd skin (a normalmap).
You also need to have a SkyCube+6.tga inside your level folder.
Apply terrainobject action to your terrain and Water action to your water terrain.

Make a WaterShader.wdl and include it inside of your level.wdl.

You can find the code for each files (WaterShader.wdl; water.fx; clip.fx) below.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////// WaterShader.wdl
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Code:


var bumpness =24;
var speed = 1;

bind <water.fx>;

entity* water_ent;

var temp2[3];

var hoch;
var hoch1 = 3.2;

SKY himmel1
{
type = SkyCube+6.tga;
layer = 3;
flags = cube,visible;
z = 0;
}

VIEW mirror
{
layer = 10;
}

function render_event();

MATERIAL waterfx
{
}

var hoch;
var hoch1 = 3.2;

ACTION water
{
water_ent = me;
var count;
var count1;
var a = 180;
var b;
my.transparent = on;
my.nofog = on;
my.material = waterfx;
my.scale_x = 50;
my.scale_y = 50;
my.z += 20;
hoch = my.z;
while(1)
{
my.z = hoch;
my.skill41=float(count);
my.skill42=float(count1);
my.skill43=float(0);
my.skill44=float(0);
a += speed*time;
a %= 360;
b += speed*time;
b %= 360;
count = sin(a)+bumpness;
count1 = cos(b)+bumpness;
hoch1 = my.z*0.049;
wait(1);
}
}

STARTER init_water
{
wait(2);

while(water_ent==null){wait(1);}

mirror.size_x = camera.size_x;
mirror.size_y = camera.size_y;

d3d_fogcolor1.red = 128;
d3d_fogcolor1.green = 128;
d3d_fogcolor1.blue = 150;

fog_color = 1;

camera.fog_start = 8000;
camera.fog_end = 10000;
mirror.fog_start = 8000;
mirror.fog_end = 10000;

camera.clip_near = 0;
mirror.clip_near = 0;
mirror.nocull = on;

mirror.bmap = bmap_for_entity(water_ent,1);
mirror.bmap = bmap_for_entity(water_ent2,1);
mirror.aspect = -1;
mirror.visible = on;


IF(d3d_shaderversion < 2020)
{
while(1)
{
draw_text("You need a video card with Pixelshader 2.0",screen_size.x-520,screen_size.y-280,vector(0,0,0));
wait(1);
}
}

loadfx();
mirror.aspect = -1;

proc_late();
while(1)
{
mirror.x = camera.x;
mirror.y = camera.y;
mirror.z = -camera.z+ (2*water_ent.z);
mirror.z = -camera.z+ (2*water_ent2.z);
mirror.pan = camera.pan;
mirror.tilt = -camera.tilt;
wait(1);
}
}

MATERIAL terrain_clipping //for clipping the terrain underwater in the mirror view

{
event = render_event();
flags = enable_view;
effect=
"
float4x4 matWorldViewProj: register(c0);
texture entSkin1;
vector vecSkill41;
float1 mtlSkill1;
struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 texCoord: TEXCOORD0;
float3 eyeLinear: TEXCOORD1;
};

VS_OUTPUT VS_p0(float4 Pos1: POSITION, float2 basemap: TEXCOORD0)
{
VS_OUTPUT Out;
Out.Pos = mul(Pos1, matWorldViewProj);
Out.texCoord = basemap;
Out.eyeLinear.x = Pos1.x;
Out.eyeLinear.y = Pos1.y;
Out.eyeLinear.z = Pos1.z;
return Out;
}
sampler BaseMap = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

float4 PS_p0( float2 texCoord: TEXCOORD0,
float3 eyeLinear: TEXCOORD1) : COLOR
{
float4 base = tex2D(BaseMap, texCoord);
clip((eyeLinear.y-vecSkill41)*mtlSkill1);
// Output
return base * 1.2;
}
technique water
{
pass p1
{
VertexShader = compile vs_1_1 VS_p0();
PixelShader = compile ps_2_0 PS_p0();
}
}
";
}

FUNCTION loadfx
{
effect_load(terrain_clip ping,"clip.fx");
effect_load(waterfx,"water.fx");
}

ACTION terrainobject
{
IF(d3d_shaderversion < 2020) { return; }
while(water_ent==null){wait(1);}
var hoch1;
my.material = terrain_clipping;
my.nofog = on;
while(1)
{
my.skill41 = float(hoch1);
wait(1);
}

}

function render_event()
{
if(render_view == camera)
{
mtl.skill1 = float(0);
}
if(render_view == mirror)
{
mtl.skill1 = float(1);
}
}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////End of WaterShader.wdl
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////Water.fx
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Code:
 


float4x4 matWorldViewProj: register(c0);
float4 matWorld;
float4 vecViewPos;

texture entSkin1;
texture entSkin2;
texture entSkin3;

vector vecSkill41;

float1 mtlSkill1;

sampler BumpMap = sampler_state
{
texture=(entSkin3);
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

sampler ReflectionMap = sampler_state
{
texture=(entSkin1);
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
};

struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 texCoord: TEXCOORD0;
float3 eyeLinear: TEXCOORD1;
float4 fresnel: COLOR0;
};

VS_OUTPUT VS_p0(float4 Pos: POSITION)
{
VS_OUTPUT Out;

float4 pos = float4(1 * Pos.x, 1 * Pos.y - 1, 1 * Pos.z, 1);
float4 pPos = mul(float4(pos.xyz,1), matWorldViewProj);

Out.Pos = pPos;

Out.texCoord.x = Pos.x * 0.5;
Out.texCoord.y = Pos.z * 0.5;

Out.eyeLinear.x = 0.5 * (pPos.z + pPos.x);
Out.eyeLinear.y = 0.5 * (pPos.z - pPos.y);
Out.eyeLinear.z = pPos.z * 1;

float3 PositionWorld = mul(Pos, matWorld);
float dist=distance(PositionWorld, vecViewPos);

if (dist<=300)
{
Out.fresnel.xyzw=dist*0.005;
}

if (dist>300)
{
Out.fresnel.xyzw=1.0;
}


return Out;
}


float4 PS_p0( float2 texCoord: TEXCOORD0,float3 eyeLinear: TEXCOORD1, float4 fresnel: COLOR0) : COLOR
{

// addiere die Geschindigkeit von vecSkill41 hinzu für Wellenbewegung
float2 waterCoord = texCoord.xy * 0.001 * vecSkill41;

// die Bumpmap mit der waterCoord Koordinaten
float4 bump = tex2D(BumpMap, waterCoord) * 2 - 1;
bump = normalize(bump);

float2 mid = eyeLinear.xy / eyeLinear.z + bump.xy * 0.1f;

// Reflektierte Wasseroberfläche
float4 reflection = tex2D(ReflectionMap, mid);

float4 final=reflection;


float3 watercolor=(0.93,0.93,0.75); //adjust this change water coloring
final.rgb*=watercolor;
final.w=(1*fresnel)*0.93;//0.8;



// Output
//return reflection;
return final;
}
technique water
{
pass p1
{
zwriteenable=false;
zenable=true;
alphablendenable=true;

VertexShader = compile vs_1_1 VS_p0();
PixelShader = compile ps_2_0 PS_p0();

}
}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////End of Water.fx
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////End of clip.fx
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Code:
 


float4x4 matWorldViewProj: register(c0);

texture entSkin1;

vector vecSkill41;

vector mtlSkill1;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 texCoord: TEXCOORD0;
float3 eyeLinear: TEXCOORD1;
};

VS_OUTPUT VS_p0(float4 Pos1: POSITION, float2 basemap: TEXCOORD0)
{
VS_OUTPUT Out;

Out.Pos = mul(Pos1, matWorldViewProj);

Out.texCoord = basemap;

Out.eyeLinear.x = Pos1.x;
Out.eyeLinear.y = Pos1.y;
Out.eyeLinear.z = Pos1.z;

return Out;
}

sampler BaseMap = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

float4 PS_p0( float2 texCoord: TEXCOORD0,
float3 eyeLinear: TEXCOORD1) : COLOR
{
float4 base = tex2D(BaseMap, texCoord);

clip((eyeLinear.y-vecSkill41)*mtlSkill1);

// Output

return base * 1.2;
}
technique water
{
pass p1
{
VertexShader = compile vs_1_1 VS_p0();
PixelShader = compile ps_2_0 PS_p0();
}
}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////End of clip.fx
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Posted By: Superku

Re: Implementing the Realtime water reflections shader - 02/22/05 02:41

Thanks, Sorcerer.
But it still doesn't work for me.
What is "Shader_water" (in action water) ?

Felix
Posted By: XNASorcerer

Re: Implementing the Realtime water reflections shader - 02/26/05 06:55

Just forget this Shader_water.
Posted By: BionicHero

Re: Implementing the Realtime water reflections shader - 03/04/05 08:17

Hi! I also tried some stuff with the shader and I have one question: my project doesn't use a skybox but uses DOME to display the sky. But the sky is not visible on the mirror bitmap. Is there anything I could do to fix this?
Posted By: Templar

Re: Implementing the Realtime water reflections sh - 03/10/05 04:00

Hi! this is a realy nice and helpful shader. However I was wondering about one thing - there's something strange going on near the very borders of the view - there are some distortions happening. It seems to be present even on the screenshots in this thread. Is there any way how to get rid of this? thanks!
Posted By: Matt_Aufderheide

Re: Implementing the Realtime water reflections sh - 03/10/05 06:46

Yes simply find this section :
sampler ReflectionMap = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;

};

and add these lines :
AddressU = CLAMP;
Addressv = CLAMP;
Posted By: Rhuarc

Re: Implementing the Realtime water reflections sh - 03/10/05 08:11

I don't think that is his problem. The problem is that the rendered texture is in a 1:1 ratio (it's square) where the screen is a 4:3 ratio. This means that the sides will be stretched.

You have to change the texture transform matrix to use a 4:3 ratio:
m11=1.25
m22=1

I'll take a look at the code and show exactly how this is done (that isn't the actual code there) if I find some time in a little bit- unless someone beats me to it

-Rhuarc
Posted By: Templar

Re: Implementing the Realtime water reflections sh - 03/10/05 18:37

great, thanks a lot! Just one more question though - would it be hard to make such changes to this shader that in case that the video card didn't support PS2.0, realtime reflections were disabled and only the skycube was used for enviromental mapping?

thanks everyone!
Posted By: Templar

Re: Implementing the Realtime water reflections sh - 03/13/05 02:57

*bump*

Rhuarc, could you (or anyone else ) please show me how to adjust the matrix to suit the screen aspect ratio? thanks!
Posted By: DARKLORD

Re: Implementing the Realtime water reflections sh - 03/25/05 19:56

I have bought the Zeraphine X3 Plugin and maybe somebody can tell me weather I can use this shader with the com edition and the plugin.
Can I use the code for the pro edition or do I have to change something in the code. If so maybe somebody using this plugin can help me to do this becaus I dont understand these shader stuff .

Thanks
Posted By: angel1

Re: Implementing the Realtime water reflections sh - 04/01/05 16:19

I would like to know also the same because I was interested in the Zeraphine X3 Plugin in order to use water shader reflections(mirror)and such with commercial edition.
© 2024 lite-C Forums