phaser/src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js

488 lines
16 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2018-04-05 08:02:36 +00:00
* @author Felipe Alfonso <@bitnenfer>
2018-02-12 16:01:20 +00:00
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2018-01-26 23:17:11 +00:00
var Class = require('../../../utils/Class');
var ShaderSourceFS = require('../shaders/ForwardDiffuse-frag.js');
2018-02-09 19:19:21 +00:00
var TextureTintPipeline = require('./TextureTintPipeline');
2018-01-30 22:46:43 +00:00
var LIGHT_COUNT = 10;
2018-01-26 23:17:11 +00:00
2018-02-09 19:19:21 +00:00
/**
* @classdesc
* ForwardDiffuseLightPipeline implements a forward rendering approach for 2D lights.
* This pipeline extends TextureTintPipeline so it implements all it's rendering functions
* and batching system.
2018-02-09 19:19:21 +00:00
*
* @class ForwardDiffuseLightPipeline
2018-04-18 11:13:49 +00:00
* @extends Phaser.Renderer.WebGL.Pipelines.TextureTintPipeline
* @memberOf Phaser.Renderer.WebGL.Pipelines
2018-02-09 19:19:21 +00:00
* @constructor
* @since 3.0.0
*
* @param {object} config - [description]
2018-02-09 19:19:21 +00:00
*/
2018-01-26 23:17:11 +00:00
var ForwardDiffuseLightPipeline = new Class({
Extends: TextureTintPipeline,
initialize:
function ForwardDiffuseLightPipeline (config)
2018-01-26 23:17:11 +00:00
{
config.fragShader = ShaderSourceFS.replace('%LIGHT_COUNT%', LIGHT_COUNT.toString());
TextureTintPipeline.call(this, config);
2018-01-26 23:17:11 +00:00
},
2018-02-09 19:19:21 +00:00
/**
2018-07-11 15:24:58 +00:00
* This function binds its base class resources and this lights 2D resources.
2018-02-09 19:19:21 +00:00
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#onBind
2018-04-19 12:28:10 +00:00
* @override
2018-02-09 19:19:21 +00:00
* @since 3.0.0
2018-07-11 15:24:58 +00:00
*
* @param {Phaser.GameObjects.GameObject} [gameObject] - The Game Object that invoked this pipeline, if any.
2018-02-09 19:19:21 +00:00
*
2018-04-18 11:13:49 +00:00
* @return {Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline} [description]
2018-02-09 19:19:21 +00:00
*/
2018-07-11 15:24:58 +00:00
onBind: function (gameObject)
2018-01-26 23:17:11 +00:00
{
TextureTintPipeline.prototype.onBind.call(this);
var renderer = this.renderer;
2018-01-30 03:38:31 +00:00
var program = this.program;
2018-01-26 23:17:11 +00:00
this.mvpUpdate();
2018-01-30 03:38:31 +00:00
renderer.setInt1(program, 'uNormSampler', 1);
2018-01-26 23:17:11 +00:00
renderer.setFloat2(program, 'uResolution', this.width, this.height);
2018-07-11 15:24:58 +00:00
if (gameObject)
{
this.setNormalMap(gameObject);
}
2018-01-26 23:17:11 +00:00
return this;
},
2018-02-09 19:19:21 +00:00
/**
* This function sets all the needed resources for each camera pass.
2018-02-09 19:19:21 +00:00
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#onRender
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
*
2018-04-18 11:13:49 +00:00
* @return {Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline} [description]
2018-02-09 19:19:21 +00:00
*/
2018-01-29 21:46:48 +00:00
onRender: function (scene, camera)
{
this.active = false;
var lightManager = scene.sys.lights;
2018-01-31 01:11:51 +00:00
if (!lightManager || lightManager.lights.length <= 0 || !lightManager.active)
2018-03-12 02:22:42 +00:00
{
// Passthru
2018-03-12 02:22:42 +00:00
return this;
}
var lights = lightManager.cull(camera);
var lightCount = Math.min(lights.length, LIGHT_COUNT);
2018-01-31 01:11:51 +00:00
if (lightCount === 0)
2018-01-31 01:11:51 +00:00
{
return this;
2018-01-31 01:11:51 +00:00
}
this.active = true;
2018-01-30 03:38:31 +00:00
var renderer = this.renderer;
var program = this.program;
2018-01-30 22:46:43 +00:00
var cameraMatrix = camera.matrix;
var point = {x: 0, y: 0};
var height = renderer.height;
2018-02-16 18:44:07 +00:00
var index;
2018-01-30 22:46:43 +00:00
2018-02-16 18:44:07 +00:00
for (index = 0; index < LIGHT_COUNT; ++index)
2018-01-31 01:11:51 +00:00
{
// Reset lights
renderer.setFloat1(program, 'uLights[' + index + '].radius', 0);
2018-01-31 01:11:51 +00:00
}
2018-01-30 22:46:43 +00:00
renderer.setFloat4(program, 'uCamera', camera.x, camera.y, camera.rotation, camera.zoom);
renderer.setFloat3(program, 'uAmbientLightColor', lightManager.ambientColor.r, lightManager.ambientColor.g, lightManager.ambientColor.b);
2018-02-16 18:44:07 +00:00
for (index = 0; index < lightCount; ++index)
2018-01-30 22:46:43 +00:00
{
var light = lights[index];
var lightName = 'uLights[' + index + '].';
2018-01-30 22:46:43 +00:00
cameraMatrix.transformPoint(light.x, light.y, point);
2018-01-30 22:46:43 +00:00
renderer.setFloat2(program, lightName + 'position', point.x - (camera.scrollX * light.scrollFactorX * camera.zoom), height - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom));
renderer.setFloat3(program, lightName + 'color', light.r, light.g, light.b);
renderer.setFloat1(program, lightName + 'intensity', light.intensity);
renderer.setFloat1(program, lightName + 'radius', light.radius);
}
2018-01-31 01:11:51 +00:00
2018-01-29 21:46:48 +00:00
return this;
},
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#drawStaticTilemapLayer
2018-04-19 12:28:10 +00:00
* @override
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
2018-02-13 00:12:17 +00:00
* @param {Phaser.Tilemaps.StaticTilemapLayer} tilemap - [description]
2018-02-09 19:19:21 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*/
drawStaticTilemapLayer: function (tilemap, camera, parentTransformMatrix)
2018-01-26 23:17:11 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = tilemap.tileset.image.dataSource[0];
2018-01-26 23:17:11 +00:00
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.drawStaticTilemapLayer.call(this, tilemap, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
2018-01-26 23:17:11 +00:00
{
2018-01-30 03:38:31 +00:00
console.warn('Normal map texture missing for using Light2D pipeline. StaticTilemapLayer rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.drawStaticTilemapLayer(tilemap, camera, parentTransformMatrix);
2018-01-26 23:17:11 +00:00
}
2018-01-30 03:38:31 +00:00
},
2018-01-26 23:17:11 +00:00
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#drawEmitterManager
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
2018-02-13 00:12:17 +00:00
* @param {Phaser.GameObjects.Particles.ParticleEmitterManager} emitterManager - [description]
2018-02-09 19:19:21 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
*/
drawEmitterManager: function (emitterManager, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = emitterManager.texture.dataSource[emitterManager.frame.sourceIndex];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.drawEmitterManager.call(this, emitterManager, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. EmitterManager rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.drawEmitterManager(emitterManager, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
},
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#drawBlitter
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
* @param {Phaser.GameObjects.Blitter} blitter - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
*/
drawBlitter: function (blitter, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = blitter.texture.dataSource[blitter.frame.sourceIndex];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.drawBlitter.call(this, blitter, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. Blitter rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.drawBlitter(blitter, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
},
2018-07-11 15:24:58 +00:00
setNormalMap: function (gameObject)
{
if (!this.active || !gameObject || !gameObject.texture)
{
return;
}
var normalTexture = gameObject.texture.dataSource[gameObject.frame.sourceIndex];
if (normalTexture)
{
// Should already be set!
// this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
this.renderer.setPipeline(gameObject.defaultPipeline);
}
},
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#batchSprite
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
* @param {Phaser.GameObjects.Sprite} sprite - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
batchSprite: function (sprite, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = sprite.texture.dataSource[sprite.frame.sourceIndex];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.batchSprite.call(this, sprite, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. Sprite rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.batchSprite(sprite, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
},
2018-07-11 15:24:58 +00:00
*/
2018-01-30 03:38:31 +00:00
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#batchMesh
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
* @param {Phaser.GameObjects.Mesh} mesh - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
*/
batchMesh: function (mesh, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = mesh.texture.dataSource[mesh.frame.sourceIndex];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.batchMesh.call(this, mesh, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. Mesh rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.batchMesh(mesh, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
},
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#batchBitmapText
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
2018-02-13 00:12:17 +00:00
* @param {Phaser.GameObjects.BitmapText} bitmapText - [description]
2018-02-09 19:19:21 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
*/
batchBitmapText: function (bitmapText, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = bitmapText.texture.dataSource[bitmapText.frame.sourceIndex];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.batchBitmapText.call(this, bitmapText, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. BitmapText rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.batchBitmapText(bitmapText, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
},
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#batchDynamicBitmapText
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
2018-02-13 00:12:17 +00:00
* @param {Phaser.GameObjects.DynamicBitmapText} bitmapText - [description]
2018-02-09 19:19:21 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
*/
batchDynamicBitmapText: function (bitmapText, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
var normalTexture = bitmapText.texture.dataSource[bitmapText.frame.sourceIndex];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.batchDynamicBitmapText.call(this, bitmapText, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. DynamicBitmapText rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.batchDynamicBitmapText(bitmapText, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
},
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#batchText
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
2018-02-13 00:12:17 +00:00
* @param {Phaser.GameObjects.Text} text - [description]
2018-02-09 19:19:21 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
*/
batchText: function (text, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = text.texture.dataSource[text.frame.sourceIndex];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.batchText.call(this, text, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. Text rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.batchText(text, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
},
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#batchDynamicTilemapLayer
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
2018-02-13 00:12:17 +00:00
* @param {Phaser.Tilemaps.DynamicTilemapLayer} tilemapLayer - [description]
2018-02-09 19:19:21 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
*/
batchDynamicTilemapLayer: function (tilemapLayer, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = tilemapLayer.tileset.image.dataSource[0];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.batchDynamicTilemapLayer.call(this, tilemapLayer, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. DynamicTilemapLayer rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.batchDynamicTilemapLayer(tilemapLayer, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
},
2018-02-09 19:19:21 +00:00
/**
* [description]
*
2018-04-18 11:13:49 +00:00
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#batchTileSprite
2018-02-09 19:19:21 +00:00
* @since 3.0.0
*
* @param {Phaser.GameObjects.TileSprite} tileSprite - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - [description]
2018-02-09 19:19:21 +00:00
*
*/
batchTileSprite: function (tileSprite, camera, parentTransformMatrix)
2018-01-30 03:38:31 +00:00
{
if (!this.active)
{
return;
}
var normalTexture = tileSprite.texture.dataSource[tileSprite.frame.sourceIndex];
2018-01-30 03:38:31 +00:00
if (normalTexture)
{
this.renderer.setPipeline(this);
this.setTexture2D(normalTexture.glTexture, 1);
TextureTintPipeline.prototype.batchTileSprite.call(this, tileSprite, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
else
{
console.warn('Normal map texture missing for using Light2D pipeline. TileSprite rendered with default pipeline.');
this.renderer.pipelines.TextureTintPipeline.batchTileSprite(tileSprite, camera, parentTransformMatrix);
2018-01-30 03:38:31 +00:00
}
2018-01-26 23:17:11 +00:00
}
});
2018-01-31 01:11:51 +00:00
ForwardDiffuseLightPipeline.LIGHT_COUNT = LIGHT_COUNT;
2018-01-26 23:17:11 +00:00
module.exports = ForwardDiffuseLightPipeline;