phaser/src/gameobjects/blitter/BlitterWebGLRenderer.js

113 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2023-01-02 17:36:27 +00:00
* @copyright 2013-2023 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
var TransformMatrix = require('../components/TransformMatrix');
2018-07-02 15:44:09 +00:00
var Utils = require('../../renderer/webgl/Utils');
var tempMatrix = new TransformMatrix();
2018-02-05 22:08:48 +00:00
/**
* Renders this Game Object with the WebGL Renderer to the given Camera.
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
* This method should not be called directly. It is a utility function of the Render module.
*
* @method Phaser.GameObjects.Blitter#renderWebGL
* @since 3.0.0
* @private
*
2018-03-28 14:04:09 +00:00
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
2018-07-02 15:44:09 +00:00
* @param {Phaser.GameObjects.Blitter} src - The Game Object being rendered in this call.
2018-02-05 22:08:48 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
2018-02-05 22:08:48 +00:00
*/
var BlitterWebGLRenderer = function (renderer, src, camera, parentMatrix)
{
var list = src.getRenderList();
var alpha = camera.alpha * src.alpha;
2022-10-09 20:40:22 +00:00
if (list.length === 0 || alpha === 0)
{
// Nothing to see, so abort early
return;
}
camera.addToRenderList(src);
2021-01-07 12:31:31 +00:00
var pipeline = renderer.pipelines.set(this.pipeline, src);
2018-07-02 15:44:09 +00:00
var cameraScrollX = camera.scrollX * src.scrollFactorX;
var cameraScrollY = camera.scrollY * src.scrollFactorY;
var calcMatrix = tempMatrix.copyFrom(camera.matrix);
2018-07-02 15:44:09 +00:00
if (parentMatrix)
{
2018-07-26 23:53:00 +00:00
calcMatrix.multiplyWithOffset(parentMatrix, -cameraScrollX, -cameraScrollY);
2018-07-02 15:44:09 +00:00
cameraScrollX = 0;
cameraScrollY = 0;
}
var blitterX = src.x - cameraScrollX;
var blitterY = src.y - cameraScrollY;
var prevTextureSourceIndex = -1;
var tintEffect = false;
var roundPixels = camera.roundPixels;
2020-11-26 09:51:40 +00:00
renderer.pipelines.preBatch(src);
2022-10-09 20:40:22 +00:00
for (var i = 0; i < list.length; i++)
2018-07-02 15:44:09 +00:00
{
2022-10-09 20:40:22 +00:00
var bob = list[i];
2018-07-02 15:44:09 +00:00
var frame = bob.frame;
var bobAlpha = bob.alpha * alpha;
if (bobAlpha === 0)
{
continue;
}
var width = frame.width;
var height = frame.height;
var x = blitterX + bob.x + frame.x;
var y = blitterY + bob.y + frame.y;
if (bob.flipX)
{
width *= -1;
x += frame.width;
}
if (bob.flipY)
{
height *= -1;
y += frame.height;
}
2022-10-09 20:40:22 +00:00
var quad = calcMatrix.setQuad(x, y, x + width, y + height, roundPixels);
2018-07-02 15:44:09 +00:00
2019-10-01 02:17:14 +00:00
var tint = Utils.getTintAppendFloatAlpha(bob.tint, bobAlpha);
2018-07-02 15:44:09 +00:00
// Bind texture only if the Texture Source is different from before
if (frame.sourceIndex !== prevTextureSourceIndex)
{
var textureUnit = pipeline.setGameObject(src, frame);
2018-07-02 15:44:09 +00:00
prevTextureSourceIndex = frame.sourceIndex;
}
2022-10-09 20:40:22 +00:00
if (pipeline.batchQuad(src, quad[0], quad[1], quad[2], quad[3], quad[4], quad[5], quad[6], quad[7], frame.u0, frame.v0, frame.u1, frame.v1, tint, tint, tint, tint, tintEffect, frame.glTexture, textureUnit))
2018-07-02 15:44:09 +00:00
{
prevTextureSourceIndex = -1;
}
}
2020-11-26 09:51:40 +00:00
renderer.pipelines.postBatch(src);
};
module.exports = BlitterWebGLRenderer;