Skip to content

Instantly share code, notes, and snippets.

@floz
Last active August 29, 2015 14:20
Show Gist options
  • Save floz/52fdf6c805dd96f4553a to your computer and use it in GitHub Desktop.
Save floz/52fdf6c805dd96f4553a to your computer and use it in GitHub Desktop.
AlphaMaskFilter2.js
/**
* @author Mat Groves http://matgroves.com/ @Doormat23
*/
/**
* The AlphaMaskFilter2 class uses the pixel values from the specified texture (called the displacement map) to perform a displacement of an object.
* You can use this filter to apply all manor of crazy warping effects
* Currently the r property of the texture is used to offset the x and the g property of the texture is used to offset the y.
*
* @class AlphaMaskFilter2
* @extends AbstractFilter
* @constructor
* @param texture {Texture} The texture used for the displacement map * must be power of 2 texture at the moment
*/
PIXI.AlphaMaskFilter2 = function(texture) {
PIXI.AbstractFilter.call(this);
this.passes = [this];
texture.baseTexture._powerOf2 = true;
// set the uniforms
this.uniforms = {
mask: {
type: 'sampler2D',
value: texture
},
mapDimensions: {
type: '2f',
value: {
x: 1,
y: 5112
}
},
size: {
type: '2f',
value: {
x: 0,
y: 0
}
},
dimensions: {
type: '4fv',
value: [0, 0, 0, 0]
}
};
if (texture.baseTexture.hasLoaded) {
this.uniforms.mapDimensions.value.x = texture.width;
this.uniforms.mapDimensions.value.y = texture.height;
} else {
this.boundLoadedFunction = this.onTextureLoaded.bind(this);
texture.baseTexture.on('loaded', this.boundLoadedFunction);
}
this.fragmentSrc = ['precision mediump float;',
'varying vec2 vTextureCoord;',
'varying vec4 vColor;',
'uniform sampler2D mask;',
'uniform sampler2D uSampler;',
'uniform vec2 offset;',
'uniform vec4 dimensions;',
'uniform vec2 mapDimensions;',
'uniform vec2 size;',
'void main(void) {',
' vec2 mapCords = vTextureCoord.xy;',
// ' mapCords'
// ' mapCords += (dimensions.zw + offset)/ dimensions.xy ;',
// ' mapCords.y *= -1.0;',
// ' mapCords.y += 1.0;',
// ' mapCords *= dimensions.xy / mapDimensions;',
' mapCords *= vec2( mapDimensions.x / size.x, mapDimensions.y / size.y );',
' mapCords *= vec2( dimensions.x / mapDimensions.x, dimensions.y / mapDimensions.y );',
' vec4 original = texture2D(uSampler, vTextureCoord);',
' float maskAlpha = texture2D(mask, mapCords).r;',
' original *= maskAlpha;',
//' original.rgb *= maskAlpha;',
' gl_FragColor = original;',
// ' gl_FragColor = texture2D(mask, mapCords);',
//' gl_FragColor = gl_FragColor;',
'}'];
};
PIXI.AlphaMaskFilter2.prototype = Object.create(PIXI.AbstractFilter.prototype);
PIXI.AlphaMaskFilter2.prototype.constructor = PIXI.AlphaMaskFilter2;
/**
* Sets the map dimensions uniforms when the texture becomes available.
*
* @method onTextureLoaded
*/
PIXI.AlphaMaskFilter2.prototype.onTextureLoaded = function() {
this.uniforms.mapDimensions.value.x = this.uniforms.mask.value.width;
this.uniforms.mapDimensions.value.y = this.uniforms.mask.value.height;
this.uniforms.mask.value.baseTexture.off('loaded', this.boundLoadedFunction);
};
/**
* The texture used for the displacement map. Must be power of 2 sized texture.
*
* @property map
* @type Texture
*/
Object.defineProperty(PIXI.AlphaMaskFilter2.prototype, 'map', {
get: function() {
return this.uniforms.mask.value;
},
set: function(value) {
this.uniforms.mask.value = value;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment