Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script type="fragshader" id="shader">
precision mediump float;//Define the precision

//We pass in a time variable
uniform float     time;
//The image we passed in
uniform sampler2D iChannel0;
uniform sampler2D iChannel1;//The mask
uniform vec2      resolution;

float waveSize = 5.0;
float amplitude = 0.04;

void main( void ) {

	vec2 uv = gl_FragCoord.xy / resolution.xy;
	uv.y = 1.0-uv.y;//Flip image vertically

	vec4 maskColor = texture2D(iChannel1, uv);
  float waterPower = 1.0 - (maskColor.r * maskColor.g * maskColor.b);

	float X = uv.x * waveSize+time;
  float Y = uv.y * waveSize+time;
	
  float Xoffset = cos(X-Y)*amplitude*cos(Y);
  float Yoffset = sin(X+Y)*amplitude*sin(Y);
  
 	
  uv.x += Xoffset* waterPower;
  uv.y += Yoffset * waterPower;
  gl_FragColor =  texture2D(iChannel0, uv);
  gl_FragColor.b *= (waterPower +1.0);
  
  
	
 }
</script>
              
            
!

CSS

              
                body {
margin:0px;
}
              
            
!

JS

              
                window.onload = function() {
        //This initializes Phaser
        var WINDOW_WIDTH = '100';//At 100% of the screen
        var WINDOW_HEIGHT = '100';//Remove the quotes to use pixels instead of percentage
        var game = new Phaser.Game(WINDOW_WIDTH, WINDOW_HEIGHT, Phaser.AUTO, '', { preload: preload, create: create,update:GameLoop });
        var gameImage;
        var maskImage;
        var uniforms;
        function preload () {
            //Here is where you would load all your images and assets
            game.load.crossOrigin = "Anonymous";//Load an image from a different domain
            game.load.image('screenshot', 'https://raw.githubusercontent.com/OmarShehata/Using-Displacement-Shaders-to-Create-an-Underwater-Effect/master/assets/game_screenshot.png');
            game.load.image('mask','https://raw.githubusercontent.com/OmarShehata/Using-Displacement-Shaders-to-Create-an-Underwater-Effect/master/assets/mask.png')
        }
        function create () {
            //This function runs on creation of the game window
            gameImage = game.add.sprite(game.world.centerX, game.world.centerY, 'screenshot');
            gameImage.anchor.setTo(0.5, 0.5);//This means the center of the sprite is its center, rather than its top left
            maskImage = game.add.sprite(0,0,'mask'); maskImage.visible=false;
          //Fit image on screen
            gameImage.scale.x = window.innerHeight/gameImage.height;
            gameImage.scale.y = gameImage.scale.x;
          
            
            //Get shader code
            var shaderCode = document.querySelector("#shader").innerHTML
            uniforms = {
              time: {type:'1f',value:0},
              iChannel0 :{type:'sampler2D',value:gameImage.texture},
              iChannel1 :{type:'sampler2D',value:maskImage.texture}
            }
            
            filter = new Phaser.Filter(game, uniforms, shaderCode);//Create phaser filter
            var width = window.innerHeight * (gameImage.width/gameImage.height)
            console.log(window.innerWidth,window.innerHeight)
            console.log(gameImage.width,gameImage.height)
            filter.setResolution(window.innerWidth,window.innerHeight)
            //Apply filter to our object
            gameImage.filters = [ filter ];
        }
  
        function GameLoop(){
            uniforms.time.value +=0.02;
        }
      
    };


              
            
!
999px

Console