@phaserjs/phaser
Version:
112 lines (111 loc) • 4.74 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
import { AddQuadVertex } from "../../components/vertices/AddQuadVertex";
import { BatchTexturedQuadBuffer } from "../../renderer/webgl1/draw/BatchTexturedQuadBuffer";
import { ClearDirty } from "../../components/dirty/ClearDirty";
import { ClearDirtyChildCache } from "../../components/dirty/ClearDirtyChildCache";
import { Color } from "../../components/color/Color";
import { DrawTexturedQuadFlipped } from "../../renderer/webgl1/draw/DrawTexturedQuadFlipped";
import { Flush } from "../../renderer/webgl1/renderpass/Flush";
import { GLTextureBinding } from "../../renderer/webgl1/textures/GLTextureBinding";
import { GetHeight } from "../../config/size/GetHeight";
import { GetResolution } from "../../config/size/GetResolution";
import { GetWidth } from "../../config/size/GetWidth";
import { HasDirtyChildCache } from "../../components/dirty/HasDirtyChildCache";
import { IsDirty } from "../../components/dirty/IsDirty";
import { Layer } from "../layer/Layer";
import { PopColor } from "../../renderer/webgl1/renderpass/PopColor";
import { PopFramebuffer } from "../../renderer/webgl1/renderpass/PopFramebuffer";
import { Rectangle } from "../../geom/rectangle/Rectangle";
import { SetColor } from "../../renderer/webgl1/renderpass/SetColor";
import { SetDirty } from "../../components/dirty/SetDirty";
import { SetDirtyChildCache } from "../../components/dirty/SetDirtyChildCache";
import { SetDirtyParents } from "../../components/dirty/SetDirtyParents";
import { SetFramebuffer } from "../../renderer/webgl1/renderpass/SetFramebuffer";
import { SetInversedQuadFromCamera } from "../../components/vertices/SetInversedQuadFromCamera";
import { SetQuadPosition } from "../../components/vertices/SetQuadPosition";
import { SetWillCacheChildren } from "../../components/permissions/SetWillCacheChildren";
import { SetWillRenderChildren } from "../../components/permissions/SetWillRenderChildren";
import { Texture } from "../../textures/Texture";
import { UnbindTexture } from "../../renderer/webgl1/renderpass/UnbindTexture";
export class RenderLayer extends Layer {
constructor(x = 0, y = 0, width = GetWidth(), height = GetHeight(), resolution = GetResolution()) {
super();
__publicField(this, "type", "RenderLayer");
__publicField(this, "color");
__publicField(this, "texture");
__publicField(this, "framebuffer");
__publicField(this, "viewport");
__publicField(this, "_x");
__publicField(this, "_y");
const id = this.id;
SetWillCacheChildren(id, true);
SetWillRenderChildren(id, true);
const texture = new Texture(null, width * resolution, height * resolution);
texture.key = `${this.type}${id}`;
const binding = new GLTextureBinding(texture, {
createFramebuffer: true
});
AddQuadVertex(id, width, height);
SetQuadPosition(id, 0, height, 0, 0, width, 0, width, height);
this.texture = texture;
this.framebuffer = binding.framebuffer;
this.color = new Color(id);
this.x = x;
this.y = y;
this.viewport = new Rectangle();
}
set x(value) {
this._x = value;
SetDirty(this.id);
}
get x() {
return this._x;
}
set y(value) {
this._y = value;
SetDirty(this.id);
}
get y() {
return this._y;
}
renderGL(renderPass) {
const id = this.id;
const view = this.viewport;
const texture = this.texture;
if (IsDirty(id)) {
const rendererHeight = renderPass.renderer.height;
view.set(-this.x, -(rendererHeight - texture.height - this.y), renderPass.renderer.width, rendererHeight);
SetDirtyChildCache(id);
}
SetColor(renderPass, this.color);
if (renderPass.isCameraDirty() || HasDirtyChildCache(id)) {
SetDirty(id);
if (texture.binding.isBound) {
UnbindTexture(texture);
}
Flush(renderPass);
SetFramebuffer(this.framebuffer, true, view);
}
}
postRenderGL(renderPass) {
const id = this.id;
const texture = this.texture;
if (IsDirty(id)) {
Flush(renderPass);
PopFramebuffer();
ClearDirty(id);
ClearDirtyChildCache(id);
SetDirtyParents(id);
SetInversedQuadFromCamera(id, renderPass.current2DCamera, this.x, this.y, texture.width, texture.height);
DrawTexturedQuadFlipped(renderPass, texture, this.x, this.y);
} else {
BatchTexturedQuadBuffer(this.texture, id, renderPass);
}
PopColor(renderPass, this.color);
}
}