Wavy Underwater Shader

This fullscreen shader tints your entire scene blue and uses a sine curve to bend and wave the scene around for a very nifty underwater effect.

Discussion Thread

Shader Code

/////////////////////////////////////////////
//
// TrueVision3d example for fullscreen shader.
// Used for scene shader and/or Draw_FullscreenQuadWithShader
// 
/////////////////////////////////////////////
uniform sampler2D texture1;
float time : TIME;

struct VS_INPUT
{
     float4 Position : POSITION;  
     float2 TexCoord : TEXCOORD0; 
};

  
struct VS_OUTPUT
{
    float4 Position : POSITION; 
    float2 TexCoord1 : TEXCOORD0;
};

// vertex shader
VS_OUTPUT VS_Main(VS_INPUT input)
{
    // just allocate an output here.
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.Position = input.Position;
    output.TexCoord1 = input.TexCoord;
    return output;
}

float4 PS_Main(VS_OUTPUT input) : COLOR
{
   	float wave=2;
   	float distortion=1;     
	float2 centerCoord=(0.5f,0.5f); 
   
    float2 distance = abs(input.TexCoord1 - centerCoord);
    float scalar = length(distance);
	
    float4 vColorBias=float4(0,0,2,2); 

    scalar = abs(1 - scalar);
        
    float sinoffset = sin((wave / scalar)+(time*2));
    
    float sinsign = cos(wave / scalar);    
    
    sinoffset = (sinoffset * distortion/32);
       
    float4 color = tex2D(texture1, input.TexCoord1+(sinoffset*sinsign));         
 	return lerp(color,vColorBias,.2);      
}

technique FullscreenTech
{   
	pass FullscreenPass0
   	{
       VertexShader = compile vs_1_1 VS_Main();
       PixelShader = compile ps_2_0 PS_Main();
   	}
}

Usage Example

//declare:
static TVShader testFS;
static TVRenderSurface testRS;
int testRSID;
//init
testRS = Core.Scene.CreateRenderSurfaceEx(-1, -1, CONST_TV_RENDERSURFACEFORMAT.TV_TEXTUREFORMAT_A8R8G8B8, true, true, 1);
testRSID = testRS.GetIndex();
testFS = Core.Scene.CreateShader();
testFS.CreateFromEffectFile(MEDIA_PATH+@"\underwater.fx");

//at end of render loop:
testRS.BltFromMainBuffer();
Core.Screen2d.Action_Begin2D();
Core.Screen2d.Draw_FullscreenQuadWithShader(testFS,0, 0,1,1,testRSID);
Core.Screen2d.Action_End2D();
 
sta_shader/wavy_underwater.txt · Last modified: 2013/11/22 13:31