Reflecting Water

From GameStudio Wiki

Jump to: navigation, search

You can find a downloadlink for a sample project with instructions on how to use it in this Forumpost.


Here you go in case it is somewhen not accesible anymore, or you don`t want to download it:


This effect needs render to texture, if your 3DGS edition does not support it, you can use the plugin created by ChrisB (just copy it into your proectfolder). This doesn`t always seem to work right, if you don`t want realtimereflections or the water is just black, you can find a solution further down.

How to integrate it into your own project: Place the water.fx file, the Shader.wdl and the waves.jpg file in your Projectfolder, add this line to your main wdl:

   include <Shader.wdl>;

and assign the action water to a flat model or terrain entity.


Copy the follwing code into a new file in SED and save it as Shader.wdl:

//use Render2Texture Dll by ChrisB
plugindir ".";                       //Comment this part
dllfunction r2T_eventSky();          //out if your
dllfunction r2T_eventEntities();     //3DGS edition
render_sky = r2T_eventSky;           //supports
render_entities = r2T_eventEntities; //render to texture.
//

bmap waterbump = <waves.jpg>;
//bmap envspec = <sky.tga>;

function mtl_ffpwater_event();

material mtl_ffpwater
 {
   skin1=waterbump;
//   skin2=envspec;

   event = mtl_ffpwater_event;
   effect = "water.fx";
}

function mtl_ffpwater_event()
{
	while(1)
	{
		//Lets make it scroll in some direction
		mtl_ffpwater.skill1 += 0.1*time_step;	//Move the water along the x axis with the given speed
		mtl_ffpwater.skill2 += 0.1*time_step; //Move the water along the y axis with the given speed
		mtl_ffpwater.matrix31 = floatd(sin(mtl_ffpwater.skill1)*100000,40000);
		mtl_ffpwater.matrix32 = floatd(cos(mtl_ffpwater.skill2)*100000,40000);
		wait(1);
	}
}

VIEW view_mirror { layer = -1; };	// render mirror view before camera view
BMAP* bmap_mirrortarget;
//panel rtt{flags = visible;}

function fx_mirror()
{
// do nothing if mirror is already running
	if (bmap_mirrortarget) { return; }

	bmap_mirrortarget = bmap_createblack(512,512,888);
	mtl_ffpwater.skin2 = bmap_mirrortarget;
	view_mirror.bmap = bmap_mirrortarget;
	view_mirror.size_x = bmap_width(bmap_mirrortarget);
	view_mirror.size_y = bmap_height(bmap_mirrortarget);
//	rtt.bmap = bmap_mirrortarget;
	
//	vec_set(view_mirror.portal_x,my.x);
//	vec_add(view_mirror.portal_x,vector(0,0,0));
//	vec_set(view_mirror.pnormal_x,vector(0,0,1.0));	// reflect upwards
//	view_mirror.portalclip = on; // clip at portal plane

	view_mirror.noshadow = on;	// suppress shadows in the mirror
//	view_mirror.nocull = on;    // view through walls
	view_mirror.noparticle = on;
//	view_mirror.noshader = on;
	
	view_mirror.visible = on;

	while (bmap_mirrortarget)
	{
		proc_late();	// camera must be moved and mtlfx_mirrorvisible set before
		
//		view_mirror.aspect = 1;//1024/768; // screen aspect, independent of render target
		view_mirror.arc    = camera.arc;
		view_mirror.fog_start = camera.fog_start;
		view_mirror.fog_end   = camera.fog_end;
		view_mirror.clip_far  = camera.clip_far;
		view_mirror.clip_near = camera.clip_near;
		view_mirror.x 	   = camera.x;
		view_mirror.y 	   = camera.y;
		view_mirror.z 	   = 2*my.z-camera.z;//view_mirror.portal_z-camera.z;	// move the camera at its mirror position
		view_mirror.pan    = camera.pan;
		view_mirror.tilt   = -camera.tilt;	// flip the vertical camera angle
		view_mirror.roll   = -camera.roll;
		
		wait(1);
	}
}

action Water
{
	mtl_ffpwater.matrix11 = float(100);	//Tilefactor of the Bumpmap in x direction
	mtl_ffpwater.matrix22 = float(100); //Tilefactor of the Bumpmap in y direction

	bmap_to_mipmap(mtl_ffpwater.skin1);
//	bmap_to_mipmap(mtl_ffpwater.skin2);

	my.material = mtl_ffpwater;
	fx_mirror();
}

Create an other file with the following code and save it as water.fx:

////////////////////////////////////////////////////////
// Environment Mapping Bump Mapping Water
//
// Version 2
//
// Eric Hendrickson-Lambert (Steempipe)
//
////////////////////////////////////////////////////////

matrix matMtl;
     
texture mtlSkin1;
texture mtlSkin2;

technique makewater2
{
	pass p0
	{
		AlphaBlendEnable=false;  
		Zenable = True;
		ZwriteEnable = True;

		BlendOp=Add;
		Lighting=True;

		Texture[0] = <mtlSkin1>;
		Texture[1] = <mtlSkin2>;
		
		magFilter[0] = linear;
		minFilter[0] = linear;
		mipFilter[0] = linear;
		
		COLOROP[0] = BumpEnvMap;
		COLORARG1[0] = Texture;
		COLORARG2[0] = Current;
		
		TexCoordIndex[0] = 1;
		TextureTransformFlags[0] = Count2;
		TextureTransform[0] = <matMtl>;
		
		
		magFilter[1] = Linear;
		minFilter[1] = Linear;
		mipFilter[1] = Linear;
		
		AddressU[1] = Clamp;
		AddressV[1] = Clamp;
		
		ColorOp[1] = Modulate;	//Modulate/AddSigned/Modulate2x/Modulate4x/Add <----------This looks all okay
		ColorArg1[1] = Texture;
		ColorArg2[1] = Current;
		
		texcoordindex[1] = cameraspaceposition | 1;    // For cubemap use: cameraspacereflectionvector
		TextureTransformFlags[1] = count3 | projected;
		TextureTransform[1] = {
				0.8, 0.0, 0.0, 0.0,//u-scale of lake rgb texture CHANGE THIS TO YOUR NEEDS!
				0.0, 1.0, 0.0, 0.0,//v-scale of lake rgb texture CHANGE THIS TO YOUR NEEDS!
				0.0, 0.0, 1.0, 0.0,
				0.0, 0.0, 0.0, 1.0
			};
	}
}

//technique fallback { pass p0 { } }

Waves.jpg:
Image:Waves.jpg


If you don`t want the realtimereflections or the water is just black use the following changes

Use this changed Shader.wdl. There won´t be any reflection now, but the effect it self should work fine:

bmap waterbump = <waves.jpg>;
bmap envspec = <sky.tga>;
function mtl_ffpwater_event();
material mtl_ffpwater
 {
   skin1=waterbump;
   skin2=envspec;
   event = mtl_ffpwater_event;
   effect = "water.fx";
}
function mtl_ffpwater_event()
{
        while(1)
        {
                //Lets make it scroll in some direction
                mtl_ffpwater.skill1 += 0.1*time_step;   //Move the water along the x axis with the given speed
                mtl_ffpwater.skill2 += 0.1*time_step; //Move the water along the y axis with the given speed
                mtl_ffpwater.matrix31 = floatd(sin(mtl_ffpwater.skill1)*100000,40000);
                mtl_ffpwater.matrix32 = floatd(cos(mtl_ffpwater.skill2)*100000,40000);
                wait(1);
        }
}
action Water
{
        mtl_ffpwater.matrix11 = float(100); //Tilefactor of the Bumpmap in x direction
        mtl_ffpwater.matrix22 = float(100); //Tilefactor of the Bumpmap in y direction
        bmap_to_mipmap(mtl_ffpwater.skin1);
        bmap_to_mipmap(mtl_ffpwater.skin2);
        my.material = mtl_ffpwater;
}

You also need this sky.jpg:
Image:sky.jpg

--Slin 04:02, 29 October 2007 (CET)

Personal tools