I made a shader that creates fake refraction and caustics for underwater. It fakes the refraction by warping the texture on the model so it isn't very accurate but it looks pretty convincing still. The shader uses 2 skins, the first one is a diffuse map and the second is a height map where the higher ares are blacker and the lower ares are whiter. The whiter it is the stronger the refraction and caustic effects look. You can adjust the refraction amount and texture tiling with object skills.
The code also includes an underwater light ray effect.
You can use the water effects in your projects as long as you give me credit for it.
Heres some pictures:
http://img207.imageshack.us/my.php?image=pic2cc3.jpghttp://img61.imageshack.us/my.php?image=pic3nk1.jpghttp://img528.imageshack.us/my.php?image=pic1ak3.jpghttp://img234.imageshack.us/my.php?image=pic4oe3.jpgHeres the code for the level:
Code:
////////////////////////////////////////////////////////////////////////
// A6 main wdl:
// Created by WED.
////////////////////////////////////////////////////////////////////////
path "C:\\Program Files\\GStudio6\\template_6";
path "C:\\Program Files\\GStudio6\\template_6\\code";
path "C:\\Program Files\\GStudio6\\template_6\\images";
path "C:\\Program Files\\GStudio6\\template_6\\sounds";
path "C:\\Program Files\\GStudio6\\template_6\\models";
string level_str = <shadertest.WMB>;
include <gid01.wdl>;
include <display00.wdl>;
include <default.fx>;
include <mtlFX.wdl>;
function main()
{
freeze_mode = 1;
gid01_level_state = gid01_level_not_loaded;
mouse_mode = 0;
wait(3);
level_load(level_str);
wait(2);
gid01_level_state = gid01_level_loaded;
freeze_mode = 0;
wait(6);
camview1();
while(1)
{
MOUSE_POS.X = POINTER.X;
MOUSE_POS.Y = POINTER.Y;
if(gid01_level_state != gid01_level_loaded)
{
freeze_mode = 1;
while(gid01_level_state != gid01_level_loaded){
wait(1);
}
freeze_mode = 0;
}
wait(1);
}
}
bmap flat = <blanknormalmap.tga>;
bmap gray = <grey2.tga>;
bmap black = <black.bmp>;
bmap waterheight = <waterheight.tga>;
bmap waterlight = <water_light.tga>;
var water_level=1;
material fake_refract
{
skin2=gray;
skin3 = waterheight;
skin4 = waterlight;
effect = "fake_refract.fx";
}
action light1{
my.invisible = on;
my.passable = on;
my.red=65;
my.green=55;
my.blue=50;
my.lightrange=25000;
}
//action: fx_water_env
//title: Faked water refraction
//skill1: Tile_x 1.0
//skill2: Tile_y 1.0
//skill3: Refraction_strength 1.0
//skill4: Refraction_tiling 2.0
action refract{
var tile_tex_x=1;
var tile_tex_y=1;
var strength=1;
var refract_tile=2;
if(my.skill1==0){
my.skill1=tile_tex_x;
}
if(my.skill2==0){
my.skill2=tile_tex_y;
}
if(my.skill3==0){
my.skill3=strength;
}
if(my.skill4==0){
my.skill4=refract_tile;
}
my.skill41=float(my.skill1);
my.skill42=float(my.skill2);
my.skill43=float(my.skill3);
my.skill44=float(my.skill4);
while(1){
if(my.z<water_level+100){
my.material=fake_refract;
}
else{
my.material=null;
}
wait(1);
}
}
function camview1{
var move[3];
player=ent_create("ball.mdl",vector(168,-184,68),null);
player.passable=on;
camera.pan=160;
camera.tilt=-24;
while(1){
camera.pan -= 14 * mouse_force.x*time;
camera.tilt = clamp(camera.tilt+8 * mouse_force.y*time,-90,90);
vec_set(move,nullvector);
if(key_w || key_cuu){
move.x=15*time*(key_shift+1);
}
if(key_a || key_cul){
move.y=15*time*(key_shift+1);
}
if(key_s || key_cud){
move.x=-15*time*(key_shift+1);
}
if(key_d || key_cur){
move.y=-15*time*(key_shift+1);
}
player.pan=camera.pan;
player.tilt=camera.tilt;
player.roll=camera.roll;
c_move(player,move,nullvector,glide+ignore_passable);
vec_set(camera.pos,player.pos);
wait(1);
}
}
bmap bmp_envcube1=<envi_snow+6.tga>;
bmap rainbow=<waves2.tga>;
function mtl_env_init()
{
bmap_to_cubemap(mtl.skin1);
while(1) {
mat_set(mtl.matrix,matViewInv);
mtl.matrix41 = 0;
mtl.matrix42 = 0;
mtl.matrix43 = 0;
wait(1);
}
}
material water{
skin1=bmp_envcube1;
skin2=rainbow;
event = mtl_env_init;
flags=tangent;
effect="ocean.fx";
}
action lightray{
my.passable=on;
my.unlit=on;
my.bright=on;
my.transparent=on;
my.scale_z=25;
my.scale_y=random(2)+.5;
my.skill1=random(360);
my.skill2=random(4)+6;
while(my.skill3==0){
my.pan=camera.pan+180;
my.tilt=0;
my.roll=0;
ang_add(my.pan,vector(0,35,0));
my.skill1+=my.skill2*time;
my.alpha=12*sin(my.skill1)+2;
wait(1);
}
}
Heres the .fx file:
Code:
float4x4 matWorldViewProj;
float4x4 matWorld;
float4x4 matViewInv;
float3x3 WldToTan;
float4 vecViewPos;
float4 vecViewDir;
float4 vecLight;
float4 vecLightPos[8];
float4 vecLightColor[8];
texture entSkin1;
texture entSkin2;
texture mtlSkin3;
texture mtlSkin4;
float4 vecTime;
float4 vecSkill41;
sampler sColorMap = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sHeightMap2 = sampler_state
{
Texture = <entSkin2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sHeightMap = sampler_state
{
Texture = <mtlSkin3>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sLightMap = sampler_state
{
Texture = <mtlSkin4>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
struct VS_OUTPUT
{
float4 oPosition : POSITION;
float2 Tex : TEXCOORD0;
float3 Light1 : TEXCOORD2;
float3 View1 : TEXCOORD3;
float3 Att1 : TEXCOORD4;
float3 Light2 : TEXCOORD5;
float3 Att2 : TEXCOORD7;
float3 outnormal : TEXCOORD1;
};
VS_OUTPUT main_vs(float4 inPosition : POSITION, float2 inTex : TEXCOORD0, float3 inNormal : NORMAL, float3 inTangent : TEXCOORD2 )
{
VS_OUTPUT Out;
Out.oPosition = mul(inPosition, matWorldViewProj);
Out.Tex = inTex.xy;
float4 Pos_World = mul( inPosition, matWorld);
float LightRange1 = 1;
float LightRange2 = 1;
float3 Viewer1 = vecViewDir;
Out.View1 = -Viewer1;
//light 1
float3 Light1 = Pos_World - vecLightPos[0];
Out.Light1 = -Light1;
Out.Att1 = LightRange1;
//light 2
float3 Light2 = Pos_World - vecLightPos[1];
Out.Light2 = -Light2;
Out.Att2 = LightRange2;
Out.outnormal = normalize(mul(inNormal,matWorld));
return Out;
}
struct PS_INPUT0
{
float2 Tex : TEXCOORD0;
float3 Light1 : TEXCOORD2;
float3 View1 : TEXCOORD3;
float3 Att1 : TEXCOORD4;
float3 Light2 : TEXCOORD5;
float3 Att2 : TEXCOORD7;
};
float4 main_ps(PS_INPUT0 IN, in float3 innormal:TEXCOORD1): COLOR
{
const float HeightScale = {0.08f};
float3 ViewDir = normalize(IN.View1);
float4 color;
float3 bumpNormal;
float Height = vecSkill41.z*HeightScale * tex2D(sHeightMap, vecSkill41.w*IN.Tex+(vecTime.w/200))* tex2D(sHeightMap2, IN.Tex);
float2 OffsetTex =Height + vecSkill41*IN.Tex;
color = tex2D(sColorMap, OffsetTex);
float4 lightmap;
lightmap = tex2D(sLightMap, 0.5*vecSkill41.w*OffsetTex+(vecTime.w/300))* tex2D(sHeightMap2, IN.Tex);
lightmap *= tex2D(sLightMap, 0.5*vecSkill41.w*OffsetTex-(vecTime.w/200))* tex2D(sHeightMap2, IN.Tex);
//light1
float3 LightDir1 = normalize(IN.Light1);
float4 diff1 = 3*saturate(dot(innormal, LightDir1));
float4 Attenuation1 = saturate(dot(IN.Att1, IN.Att1));
//light2
float3 LightDir2 = normalize(IN.Light2);
float4 diff2 = 3*saturate(dot(innormal, LightDir2));
float4 Attenuation2 = saturate(dot(IN.Att2, IN.Att2));
return (
(1.6 * color*lightmap) +(0.3*color)+
(((color * diff1) * (Attenuation1))*vecLightColor[0])+
(((color * diff2) * (Attenuation2))*vecLightColor[1])
);
}
technique Refract{
pass P0{
VertexShader = compile vs_2_0 main_vs();
PixelShader = compile ps_2_0 main_ps();
}
}
EDIT: here are the pictures i used with the shader:
http://img523.imageshack.us/my.php?image=waterlightxe4.pnghttp://img523.imageshack.us/my.php?image=waterheightdf4.pngheres the whole folder with all the files in it:
http://putstuff.putfile.com/89658/8951936