how to visualize water area in plsm2? give me advice PLS.

goldenhyl1

16-07-2006 02:40:07

who know this topic and give me some advaces better for demo source code. I wish plsm2 core team could provide this function such as define a value to identify land and water area.

anyone have ideas could talk here. thanks!

HexiDave

16-07-2006 03:24:22

Well for the most part you don't attach water directly to terrain, you have a plane of water which you throw a shader on. Here's the shader I mangled out of ATI's RenderMonkey shader editor: link

I had to adjust the dimensions and scale, so it's a bit funky up close, but the waterplane is currently pretty huge and I've done no optimisations to the shader and it still runs well. Here's the code (all from RenderMonkey with adjustments to work with Ogre):

MyOcean.material
//Effect: Ocean
material MyOcean.Ocean
{
technique
{

pass
{
depth_check on
cull_hardware none
fragment_program_ref Ocean.PS
{
//Shader Constant: fadeBias
param_named fadeBias float4 0.600000 0.0 0.0 0.0
//Shader Constant: fadeExp
param_named fadeExp float4 6.080000 0.0 0.0 0.0
//Shader Constant: noiseSpeed
param_named noiseSpeed float4 0.18000 0.0 0.0 0.0
//Shader Constant: time_0_X
param_named_auto time_0_X time 0.05
//Shader Constant: waterColor
param_named waterColor float4 0.195489 0.345865 0.684210 1.000000
//Shader Constant: waveSpeed
param_named waveSpeed float4 0.3400 0.0 0.0 0.0
}

vertex_program_ref Ocean.VS
{
//Shader Constant: scale
param_named scale float4 0.01001 0.00554 0.01001 1.000000
//Shader Constant: view_position
param_named_auto view_position camera_position_object_space
//Shader Constant: view_proj_matrix
param_named_auto view_proj_matrix worldviewproj_matrix
}

texture_unit
{
texture NoiseVolume.dds 3d
tex_address_mode wrap
filtering linear linear linear
}

texture_unit
{
texture Snow.dds cubic
tex_address_mode clamp
filtering linear linear linear
}
}
}
}


MyOcean.program
fragment_program Ocean.PS hlsl
{
source OceanPS.source
target ps_2_0
entry_point main
}

vertex_program Ocean.VS hlsl
{
source OceanVS.source
target vs_1_1
entry_point main
}


OceanPS.source
float waveSpeed: register(c2);
float noiseSpeed: register(c3);
float fadeBias: register(c4);
float fadeExp: register(c5);
float time_0_X: register(c0);
float4 waterColor: register(c1);
sampler Noise: register(s0);
sampler skyBox: register(s1);
float4 main(float3 pos: TEXCOORD0, float3 normal: TEXCOORD1, float3 vVec: TEXCOORD2) : COLOR {
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)));
}


OceanVS.source
float4x4 view_proj_matrix: register(c0);
float4 scale: register(c5);
float4 view_position;
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;

// 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;
}


The link for the two texture files mentioned are here: link

To actually apply the material, just use something like this:
Entity* pPlaneEnt;

Plane oceanPlane;
oceanPlane.normal = Vector3::UNIT_Y;
oceanPlane.d = 0;
MeshManager::getSingleton().createPlane("OceanPlane",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
oceanPlane,
15000,15000,10,10,true,1,10,10,Vector3::UNIT_Z);
pPlaneEnt = mSceneMgr->createEntity( "MyPlane", "OceanPlane" );
pPlaneEnt->setMaterialName("MyOcean.Ocean");


Something like that... if you need to see how the different variables work defined in the shader, you'll need to download RenderMonkey and play with the Reflection Refraction -> Ocean shader.

I take no credit for this as it was in the RM shader examples.

Hope this helps :D
(Bonus: you can use that plane information in OgreNewt bouyancy callbacks :P )

goldenhyl1

16-07-2006 03:56:33

thank you kind help, i will do it in your direction, thanks.