Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*:
- * @plugindesc v0.01
- * <Iavra Lighting>
- * @author Iavra
- */
- (function($, undefined) {
- "use strict";
- /**
- * Basic helper function to extend objects. Mainly used for inheritance and other prototype-related operations.
- */
- $._extend || ($._extend = function(b, e) { for(var k in e) { b[k] = e[k]; } return b; });
- //=============================================================================
- // IAVRA.LIGHTING
- //=============================================================================
- $.LIGHTING = {
- Layer: function() { this.initialize(); },
- RectangularToPolarFilter: function() { this.initialize(); },
- PolarToRectangularFilter: function() { this.initialize(); }
- }
- //=============================================================================
- // IAVRA.LIGHTING.Layer
- //=============================================================================
- $.LIGHTING.Layer.prototype = $._extend(Object.create(PIXI.Container.prototype), {
- initialize: function() {
- PIXI.Container.call(this);
- //this.addChild(this._demo = PIXI.Sprite.fromImage('img/occlusion.png'));
- this._occlusion = PIXI.Sprite.fromImage('img/occlusion.png');
- this._test = new PIXI.Sprite(PIXI.RenderTexture.create(500, 500));
- this.addChild(this._test);
- this._test.filters = [new $.LIGHTING.RectangularToPolarFilter(), new $.LIGHTING.PolarToRectangularFilter()];
- Window.x = 0;
- },
- update: function() {
- this._occlusion.x = Window.x;
- Graphics._renderer.render(this._occlusion, this._test.texture, true);
- }
- });
- //=============================================================================
- // IAVRA.LIGHTING.RectangularToPolarFilter
- //=============================================================================
- /**
- * First render pass used to create the shadow map from a given occluder map. Converts rectangular to polar
- * coordinates and smears down the result as either transparent (shadow) or white (light).
- */
- $.LIGHTING.RectangularToPolarFilter.prototype = $._extend(Object.create(PIXI.Filter.prototype), {
- initialize: function() {
- PIXI.Filter.call(this, null, [
- '#define PI 3.1415926535897932384626433832795',
- 'precision highp float;',
- 'varying vec2 vTextureCoord;',
- 'uniform sampler2D uSampler;',
- 'uniform mat3 mappedMatrix;',
- 'uniform mat3 unmappedMatrix;',
- 'const float h = 500.0;',
- 'void main(void) {',
- ' vec3 mappedCoord = vec3(vTextureCoord, 1.0) * mappedMatrix;',
- ' for(float y = 0.0; y < h; y += 1.0) {',
- ' if(y / h > mappedCoord.y) { break; }',
- ' vec2 norm = vec2(mappedCoord.x, y / h) * 2.0 - 1.0;',
- ' float theta = PI + norm.x * PI;',
- ' float r = (1.0 + norm.y) * 0.5;',
- ' vec2 coord = vec2(-r * sin(theta), -r * cos(theta)) / 2.0 + 0.5;',
- ' vec3 unmappedCoord = vec3(coord, 1.0) * unmappedMatrix;',
- ' if(texture2D(uSampler, unmappedCoord.xy).a > 0.0) {',
- ' gl_FragColor = vec4(0.0);',
- ' return;',
- ' }',
- ' }',
- ' gl_FragColor = vec4(1.0);',
- '}'
- ].join('\n'));
- this.uniforms.mappedMatrix = new PIXI.Matrix();
- this.uniforms.unmappedMatrix = new PIXI.Matrix();
- this.padding = 0;
- },
- apply: function(filterManager, input, output) {
- filterManager.calculateNormalizedScreenSpaceMatrix(this.uniforms.mappedMatrix);
- this.uniforms.mappedMatrix.copy(this.uniforms.unmappedMatrix).invert();
- filterManager.applyFilter(this, input, output);
- }
- });
- //=============================================================================
- // IAVRA.LIGHTING.PolarToRectangularFilter
- //=============================================================================
- /**
- * Second render pass used to create the shadow map from a given occluder map. Converts polar to rectangular
- * coordinates, which unwraps the shadow map created in the first step.
- */
- $.LIGHTING.PolarToRectangularFilter.prototype = $._extend(Object.create(PIXI.Filter.prototype), {
- initialize: function() {
- PIXI.Filter.call(this, null, [
- '#define PI 3.1415926535897932384626433832795',
- 'precision highp float;',
- 'varying vec2 vTextureCoord;',
- 'uniform sampler2D uSampler;',
- 'uniform vec4 filterClamp;',
- 'uniform mat3 mappedMatrix;',
- 'uniform mat3 unmappedMatrix;',
- 'void main(void) {',
- ' vec2 norm = (vec3(vTextureCoord, 1.0) * mappedMatrix).xy * 2.0 - 1.0;',
- ' float theta = PI + atan(norm.x, norm.y);',
- ' float r = length(norm);',
- ' vec2 coord = vec2(theta / (2.0 * PI), r);',
- ' vec2 unmappedCoord = (vec3(coord, 1.0) * unmappedMatrix).xy;',
- ' gl_FragColor = texture2D(uSampler, clamp(unmappedCoord, filterClamp.xy, filterClamp.zw));',
- '}'
- ].join('\n'));
- this.uniforms.mappedMatrix = new PIXI.Matrix();
- this.uniforms.unmappedMatrix = new PIXI.Matrix();
- },
- apply: function(filterManager, input, output) {
- filterManager.calculateNormalizedScreenSpaceMatrix(this.uniforms.mappedMatrix);
- this.uniforms.mappedMatrix.copy(this.uniforms.unmappedMatrix).invert();
- filterManager.applyFilter(this, input, output);
- }
- });
- //=============================================================================
- // Spriteset_Map
- //=============================================================================
- var _spritesetMap_createUpperLayer = Spriteset_Map.prototype.createUpperLayer;
- Spriteset_Map.prototype.createUpperLayer = function() {
- this.addChild(new $.LIGHTING.Layer());
- _spritesetMap_createUpperLayer.call(this);
- };
- })(this.IAVRA || (this.IAVRA = {}));
- /*
- '#define PI 3.1415926535897932384626433832795',
- 'varying vec2 vTextureCoord;',
- 'uniform sampler2D uSampler;',
- 'uniform mat3 mappedMatrix;',
- 'uniform mat3 unmappedMatrix;',
- 'void main(void) {',
- ' vec2 norm = (vec3(vTextureCoord, 1.0) * mappedMatrix).xy * 2.0 - 1.0;',
- ' float theta = PI + norm.x * PI;',
- ' float r = (1.0 + norm.y) * 0.5;',
- ' vec2 coord = vec2(-r * sin(theta), -r * cos(theta)) / 2.0 + 0.5;',
- ' gl_FragColor = texture2D(uSampler, (vec3(coord, 1.0) * unmappedMatrix).xy);',
- '}'
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement