0.Defining the Shader
The easiest place to define a GLSL is the HTML page. By setting the type to ‘shader’ in a script tag we can easily write our GLSL code.We can create the above image effect using the following shader code.
<script id="shader" type="shader"> precision mediump float; varying vec2 vTextureCoord;//default Texture coords uniform sampler2D uSampler;//default texture uniform sampler2D offSetSampler;//values are passed from Pixi.js uniform float uniform_float;//values are passeed from Pixi.js vec4 ColorUT; void main(){ ColorUT = texture2D(offSetSampler, fract(vTextureCoord+uniform_float)); gl_FragColor = texture2D(uSampler, vTextureCoord+ColorUT.xy*0.02); } </script>
1. Defining in Pixi
var frag_shaderCode = document.getElementById("shader").innerHTML; //Pass Uniform data to Shader simpleShader=newPIXI.Filter(undefined, frag_shaderCode, UniformData); //default vertex shader will be called
We need to apply the defined shader to a Pixi container. The rendered image texture from the applied container and its texture coordinates are automatically passed by Pixi to ‘vTextureCoord’ and ‘uSampler’ variables in the shader.
2.Passing data
When we need to pass values to defined uniforms in the shader we must
define an object and define member variables using the same name which we used in the shader.
var UniformData = { uniform_float:0.0 }; ....UniformData.offSetSampler = PIXI_Loader.resources["images/waterOff.jpg"].texture;
3.Updating data from Pixi
In the game loop
//Update Shader Data UniformData.uniform_float=smoothStep;