@openspecui/web 0.9.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/BufferResource-CVUoegR6.js +185 -0
- package/dist/assets/CanvasRenderer-BEIcB8i1.js +1 -0
- package/dist/assets/Filter-Bu_qhr6H.js +1 -0
- package/dist/assets/RenderTargetSystem-DWouFDxU.js +172 -0
- package/dist/assets/WebGLRenderer-6FH_N1FV.js +156 -0
- package/dist/assets/WebGPURenderer-B8sJk3Sv.js +41 -0
- package/dist/assets/browserAll-CLKeV1yb.js +14 -0
- package/dist/assets/gemini-Bk-V9kKu.png +0 -0
- package/dist/assets/{index-Bp00uZNc.js → index-BE5-y0_g.js} +1 -1
- package/{dist-ssg/client/assets/index-CCfVkFzN.js → dist/assets/index-BPCTI2mG.js} +1 -1
- package/dist/assets/{index-BsTieXqQ.js → index-BRp8MJ9v.js} +1 -1
- package/dist/assets/{index-8c6bEJ99.js → index-BlZ-sasH.js} +1 -1
- package/dist/assets/{index-Bafja8o4.js → index-Bp_dnlLF.js} +1 -1
- package/{dist-ssg/client/assets/index-ftYom_wU.js → dist/assets/index-BtNuxyw4.js} +1 -1
- package/dist/assets/index-Bv7pWR8R.js +7 -0
- package/{dist-ssg/client/assets/index-D3mXuuih.js → dist/assets/index-Byr3HkRi.js} +1 -1
- package/dist/assets/index-CEHMo0EU.js +1385 -0
- package/dist/assets/{index-eA_XNQ_L.js → index-CEKSUzvw.js} +1 -1
- package/dist/assets/index-CEf9wXLh.css +1 -0
- package/{dist-ssg/client/assets/index-ArhptQw0.js → dist/assets/index-CX13iBBs.js} +1 -1
- package/dist/assets/index-CoOT7eZ9.js +1 -0
- package/{dist-ssg/client/assets/index-B1hpa--1.js → dist/assets/index-D4AU46yO.js} +1 -1
- package/dist/assets/{index-BvGAWAqS.js → index-DXRZmZm8.js} +1 -1
- package/{dist-ssg/client/assets/index-AbWe21oh.js → dist/assets/index-eQZwF8qE.js} +1 -1
- package/dist/assets/{index-gvPT4BlL.js → index-mWXhCp9j.js} +1 -1
- package/dist/assets/webworkerAll-DjWoTx9g.js +83 -0
- package/dist/index.html +2 -2
- package/dist-ssg/client/.vite/ssr-manifest.json +3094 -504
- package/dist-ssg/client/assets/BufferResource-CVUoegR6.js +185 -0
- package/dist-ssg/client/assets/CanvasRenderer-BEIcB8i1.js +1 -0
- package/dist-ssg/client/assets/Filter-Bu_qhr6H.js +1 -0
- package/dist-ssg/client/assets/RenderTargetSystem-DWouFDxU.js +172 -0
- package/dist-ssg/client/assets/WebGLRenderer-6FH_N1FV.js +156 -0
- package/dist-ssg/client/assets/WebGPURenderer-B8sJk3Sv.js +41 -0
- package/dist-ssg/client/assets/browserAll-CLKeV1yb.js +14 -0
- package/dist-ssg/client/assets/gemini-Bk-V9kKu.png +0 -0
- package/dist-ssg/client/assets/{index-Bp00uZNc.js → index-BE5-y0_g.js} +1 -1
- package/{dist/assets/index-CCfVkFzN.js → dist-ssg/client/assets/index-BPCTI2mG.js} +1 -1
- package/dist-ssg/client/assets/{index-BsTieXqQ.js → index-BRp8MJ9v.js} +1 -1
- package/dist-ssg/client/assets/{index-8c6bEJ99.js → index-BlZ-sasH.js} +1 -1
- package/dist-ssg/client/assets/{index-Bafja8o4.js → index-Bp_dnlLF.js} +1 -1
- package/{dist/assets/index-ftYom_wU.js → dist-ssg/client/assets/index-BtNuxyw4.js} +1 -1
- package/dist-ssg/client/assets/index-Bv7pWR8R.js +7 -0
- package/{dist/assets/index-D3mXuuih.js → dist-ssg/client/assets/index-Byr3HkRi.js} +1 -1
- package/dist-ssg/client/assets/index-CEHMo0EU.js +1385 -0
- package/dist-ssg/client/assets/{index-eA_XNQ_L.js → index-CEKSUzvw.js} +1 -1
- package/dist-ssg/client/assets/index-CEf9wXLh.css +1 -0
- package/{dist/assets/index-ArhptQw0.js → dist-ssg/client/assets/index-CX13iBBs.js} +1 -1
- package/dist-ssg/client/assets/index-CoOT7eZ9.js +1 -0
- package/{dist/assets/index-B1hpa--1.js → dist-ssg/client/assets/index-D4AU46yO.js} +1 -1
- package/dist-ssg/client/assets/{index-BvGAWAqS.js → index-DXRZmZm8.js} +1 -1
- package/{dist/assets/index-AbWe21oh.js → dist-ssg/client/assets/index-eQZwF8qE.js} +1 -1
- package/dist-ssg/client/assets/{index-gvPT4BlL.js → index-mWXhCp9j.js} +1 -1
- package/dist-ssg/client/assets/webworkerAll-DjWoTx9g.js +83 -0
- package/dist-ssg/client/index.html +2 -2
- package/dist-ssg/server/assets/BufferResource-BXrsGVSz.js +592 -0
- package/dist-ssg/server/assets/CanvasRenderer-D9aMd7WV.js +1530 -0
- package/dist-ssg/server/assets/Filter-ClU0-pLL.js +80 -0
- package/dist-ssg/server/assets/RenderTargetSystem-CVz6i60H.js +3037 -0
- package/dist-ssg/server/assets/WebGLRenderer-B0I5TP5d.js +3887 -0
- package/dist-ssg/server/assets/WebGPURenderer-DCgUFny7.js +2146 -0
- package/dist-ssg/server/assets/browserAll-BixK1BYs.js +2691 -0
- package/dist-ssg/server/assets/{index-BUANIFyF.js → index-3uSTc-o9.js} +3 -1
- package/dist-ssg/server/assets/{index-D0JVKGRJ.js → index-8uE7RyRi.js} +3 -1
- package/dist-ssg/server/assets/{index-DCGDP0cs.js → index-BDzDVVaf.js} +2 -0
- package/dist-ssg/server/assets/{index-oPcprgZH.js → index-BkJYfA64.js} +3 -1
- package/dist-ssg/server/assets/{index-CAP0cmVO.js → index-BvGNqnLD.js} +3 -1
- package/dist-ssg/server/assets/{index-DsfT46da.js → index-BvURgefh.js} +3 -1
- package/dist-ssg/server/assets/{index-Dk9q2o0C.js → index-C2CuXbSQ.js} +3 -1
- package/dist-ssg/server/assets/{index-Cmnd0jiw.js → index-C3RtR5EA.js} +3 -1
- package/dist-ssg/server/assets/{index-CFKaffPZ.js → index-CZtnphnE.js} +3 -1
- package/dist-ssg/server/assets/{index-D6n8WPGB.js → index-D0DRToHj.js} +3 -1
- package/dist-ssg/server/assets/{index-mJoWrrNO.js → index-D5MdLWau.js} +3 -1
- package/dist-ssg/server/assets/{index-Brcpp_nj.js → index-DBYODvy4.js} +3 -1
- package/dist-ssg/server/assets/{index-DYmgiM6_.js → index-DL23PkQi.js} +3 -1
- package/dist-ssg/server/assets/{index-CnRKREoz.js → index-DfcLdBOi.js} +3 -1
- package/dist-ssg/server/assets/{index-Bzw5T-vd.js → index-O2XMojWG.js} +3 -1
- package/dist-ssg/server/assets/init-CnkBvt-J.js +666 -0
- package/dist-ssg/server/assets/webworkerAll-DNiMFaVV.js +12 -0
- package/dist-ssg/server/entry-server.js +87120 -34846
- package/package.json +23 -5
- package/dist/assets/index-D-Urq2hl.css +0 -1
- package/dist/assets/index-DFOLYN6W.js +0 -1
- package/dist/assets/index-DpxkOmNJ.js +0 -7
- package/dist/assets/index-YZ-iXB95.js +0 -309
- package/dist-ssg/client/assets/index-D-Urq2hl.css +0 -1
- package/dist-ssg/client/assets/index-DFOLYN6W.js +0 -1
- package/dist-ssg/client/assets/index-DpxkOmNJ.js +0 -7
- package/dist-ssg/client/assets/index-YZ-iXB95.js +0 -309
|
@@ -0,0 +1,3037 @@
|
|
|
1
|
+
import { ao as TextureMatrix, U as UniformGroup, M as Matrix, G as GpuProgram, u as GlProgram, r as State, ap as DefaultBatcher, y as ExtensionType, H as extensions, aq as BigPool, ar as getGlobalBounds, F as Bounds, T as TexturePool, R as RendererType, as as FilterEffect, at as Sprite, z as Texture, K as STENCIL_MODES, ah as CLEAR, au as BatchableSprite, D as warn, av as RenderGroup, aw as multiplyColors, ax as UPDATE_VISIBLE, ay as UPDATE_COLOR, az as UPDATE_BLEND, ac as Container, aA as TextureStyle, Z as Color, X as DOMAdapter, ai as TextureSource, aB as Rectangle, aC as getLocalBounds, aa as Point, aD as color32BitToUniform, B as BindGroup, a8 as Ticker, aE as VERSION, aF as deprecation, W as uid$1, a6 as CanvasSource, aG as GlobalResourceRegistry, aH as v8_0_0, aI as RendererInitHook, aJ as SystemRunner } from "../entry-server.js";
|
|
2
|
+
import { F as Filter } from "./Filter-ClU0-pLL.js";
|
|
3
|
+
var fragment = "in vec2 vMaskCoord;\nin vec2 vTextureCoord;\n\nuniform sampler2D uTexture;\nuniform sampler2D uMaskTexture;\n\nuniform float uAlpha;\nuniform vec4 uMaskClamp;\nuniform float uInverse;\n\nout vec4 finalColor;\n\nvoid main(void)\n{\n float clip = step(3.5,\n step(uMaskClamp.x, vMaskCoord.x) +\n step(uMaskClamp.y, vMaskCoord.y) +\n step(vMaskCoord.x, uMaskClamp.z) +\n step(vMaskCoord.y, uMaskClamp.w));\n\n // TODO look into why this is needed\n float npmAlpha = uAlpha;\n vec4 original = texture(uTexture, vTextureCoord);\n vec4 masky = texture(uMaskTexture, vMaskCoord);\n float alphaMul = 1.0 - npmAlpha * (1.0 - masky.a);\n\n float a = alphaMul * masky.r * npmAlpha * clip;\n\n if (uInverse == 1.0) {\n a = 1.0 - a;\n }\n\n finalColor = original * a;\n}\n";
|
|
4
|
+
var vertex = "in vec2 aPosition;\n\nout vec2 vTextureCoord;\nout vec2 vMaskCoord;\n\n\nuniform vec4 uInputSize;\nuniform vec4 uOutputFrame;\nuniform vec4 uOutputTexture;\nuniform mat3 uFilterMatrix;\n\nvec4 filterVertexPosition( vec2 aPosition )\n{\n vec2 position = aPosition * uOutputFrame.zw + uOutputFrame.xy;\n \n position.x = position.x * (2.0 / uOutputTexture.x) - 1.0;\n position.y = position.y * (2.0*uOutputTexture.z / uOutputTexture.y) - uOutputTexture.z;\n\n return vec4(position, 0.0, 1.0);\n}\n\nvec2 filterTextureCoord( vec2 aPosition )\n{\n return aPosition * (uOutputFrame.zw * uInputSize.zw);\n}\n\nvec2 getFilterCoord( vec2 aPosition )\n{\n return ( uFilterMatrix * vec3( filterTextureCoord(aPosition), 1.0) ).xy;\n} \n\nvoid main(void)\n{\n gl_Position = filterVertexPosition(aPosition);\n vTextureCoord = filterTextureCoord(aPosition);\n vMaskCoord = getFilterCoord(aPosition);\n}\n";
|
|
5
|
+
var source = "struct GlobalFilterUniforms {\n uInputSize:vec4<f32>,\n uInputPixel:vec4<f32>,\n uInputClamp:vec4<f32>,\n uOutputFrame:vec4<f32>,\n uGlobalFrame:vec4<f32>,\n uOutputTexture:vec4<f32>,\n};\n\nstruct MaskUniforms {\n uFilterMatrix:mat3x3<f32>,\n uMaskClamp:vec4<f32>,\n uAlpha:f32,\n uInverse:f32,\n};\n\n@group(0) @binding(0) var<uniform> gfu: GlobalFilterUniforms;\n@group(0) @binding(1) var uTexture: texture_2d<f32>;\n@group(0) @binding(2) var uSampler : sampler;\n\n@group(1) @binding(0) var<uniform> filterUniforms : MaskUniforms;\n@group(1) @binding(1) var uMaskTexture: texture_2d<f32>;\n\nstruct VSOutput {\n @builtin(position) position: vec4<f32>,\n @location(0) uv : vec2<f32>,\n @location(1) filterUv : vec2<f32>,\n};\n\nfn filterVertexPosition(aPosition:vec2<f32>) -> vec4<f32>\n{\n var position = aPosition * gfu.uOutputFrame.zw + gfu.uOutputFrame.xy;\n\n position.x = position.x * (2.0 / gfu.uOutputTexture.x) - 1.0;\n position.y = position.y * (2.0*gfu.uOutputTexture.z / gfu.uOutputTexture.y) - gfu.uOutputTexture.z;\n\n return vec4(position, 0.0, 1.0);\n}\n\nfn filterTextureCoord( aPosition:vec2<f32> ) -> vec2<f32>\n{\n return aPosition * (gfu.uOutputFrame.zw * gfu.uInputSize.zw);\n}\n\nfn globalTextureCoord( aPosition:vec2<f32> ) -> vec2<f32>\n{\n return (aPosition.xy / gfu.uGlobalFrame.zw) + (gfu.uGlobalFrame.xy / gfu.uGlobalFrame.zw);\n}\n\nfn getFilterCoord(aPosition:vec2<f32> ) -> vec2<f32>\n{\n return ( filterUniforms.uFilterMatrix * vec3( filterTextureCoord(aPosition), 1.0) ).xy;\n}\n\nfn getSize() -> vec2<f32>\n{\n return gfu.uGlobalFrame.zw;\n}\n\n@vertex\nfn mainVertex(\n @location(0) aPosition : vec2<f32>,\n) -> VSOutput {\n return VSOutput(\n filterVertexPosition(aPosition),\n filterTextureCoord(aPosition),\n getFilterCoord(aPosition)\n );\n}\n\n@fragment\nfn mainFragment(\n @location(0) uv: vec2<f32>,\n @location(1) filterUv: vec2<f32>,\n @builtin(position) position: vec4<f32>\n) -> @location(0) vec4<f32> {\n\n var maskClamp = filterUniforms.uMaskClamp;\n var uAlpha = filterUniforms.uAlpha;\n\n var clip = step(3.5,\n step(maskClamp.x, filterUv.x) +\n step(maskClamp.y, filterUv.y) +\n step(filterUv.x, maskClamp.z) +\n step(filterUv.y, maskClamp.w));\n\n var mask = textureSample(uMaskTexture, uSampler, filterUv);\n var source = textureSample(uTexture, uSampler, uv);\n var alphaMul = 1.0 - uAlpha * (1.0 - mask.a);\n\n var a: f32 = alphaMul * mask.r * uAlpha * clip;\n\n if (filterUniforms.uInverse == 1.0) {\n a = 1.0 - a;\n }\n\n return source * a;\n}\n";
|
|
6
|
+
class MaskFilter extends Filter {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
const { sprite, ...rest } = options;
|
|
9
|
+
const textureMatrix = new TextureMatrix(sprite.texture);
|
|
10
|
+
const filterUniforms = new UniformGroup({
|
|
11
|
+
uFilterMatrix: { value: new Matrix(), type: "mat3x3<f32>" },
|
|
12
|
+
uMaskClamp: { value: textureMatrix.uClampFrame, type: "vec4<f32>" },
|
|
13
|
+
uAlpha: { value: 1, type: "f32" },
|
|
14
|
+
uInverse: { value: options.inverse ? 1 : 0, type: "f32" }
|
|
15
|
+
});
|
|
16
|
+
const gpuProgram = GpuProgram.from({
|
|
17
|
+
vertex: {
|
|
18
|
+
source,
|
|
19
|
+
entryPoint: "mainVertex"
|
|
20
|
+
},
|
|
21
|
+
fragment: {
|
|
22
|
+
source,
|
|
23
|
+
entryPoint: "mainFragment"
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const glProgram = GlProgram.from({
|
|
27
|
+
vertex,
|
|
28
|
+
fragment,
|
|
29
|
+
name: "mask-filter"
|
|
30
|
+
});
|
|
31
|
+
super({
|
|
32
|
+
...rest,
|
|
33
|
+
gpuProgram,
|
|
34
|
+
glProgram,
|
|
35
|
+
clipToViewport: false,
|
|
36
|
+
resources: {
|
|
37
|
+
filterUniforms,
|
|
38
|
+
uMaskTexture: sprite.texture.source
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
this.sprite = sprite;
|
|
42
|
+
this._textureMatrix = textureMatrix;
|
|
43
|
+
}
|
|
44
|
+
set inverse(value) {
|
|
45
|
+
this.resources.filterUniforms.uniforms.uInverse = value ? 1 : 0;
|
|
46
|
+
}
|
|
47
|
+
get inverse() {
|
|
48
|
+
return this.resources.filterUniforms.uniforms.uInverse === 1;
|
|
49
|
+
}
|
|
50
|
+
apply(filterManager, input, output, clearMode) {
|
|
51
|
+
this._textureMatrix.texture = this.sprite.texture;
|
|
52
|
+
filterManager.calculateSpriteMatrix(
|
|
53
|
+
this.resources.filterUniforms.uniforms.uFilterMatrix,
|
|
54
|
+
this.sprite
|
|
55
|
+
).prepend(this._textureMatrix.mapCoord);
|
|
56
|
+
this.resources.uMaskTexture = this.sprite.texture.source;
|
|
57
|
+
filterManager.applyFilter(this, input, output, clearMode);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const _BatcherPipe = class _BatcherPipe2 {
|
|
61
|
+
constructor(renderer, adaptor) {
|
|
62
|
+
this.state = State.for2d();
|
|
63
|
+
this._batchersByInstructionSet = /* @__PURE__ */ Object.create(null);
|
|
64
|
+
this._activeBatches = /* @__PURE__ */ Object.create(null);
|
|
65
|
+
this.renderer = renderer;
|
|
66
|
+
this._adaptor = adaptor;
|
|
67
|
+
this._adaptor.init?.(this);
|
|
68
|
+
}
|
|
69
|
+
static getBatcher(name) {
|
|
70
|
+
return new this._availableBatchers[name]();
|
|
71
|
+
}
|
|
72
|
+
buildStart(instructionSet) {
|
|
73
|
+
let batchers = this._batchersByInstructionSet[instructionSet.uid];
|
|
74
|
+
if (!batchers) {
|
|
75
|
+
batchers = this._batchersByInstructionSet[instructionSet.uid] = /* @__PURE__ */ Object.create(null);
|
|
76
|
+
batchers.default || (batchers.default = new DefaultBatcher({
|
|
77
|
+
maxTextures: this.renderer.limits.maxBatchableTextures
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
this._activeBatches = batchers;
|
|
81
|
+
this._activeBatch = this._activeBatches.default;
|
|
82
|
+
for (const i in this._activeBatches) {
|
|
83
|
+
this._activeBatches[i].begin();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
addToBatch(batchableObject, instructionSet) {
|
|
87
|
+
if (this._activeBatch.name !== batchableObject.batcherName) {
|
|
88
|
+
this._activeBatch.break(instructionSet);
|
|
89
|
+
let batch = this._activeBatches[batchableObject.batcherName];
|
|
90
|
+
if (!batch) {
|
|
91
|
+
batch = this._activeBatches[batchableObject.batcherName] = _BatcherPipe2.getBatcher(batchableObject.batcherName);
|
|
92
|
+
batch.begin();
|
|
93
|
+
}
|
|
94
|
+
this._activeBatch = batch;
|
|
95
|
+
}
|
|
96
|
+
this._activeBatch.add(batchableObject);
|
|
97
|
+
}
|
|
98
|
+
break(instructionSet) {
|
|
99
|
+
this._activeBatch.break(instructionSet);
|
|
100
|
+
}
|
|
101
|
+
buildEnd(instructionSet) {
|
|
102
|
+
this._activeBatch.break(instructionSet);
|
|
103
|
+
const batches = this._activeBatches;
|
|
104
|
+
for (const i in batches) {
|
|
105
|
+
const batch = batches[i];
|
|
106
|
+
const geometry = batch.geometry;
|
|
107
|
+
geometry.indexBuffer.setDataWithSize(batch.indexBuffer, batch.indexSize, true);
|
|
108
|
+
geometry.buffers[0].setDataWithSize(batch.attributeBuffer.float32View, batch.attributeSize, false);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
upload(instructionSet) {
|
|
112
|
+
const batchers = this._batchersByInstructionSet[instructionSet.uid];
|
|
113
|
+
for (const i in batchers) {
|
|
114
|
+
const batcher = batchers[i];
|
|
115
|
+
const geometry = batcher.geometry;
|
|
116
|
+
if (batcher.dirty) {
|
|
117
|
+
batcher.dirty = false;
|
|
118
|
+
geometry.buffers[0].update(batcher.attributeSize * 4);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
execute(batch) {
|
|
123
|
+
if (batch.action === "startBatch") {
|
|
124
|
+
const batcher = batch.batcher;
|
|
125
|
+
const geometry = batcher.geometry;
|
|
126
|
+
const shader = batcher.shader;
|
|
127
|
+
this._adaptor.start(this, geometry, shader);
|
|
128
|
+
}
|
|
129
|
+
this._adaptor.execute(this, batch);
|
|
130
|
+
}
|
|
131
|
+
destroy() {
|
|
132
|
+
this.state = null;
|
|
133
|
+
this.renderer = null;
|
|
134
|
+
this._adaptor = null;
|
|
135
|
+
for (const i in this._activeBatches) {
|
|
136
|
+
this._activeBatches[i].destroy();
|
|
137
|
+
}
|
|
138
|
+
this._activeBatches = null;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
_BatcherPipe.extension = {
|
|
142
|
+
type: [
|
|
143
|
+
ExtensionType.WebGLPipes,
|
|
144
|
+
ExtensionType.WebGPUPipes,
|
|
145
|
+
ExtensionType.CanvasPipes
|
|
146
|
+
],
|
|
147
|
+
name: "batch"
|
|
148
|
+
};
|
|
149
|
+
_BatcherPipe._availableBatchers = /* @__PURE__ */ Object.create(null);
|
|
150
|
+
let BatcherPipe = _BatcherPipe;
|
|
151
|
+
extensions.handleByMap(ExtensionType.Batcher, BatcherPipe._availableBatchers);
|
|
152
|
+
extensions.add(DefaultBatcher);
|
|
153
|
+
const tempBounds$1 = new Bounds();
|
|
154
|
+
class AlphaMaskEffect extends FilterEffect {
|
|
155
|
+
constructor() {
|
|
156
|
+
super();
|
|
157
|
+
this.filters = [new MaskFilter({
|
|
158
|
+
sprite: new Sprite(Texture.EMPTY),
|
|
159
|
+
inverse: false,
|
|
160
|
+
resolution: "inherit",
|
|
161
|
+
antialias: "inherit"
|
|
162
|
+
})];
|
|
163
|
+
}
|
|
164
|
+
get sprite() {
|
|
165
|
+
return this.filters[0].sprite;
|
|
166
|
+
}
|
|
167
|
+
set sprite(value) {
|
|
168
|
+
this.filters[0].sprite = value;
|
|
169
|
+
}
|
|
170
|
+
get inverse() {
|
|
171
|
+
return this.filters[0].inverse;
|
|
172
|
+
}
|
|
173
|
+
set inverse(value) {
|
|
174
|
+
this.filters[0].inverse = value;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
class AlphaMaskPipe {
|
|
178
|
+
constructor(renderer) {
|
|
179
|
+
this._activeMaskStage = [];
|
|
180
|
+
this._renderer = renderer;
|
|
181
|
+
}
|
|
182
|
+
push(mask, maskedContainer, instructionSet) {
|
|
183
|
+
const renderer = this._renderer;
|
|
184
|
+
renderer.renderPipes.batch.break(instructionSet);
|
|
185
|
+
instructionSet.add({
|
|
186
|
+
renderPipeId: "alphaMask",
|
|
187
|
+
action: "pushMaskBegin",
|
|
188
|
+
mask,
|
|
189
|
+
inverse: maskedContainer._maskOptions.inverse,
|
|
190
|
+
canBundle: false,
|
|
191
|
+
maskedContainer
|
|
192
|
+
});
|
|
193
|
+
mask.inverse = maskedContainer._maskOptions.inverse;
|
|
194
|
+
if (mask.renderMaskToTexture) {
|
|
195
|
+
const maskContainer = mask.mask;
|
|
196
|
+
maskContainer.includeInBuild = true;
|
|
197
|
+
maskContainer.collectRenderables(
|
|
198
|
+
instructionSet,
|
|
199
|
+
renderer,
|
|
200
|
+
null
|
|
201
|
+
);
|
|
202
|
+
maskContainer.includeInBuild = false;
|
|
203
|
+
}
|
|
204
|
+
renderer.renderPipes.batch.break(instructionSet);
|
|
205
|
+
instructionSet.add({
|
|
206
|
+
renderPipeId: "alphaMask",
|
|
207
|
+
action: "pushMaskEnd",
|
|
208
|
+
mask,
|
|
209
|
+
maskedContainer,
|
|
210
|
+
inverse: maskedContainer._maskOptions.inverse,
|
|
211
|
+
canBundle: false
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
pop(mask, _maskedContainer, instructionSet) {
|
|
215
|
+
const renderer = this._renderer;
|
|
216
|
+
renderer.renderPipes.batch.break(instructionSet);
|
|
217
|
+
instructionSet.add({
|
|
218
|
+
renderPipeId: "alphaMask",
|
|
219
|
+
action: "popMaskEnd",
|
|
220
|
+
mask,
|
|
221
|
+
inverse: _maskedContainer._maskOptions.inverse,
|
|
222
|
+
canBundle: false
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
execute(instruction) {
|
|
226
|
+
const renderer = this._renderer;
|
|
227
|
+
const renderMask = instruction.mask.renderMaskToTexture;
|
|
228
|
+
if (instruction.action === "pushMaskBegin") {
|
|
229
|
+
const filterEffect = BigPool.get(AlphaMaskEffect);
|
|
230
|
+
filterEffect.inverse = instruction.inverse;
|
|
231
|
+
if (renderMask) {
|
|
232
|
+
instruction.mask.mask.measurable = true;
|
|
233
|
+
const bounds = getGlobalBounds(instruction.mask.mask, true, tempBounds$1);
|
|
234
|
+
instruction.mask.mask.measurable = false;
|
|
235
|
+
bounds.ceil();
|
|
236
|
+
const colorTextureSource = renderer.renderTarget.renderTarget.colorTexture.source;
|
|
237
|
+
const filterTexture = TexturePool.getOptimalTexture(
|
|
238
|
+
bounds.width,
|
|
239
|
+
bounds.height,
|
|
240
|
+
colorTextureSource._resolution,
|
|
241
|
+
colorTextureSource.antialias
|
|
242
|
+
);
|
|
243
|
+
renderer.renderTarget.push(filterTexture, true);
|
|
244
|
+
renderer.globalUniforms.push({
|
|
245
|
+
offset: bounds,
|
|
246
|
+
worldColor: 4294967295
|
|
247
|
+
});
|
|
248
|
+
const sprite = filterEffect.sprite;
|
|
249
|
+
sprite.texture = filterTexture;
|
|
250
|
+
sprite.worldTransform.tx = bounds.minX;
|
|
251
|
+
sprite.worldTransform.ty = bounds.minY;
|
|
252
|
+
this._activeMaskStage.push({
|
|
253
|
+
filterEffect,
|
|
254
|
+
maskedContainer: instruction.maskedContainer,
|
|
255
|
+
filterTexture
|
|
256
|
+
});
|
|
257
|
+
} else {
|
|
258
|
+
filterEffect.sprite = instruction.mask.mask;
|
|
259
|
+
this._activeMaskStage.push({
|
|
260
|
+
filterEffect,
|
|
261
|
+
maskedContainer: instruction.maskedContainer
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
} else if (instruction.action === "pushMaskEnd") {
|
|
265
|
+
const maskData = this._activeMaskStage[this._activeMaskStage.length - 1];
|
|
266
|
+
if (renderMask) {
|
|
267
|
+
if (renderer.type === RendererType.WEBGL) {
|
|
268
|
+
renderer.renderTarget.finishRenderPass();
|
|
269
|
+
}
|
|
270
|
+
renderer.renderTarget.pop();
|
|
271
|
+
renderer.globalUniforms.pop();
|
|
272
|
+
}
|
|
273
|
+
renderer.filter.push({
|
|
274
|
+
renderPipeId: "filter",
|
|
275
|
+
action: "pushFilter",
|
|
276
|
+
container: maskData.maskedContainer,
|
|
277
|
+
filterEffect: maskData.filterEffect,
|
|
278
|
+
canBundle: false
|
|
279
|
+
});
|
|
280
|
+
} else if (instruction.action === "popMaskEnd") {
|
|
281
|
+
renderer.filter.pop();
|
|
282
|
+
const maskData = this._activeMaskStage.pop();
|
|
283
|
+
if (renderMask) {
|
|
284
|
+
TexturePool.returnTexture(maskData.filterTexture);
|
|
285
|
+
}
|
|
286
|
+
BigPool.return(maskData.filterEffect);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
destroy() {
|
|
290
|
+
this._renderer = null;
|
|
291
|
+
this._activeMaskStage = null;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
AlphaMaskPipe.extension = {
|
|
295
|
+
type: [
|
|
296
|
+
ExtensionType.WebGLPipes,
|
|
297
|
+
ExtensionType.WebGPUPipes,
|
|
298
|
+
ExtensionType.CanvasPipes
|
|
299
|
+
],
|
|
300
|
+
name: "alphaMask"
|
|
301
|
+
};
|
|
302
|
+
class ColorMaskPipe {
|
|
303
|
+
constructor(renderer) {
|
|
304
|
+
this._colorStack = [];
|
|
305
|
+
this._colorStackIndex = 0;
|
|
306
|
+
this._currentColor = 0;
|
|
307
|
+
this._renderer = renderer;
|
|
308
|
+
}
|
|
309
|
+
buildStart() {
|
|
310
|
+
this._colorStack[0] = 15;
|
|
311
|
+
this._colorStackIndex = 1;
|
|
312
|
+
this._currentColor = 15;
|
|
313
|
+
}
|
|
314
|
+
push(mask, _container, instructionSet) {
|
|
315
|
+
const renderer = this._renderer;
|
|
316
|
+
renderer.renderPipes.batch.break(instructionSet);
|
|
317
|
+
const colorStack = this._colorStack;
|
|
318
|
+
colorStack[this._colorStackIndex] = colorStack[this._colorStackIndex - 1] & mask.mask;
|
|
319
|
+
const currentColor = this._colorStack[this._colorStackIndex];
|
|
320
|
+
if (currentColor !== this._currentColor) {
|
|
321
|
+
this._currentColor = currentColor;
|
|
322
|
+
instructionSet.add({
|
|
323
|
+
renderPipeId: "colorMask",
|
|
324
|
+
colorMask: currentColor,
|
|
325
|
+
canBundle: false
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
this._colorStackIndex++;
|
|
329
|
+
}
|
|
330
|
+
pop(_mask, _container, instructionSet) {
|
|
331
|
+
const renderer = this._renderer;
|
|
332
|
+
renderer.renderPipes.batch.break(instructionSet);
|
|
333
|
+
const colorStack = this._colorStack;
|
|
334
|
+
this._colorStackIndex--;
|
|
335
|
+
const currentColor = colorStack[this._colorStackIndex - 1];
|
|
336
|
+
if (currentColor !== this._currentColor) {
|
|
337
|
+
this._currentColor = currentColor;
|
|
338
|
+
instructionSet.add({
|
|
339
|
+
renderPipeId: "colorMask",
|
|
340
|
+
colorMask: currentColor,
|
|
341
|
+
canBundle: false
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
execute(instruction) {
|
|
346
|
+
const renderer = this._renderer;
|
|
347
|
+
renderer.colorMask.setMask(instruction.colorMask);
|
|
348
|
+
}
|
|
349
|
+
destroy() {
|
|
350
|
+
this._renderer = null;
|
|
351
|
+
this._colorStack = null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
ColorMaskPipe.extension = {
|
|
355
|
+
type: [
|
|
356
|
+
ExtensionType.WebGLPipes,
|
|
357
|
+
ExtensionType.WebGPUPipes
|
|
358
|
+
],
|
|
359
|
+
name: "colorMask"
|
|
360
|
+
};
|
|
361
|
+
class StencilMaskPipe {
|
|
362
|
+
constructor(renderer) {
|
|
363
|
+
this._maskStackHash = {};
|
|
364
|
+
this._maskHash = /* @__PURE__ */ new WeakMap();
|
|
365
|
+
this._renderer = renderer;
|
|
366
|
+
}
|
|
367
|
+
push(mask, _container, instructionSet) {
|
|
368
|
+
var _a;
|
|
369
|
+
const effect = mask;
|
|
370
|
+
const renderer = this._renderer;
|
|
371
|
+
renderer.renderPipes.batch.break(instructionSet);
|
|
372
|
+
renderer.renderPipes.blendMode.setBlendMode(effect.mask, "none", instructionSet);
|
|
373
|
+
instructionSet.add({
|
|
374
|
+
renderPipeId: "stencilMask",
|
|
375
|
+
action: "pushMaskBegin",
|
|
376
|
+
mask,
|
|
377
|
+
inverse: _container._maskOptions.inverse,
|
|
378
|
+
canBundle: false
|
|
379
|
+
});
|
|
380
|
+
const maskContainer = effect.mask;
|
|
381
|
+
maskContainer.includeInBuild = true;
|
|
382
|
+
if (!this._maskHash.has(effect)) {
|
|
383
|
+
this._maskHash.set(effect, {
|
|
384
|
+
instructionsStart: 0,
|
|
385
|
+
instructionsLength: 0
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
const maskData = this._maskHash.get(effect);
|
|
389
|
+
maskData.instructionsStart = instructionSet.instructionSize;
|
|
390
|
+
maskContainer.collectRenderables(
|
|
391
|
+
instructionSet,
|
|
392
|
+
renderer,
|
|
393
|
+
null
|
|
394
|
+
);
|
|
395
|
+
maskContainer.includeInBuild = false;
|
|
396
|
+
renderer.renderPipes.batch.break(instructionSet);
|
|
397
|
+
instructionSet.add({
|
|
398
|
+
renderPipeId: "stencilMask",
|
|
399
|
+
action: "pushMaskEnd",
|
|
400
|
+
mask,
|
|
401
|
+
inverse: _container._maskOptions.inverse,
|
|
402
|
+
canBundle: false
|
|
403
|
+
});
|
|
404
|
+
const instructionsLength = instructionSet.instructionSize - maskData.instructionsStart - 1;
|
|
405
|
+
maskData.instructionsLength = instructionsLength;
|
|
406
|
+
const renderTargetUid = renderer.renderTarget.renderTarget.uid;
|
|
407
|
+
(_a = this._maskStackHash)[renderTargetUid] ?? (_a[renderTargetUid] = 0);
|
|
408
|
+
}
|
|
409
|
+
pop(mask, _container, instructionSet) {
|
|
410
|
+
const effect = mask;
|
|
411
|
+
const renderer = this._renderer;
|
|
412
|
+
renderer.renderPipes.batch.break(instructionSet);
|
|
413
|
+
renderer.renderPipes.blendMode.setBlendMode(effect.mask, "none", instructionSet);
|
|
414
|
+
instructionSet.add({
|
|
415
|
+
renderPipeId: "stencilMask",
|
|
416
|
+
action: "popMaskBegin",
|
|
417
|
+
inverse: _container._maskOptions.inverse,
|
|
418
|
+
canBundle: false
|
|
419
|
+
});
|
|
420
|
+
const maskData = this._maskHash.get(mask);
|
|
421
|
+
for (let i = 0; i < maskData.instructionsLength; i++) {
|
|
422
|
+
instructionSet.instructions[instructionSet.instructionSize++] = instructionSet.instructions[maskData.instructionsStart++];
|
|
423
|
+
}
|
|
424
|
+
instructionSet.add({
|
|
425
|
+
renderPipeId: "stencilMask",
|
|
426
|
+
action: "popMaskEnd",
|
|
427
|
+
canBundle: false
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
execute(instruction) {
|
|
431
|
+
var _a;
|
|
432
|
+
const renderer = this._renderer;
|
|
433
|
+
const gpuRenderer = renderer;
|
|
434
|
+
const renderTargetUid = renderer.renderTarget.renderTarget.uid;
|
|
435
|
+
let maskStackIndex = (_a = this._maskStackHash)[renderTargetUid] ?? (_a[renderTargetUid] = 0);
|
|
436
|
+
if (instruction.action === "pushMaskBegin") {
|
|
437
|
+
gpuRenderer.renderTarget.ensureDepthStencil();
|
|
438
|
+
gpuRenderer.stencil.setStencilMode(STENCIL_MODES.RENDERING_MASK_ADD, maskStackIndex);
|
|
439
|
+
maskStackIndex++;
|
|
440
|
+
gpuRenderer.colorMask.setMask(0);
|
|
441
|
+
} else if (instruction.action === "pushMaskEnd") {
|
|
442
|
+
if (instruction.inverse) {
|
|
443
|
+
gpuRenderer.stencil.setStencilMode(STENCIL_MODES.INVERSE_MASK_ACTIVE, maskStackIndex);
|
|
444
|
+
} else {
|
|
445
|
+
gpuRenderer.stencil.setStencilMode(STENCIL_MODES.MASK_ACTIVE, maskStackIndex);
|
|
446
|
+
}
|
|
447
|
+
gpuRenderer.colorMask.setMask(15);
|
|
448
|
+
} else if (instruction.action === "popMaskBegin") {
|
|
449
|
+
gpuRenderer.colorMask.setMask(0);
|
|
450
|
+
if (maskStackIndex !== 0) {
|
|
451
|
+
gpuRenderer.stencil.setStencilMode(STENCIL_MODES.RENDERING_MASK_REMOVE, maskStackIndex);
|
|
452
|
+
} else {
|
|
453
|
+
gpuRenderer.renderTarget.clear(null, CLEAR.STENCIL);
|
|
454
|
+
gpuRenderer.stencil.setStencilMode(STENCIL_MODES.DISABLED, maskStackIndex);
|
|
455
|
+
}
|
|
456
|
+
maskStackIndex--;
|
|
457
|
+
} else if (instruction.action === "popMaskEnd") {
|
|
458
|
+
if (instruction.inverse) {
|
|
459
|
+
gpuRenderer.stencil.setStencilMode(STENCIL_MODES.INVERSE_MASK_ACTIVE, maskStackIndex);
|
|
460
|
+
} else {
|
|
461
|
+
gpuRenderer.stencil.setStencilMode(STENCIL_MODES.MASK_ACTIVE, maskStackIndex);
|
|
462
|
+
}
|
|
463
|
+
gpuRenderer.colorMask.setMask(15);
|
|
464
|
+
}
|
|
465
|
+
this._maskStackHash[renderTargetUid] = maskStackIndex;
|
|
466
|
+
}
|
|
467
|
+
destroy() {
|
|
468
|
+
this._renderer = null;
|
|
469
|
+
this._maskStackHash = null;
|
|
470
|
+
this._maskHash = null;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
StencilMaskPipe.extension = {
|
|
474
|
+
type: [
|
|
475
|
+
ExtensionType.WebGLPipes,
|
|
476
|
+
ExtensionType.WebGPUPipes
|
|
477
|
+
],
|
|
478
|
+
name: "stencilMask"
|
|
479
|
+
};
|
|
480
|
+
class CustomRenderPipe {
|
|
481
|
+
constructor(renderer) {
|
|
482
|
+
this._renderer = renderer;
|
|
483
|
+
}
|
|
484
|
+
updateRenderable() {
|
|
485
|
+
}
|
|
486
|
+
destroyRenderable() {
|
|
487
|
+
}
|
|
488
|
+
validateRenderable() {
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
addRenderable(container, instructionSet) {
|
|
492
|
+
this._renderer.renderPipes.batch.break(instructionSet);
|
|
493
|
+
instructionSet.add(container);
|
|
494
|
+
}
|
|
495
|
+
execute(container) {
|
|
496
|
+
if (!container.isRenderable) return;
|
|
497
|
+
container.render(this._renderer);
|
|
498
|
+
}
|
|
499
|
+
destroy() {
|
|
500
|
+
this._renderer = null;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
CustomRenderPipe.extension = {
|
|
504
|
+
type: [
|
|
505
|
+
ExtensionType.WebGLPipes,
|
|
506
|
+
ExtensionType.WebGPUPipes,
|
|
507
|
+
ExtensionType.CanvasPipes
|
|
508
|
+
],
|
|
509
|
+
name: "customRender"
|
|
510
|
+
};
|
|
511
|
+
function executeInstructions(renderGroup, renderer) {
|
|
512
|
+
const instructionSet = renderGroup.instructionSet;
|
|
513
|
+
const instructions = instructionSet.instructions;
|
|
514
|
+
for (let i = 0; i < instructionSet.instructionSize; i++) {
|
|
515
|
+
const instruction = instructions[i];
|
|
516
|
+
renderer[instruction.renderPipeId].execute(instruction);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
class RenderGroupPipe {
|
|
520
|
+
constructor(renderer) {
|
|
521
|
+
this._renderer = renderer;
|
|
522
|
+
}
|
|
523
|
+
addRenderGroup(renderGroup, instructionSet) {
|
|
524
|
+
if (renderGroup.isCachedAsTexture) {
|
|
525
|
+
this._addRenderableCacheAsTexture(renderGroup, instructionSet);
|
|
526
|
+
} else {
|
|
527
|
+
this._addRenderableDirect(renderGroup, instructionSet);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
execute(renderGroup) {
|
|
531
|
+
if (!renderGroup.isRenderable) return;
|
|
532
|
+
if (renderGroup.isCachedAsTexture) {
|
|
533
|
+
this._executeCacheAsTexture(renderGroup);
|
|
534
|
+
} else {
|
|
535
|
+
this._executeDirect(renderGroup);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
destroy() {
|
|
539
|
+
this._renderer = null;
|
|
540
|
+
}
|
|
541
|
+
_addRenderableDirect(renderGroup, instructionSet) {
|
|
542
|
+
this._renderer.renderPipes.batch.break(instructionSet);
|
|
543
|
+
if (renderGroup._batchableRenderGroup) {
|
|
544
|
+
BigPool.return(renderGroup._batchableRenderGroup);
|
|
545
|
+
renderGroup._batchableRenderGroup = null;
|
|
546
|
+
}
|
|
547
|
+
instructionSet.add(renderGroup);
|
|
548
|
+
}
|
|
549
|
+
_addRenderableCacheAsTexture(renderGroup, instructionSet) {
|
|
550
|
+
const batchableRenderGroup = renderGroup._batchableRenderGroup ?? (renderGroup._batchableRenderGroup = BigPool.get(BatchableSprite));
|
|
551
|
+
batchableRenderGroup.renderable = renderGroup.root;
|
|
552
|
+
batchableRenderGroup.transform = renderGroup.root.relativeGroupTransform;
|
|
553
|
+
batchableRenderGroup.texture = renderGroup.texture;
|
|
554
|
+
batchableRenderGroup.bounds = renderGroup._textureBounds;
|
|
555
|
+
instructionSet.add(renderGroup);
|
|
556
|
+
this._renderer.renderPipes.blendMode.pushBlendMode(renderGroup, renderGroup.root.groupBlendMode, instructionSet);
|
|
557
|
+
this._renderer.renderPipes.batch.addToBatch(batchableRenderGroup, instructionSet);
|
|
558
|
+
this._renderer.renderPipes.blendMode.popBlendMode(instructionSet);
|
|
559
|
+
}
|
|
560
|
+
_executeCacheAsTexture(renderGroup) {
|
|
561
|
+
if (renderGroup.textureNeedsUpdate) {
|
|
562
|
+
renderGroup.textureNeedsUpdate = false;
|
|
563
|
+
const worldTransformMatrix = new Matrix().translate(
|
|
564
|
+
-renderGroup._textureBounds.x,
|
|
565
|
+
-renderGroup._textureBounds.y
|
|
566
|
+
);
|
|
567
|
+
this._renderer.renderTarget.push(renderGroup.texture, true, null, renderGroup.texture.frame);
|
|
568
|
+
this._renderer.globalUniforms.push({
|
|
569
|
+
worldTransformMatrix,
|
|
570
|
+
worldColor: 4294967295,
|
|
571
|
+
offset: { x: 0, y: 0 }
|
|
572
|
+
});
|
|
573
|
+
executeInstructions(renderGroup, this._renderer.renderPipes);
|
|
574
|
+
this._renderer.renderTarget.finishRenderPass();
|
|
575
|
+
this._renderer.renderTarget.pop();
|
|
576
|
+
this._renderer.globalUniforms.pop();
|
|
577
|
+
}
|
|
578
|
+
renderGroup._batchableRenderGroup._batcher.updateElement(renderGroup._batchableRenderGroup);
|
|
579
|
+
renderGroup._batchableRenderGroup._batcher.geometry.buffers[0].update();
|
|
580
|
+
}
|
|
581
|
+
_executeDirect(renderGroup) {
|
|
582
|
+
this._renderer.globalUniforms.push({
|
|
583
|
+
worldTransformMatrix: renderGroup.inverseParentTextureTransform,
|
|
584
|
+
worldColor: renderGroup.worldColorAlpha
|
|
585
|
+
});
|
|
586
|
+
executeInstructions(renderGroup, this._renderer.renderPipes);
|
|
587
|
+
this._renderer.globalUniforms.pop();
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
RenderGroupPipe.extension = {
|
|
591
|
+
type: [
|
|
592
|
+
ExtensionType.WebGLPipes,
|
|
593
|
+
ExtensionType.WebGPUPipes,
|
|
594
|
+
ExtensionType.CanvasPipes
|
|
595
|
+
],
|
|
596
|
+
name: "renderGroup"
|
|
597
|
+
};
|
|
598
|
+
class SpritePipe {
|
|
599
|
+
constructor(renderer) {
|
|
600
|
+
this._renderer = renderer;
|
|
601
|
+
}
|
|
602
|
+
addRenderable(sprite, instructionSet) {
|
|
603
|
+
const gpuSprite = this._getGpuSprite(sprite);
|
|
604
|
+
if (sprite.didViewUpdate) this._updateBatchableSprite(sprite, gpuSprite);
|
|
605
|
+
this._renderer.renderPipes.batch.addToBatch(gpuSprite, instructionSet);
|
|
606
|
+
}
|
|
607
|
+
updateRenderable(sprite) {
|
|
608
|
+
const gpuSprite = this._getGpuSprite(sprite);
|
|
609
|
+
if (sprite.didViewUpdate) this._updateBatchableSprite(sprite, gpuSprite);
|
|
610
|
+
gpuSprite._batcher.updateElement(gpuSprite);
|
|
611
|
+
}
|
|
612
|
+
validateRenderable(sprite) {
|
|
613
|
+
const gpuSprite = this._getGpuSprite(sprite);
|
|
614
|
+
return !gpuSprite._batcher.checkAndUpdateTexture(
|
|
615
|
+
gpuSprite,
|
|
616
|
+
sprite._texture
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
_updateBatchableSprite(sprite, batchableSprite) {
|
|
620
|
+
batchableSprite.bounds = sprite.visualBounds;
|
|
621
|
+
batchableSprite.texture = sprite._texture;
|
|
622
|
+
}
|
|
623
|
+
_getGpuSprite(sprite) {
|
|
624
|
+
return sprite._gpuData[this._renderer.uid] || this._initGPUSprite(sprite);
|
|
625
|
+
}
|
|
626
|
+
_initGPUSprite(sprite) {
|
|
627
|
+
const batchableSprite = new BatchableSprite();
|
|
628
|
+
batchableSprite.renderable = sprite;
|
|
629
|
+
batchableSprite.transform = sprite.groupTransform;
|
|
630
|
+
batchableSprite.texture = sprite._texture;
|
|
631
|
+
batchableSprite.bounds = sprite.visualBounds;
|
|
632
|
+
batchableSprite.roundPixels = this._renderer._roundPixels | sprite._roundPixels;
|
|
633
|
+
sprite._gpuData[this._renderer.uid] = batchableSprite;
|
|
634
|
+
return batchableSprite;
|
|
635
|
+
}
|
|
636
|
+
destroy() {
|
|
637
|
+
this._renderer = null;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
SpritePipe.extension = {
|
|
641
|
+
type: [
|
|
642
|
+
ExtensionType.WebGLPipes,
|
|
643
|
+
ExtensionType.WebGPUPipes,
|
|
644
|
+
ExtensionType.CanvasPipes
|
|
645
|
+
],
|
|
646
|
+
name: "sprite"
|
|
647
|
+
};
|
|
648
|
+
const BLEND_MODE_FILTERS = {};
|
|
649
|
+
extensions.handle(ExtensionType.BlendMode, (value) => {
|
|
650
|
+
if (!value.name) {
|
|
651
|
+
throw new Error("BlendMode extension must have a name property");
|
|
652
|
+
}
|
|
653
|
+
BLEND_MODE_FILTERS[value.name] = value.ref;
|
|
654
|
+
}, (value) => {
|
|
655
|
+
delete BLEND_MODE_FILTERS[value.name];
|
|
656
|
+
});
|
|
657
|
+
class BlendModePipe {
|
|
658
|
+
constructor(renderer) {
|
|
659
|
+
this._blendModeStack = [];
|
|
660
|
+
this._isAdvanced = false;
|
|
661
|
+
this._filterHash = /* @__PURE__ */ Object.create(null);
|
|
662
|
+
this._renderer = renderer;
|
|
663
|
+
this._renderer.runners.prerender.add(this);
|
|
664
|
+
}
|
|
665
|
+
prerender() {
|
|
666
|
+
this._activeBlendMode = "normal";
|
|
667
|
+
this._isAdvanced = false;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Push a blend mode onto the internal stack and apply it to the instruction set if needed.
|
|
671
|
+
* @param renderable - The renderable or {@link RenderGroup} associated with the change.
|
|
672
|
+
* @param blendMode - The blend mode to activate.
|
|
673
|
+
* @param instructionSet - The instruction set being built.
|
|
674
|
+
*/
|
|
675
|
+
pushBlendMode(renderable, blendMode, instructionSet) {
|
|
676
|
+
this._blendModeStack.push(blendMode);
|
|
677
|
+
this.setBlendMode(renderable, blendMode, instructionSet);
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Pop the last blend mode from the stack and apply the new top-of-stack mode.
|
|
681
|
+
* @param instructionSet - The instruction set being built.
|
|
682
|
+
*/
|
|
683
|
+
popBlendMode(instructionSet) {
|
|
684
|
+
this._blendModeStack.pop();
|
|
685
|
+
const blendMode = this._blendModeStack[this._activeBlendMode.length - 1] ?? "normal";
|
|
686
|
+
this.setBlendMode(null, blendMode, instructionSet);
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Ensure a blend mode switch is added to the instruction set when the mode changes.
|
|
690
|
+
* If an advanced blend mode is active, subsequent renderables will be collected so they can be
|
|
691
|
+
* rendered within a single filter pass.
|
|
692
|
+
* @param renderable - The renderable or {@link RenderGroup} to associate with the change, or null when unwinding.
|
|
693
|
+
* @param blendMode - The target blend mode.
|
|
694
|
+
* @param instructionSet - The instruction set being built.
|
|
695
|
+
*/
|
|
696
|
+
setBlendMode(renderable, blendMode, instructionSet) {
|
|
697
|
+
const isRenderGroup = renderable instanceof RenderGroup;
|
|
698
|
+
if (this._activeBlendMode === blendMode) {
|
|
699
|
+
if (this._isAdvanced && renderable && !isRenderGroup) {
|
|
700
|
+
this._renderableList?.push(renderable);
|
|
701
|
+
}
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
if (this._isAdvanced) this._endAdvancedBlendMode(instructionSet);
|
|
705
|
+
this._activeBlendMode = blendMode;
|
|
706
|
+
if (!renderable) return;
|
|
707
|
+
this._isAdvanced = !!BLEND_MODE_FILTERS[blendMode];
|
|
708
|
+
if (this._isAdvanced) this._beginAdvancedBlendMode(renderable, instructionSet);
|
|
709
|
+
}
|
|
710
|
+
_beginAdvancedBlendMode(renderable, instructionSet) {
|
|
711
|
+
this._renderer.renderPipes.batch.break(instructionSet);
|
|
712
|
+
const blendMode = this._activeBlendMode;
|
|
713
|
+
if (!BLEND_MODE_FILTERS[blendMode]) {
|
|
714
|
+
warn(`Unable to assign BlendMode: '${blendMode}'. You may want to include: import 'pixi.js/advanced-blend-modes'`);
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
const filterEffect = this._ensureFilterEffect(blendMode);
|
|
718
|
+
const isRenderGroup = renderable instanceof RenderGroup;
|
|
719
|
+
const instruction = {
|
|
720
|
+
renderPipeId: "filter",
|
|
721
|
+
action: "pushFilter",
|
|
722
|
+
filterEffect,
|
|
723
|
+
renderables: isRenderGroup ? null : [renderable],
|
|
724
|
+
container: isRenderGroup ? renderable.root : null,
|
|
725
|
+
canBundle: false
|
|
726
|
+
};
|
|
727
|
+
this._renderableList = instruction.renderables;
|
|
728
|
+
instructionSet.add(instruction);
|
|
729
|
+
}
|
|
730
|
+
_ensureFilterEffect(blendMode) {
|
|
731
|
+
let filterEffect = this._filterHash[blendMode];
|
|
732
|
+
if (!filterEffect) {
|
|
733
|
+
filterEffect = this._filterHash[blendMode] = new FilterEffect();
|
|
734
|
+
filterEffect.filters = [new BLEND_MODE_FILTERS[blendMode]()];
|
|
735
|
+
}
|
|
736
|
+
return filterEffect;
|
|
737
|
+
}
|
|
738
|
+
_endAdvancedBlendMode(instructionSet) {
|
|
739
|
+
this._isAdvanced = false;
|
|
740
|
+
this._renderableList = null;
|
|
741
|
+
this._renderer.renderPipes.batch.break(instructionSet);
|
|
742
|
+
instructionSet.add({
|
|
743
|
+
renderPipeId: "filter",
|
|
744
|
+
action: "popFilter",
|
|
745
|
+
canBundle: false
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* called when the instruction build process is starting this will reset internally to the default blend mode
|
|
750
|
+
* @internal
|
|
751
|
+
*/
|
|
752
|
+
buildStart() {
|
|
753
|
+
this._isAdvanced = false;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* called when the instruction build process is finished, ensuring that if there is an advanced blend mode
|
|
757
|
+
* active, we add the final render instructions added to the instruction set
|
|
758
|
+
* @param instructionSet - The instruction set we are adding to
|
|
759
|
+
* @internal
|
|
760
|
+
*/
|
|
761
|
+
buildEnd(instructionSet) {
|
|
762
|
+
if (!this._isAdvanced) return;
|
|
763
|
+
this._endAdvancedBlendMode(instructionSet);
|
|
764
|
+
}
|
|
765
|
+
/** @internal */
|
|
766
|
+
destroy() {
|
|
767
|
+
this._renderer = null;
|
|
768
|
+
this._renderableList = null;
|
|
769
|
+
for (const i in this._filterHash) {
|
|
770
|
+
this._filterHash[i].destroy();
|
|
771
|
+
}
|
|
772
|
+
this._filterHash = null;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
BlendModePipe.extension = {
|
|
776
|
+
type: [
|
|
777
|
+
ExtensionType.WebGLPipes,
|
|
778
|
+
ExtensionType.WebGPUPipes,
|
|
779
|
+
ExtensionType.CanvasPipes
|
|
780
|
+
],
|
|
781
|
+
name: "blendMode"
|
|
782
|
+
};
|
|
783
|
+
function clearList(list, index) {
|
|
784
|
+
index || (index = 0);
|
|
785
|
+
for (let j = index; j < list.length; j++) {
|
|
786
|
+
if (list[j]) {
|
|
787
|
+
list[j] = null;
|
|
788
|
+
} else {
|
|
789
|
+
break;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
const tempContainer = new Container();
|
|
794
|
+
const UPDATE_BLEND_COLOR_VISIBLE = UPDATE_VISIBLE | UPDATE_COLOR | UPDATE_BLEND;
|
|
795
|
+
function updateRenderGroupTransforms(renderGroup, updateChildRenderGroups = false) {
|
|
796
|
+
updateRenderGroupTransform(renderGroup);
|
|
797
|
+
const childrenToUpdate = renderGroup.childrenToUpdate;
|
|
798
|
+
const updateTick = renderGroup.updateTick++;
|
|
799
|
+
for (const j in childrenToUpdate) {
|
|
800
|
+
const renderGroupDepth = Number(j);
|
|
801
|
+
const childrenAtDepth = childrenToUpdate[j];
|
|
802
|
+
const list = childrenAtDepth.list;
|
|
803
|
+
const index = childrenAtDepth.index;
|
|
804
|
+
for (let i = 0; i < index; i++) {
|
|
805
|
+
const child = list[i];
|
|
806
|
+
if (child.parentRenderGroup === renderGroup && child.relativeRenderGroupDepth === renderGroupDepth) {
|
|
807
|
+
updateTransformAndChildren(child, updateTick, 0);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
clearList(list, index);
|
|
811
|
+
childrenAtDepth.index = 0;
|
|
812
|
+
}
|
|
813
|
+
if (updateChildRenderGroups) {
|
|
814
|
+
for (let i = 0; i < renderGroup.renderGroupChildren.length; i++) {
|
|
815
|
+
updateRenderGroupTransforms(renderGroup.renderGroupChildren[i], updateChildRenderGroups);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
function updateRenderGroupTransform(renderGroup) {
|
|
820
|
+
const root = renderGroup.root;
|
|
821
|
+
let worldAlpha;
|
|
822
|
+
if (renderGroup.renderGroupParent) {
|
|
823
|
+
const renderGroupParent = renderGroup.renderGroupParent;
|
|
824
|
+
renderGroup.worldTransform.appendFrom(
|
|
825
|
+
root.relativeGroupTransform,
|
|
826
|
+
renderGroupParent.worldTransform
|
|
827
|
+
);
|
|
828
|
+
renderGroup.worldColor = multiplyColors(
|
|
829
|
+
root.groupColor,
|
|
830
|
+
renderGroupParent.worldColor
|
|
831
|
+
);
|
|
832
|
+
worldAlpha = root.groupAlpha * renderGroupParent.worldAlpha;
|
|
833
|
+
} else {
|
|
834
|
+
renderGroup.worldTransform.copyFrom(root.localTransform);
|
|
835
|
+
renderGroup.worldColor = root.localColor;
|
|
836
|
+
worldAlpha = root.localAlpha;
|
|
837
|
+
}
|
|
838
|
+
worldAlpha = worldAlpha < 0 ? 0 : worldAlpha > 1 ? 1 : worldAlpha;
|
|
839
|
+
renderGroup.worldAlpha = worldAlpha;
|
|
840
|
+
renderGroup.worldColorAlpha = renderGroup.worldColor + ((worldAlpha * 255 | 0) << 24);
|
|
841
|
+
}
|
|
842
|
+
function updateTransformAndChildren(container, updateTick, updateFlags) {
|
|
843
|
+
if (updateTick === container.updateTick) return;
|
|
844
|
+
container.updateTick = updateTick;
|
|
845
|
+
container.didChange = false;
|
|
846
|
+
const localTransform = container.localTransform;
|
|
847
|
+
container.updateLocalTransform();
|
|
848
|
+
const parent = container.parent;
|
|
849
|
+
if (parent && !parent.renderGroup) {
|
|
850
|
+
updateFlags |= container._updateFlags;
|
|
851
|
+
container.relativeGroupTransform.appendFrom(
|
|
852
|
+
localTransform,
|
|
853
|
+
parent.relativeGroupTransform
|
|
854
|
+
);
|
|
855
|
+
if (updateFlags & UPDATE_BLEND_COLOR_VISIBLE) {
|
|
856
|
+
updateColorBlendVisibility(container, parent, updateFlags);
|
|
857
|
+
}
|
|
858
|
+
} else {
|
|
859
|
+
updateFlags = container._updateFlags;
|
|
860
|
+
container.relativeGroupTransform.copyFrom(localTransform);
|
|
861
|
+
if (updateFlags & UPDATE_BLEND_COLOR_VISIBLE) {
|
|
862
|
+
updateColorBlendVisibility(container, tempContainer, updateFlags);
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
if (!container.renderGroup) {
|
|
866
|
+
const children = container.children;
|
|
867
|
+
const length = children.length;
|
|
868
|
+
for (let i = 0; i < length; i++) {
|
|
869
|
+
updateTransformAndChildren(children[i], updateTick, updateFlags);
|
|
870
|
+
}
|
|
871
|
+
const renderGroup = container.parentRenderGroup;
|
|
872
|
+
const renderable = container;
|
|
873
|
+
if (renderable.renderPipeId && !renderGroup.structureDidChange) {
|
|
874
|
+
renderGroup.updateRenderable(renderable);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
function updateColorBlendVisibility(container, parent, updateFlags) {
|
|
879
|
+
if (updateFlags & UPDATE_COLOR) {
|
|
880
|
+
container.groupColor = multiplyColors(
|
|
881
|
+
container.localColor,
|
|
882
|
+
parent.groupColor
|
|
883
|
+
);
|
|
884
|
+
let groupAlpha = container.localAlpha * parent.groupAlpha;
|
|
885
|
+
groupAlpha = groupAlpha < 0 ? 0 : groupAlpha > 1 ? 1 : groupAlpha;
|
|
886
|
+
container.groupAlpha = groupAlpha;
|
|
887
|
+
container.groupColorAlpha = container.groupColor + ((groupAlpha * 255 | 0) << 24);
|
|
888
|
+
}
|
|
889
|
+
if (updateFlags & UPDATE_BLEND) {
|
|
890
|
+
container.groupBlendMode = container.localBlendMode === "inherit" ? parent.groupBlendMode : container.localBlendMode;
|
|
891
|
+
}
|
|
892
|
+
if (updateFlags & UPDATE_VISIBLE) {
|
|
893
|
+
container.globalDisplayStatus = container.localDisplayStatus & parent.globalDisplayStatus;
|
|
894
|
+
}
|
|
895
|
+
container._updateFlags = 0;
|
|
896
|
+
}
|
|
897
|
+
function validateRenderables(renderGroup, renderPipes) {
|
|
898
|
+
const { list } = renderGroup.childrenRenderablesToUpdate;
|
|
899
|
+
let rebuildRequired = false;
|
|
900
|
+
for (let i = 0; i < renderGroup.childrenRenderablesToUpdate.index; i++) {
|
|
901
|
+
const container = list[i];
|
|
902
|
+
const renderable = container;
|
|
903
|
+
const pipe = renderPipes[renderable.renderPipeId];
|
|
904
|
+
rebuildRequired = pipe.validateRenderable(container);
|
|
905
|
+
if (rebuildRequired) {
|
|
906
|
+
break;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
renderGroup.structureDidChange = rebuildRequired;
|
|
910
|
+
return rebuildRequired;
|
|
911
|
+
}
|
|
912
|
+
const tempMatrix = new Matrix();
|
|
913
|
+
class RenderGroupSystem {
|
|
914
|
+
constructor(renderer) {
|
|
915
|
+
this._renderer = renderer;
|
|
916
|
+
}
|
|
917
|
+
render({ container, transform }) {
|
|
918
|
+
const parent = container.parent;
|
|
919
|
+
const renderGroupParent = container.renderGroup.renderGroupParent;
|
|
920
|
+
container.parent = null;
|
|
921
|
+
container.renderGroup.renderGroupParent = null;
|
|
922
|
+
const renderer = this._renderer;
|
|
923
|
+
const originalLocalTransform = tempMatrix;
|
|
924
|
+
if (transform) {
|
|
925
|
+
originalLocalTransform.copyFrom(container.renderGroup.localTransform);
|
|
926
|
+
container.renderGroup.localTransform.copyFrom(transform);
|
|
927
|
+
}
|
|
928
|
+
const renderPipes = renderer.renderPipes;
|
|
929
|
+
this._updateCachedRenderGroups(container.renderGroup, null);
|
|
930
|
+
this._updateRenderGroups(container.renderGroup);
|
|
931
|
+
renderer.globalUniforms.start({
|
|
932
|
+
worldTransformMatrix: transform ? container.renderGroup.localTransform : container.renderGroup.worldTransform,
|
|
933
|
+
worldColor: container.renderGroup.worldColorAlpha
|
|
934
|
+
});
|
|
935
|
+
executeInstructions(container.renderGroup, renderPipes);
|
|
936
|
+
if (renderPipes.uniformBatch) {
|
|
937
|
+
renderPipes.uniformBatch.renderEnd();
|
|
938
|
+
}
|
|
939
|
+
if (transform) {
|
|
940
|
+
container.renderGroup.localTransform.copyFrom(originalLocalTransform);
|
|
941
|
+
}
|
|
942
|
+
container.parent = parent;
|
|
943
|
+
container.renderGroup.renderGroupParent = renderGroupParent;
|
|
944
|
+
}
|
|
945
|
+
destroy() {
|
|
946
|
+
this._renderer = null;
|
|
947
|
+
}
|
|
948
|
+
_updateCachedRenderGroups(renderGroup, closestCacheAsTexture) {
|
|
949
|
+
renderGroup._parentCacheAsTextureRenderGroup = closestCacheAsTexture;
|
|
950
|
+
if (renderGroup.isCachedAsTexture) {
|
|
951
|
+
if (!renderGroup.textureNeedsUpdate) return;
|
|
952
|
+
closestCacheAsTexture = renderGroup;
|
|
953
|
+
}
|
|
954
|
+
for (let i = renderGroup.renderGroupChildren.length - 1; i >= 0; i--) {
|
|
955
|
+
this._updateCachedRenderGroups(renderGroup.renderGroupChildren[i], closestCacheAsTexture);
|
|
956
|
+
}
|
|
957
|
+
renderGroup.invalidateMatrices();
|
|
958
|
+
if (renderGroup.isCachedAsTexture) {
|
|
959
|
+
if (renderGroup.textureNeedsUpdate) {
|
|
960
|
+
const bounds = renderGroup.root.getLocalBounds();
|
|
961
|
+
const renderer = this._renderer;
|
|
962
|
+
const resolution = renderGroup.textureOptions.resolution || renderer.view.resolution;
|
|
963
|
+
const antialias = renderGroup.textureOptions.antialias ?? renderer.view.antialias;
|
|
964
|
+
const scaleMode = renderGroup.textureOptions.scaleMode ?? "linear";
|
|
965
|
+
const lastTexture = renderGroup.texture;
|
|
966
|
+
bounds.ceil();
|
|
967
|
+
if (renderGroup.texture) {
|
|
968
|
+
TexturePool.returnTexture(renderGroup.texture, true);
|
|
969
|
+
}
|
|
970
|
+
const texture = TexturePool.getOptimalTexture(
|
|
971
|
+
bounds.width,
|
|
972
|
+
bounds.height,
|
|
973
|
+
resolution,
|
|
974
|
+
antialias
|
|
975
|
+
);
|
|
976
|
+
texture._source.style = new TextureStyle({ scaleMode });
|
|
977
|
+
renderGroup.texture = texture;
|
|
978
|
+
renderGroup._textureBounds || (renderGroup._textureBounds = new Bounds());
|
|
979
|
+
renderGroup._textureBounds.copyFrom(bounds);
|
|
980
|
+
if (lastTexture !== renderGroup.texture) {
|
|
981
|
+
if (renderGroup.renderGroupParent) {
|
|
982
|
+
renderGroup.renderGroupParent.structureDidChange = true;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
} else if (renderGroup.texture) {
|
|
987
|
+
TexturePool.returnTexture(renderGroup.texture, true);
|
|
988
|
+
renderGroup.texture = null;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
_updateRenderGroups(renderGroup) {
|
|
992
|
+
const renderer = this._renderer;
|
|
993
|
+
const renderPipes = renderer.renderPipes;
|
|
994
|
+
renderGroup.runOnRender(renderer);
|
|
995
|
+
renderGroup.instructionSet.renderPipes = renderPipes;
|
|
996
|
+
if (!renderGroup.structureDidChange) {
|
|
997
|
+
validateRenderables(renderGroup, renderPipes);
|
|
998
|
+
} else {
|
|
999
|
+
clearList(renderGroup.childrenRenderablesToUpdate.list, 0);
|
|
1000
|
+
}
|
|
1001
|
+
updateRenderGroupTransforms(renderGroup);
|
|
1002
|
+
if (renderGroup.structureDidChange) {
|
|
1003
|
+
renderGroup.structureDidChange = false;
|
|
1004
|
+
this._buildInstructions(renderGroup, renderer);
|
|
1005
|
+
} else {
|
|
1006
|
+
this._updateRenderables(renderGroup);
|
|
1007
|
+
}
|
|
1008
|
+
renderGroup.childrenRenderablesToUpdate.index = 0;
|
|
1009
|
+
renderer.renderPipes.batch.upload(renderGroup.instructionSet);
|
|
1010
|
+
if (renderGroup.isCachedAsTexture && !renderGroup.textureNeedsUpdate) return;
|
|
1011
|
+
for (let i = 0; i < renderGroup.renderGroupChildren.length; i++) {
|
|
1012
|
+
this._updateRenderGroups(renderGroup.renderGroupChildren[i]);
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
_updateRenderables(renderGroup) {
|
|
1016
|
+
const { list, index } = renderGroup.childrenRenderablesToUpdate;
|
|
1017
|
+
for (let i = 0; i < index; i++) {
|
|
1018
|
+
const container = list[i];
|
|
1019
|
+
if (container.didViewUpdate) {
|
|
1020
|
+
renderGroup.updateRenderable(container);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
clearList(list, index);
|
|
1024
|
+
}
|
|
1025
|
+
_buildInstructions(renderGroup, rendererOrPipes) {
|
|
1026
|
+
const root = renderGroup.root;
|
|
1027
|
+
const instructionSet = renderGroup.instructionSet;
|
|
1028
|
+
instructionSet.reset();
|
|
1029
|
+
const renderer = rendererOrPipes.renderPipes ? rendererOrPipes : rendererOrPipes.batch.renderer;
|
|
1030
|
+
const renderPipes = renderer.renderPipes;
|
|
1031
|
+
renderPipes.batch.buildStart(instructionSet);
|
|
1032
|
+
renderPipes.blendMode.buildStart();
|
|
1033
|
+
renderPipes.colorMask.buildStart();
|
|
1034
|
+
if (root.sortableChildren) {
|
|
1035
|
+
root.sortChildren();
|
|
1036
|
+
}
|
|
1037
|
+
root.collectRenderablesWithEffects(instructionSet, renderer, null);
|
|
1038
|
+
renderPipes.batch.buildEnd(instructionSet);
|
|
1039
|
+
renderPipes.blendMode.buildEnd(instructionSet);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
RenderGroupSystem.extension = {
|
|
1043
|
+
type: [
|
|
1044
|
+
ExtensionType.WebGLSystem,
|
|
1045
|
+
ExtensionType.WebGPUSystem,
|
|
1046
|
+
ExtensionType.CanvasSystem
|
|
1047
|
+
],
|
|
1048
|
+
name: "renderGroup"
|
|
1049
|
+
};
|
|
1050
|
+
const _BackgroundSystem = class _BackgroundSystem2 {
|
|
1051
|
+
constructor() {
|
|
1052
|
+
this.clearBeforeRender = true;
|
|
1053
|
+
this._backgroundColor = new Color(0);
|
|
1054
|
+
this.color = this._backgroundColor;
|
|
1055
|
+
this.alpha = 1;
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* initiates the background system
|
|
1059
|
+
* @param options - the options for the background colors
|
|
1060
|
+
*/
|
|
1061
|
+
init(options) {
|
|
1062
|
+
options = { ..._BackgroundSystem2.defaultOptions, ...options };
|
|
1063
|
+
this.clearBeforeRender = options.clearBeforeRender;
|
|
1064
|
+
this.color = options.background || options.backgroundColor || this._backgroundColor;
|
|
1065
|
+
this.alpha = options.backgroundAlpha;
|
|
1066
|
+
this._backgroundColor.setAlpha(options.backgroundAlpha);
|
|
1067
|
+
}
|
|
1068
|
+
/** The background color to fill if not transparent */
|
|
1069
|
+
get color() {
|
|
1070
|
+
return this._backgroundColor;
|
|
1071
|
+
}
|
|
1072
|
+
set color(value) {
|
|
1073
|
+
const incoming = Color.shared.setValue(value);
|
|
1074
|
+
if (incoming.alpha < 1 && this._backgroundColor.alpha === 1) {
|
|
1075
|
+
warn(
|
|
1076
|
+
"Cannot set a transparent background on an opaque canvas. To enable transparency, set backgroundAlpha < 1 when initializing your Application."
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
this._backgroundColor.setValue(value);
|
|
1080
|
+
}
|
|
1081
|
+
/** The background color alpha. Setting this to 0 will make the canvas transparent. */
|
|
1082
|
+
get alpha() {
|
|
1083
|
+
return this._backgroundColor.alpha;
|
|
1084
|
+
}
|
|
1085
|
+
set alpha(value) {
|
|
1086
|
+
this._backgroundColor.setAlpha(value);
|
|
1087
|
+
}
|
|
1088
|
+
/** The background color as an [R, G, B, A] array. */
|
|
1089
|
+
get colorRgba() {
|
|
1090
|
+
return this._backgroundColor.toArray();
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* destroys the background system
|
|
1094
|
+
* @internal
|
|
1095
|
+
*/
|
|
1096
|
+
destroy() {
|
|
1097
|
+
}
|
|
1098
|
+
};
|
|
1099
|
+
_BackgroundSystem.extension = {
|
|
1100
|
+
type: [
|
|
1101
|
+
ExtensionType.WebGLSystem,
|
|
1102
|
+
ExtensionType.WebGPUSystem,
|
|
1103
|
+
ExtensionType.CanvasSystem
|
|
1104
|
+
],
|
|
1105
|
+
name: "background",
|
|
1106
|
+
priority: 0
|
|
1107
|
+
};
|
|
1108
|
+
_BackgroundSystem.defaultOptions = {
|
|
1109
|
+
/**
|
|
1110
|
+
* {@link WebGLOptions.backgroundAlpha}
|
|
1111
|
+
* @default 1
|
|
1112
|
+
*/
|
|
1113
|
+
backgroundAlpha: 1,
|
|
1114
|
+
/**
|
|
1115
|
+
* {@link WebGLOptions.backgroundColor}
|
|
1116
|
+
* @default 0x000000
|
|
1117
|
+
*/
|
|
1118
|
+
backgroundColor: 0,
|
|
1119
|
+
/**
|
|
1120
|
+
* {@link WebGLOptions.clearBeforeRender}
|
|
1121
|
+
* @default true
|
|
1122
|
+
*/
|
|
1123
|
+
clearBeforeRender: true
|
|
1124
|
+
};
|
|
1125
|
+
let BackgroundSystem = _BackgroundSystem;
|
|
1126
|
+
const imageTypes = {
|
|
1127
|
+
png: "image/png",
|
|
1128
|
+
jpg: "image/jpeg",
|
|
1129
|
+
webp: "image/webp"
|
|
1130
|
+
};
|
|
1131
|
+
const _ExtractSystem = class _ExtractSystem2 {
|
|
1132
|
+
/** @param renderer - The renderer this System works for. */
|
|
1133
|
+
constructor(renderer) {
|
|
1134
|
+
this._renderer = renderer;
|
|
1135
|
+
}
|
|
1136
|
+
_normalizeOptions(options, defaults = {}) {
|
|
1137
|
+
if (options instanceof Container || options instanceof Texture) {
|
|
1138
|
+
return {
|
|
1139
|
+
target: options,
|
|
1140
|
+
...defaults
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
return {
|
|
1144
|
+
...defaults,
|
|
1145
|
+
...options
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Creates an IImage from a display object or texture.
|
|
1150
|
+
* @param options - Options for creating the image, or the target to extract
|
|
1151
|
+
* @returns Promise that resolves with the generated IImage
|
|
1152
|
+
* @example
|
|
1153
|
+
* ```ts
|
|
1154
|
+
* // Basic usage with a sprite
|
|
1155
|
+
* const sprite = new Sprite(texture);
|
|
1156
|
+
* const image = await renderer.extract.image(sprite);
|
|
1157
|
+
* document.body.appendChild(image);
|
|
1158
|
+
*
|
|
1159
|
+
* // Advanced usage with options
|
|
1160
|
+
* const image = await renderer.extract.image({
|
|
1161
|
+
* target: container,
|
|
1162
|
+
* format: 'webp',
|
|
1163
|
+
* quality: 0.8,
|
|
1164
|
+
* frame: new Rectangle(0, 0, 100, 100),
|
|
1165
|
+
* resolution: 2,
|
|
1166
|
+
* clearColor: '#ff0000',
|
|
1167
|
+
* antialias: true
|
|
1168
|
+
* });
|
|
1169
|
+
*
|
|
1170
|
+
* // Extract directly from a texture
|
|
1171
|
+
* const texture = Texture.from('myTexture.png');
|
|
1172
|
+
* const image = await renderer.extract.image(texture);
|
|
1173
|
+
* ```
|
|
1174
|
+
* @see {@link ExtractImageOptions} For detailed options
|
|
1175
|
+
* @see {@link ExtractSystem.base64} For base64 string output
|
|
1176
|
+
* @see {@link ExtractSystem.canvas} For canvas output
|
|
1177
|
+
* @see {@link ImageLike} For the image interface
|
|
1178
|
+
* @category rendering
|
|
1179
|
+
*/
|
|
1180
|
+
async image(options) {
|
|
1181
|
+
const image = DOMAdapter.get().createImage();
|
|
1182
|
+
image.src = await this.base64(options);
|
|
1183
|
+
return image;
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* Converts the target into a base64 encoded string.
|
|
1187
|
+
*
|
|
1188
|
+
* This method works by first creating
|
|
1189
|
+
* a canvas using `Extract.canvas` and then converting it to a base64 string.
|
|
1190
|
+
* @param options - The options for creating the base64 string, or the target to extract
|
|
1191
|
+
* @returns Promise that resolves with the base64 encoded string
|
|
1192
|
+
* @example
|
|
1193
|
+
* ```ts
|
|
1194
|
+
* // Basic usage with a sprite
|
|
1195
|
+
* const sprite = new Sprite(texture);
|
|
1196
|
+
* const base64 = await renderer.extract.base64(sprite);
|
|
1197
|
+
* console.log(base64); // data:image/png;base64,...
|
|
1198
|
+
*
|
|
1199
|
+
* // Advanced usage with options
|
|
1200
|
+
* const base64 = await renderer.extract.base64({
|
|
1201
|
+
* target: container,
|
|
1202
|
+
* format: 'webp',
|
|
1203
|
+
* quality: 0.8,
|
|
1204
|
+
* frame: new Rectangle(0, 0, 100, 100),
|
|
1205
|
+
* resolution: 2
|
|
1206
|
+
* });
|
|
1207
|
+
* ```
|
|
1208
|
+
* @throws Will throw an error if the platform doesn't support any of:
|
|
1209
|
+
* - ICanvas.toDataURL
|
|
1210
|
+
* - ICanvas.toBlob
|
|
1211
|
+
* - ICanvas.convertToBlob
|
|
1212
|
+
* @see {@link ExtractImageOptions} For detailed options
|
|
1213
|
+
* @see {@link ExtractSystem.canvas} For canvas output
|
|
1214
|
+
* @see {@link ExtractSystem.image} For HTMLImage output
|
|
1215
|
+
* @category rendering
|
|
1216
|
+
*/
|
|
1217
|
+
async base64(options) {
|
|
1218
|
+
options = this._normalizeOptions(
|
|
1219
|
+
options,
|
|
1220
|
+
_ExtractSystem2.defaultImageOptions
|
|
1221
|
+
);
|
|
1222
|
+
const { format, quality } = options;
|
|
1223
|
+
const canvas = this.canvas(options);
|
|
1224
|
+
if (canvas.toBlob !== void 0) {
|
|
1225
|
+
return new Promise((resolve, reject) => {
|
|
1226
|
+
canvas.toBlob((blob) => {
|
|
1227
|
+
if (!blob) {
|
|
1228
|
+
reject(new Error("ICanvas.toBlob failed!"));
|
|
1229
|
+
return;
|
|
1230
|
+
}
|
|
1231
|
+
const reader = new FileReader();
|
|
1232
|
+
reader.onload = () => resolve(reader.result);
|
|
1233
|
+
reader.onerror = reject;
|
|
1234
|
+
reader.readAsDataURL(blob);
|
|
1235
|
+
}, imageTypes[format], quality);
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
if (canvas.toDataURL !== void 0) {
|
|
1239
|
+
return canvas.toDataURL(imageTypes[format], quality);
|
|
1240
|
+
}
|
|
1241
|
+
if (canvas.convertToBlob !== void 0) {
|
|
1242
|
+
const blob = await canvas.convertToBlob({ type: imageTypes[format], quality });
|
|
1243
|
+
return new Promise((resolve, reject) => {
|
|
1244
|
+
const reader = new FileReader();
|
|
1245
|
+
reader.onload = () => resolve(reader.result);
|
|
1246
|
+
reader.onerror = reject;
|
|
1247
|
+
reader.readAsDataURL(blob);
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
throw new Error("Extract.base64() requires ICanvas.toDataURL, ICanvas.toBlob, or ICanvas.convertToBlob to be implemented");
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Creates a Canvas element, renders the target to it and returns it.
|
|
1254
|
+
* This method is useful for creating static images or when you need direct canvas access.
|
|
1255
|
+
* @param options - The options for creating the canvas, or the target to extract
|
|
1256
|
+
* @returns A Canvas element with the texture rendered on
|
|
1257
|
+
* @example
|
|
1258
|
+
* ```ts
|
|
1259
|
+
* // Basic canvas extraction from a sprite
|
|
1260
|
+
* const sprite = new Sprite(texture);
|
|
1261
|
+
* const canvas = renderer.extract.canvas(sprite);
|
|
1262
|
+
* document.body.appendChild(canvas);
|
|
1263
|
+
*
|
|
1264
|
+
* // Extract with custom region
|
|
1265
|
+
* const canvas = renderer.extract.canvas({
|
|
1266
|
+
* target: container,
|
|
1267
|
+
* frame: new Rectangle(0, 0, 100, 100)
|
|
1268
|
+
* });
|
|
1269
|
+
*
|
|
1270
|
+
* // Extract with high resolution
|
|
1271
|
+
* const canvas = renderer.extract.canvas({
|
|
1272
|
+
* target: sprite,
|
|
1273
|
+
* resolution: 2,
|
|
1274
|
+
* clearColor: '#ff0000'
|
|
1275
|
+
* });
|
|
1276
|
+
*
|
|
1277
|
+
* // Extract directly from a texture
|
|
1278
|
+
* const texture = Texture.from('myTexture.png');
|
|
1279
|
+
* const canvas = renderer.extract.canvas(texture);
|
|
1280
|
+
*
|
|
1281
|
+
* // Extract with anti-aliasing
|
|
1282
|
+
* const canvas = renderer.extract.canvas({
|
|
1283
|
+
* target: graphics,
|
|
1284
|
+
* antialias: true
|
|
1285
|
+
* });
|
|
1286
|
+
* ```
|
|
1287
|
+
* @see {@link ExtractOptions} For detailed options
|
|
1288
|
+
* @see {@link ExtractSystem.image} For HTMLImage output
|
|
1289
|
+
* @see {@link ExtractSystem.pixels} For raw pixel data
|
|
1290
|
+
* @category rendering
|
|
1291
|
+
*/
|
|
1292
|
+
canvas(options) {
|
|
1293
|
+
options = this._normalizeOptions(options);
|
|
1294
|
+
const target = options.target;
|
|
1295
|
+
const renderer = this._renderer;
|
|
1296
|
+
if (target instanceof Texture) {
|
|
1297
|
+
return renderer.texture.generateCanvas(target);
|
|
1298
|
+
}
|
|
1299
|
+
const texture = renderer.textureGenerator.generateTexture(options);
|
|
1300
|
+
const canvas = renderer.texture.generateCanvas(texture);
|
|
1301
|
+
texture.destroy(true);
|
|
1302
|
+
return canvas;
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Returns a one-dimensional array containing the pixel data of the entire texture in RGBA order,
|
|
1306
|
+
* with integer values between 0 and 255 (inclusive).
|
|
1307
|
+
* > [!NOE] The returned array is a flat Uint8Array where every 4 values represent RGBA
|
|
1308
|
+
* @param options - The options for extracting the image, or the target to extract
|
|
1309
|
+
* @returns One-dimensional Uint8Array containing the pixel data in RGBA format
|
|
1310
|
+
* @example
|
|
1311
|
+
* ```ts
|
|
1312
|
+
* // Basic pixel extraction
|
|
1313
|
+
* const sprite = new Sprite(texture);
|
|
1314
|
+
* const pixels = renderer.extract.pixels(sprite);
|
|
1315
|
+
* console.log(pixels[0], pixels[1], pixels[2], pixels[3]); // R,G,B,A values
|
|
1316
|
+
*
|
|
1317
|
+
* // Extract with custom region
|
|
1318
|
+
* const pixels = renderer.extract.pixels({
|
|
1319
|
+
* target: sprite,
|
|
1320
|
+
* frame: new Rectangle(0, 0, 100, 100)
|
|
1321
|
+
* });
|
|
1322
|
+
*
|
|
1323
|
+
* // Extract with high resolution
|
|
1324
|
+
* const pixels = renderer.extract.pixels({
|
|
1325
|
+
* target: sprite,
|
|
1326
|
+
* resolution: 2
|
|
1327
|
+
* });
|
|
1328
|
+
* ```
|
|
1329
|
+
* @see {@link ExtractOptions} For detailed options
|
|
1330
|
+
* @see {@link ExtractSystem.canvas} For canvas output
|
|
1331
|
+
* @see {@link ExtractSystem.image} For image output
|
|
1332
|
+
* @category rendering
|
|
1333
|
+
*/
|
|
1334
|
+
pixels(options) {
|
|
1335
|
+
options = this._normalizeOptions(options);
|
|
1336
|
+
const target = options.target;
|
|
1337
|
+
const renderer = this._renderer;
|
|
1338
|
+
const texture = target instanceof Texture ? target : renderer.textureGenerator.generateTexture(options);
|
|
1339
|
+
const pixelInfo = renderer.texture.getPixels(texture);
|
|
1340
|
+
if (target instanceof Container) {
|
|
1341
|
+
texture.destroy(true);
|
|
1342
|
+
}
|
|
1343
|
+
return pixelInfo;
|
|
1344
|
+
}
|
|
1345
|
+
/**
|
|
1346
|
+
* Creates a texture from a display object or existing texture.
|
|
1347
|
+
*
|
|
1348
|
+
* This is useful for creating
|
|
1349
|
+
* reusable textures from rendered content or making copies of existing textures.
|
|
1350
|
+
* > [!NOTE] The returned texture should be destroyed when no longer needed
|
|
1351
|
+
* @param options - The options for creating the texture, or the target to extract
|
|
1352
|
+
* @returns A new texture containing the extracted content
|
|
1353
|
+
* @example
|
|
1354
|
+
* ```ts
|
|
1355
|
+
* // Basic texture extraction from a sprite
|
|
1356
|
+
* const sprite = new Sprite(texture);
|
|
1357
|
+
* const extractedTexture = renderer.extract.texture(sprite);
|
|
1358
|
+
*
|
|
1359
|
+
* // Extract with custom region
|
|
1360
|
+
* const regionTexture = renderer.extract.texture({
|
|
1361
|
+
* target: container,
|
|
1362
|
+
* frame: new Rectangle(0, 0, 100, 100)
|
|
1363
|
+
* });
|
|
1364
|
+
*
|
|
1365
|
+
* // Extract with high resolution
|
|
1366
|
+
* const hiResTexture = renderer.extract.texture({
|
|
1367
|
+
* target: sprite,
|
|
1368
|
+
* resolution: 2,
|
|
1369
|
+
* clearColor: '#ff0000'
|
|
1370
|
+
* });
|
|
1371
|
+
*
|
|
1372
|
+
* // Create a new sprite from extracted texture
|
|
1373
|
+
* const newSprite = new Sprite(
|
|
1374
|
+
* renderer.extract.texture({
|
|
1375
|
+
* target: graphics,
|
|
1376
|
+
* antialias: true
|
|
1377
|
+
* })
|
|
1378
|
+
* );
|
|
1379
|
+
*
|
|
1380
|
+
* // Clean up when done
|
|
1381
|
+
* extractedTexture.destroy(true);
|
|
1382
|
+
* ```
|
|
1383
|
+
* @see {@link ExtractOptions} For detailed options
|
|
1384
|
+
* @see {@link Texture} For texture management
|
|
1385
|
+
* @see {@link GenerateTextureSystem} For texture generation
|
|
1386
|
+
* @category rendering
|
|
1387
|
+
*/
|
|
1388
|
+
texture(options) {
|
|
1389
|
+
options = this._normalizeOptions(options);
|
|
1390
|
+
if (options.target instanceof Texture) return options.target;
|
|
1391
|
+
return this._renderer.textureGenerator.generateTexture(options);
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Extracts and downloads content from the renderer as an image file.
|
|
1395
|
+
* This is a convenient way to save screenshots or export rendered content.
|
|
1396
|
+
* > [!NOTE] The download will use PNG format regardless of the filename extension
|
|
1397
|
+
* @param options - The options for downloading and extracting the image, or the target to extract
|
|
1398
|
+
* @example
|
|
1399
|
+
* ```ts
|
|
1400
|
+
* // Basic download with default filename
|
|
1401
|
+
* const sprite = new Sprite(texture);
|
|
1402
|
+
* renderer.extract.download(sprite); // Downloads as 'image.png'
|
|
1403
|
+
*
|
|
1404
|
+
* // Download with custom filename
|
|
1405
|
+
* renderer.extract.download({
|
|
1406
|
+
* target: sprite,
|
|
1407
|
+
* filename: 'screenshot.png'
|
|
1408
|
+
* });
|
|
1409
|
+
*
|
|
1410
|
+
* // Download with custom region
|
|
1411
|
+
* renderer.extract.download({
|
|
1412
|
+
* target: container,
|
|
1413
|
+
* filename: 'region.png',
|
|
1414
|
+
* frame: new Rectangle(0, 0, 100, 100)
|
|
1415
|
+
* });
|
|
1416
|
+
*
|
|
1417
|
+
* // Download with high resolution and background
|
|
1418
|
+
* renderer.extract.download({
|
|
1419
|
+
* target: stage,
|
|
1420
|
+
* filename: 'hd-screenshot.png',
|
|
1421
|
+
* resolution: 2,
|
|
1422
|
+
* clearColor: '#ff0000'
|
|
1423
|
+
* });
|
|
1424
|
+
*
|
|
1425
|
+
* // Download with anti-aliasing
|
|
1426
|
+
* renderer.extract.download({
|
|
1427
|
+
* target: graphics,
|
|
1428
|
+
* filename: 'smooth.png',
|
|
1429
|
+
* antialias: true
|
|
1430
|
+
* });
|
|
1431
|
+
* ```
|
|
1432
|
+
* @see {@link ExtractDownloadOptions} For detailed options
|
|
1433
|
+
* @see {@link ExtractSystem.image} For creating images without download
|
|
1434
|
+
* @see {@link ExtractSystem.canvas} For canvas output
|
|
1435
|
+
* @category rendering
|
|
1436
|
+
*/
|
|
1437
|
+
download(options) {
|
|
1438
|
+
options = this._normalizeOptions(options);
|
|
1439
|
+
const canvas = this.canvas(options);
|
|
1440
|
+
const link = document.createElement("a");
|
|
1441
|
+
link.download = options.filename ?? "image.png";
|
|
1442
|
+
link.href = canvas.toDataURL("image/png");
|
|
1443
|
+
document.body.appendChild(link);
|
|
1444
|
+
link.click();
|
|
1445
|
+
document.body.removeChild(link);
|
|
1446
|
+
}
|
|
1447
|
+
/**
|
|
1448
|
+
* Logs the target to the console as an image. This is a useful way to debug what's happening in the renderer.
|
|
1449
|
+
* The image will be displayed in the browser's console using CSS background images.
|
|
1450
|
+
* @param options - The options for logging the image, or the target to log
|
|
1451
|
+
* @param options.width - The width of the logged image preview in the console (in pixels)
|
|
1452
|
+
* @example
|
|
1453
|
+
* ```ts
|
|
1454
|
+
* // Basic usage
|
|
1455
|
+
* const sprite = new Sprite(texture);
|
|
1456
|
+
* renderer.extract.log(sprite);
|
|
1457
|
+
* ```
|
|
1458
|
+
* @see {@link ExtractSystem.canvas} For getting raw canvas output
|
|
1459
|
+
* @see {@link ExtractSystem.pixels} For raw pixel data
|
|
1460
|
+
* @category rendering
|
|
1461
|
+
* @advanced
|
|
1462
|
+
*/
|
|
1463
|
+
log(options) {
|
|
1464
|
+
const width = options.width ?? 200;
|
|
1465
|
+
options = this._normalizeOptions(options);
|
|
1466
|
+
const canvas = this.canvas(options);
|
|
1467
|
+
const base64 = canvas.toDataURL();
|
|
1468
|
+
console.log(`[Pixi Texture] ${canvas.width}px ${canvas.height}px`);
|
|
1469
|
+
const style = [
|
|
1470
|
+
"font-size: 1px;",
|
|
1471
|
+
`padding: ${width}px ${300}px;`,
|
|
1472
|
+
`background: url(${base64}) no-repeat;`,
|
|
1473
|
+
"background-size: contain;"
|
|
1474
|
+
].join(" ");
|
|
1475
|
+
console.log("%c ", style);
|
|
1476
|
+
}
|
|
1477
|
+
destroy() {
|
|
1478
|
+
this._renderer = null;
|
|
1479
|
+
}
|
|
1480
|
+
};
|
|
1481
|
+
_ExtractSystem.extension = {
|
|
1482
|
+
type: [
|
|
1483
|
+
ExtensionType.WebGLSystem,
|
|
1484
|
+
ExtensionType.WebGPUSystem,
|
|
1485
|
+
ExtensionType.CanvasSystem
|
|
1486
|
+
],
|
|
1487
|
+
name: "extract"
|
|
1488
|
+
};
|
|
1489
|
+
_ExtractSystem.defaultImageOptions = {
|
|
1490
|
+
format: "png",
|
|
1491
|
+
quality: 1
|
|
1492
|
+
};
|
|
1493
|
+
let ExtractSystem = _ExtractSystem;
|
|
1494
|
+
class RenderTexture extends Texture {
|
|
1495
|
+
/**
|
|
1496
|
+
* Creates a RenderTexture. Pass `dynamic: true` in options to allow resizing after creation.
|
|
1497
|
+
* @param options - Options for the RenderTexture, including width, height, and dynamic.
|
|
1498
|
+
* @returns A new RenderTexture instance.
|
|
1499
|
+
* @example
|
|
1500
|
+
* const rt = RenderTexture.create({ width: 100, height: 100, dynamic: true });
|
|
1501
|
+
* rt.resize(500, 500);
|
|
1502
|
+
*/
|
|
1503
|
+
static create(options) {
|
|
1504
|
+
const { dynamic, ...rest } = options;
|
|
1505
|
+
return new RenderTexture({
|
|
1506
|
+
source: new TextureSource(rest),
|
|
1507
|
+
dynamic: dynamic ?? false
|
|
1508
|
+
});
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Resizes the render texture.
|
|
1512
|
+
* @param width - The new width of the render texture.
|
|
1513
|
+
* @param height - The new height of the render texture.
|
|
1514
|
+
* @param resolution - The new resolution of the render texture.
|
|
1515
|
+
* @returns This texture.
|
|
1516
|
+
*/
|
|
1517
|
+
resize(width, height, resolution) {
|
|
1518
|
+
this.source.resize(width, height, resolution);
|
|
1519
|
+
return this;
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
const tempRect = new Rectangle();
|
|
1523
|
+
const tempBounds = new Bounds();
|
|
1524
|
+
const noColor = [0, 0, 0, 0];
|
|
1525
|
+
class GenerateTextureSystem {
|
|
1526
|
+
constructor(renderer) {
|
|
1527
|
+
this._renderer = renderer;
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* Creates a texture from a display object that can be used for creating sprites and other textures.
|
|
1531
|
+
* This is particularly useful for optimizing performance when a complex container needs to be reused.
|
|
1532
|
+
* @param options - Generate texture options or a container to convert to texture
|
|
1533
|
+
* @returns A new RenderTexture containing the rendered display object
|
|
1534
|
+
* @example
|
|
1535
|
+
* ```ts
|
|
1536
|
+
* // Basic usage with a container
|
|
1537
|
+
* const container = new Container();
|
|
1538
|
+
* container.addChild(
|
|
1539
|
+
* new Graphics()
|
|
1540
|
+
* .circle(0, 0, 50)
|
|
1541
|
+
* .fill('red')
|
|
1542
|
+
* );
|
|
1543
|
+
*
|
|
1544
|
+
* const texture = renderer.textureGenerator.generateTexture(container);
|
|
1545
|
+
*
|
|
1546
|
+
* // Advanced usage with options
|
|
1547
|
+
* const texture = renderer.textureGenerator.generateTexture({
|
|
1548
|
+
* target: container,
|
|
1549
|
+
* frame: new Rectangle(0, 0, 100, 100), // Specific region
|
|
1550
|
+
* resolution: 2, // High DPI
|
|
1551
|
+
* clearColor: '#ff0000', // Red background
|
|
1552
|
+
* antialias: true // Smooth edges
|
|
1553
|
+
* });
|
|
1554
|
+
*
|
|
1555
|
+
* // Create a sprite from the generated texture
|
|
1556
|
+
* const sprite = new Sprite(texture);
|
|
1557
|
+
*
|
|
1558
|
+
* // Clean up when done
|
|
1559
|
+
* texture.destroy(true);
|
|
1560
|
+
* ```
|
|
1561
|
+
* @see {@link GenerateTextureOptions} For detailed texture generation options
|
|
1562
|
+
* @see {@link RenderTexture} For the type of texture created
|
|
1563
|
+
* @category rendering
|
|
1564
|
+
*/
|
|
1565
|
+
generateTexture(options) {
|
|
1566
|
+
if (options instanceof Container) {
|
|
1567
|
+
options = {
|
|
1568
|
+
target: options,
|
|
1569
|
+
frame: void 0,
|
|
1570
|
+
textureSourceOptions: {},
|
|
1571
|
+
resolution: void 0
|
|
1572
|
+
};
|
|
1573
|
+
}
|
|
1574
|
+
const resolution = options.resolution || this._renderer.resolution;
|
|
1575
|
+
const antialias = options.antialias || this._renderer.view.antialias;
|
|
1576
|
+
const container = options.target;
|
|
1577
|
+
let clearColor = options.clearColor;
|
|
1578
|
+
if (clearColor) {
|
|
1579
|
+
const isRGBAArray = Array.isArray(clearColor) && clearColor.length === 4;
|
|
1580
|
+
clearColor = isRGBAArray ? clearColor : Color.shared.setValue(clearColor).toArray();
|
|
1581
|
+
} else {
|
|
1582
|
+
clearColor = noColor;
|
|
1583
|
+
}
|
|
1584
|
+
const region = options.frame?.copyTo(tempRect) || getLocalBounds(container, tempBounds).rectangle;
|
|
1585
|
+
region.width = Math.max(region.width, 1 / resolution) | 0;
|
|
1586
|
+
region.height = Math.max(region.height, 1 / resolution) | 0;
|
|
1587
|
+
const target = RenderTexture.create({
|
|
1588
|
+
...options.textureSourceOptions,
|
|
1589
|
+
width: region.width,
|
|
1590
|
+
height: region.height,
|
|
1591
|
+
resolution,
|
|
1592
|
+
antialias
|
|
1593
|
+
});
|
|
1594
|
+
const transform = Matrix.shared.translate(-region.x, -region.y);
|
|
1595
|
+
this._renderer.render({
|
|
1596
|
+
container,
|
|
1597
|
+
transform,
|
|
1598
|
+
target,
|
|
1599
|
+
clearColor
|
|
1600
|
+
});
|
|
1601
|
+
target.source.updateMipmaps();
|
|
1602
|
+
return target;
|
|
1603
|
+
}
|
|
1604
|
+
destroy() {
|
|
1605
|
+
this._renderer = null;
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
GenerateTextureSystem.extension = {
|
|
1609
|
+
type: [
|
|
1610
|
+
ExtensionType.WebGLSystem,
|
|
1611
|
+
ExtensionType.WebGPUSystem,
|
|
1612
|
+
ExtensionType.CanvasSystem
|
|
1613
|
+
],
|
|
1614
|
+
name: "textureGenerator"
|
|
1615
|
+
};
|
|
1616
|
+
function cleanHash(hash) {
|
|
1617
|
+
let clean = false;
|
|
1618
|
+
for (const i in hash) {
|
|
1619
|
+
if (hash[i] == void 0) {
|
|
1620
|
+
clean = true;
|
|
1621
|
+
break;
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
if (!clean) return hash;
|
|
1625
|
+
const cleanHash2 = /* @__PURE__ */ Object.create(null);
|
|
1626
|
+
for (const i in hash) {
|
|
1627
|
+
const value = hash[i];
|
|
1628
|
+
if (value) {
|
|
1629
|
+
cleanHash2[i] = value;
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
return cleanHash2;
|
|
1633
|
+
}
|
|
1634
|
+
function cleanArray(arr) {
|
|
1635
|
+
let offset = 0;
|
|
1636
|
+
for (let i = 0; i < arr.length; i++) {
|
|
1637
|
+
if (arr[i] == void 0) {
|
|
1638
|
+
offset++;
|
|
1639
|
+
} else {
|
|
1640
|
+
arr[i - offset] = arr[i];
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
arr.length -= offset;
|
|
1644
|
+
return arr;
|
|
1645
|
+
}
|
|
1646
|
+
const _GCSystem = class _GCSystem2 {
|
|
1647
|
+
/**
|
|
1648
|
+
* Creates a new GCSystem instance.
|
|
1649
|
+
* @param renderer - The renderer this garbage collection system works for
|
|
1650
|
+
*/
|
|
1651
|
+
constructor(renderer) {
|
|
1652
|
+
this._managedResources = [];
|
|
1653
|
+
this._managedResourceHashes = [];
|
|
1654
|
+
this._managedCollections = [];
|
|
1655
|
+
this._ready = false;
|
|
1656
|
+
this._renderer = renderer;
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* Initializes the garbage collection system with the provided options.
|
|
1660
|
+
* @param options - Configuration options
|
|
1661
|
+
*/
|
|
1662
|
+
init(options) {
|
|
1663
|
+
options = { ..._GCSystem2.defaultOptions, ...options };
|
|
1664
|
+
this.maxUnusedTime = options.gcMaxUnusedTime;
|
|
1665
|
+
this._frequency = options.gcFrequency;
|
|
1666
|
+
this.enabled = options.gcActive;
|
|
1667
|
+
this.now = performance.now();
|
|
1668
|
+
}
|
|
1669
|
+
/**
|
|
1670
|
+
* Gets whether the garbage collection system is currently enabled.
|
|
1671
|
+
* @returns True if GC is enabled, false otherwise
|
|
1672
|
+
*/
|
|
1673
|
+
get enabled() {
|
|
1674
|
+
return !!this._handler;
|
|
1675
|
+
}
|
|
1676
|
+
/**
|
|
1677
|
+
* Enables or disables the garbage collection system.
|
|
1678
|
+
* When enabled, schedules periodic cleanup of resources.
|
|
1679
|
+
* When disabled, cancels all scheduled cleanups.
|
|
1680
|
+
*/
|
|
1681
|
+
set enabled(value) {
|
|
1682
|
+
if (this.enabled === value) return;
|
|
1683
|
+
if (value) {
|
|
1684
|
+
this._handler = this._renderer.scheduler.repeat(
|
|
1685
|
+
() => {
|
|
1686
|
+
this._ready = true;
|
|
1687
|
+
},
|
|
1688
|
+
this._frequency,
|
|
1689
|
+
false
|
|
1690
|
+
);
|
|
1691
|
+
this._collectionsHandler = this._renderer.scheduler.repeat(
|
|
1692
|
+
() => {
|
|
1693
|
+
for (const hash of this._managedCollections) {
|
|
1694
|
+
const { context, collection, type } = hash;
|
|
1695
|
+
if (type === "hash") {
|
|
1696
|
+
context[collection] = cleanHash(context[collection]);
|
|
1697
|
+
} else {
|
|
1698
|
+
context[collection] = cleanArray(context[collection]);
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
},
|
|
1702
|
+
this._frequency
|
|
1703
|
+
);
|
|
1704
|
+
} else {
|
|
1705
|
+
this._renderer.scheduler.cancel(this._handler);
|
|
1706
|
+
this._renderer.scheduler.cancel(this._collectionsHandler);
|
|
1707
|
+
this._handler = 0;
|
|
1708
|
+
this._collectionsHandler = 0;
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
/**
|
|
1712
|
+
* Called before rendering. Updates the current timestamp.
|
|
1713
|
+
* @param options - The render options
|
|
1714
|
+
* @param options.container - The container to render
|
|
1715
|
+
*/
|
|
1716
|
+
prerender({ container }) {
|
|
1717
|
+
this.now = performance.now();
|
|
1718
|
+
container.renderGroup.gcTick = this._renderer.tick++;
|
|
1719
|
+
this._updateInstructionGCTick(container.renderGroup, container.renderGroup.gcTick);
|
|
1720
|
+
}
|
|
1721
|
+
/** Performs garbage collection after rendering. */
|
|
1722
|
+
postrender() {
|
|
1723
|
+
if (!this._ready || !this.enabled) return;
|
|
1724
|
+
this.run();
|
|
1725
|
+
this._ready = false;
|
|
1726
|
+
}
|
|
1727
|
+
/**
|
|
1728
|
+
* Updates the GC tick counter for a render group and its children.
|
|
1729
|
+
* @param renderGroup - The render group to update
|
|
1730
|
+
* @param gcTick - The new tick value
|
|
1731
|
+
*/
|
|
1732
|
+
_updateInstructionGCTick(renderGroup, gcTick) {
|
|
1733
|
+
renderGroup.instructionSet.gcTick = gcTick;
|
|
1734
|
+
renderGroup.gcTick = gcTick;
|
|
1735
|
+
for (const child of renderGroup.renderGroupChildren) {
|
|
1736
|
+
this._updateInstructionGCTick(child, gcTick);
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
/**
|
|
1740
|
+
* Registers a collection for garbage collection tracking.
|
|
1741
|
+
* @param context - The object containing the collection
|
|
1742
|
+
* @param collection - The property name on context that holds the collection
|
|
1743
|
+
* @param type - The type of collection to track ('hash' or 'array')
|
|
1744
|
+
*/
|
|
1745
|
+
addCollection(context, collection, type) {
|
|
1746
|
+
this._managedCollections.push({
|
|
1747
|
+
context,
|
|
1748
|
+
collection,
|
|
1749
|
+
type
|
|
1750
|
+
});
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Registers a resource for garbage collection tracking.
|
|
1754
|
+
* @param resource - The resource to track
|
|
1755
|
+
* @param type - The type of resource to track
|
|
1756
|
+
*/
|
|
1757
|
+
addResource(resource, type) {
|
|
1758
|
+
if (resource._gcLastUsed !== -1) {
|
|
1759
|
+
resource._gcLastUsed = this.now;
|
|
1760
|
+
resource._onTouch?.(this.now);
|
|
1761
|
+
return;
|
|
1762
|
+
}
|
|
1763
|
+
const index = this._managedResources.length;
|
|
1764
|
+
resource._gcData = {
|
|
1765
|
+
index,
|
|
1766
|
+
type
|
|
1767
|
+
};
|
|
1768
|
+
resource._gcLastUsed = this.now;
|
|
1769
|
+
resource._onTouch?.(this.now);
|
|
1770
|
+
resource.once("unload", this.removeResource, this);
|
|
1771
|
+
this._managedResources.push(resource);
|
|
1772
|
+
}
|
|
1773
|
+
/**
|
|
1774
|
+
* Removes a resource from garbage collection tracking.
|
|
1775
|
+
* Call this when manually destroying a resource.
|
|
1776
|
+
* @param resource - The resource to stop tracking
|
|
1777
|
+
*/
|
|
1778
|
+
removeResource(resource) {
|
|
1779
|
+
const gcData = resource._gcData;
|
|
1780
|
+
if (!gcData) return;
|
|
1781
|
+
const index = gcData.index;
|
|
1782
|
+
const last = this._managedResources.length - 1;
|
|
1783
|
+
if (index !== last) {
|
|
1784
|
+
const lastResource = this._managedResources[last];
|
|
1785
|
+
this._managedResources[index] = lastResource;
|
|
1786
|
+
lastResource._gcData.index = index;
|
|
1787
|
+
}
|
|
1788
|
+
this._managedResources.length--;
|
|
1789
|
+
resource._gcData = null;
|
|
1790
|
+
resource._gcLastUsed = -1;
|
|
1791
|
+
}
|
|
1792
|
+
/**
|
|
1793
|
+
* Registers a hash-based resource collection for garbage collection tracking.
|
|
1794
|
+
* Resources in the hash will be automatically tracked and cleaned up when unused.
|
|
1795
|
+
* @param context - The object containing the hash property
|
|
1796
|
+
* @param hash - The property name on context that holds the resource hash
|
|
1797
|
+
* @param type - The type of resources in the hash ('resource' or 'renderable')
|
|
1798
|
+
* @param priority - Processing priority (lower values are processed first)
|
|
1799
|
+
*/
|
|
1800
|
+
addResourceHash(context, hash, type, priority = 0) {
|
|
1801
|
+
this._managedResourceHashes.push({
|
|
1802
|
+
context,
|
|
1803
|
+
hash,
|
|
1804
|
+
type,
|
|
1805
|
+
priority
|
|
1806
|
+
});
|
|
1807
|
+
this._managedResourceHashes.sort((a, b) => a.priority - b.priority);
|
|
1808
|
+
}
|
|
1809
|
+
/**
|
|
1810
|
+
* Performs garbage collection by cleaning up unused resources.
|
|
1811
|
+
* Removes resources that haven't been used for longer than maxUnusedTime.
|
|
1812
|
+
*/
|
|
1813
|
+
run() {
|
|
1814
|
+
const now = performance.now();
|
|
1815
|
+
const managedResourceHashes = this._managedResourceHashes;
|
|
1816
|
+
for (const hashEntry of managedResourceHashes) {
|
|
1817
|
+
this.runOnHash(hashEntry, now);
|
|
1818
|
+
}
|
|
1819
|
+
let writeIndex = 0;
|
|
1820
|
+
for (let i = 0; i < this._managedResources.length; i++) {
|
|
1821
|
+
const resource = this._managedResources[i];
|
|
1822
|
+
writeIndex = this.runOnResource(resource, now, writeIndex);
|
|
1823
|
+
}
|
|
1824
|
+
this._managedResources.length = writeIndex;
|
|
1825
|
+
}
|
|
1826
|
+
updateRenderableGCTick(renderable, now) {
|
|
1827
|
+
const renderGroup = renderable.renderGroup ?? renderable.parentRenderGroup;
|
|
1828
|
+
const currentTick = renderGroup?.instructionSet?.gcTick ?? -1;
|
|
1829
|
+
if ((renderGroup?.gcTick ?? 0) === currentTick) {
|
|
1830
|
+
renderable._gcLastUsed = now;
|
|
1831
|
+
renderable._onTouch?.(now);
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
runOnResource(resource, now, writeIndex) {
|
|
1835
|
+
const gcData = resource._gcData;
|
|
1836
|
+
if (gcData.type === "renderable") {
|
|
1837
|
+
this.updateRenderableGCTick(resource, now);
|
|
1838
|
+
}
|
|
1839
|
+
const isRecentlyUsed = now - resource._gcLastUsed < this.maxUnusedTime;
|
|
1840
|
+
if (isRecentlyUsed || !resource.autoGarbageCollect) {
|
|
1841
|
+
this._managedResources[writeIndex] = resource;
|
|
1842
|
+
gcData.index = writeIndex;
|
|
1843
|
+
writeIndex++;
|
|
1844
|
+
} else {
|
|
1845
|
+
resource.unload();
|
|
1846
|
+
resource._gcData = null;
|
|
1847
|
+
resource._gcLastUsed = -1;
|
|
1848
|
+
resource.off("unload", this.removeResource, this);
|
|
1849
|
+
}
|
|
1850
|
+
return writeIndex;
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Creates a clone of the hash, copying all non-null entries up to (but not including) the stop key.
|
|
1854
|
+
* @param hashValue - The original hash to clone from
|
|
1855
|
+
* @param stopKey - The key to stop at (exclusive)
|
|
1856
|
+
* @returns A new hash object with copied entries
|
|
1857
|
+
*/
|
|
1858
|
+
_createHashClone(hashValue, stopKey) {
|
|
1859
|
+
const hashClone = /* @__PURE__ */ Object.create(null);
|
|
1860
|
+
for (const k in hashValue) {
|
|
1861
|
+
if (k === stopKey) break;
|
|
1862
|
+
if (hashValue[k] !== null) hashClone[k] = hashValue[k];
|
|
1863
|
+
}
|
|
1864
|
+
return hashClone;
|
|
1865
|
+
}
|
|
1866
|
+
runOnHash(hashEntry, now) {
|
|
1867
|
+
const { context, hash, type } = hashEntry;
|
|
1868
|
+
const hashValue = context[hash];
|
|
1869
|
+
let hashClone = null;
|
|
1870
|
+
let nullCount = 0;
|
|
1871
|
+
for (const key in hashValue) {
|
|
1872
|
+
const resource = hashValue[key];
|
|
1873
|
+
if (resource === null) {
|
|
1874
|
+
nullCount++;
|
|
1875
|
+
if (nullCount === 1e4 && !hashClone) {
|
|
1876
|
+
hashClone = this._createHashClone(hashValue, key);
|
|
1877
|
+
}
|
|
1878
|
+
continue;
|
|
1879
|
+
}
|
|
1880
|
+
if (resource._gcLastUsed === -1) {
|
|
1881
|
+
resource._gcLastUsed = now;
|
|
1882
|
+
resource._onTouch?.(now);
|
|
1883
|
+
if (hashClone) hashClone[key] = resource;
|
|
1884
|
+
continue;
|
|
1885
|
+
}
|
|
1886
|
+
if (type === "renderable") {
|
|
1887
|
+
this.updateRenderableGCTick(resource, now);
|
|
1888
|
+
}
|
|
1889
|
+
const isRecentlyUsed = now - resource._gcLastUsed < this.maxUnusedTime;
|
|
1890
|
+
if (!isRecentlyUsed && resource.autoGarbageCollect) {
|
|
1891
|
+
if (!hashClone) {
|
|
1892
|
+
if (nullCount + 1 !== 1e4) {
|
|
1893
|
+
hashValue[key] = null;
|
|
1894
|
+
nullCount++;
|
|
1895
|
+
} else {
|
|
1896
|
+
hashClone = this._createHashClone(hashValue, key);
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
if (type === "renderable") {
|
|
1900
|
+
const res = resource;
|
|
1901
|
+
const renderGroup = res.renderGroup ?? res.parentRenderGroup;
|
|
1902
|
+
if (renderGroup) renderGroup.structureDidChange = true;
|
|
1903
|
+
}
|
|
1904
|
+
resource.unload();
|
|
1905
|
+
resource._gcData = null;
|
|
1906
|
+
resource._gcLastUsed = -1;
|
|
1907
|
+
} else if (hashClone) {
|
|
1908
|
+
hashClone[key] = resource;
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
if (hashClone) {
|
|
1912
|
+
context[hash] = hashClone;
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
/** Cleans up the garbage collection system. Disables GC and removes all tracked resources. */
|
|
1916
|
+
destroy() {
|
|
1917
|
+
this.enabled = false;
|
|
1918
|
+
this._managedResources.forEach((resource) => {
|
|
1919
|
+
resource.off("unload", this.removeResource, this);
|
|
1920
|
+
});
|
|
1921
|
+
this._managedResources.length = 0;
|
|
1922
|
+
this._managedResourceHashes.length = 0;
|
|
1923
|
+
this._managedCollections.length = 0;
|
|
1924
|
+
this._renderer = null;
|
|
1925
|
+
}
|
|
1926
|
+
};
|
|
1927
|
+
_GCSystem.extension = {
|
|
1928
|
+
type: [
|
|
1929
|
+
ExtensionType.WebGLSystem,
|
|
1930
|
+
ExtensionType.WebGPUSystem,
|
|
1931
|
+
ExtensionType.CanvasSystem
|
|
1932
|
+
],
|
|
1933
|
+
name: "gc",
|
|
1934
|
+
priority: 0
|
|
1935
|
+
};
|
|
1936
|
+
_GCSystem.defaultOptions = {
|
|
1937
|
+
/** Enable/disable the garbage collector */
|
|
1938
|
+
gcActive: true,
|
|
1939
|
+
/** Time in ms before an unused resource is collected (default 1 minute) */
|
|
1940
|
+
gcMaxUnusedTime: 6e4,
|
|
1941
|
+
/** How often to run garbage collection in ms (default 30 seconds) */
|
|
1942
|
+
gcFrequency: 3e4
|
|
1943
|
+
};
|
|
1944
|
+
let GCSystem = _GCSystem;
|
|
1945
|
+
class GlobalUniformSystem {
|
|
1946
|
+
constructor(renderer) {
|
|
1947
|
+
this._stackIndex = 0;
|
|
1948
|
+
this._globalUniformDataStack = [];
|
|
1949
|
+
this._uniformsPool = [];
|
|
1950
|
+
this._activeUniforms = [];
|
|
1951
|
+
this._bindGroupPool = [];
|
|
1952
|
+
this._activeBindGroups = [];
|
|
1953
|
+
this._renderer = renderer;
|
|
1954
|
+
}
|
|
1955
|
+
reset() {
|
|
1956
|
+
this._stackIndex = 0;
|
|
1957
|
+
for (let i = 0; i < this._activeUniforms.length; i++) {
|
|
1958
|
+
this._uniformsPool.push(this._activeUniforms[i]);
|
|
1959
|
+
}
|
|
1960
|
+
for (let i = 0; i < this._activeBindGroups.length; i++) {
|
|
1961
|
+
this._bindGroupPool.push(this._activeBindGroups[i]);
|
|
1962
|
+
}
|
|
1963
|
+
this._activeUniforms.length = 0;
|
|
1964
|
+
this._activeBindGroups.length = 0;
|
|
1965
|
+
}
|
|
1966
|
+
start(options) {
|
|
1967
|
+
this.reset();
|
|
1968
|
+
this.push(options);
|
|
1969
|
+
}
|
|
1970
|
+
bind({
|
|
1971
|
+
size,
|
|
1972
|
+
projectionMatrix,
|
|
1973
|
+
worldTransformMatrix,
|
|
1974
|
+
worldColor,
|
|
1975
|
+
offset
|
|
1976
|
+
}) {
|
|
1977
|
+
const renderTarget = this._renderer.renderTarget.renderTarget;
|
|
1978
|
+
const currentGlobalUniformData = this._stackIndex ? this._globalUniformDataStack[this._stackIndex - 1] : {
|
|
1979
|
+
worldTransformMatrix: new Matrix(),
|
|
1980
|
+
worldColor: 4294967295,
|
|
1981
|
+
offset: new Point()
|
|
1982
|
+
};
|
|
1983
|
+
const globalUniformData = {
|
|
1984
|
+
projectionMatrix: projectionMatrix || this._renderer.renderTarget.projectionMatrix,
|
|
1985
|
+
resolution: size || renderTarget.size,
|
|
1986
|
+
worldTransformMatrix: worldTransformMatrix || currentGlobalUniformData.worldTransformMatrix,
|
|
1987
|
+
worldColor: worldColor || currentGlobalUniformData.worldColor,
|
|
1988
|
+
offset: offset || currentGlobalUniformData.offset,
|
|
1989
|
+
bindGroup: null
|
|
1990
|
+
};
|
|
1991
|
+
const uniformGroup = this._uniformsPool.pop() || this._createUniforms();
|
|
1992
|
+
this._activeUniforms.push(uniformGroup);
|
|
1993
|
+
const uniforms = uniformGroup.uniforms;
|
|
1994
|
+
uniforms.uProjectionMatrix = globalUniformData.projectionMatrix;
|
|
1995
|
+
uniforms.uResolution = globalUniformData.resolution;
|
|
1996
|
+
uniforms.uWorldTransformMatrix.copyFrom(globalUniformData.worldTransformMatrix);
|
|
1997
|
+
uniforms.uWorldTransformMatrix.tx -= globalUniformData.offset.x;
|
|
1998
|
+
uniforms.uWorldTransformMatrix.ty -= globalUniformData.offset.y;
|
|
1999
|
+
color32BitToUniform(
|
|
2000
|
+
globalUniformData.worldColor,
|
|
2001
|
+
uniforms.uWorldColorAlpha,
|
|
2002
|
+
0
|
|
2003
|
+
);
|
|
2004
|
+
uniformGroup.update();
|
|
2005
|
+
let bindGroup;
|
|
2006
|
+
if (this._renderer.renderPipes.uniformBatch) {
|
|
2007
|
+
bindGroup = this._renderer.renderPipes.uniformBatch.getUniformBindGroup(uniformGroup, false);
|
|
2008
|
+
} else {
|
|
2009
|
+
bindGroup = this._bindGroupPool.pop() || new BindGroup();
|
|
2010
|
+
this._activeBindGroups.push(bindGroup);
|
|
2011
|
+
bindGroup.setResource(uniformGroup, 0);
|
|
2012
|
+
}
|
|
2013
|
+
globalUniformData.bindGroup = bindGroup;
|
|
2014
|
+
this._currentGlobalUniformData = globalUniformData;
|
|
2015
|
+
}
|
|
2016
|
+
push(options) {
|
|
2017
|
+
this.bind(options);
|
|
2018
|
+
this._globalUniformDataStack[this._stackIndex++] = this._currentGlobalUniformData;
|
|
2019
|
+
}
|
|
2020
|
+
pop() {
|
|
2021
|
+
this._currentGlobalUniformData = this._globalUniformDataStack[--this._stackIndex - 1];
|
|
2022
|
+
if (this._renderer.type === RendererType.WEBGL) {
|
|
2023
|
+
this._currentGlobalUniformData.bindGroup.resources[0].update();
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
get bindGroup() {
|
|
2027
|
+
return this._currentGlobalUniformData.bindGroup;
|
|
2028
|
+
}
|
|
2029
|
+
get globalUniformData() {
|
|
2030
|
+
return this._currentGlobalUniformData;
|
|
2031
|
+
}
|
|
2032
|
+
get uniformGroup() {
|
|
2033
|
+
return this._currentGlobalUniformData.bindGroup.resources[0];
|
|
2034
|
+
}
|
|
2035
|
+
_createUniforms() {
|
|
2036
|
+
const globalUniforms = new UniformGroup({
|
|
2037
|
+
uProjectionMatrix: { value: new Matrix(), type: "mat3x3<f32>" },
|
|
2038
|
+
uWorldTransformMatrix: { value: new Matrix(), type: "mat3x3<f32>" },
|
|
2039
|
+
// TODO - someone smart - set this to be a unorm8x4 rather than a vec4<f32>
|
|
2040
|
+
uWorldColorAlpha: { value: new Float32Array(4), type: "vec4<f32>" },
|
|
2041
|
+
uResolution: { value: [0, 0], type: "vec2<f32>" }
|
|
2042
|
+
}, {
|
|
2043
|
+
isStatic: true
|
|
2044
|
+
});
|
|
2045
|
+
return globalUniforms;
|
|
2046
|
+
}
|
|
2047
|
+
destroy() {
|
|
2048
|
+
this._renderer = null;
|
|
2049
|
+
this._globalUniformDataStack.length = 0;
|
|
2050
|
+
this._uniformsPool.length = 0;
|
|
2051
|
+
this._activeUniforms.length = 0;
|
|
2052
|
+
this._bindGroupPool.length = 0;
|
|
2053
|
+
this._activeBindGroups.length = 0;
|
|
2054
|
+
this._currentGlobalUniformData = null;
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
GlobalUniformSystem.extension = {
|
|
2058
|
+
type: [
|
|
2059
|
+
ExtensionType.WebGLSystem,
|
|
2060
|
+
ExtensionType.WebGPUSystem,
|
|
2061
|
+
ExtensionType.CanvasSystem
|
|
2062
|
+
],
|
|
2063
|
+
name: "globalUniforms"
|
|
2064
|
+
};
|
|
2065
|
+
let uid = 1;
|
|
2066
|
+
class SchedulerSystem {
|
|
2067
|
+
constructor() {
|
|
2068
|
+
this._tasks = [];
|
|
2069
|
+
this._offset = 0;
|
|
2070
|
+
}
|
|
2071
|
+
/** Initializes the scheduler system and starts the ticker. */
|
|
2072
|
+
init() {
|
|
2073
|
+
Ticker.system.add(this._update, this);
|
|
2074
|
+
}
|
|
2075
|
+
/**
|
|
2076
|
+
* Schedules a repeating task.
|
|
2077
|
+
* @param func - The function to execute.
|
|
2078
|
+
* @param duration - The interval duration in milliseconds.
|
|
2079
|
+
* @param useOffset - this will spread out tasks so that they do not all run at the same time
|
|
2080
|
+
* @returns The unique identifier for the scheduled task.
|
|
2081
|
+
*/
|
|
2082
|
+
repeat(func, duration, useOffset = true) {
|
|
2083
|
+
const id = uid++;
|
|
2084
|
+
let offset = 0;
|
|
2085
|
+
if (useOffset) {
|
|
2086
|
+
this._offset += 1e3;
|
|
2087
|
+
offset = this._offset;
|
|
2088
|
+
}
|
|
2089
|
+
this._tasks.push({
|
|
2090
|
+
func,
|
|
2091
|
+
duration,
|
|
2092
|
+
start: performance.now(),
|
|
2093
|
+
offset,
|
|
2094
|
+
last: performance.now(),
|
|
2095
|
+
repeat: true,
|
|
2096
|
+
id
|
|
2097
|
+
});
|
|
2098
|
+
return id;
|
|
2099
|
+
}
|
|
2100
|
+
/**
|
|
2101
|
+
* Cancels a scheduled task.
|
|
2102
|
+
* @param id - The unique identifier of the task to cancel.
|
|
2103
|
+
*/
|
|
2104
|
+
cancel(id) {
|
|
2105
|
+
for (let i = 0; i < this._tasks.length; i++) {
|
|
2106
|
+
if (this._tasks[i].id === id) {
|
|
2107
|
+
this._tasks.splice(i, 1);
|
|
2108
|
+
return;
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
/**
|
|
2113
|
+
* Updates and executes the scheduled tasks.
|
|
2114
|
+
* @private
|
|
2115
|
+
*/
|
|
2116
|
+
_update() {
|
|
2117
|
+
const now = performance.now();
|
|
2118
|
+
for (let i = 0; i < this._tasks.length; i++) {
|
|
2119
|
+
const task = this._tasks[i];
|
|
2120
|
+
if (now - task.offset - task.last >= task.duration) {
|
|
2121
|
+
const elapsed = now - task.start;
|
|
2122
|
+
task.func(elapsed);
|
|
2123
|
+
task.last = now;
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
/**
|
|
2128
|
+
* Destroys the scheduler system and removes all tasks.
|
|
2129
|
+
* @internal
|
|
2130
|
+
*/
|
|
2131
|
+
destroy() {
|
|
2132
|
+
Ticker.system.remove(this._update, this);
|
|
2133
|
+
this._tasks.length = 0;
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
SchedulerSystem.extension = {
|
|
2137
|
+
type: [
|
|
2138
|
+
ExtensionType.WebGLSystem,
|
|
2139
|
+
ExtensionType.WebGPUSystem,
|
|
2140
|
+
ExtensionType.CanvasSystem
|
|
2141
|
+
],
|
|
2142
|
+
name: "scheduler",
|
|
2143
|
+
priority: 0
|
|
2144
|
+
};
|
|
2145
|
+
let saidHello = false;
|
|
2146
|
+
function sayHello(type) {
|
|
2147
|
+
if (saidHello) {
|
|
2148
|
+
return;
|
|
2149
|
+
}
|
|
2150
|
+
if (DOMAdapter.get().getNavigator().userAgent.toLowerCase().indexOf("chrome") > -1) {
|
|
2151
|
+
const args = [
|
|
2152
|
+
`%c %c %c %c %c PixiJS %c v${VERSION} (${type}) http://www.pixijs.com/
|
|
2153
|
+
|
|
2154
|
+
`,
|
|
2155
|
+
"background: #E72264; padding:5px 0;",
|
|
2156
|
+
"background: #6CA2EA; padding:5px 0;",
|
|
2157
|
+
"background: #B5D33D; padding:5px 0;",
|
|
2158
|
+
"background: #FED23F; padding:5px 0;",
|
|
2159
|
+
"color: #FFFFFF; background: #E72264; padding:5px 0;",
|
|
2160
|
+
"color: #E72264; background: #FFFFFF; padding:5px 0;"
|
|
2161
|
+
];
|
|
2162
|
+
globalThis.console.log(...args);
|
|
2163
|
+
} else if (globalThis.console) {
|
|
2164
|
+
globalThis.console.log(`PixiJS ${VERSION} - ${type} - http://www.pixijs.com/`);
|
|
2165
|
+
}
|
|
2166
|
+
saidHello = true;
|
|
2167
|
+
}
|
|
2168
|
+
class HelloSystem {
|
|
2169
|
+
constructor(renderer) {
|
|
2170
|
+
this._renderer = renderer;
|
|
2171
|
+
}
|
|
2172
|
+
/**
|
|
2173
|
+
* It all starts here! This initiates every system, passing in the options for any system by name.
|
|
2174
|
+
* @param options - the config for the renderer and all its systems
|
|
2175
|
+
*/
|
|
2176
|
+
init(options) {
|
|
2177
|
+
if (options.hello) {
|
|
2178
|
+
let name = this._renderer.name;
|
|
2179
|
+
if (this._renderer.type === RendererType.WEBGL) {
|
|
2180
|
+
name += ` ${this._renderer.context.webGLVersion}`;
|
|
2181
|
+
}
|
|
2182
|
+
sayHello(name);
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
HelloSystem.extension = {
|
|
2187
|
+
type: [
|
|
2188
|
+
ExtensionType.WebGLSystem,
|
|
2189
|
+
ExtensionType.WebGPUSystem,
|
|
2190
|
+
ExtensionType.CanvasSystem
|
|
2191
|
+
],
|
|
2192
|
+
name: "hello",
|
|
2193
|
+
priority: -2
|
|
2194
|
+
};
|
|
2195
|
+
HelloSystem.defaultOptions = {
|
|
2196
|
+
/** {@link WebGLOptions.hello} */
|
|
2197
|
+
hello: false
|
|
2198
|
+
};
|
|
2199
|
+
const _RenderableGCSystem = class _RenderableGCSystem2 {
|
|
2200
|
+
/**
|
|
2201
|
+
* Creates a new RenderableGCSystem instance.
|
|
2202
|
+
* @param renderer - The renderer this garbage collection system works for
|
|
2203
|
+
*/
|
|
2204
|
+
constructor(renderer) {
|
|
2205
|
+
this._renderer = renderer;
|
|
2206
|
+
}
|
|
2207
|
+
/**
|
|
2208
|
+
* Initializes the garbage collection system with the provided options.
|
|
2209
|
+
* @param options - Configuration options for the renderer
|
|
2210
|
+
*/
|
|
2211
|
+
init(options) {
|
|
2212
|
+
options = { ..._RenderableGCSystem2.defaultOptions, ...options };
|
|
2213
|
+
this.maxUnusedTime = options.renderableGCMaxUnusedTime;
|
|
2214
|
+
}
|
|
2215
|
+
/**
|
|
2216
|
+
* Gets whether the garbage collection system is currently enabled.
|
|
2217
|
+
* @returns True if GC is enabled, false otherwise
|
|
2218
|
+
*/
|
|
2219
|
+
get enabled() {
|
|
2220
|
+
deprecation("8.15.0", "RenderableGCSystem.enabled is deprecated, please use the GCSystem.enabled instead.");
|
|
2221
|
+
return this._renderer.gc.enabled;
|
|
2222
|
+
}
|
|
2223
|
+
/**
|
|
2224
|
+
* Enables or disables the garbage collection system.
|
|
2225
|
+
* When enabled, schedules periodic cleanup of resources.
|
|
2226
|
+
* When disabled, cancels all scheduled cleanups.
|
|
2227
|
+
*/
|
|
2228
|
+
set enabled(value) {
|
|
2229
|
+
deprecation("8.15.0", "RenderableGCSystem.enabled is deprecated, please use the GCSystem.enabled instead.");
|
|
2230
|
+
this._renderer.gc.enabled = value;
|
|
2231
|
+
}
|
|
2232
|
+
/**
|
|
2233
|
+
* Adds a hash table to be managed by the garbage collector.
|
|
2234
|
+
* @param context - The object containing the hash table
|
|
2235
|
+
* @param hash - The property name of the hash table
|
|
2236
|
+
*/
|
|
2237
|
+
addManagedHash(context, hash) {
|
|
2238
|
+
deprecation("8.15.0", "RenderableGCSystem.addManagedHash is deprecated, please use the GCSystem.addCollection instead.");
|
|
2239
|
+
this._renderer.gc.addCollection(context, hash, "hash");
|
|
2240
|
+
}
|
|
2241
|
+
/**
|
|
2242
|
+
* Adds an array to be managed by the garbage collector.
|
|
2243
|
+
* @param context - The object containing the array
|
|
2244
|
+
* @param hash - The property name of the array
|
|
2245
|
+
*/
|
|
2246
|
+
addManagedArray(context, hash) {
|
|
2247
|
+
deprecation("8.15.0", "RenderableGCSystem.addManagedArray is deprecated, please use the GCSystem.addCollection instead.");
|
|
2248
|
+
this._renderer.gc.addCollection(context, hash, "array");
|
|
2249
|
+
}
|
|
2250
|
+
/**
|
|
2251
|
+
* Starts tracking a renderable for garbage collection.
|
|
2252
|
+
* @param _renderable - The renderable to track
|
|
2253
|
+
* @deprecated since 8.15.0
|
|
2254
|
+
*/
|
|
2255
|
+
addRenderable(_renderable) {
|
|
2256
|
+
deprecation("8.15.0", "RenderableGCSystem.addRenderable is deprecated, please use the GCSystem instead.");
|
|
2257
|
+
this._renderer.gc.addResource(_renderable, "renderable");
|
|
2258
|
+
}
|
|
2259
|
+
/**
|
|
2260
|
+
* Performs garbage collection by cleaning up unused renderables.
|
|
2261
|
+
* Removes renderables that haven't been used for longer than maxUnusedTime.
|
|
2262
|
+
*/
|
|
2263
|
+
run() {
|
|
2264
|
+
deprecation("8.15.0", "RenderableGCSystem.run is deprecated, please use the GCSystem instead.");
|
|
2265
|
+
this._renderer.gc.run();
|
|
2266
|
+
}
|
|
2267
|
+
/** Cleans up the garbage collection system. Disables GC and removes all tracked resources. */
|
|
2268
|
+
destroy() {
|
|
2269
|
+
this._renderer = null;
|
|
2270
|
+
}
|
|
2271
|
+
};
|
|
2272
|
+
_RenderableGCSystem.extension = {
|
|
2273
|
+
type: [
|
|
2274
|
+
ExtensionType.WebGLSystem,
|
|
2275
|
+
ExtensionType.WebGPUSystem,
|
|
2276
|
+
ExtensionType.CanvasSystem
|
|
2277
|
+
],
|
|
2278
|
+
name: "renderableGC",
|
|
2279
|
+
priority: 0
|
|
2280
|
+
};
|
|
2281
|
+
_RenderableGCSystem.defaultOptions = {
|
|
2282
|
+
/** Enable/disable the garbage collector */
|
|
2283
|
+
renderableGCActive: true,
|
|
2284
|
+
/** Time in ms before an unused resource is collected (default 1 minute) */
|
|
2285
|
+
renderableGCMaxUnusedTime: 6e4,
|
|
2286
|
+
/** How often to run garbage collection in ms (default 30 seconds) */
|
|
2287
|
+
renderableGCFrequency: 3e4
|
|
2288
|
+
};
|
|
2289
|
+
let RenderableGCSystem = _RenderableGCSystem;
|
|
2290
|
+
const _TextureGCSystem = class _TextureGCSystem2 {
|
|
2291
|
+
/**
|
|
2292
|
+
* Frame count since started.
|
|
2293
|
+
* @readonly
|
|
2294
|
+
* @deprecated since 8.15.0
|
|
2295
|
+
*/
|
|
2296
|
+
get count() {
|
|
2297
|
+
return this._renderer.tick;
|
|
2298
|
+
}
|
|
2299
|
+
/**
|
|
2300
|
+
* Frame count since last garbage collection.
|
|
2301
|
+
* @readonly
|
|
2302
|
+
* @deprecated since 8.15.0
|
|
2303
|
+
*/
|
|
2304
|
+
get checkCount() {
|
|
2305
|
+
return this._checkCount;
|
|
2306
|
+
}
|
|
2307
|
+
set checkCount(value) {
|
|
2308
|
+
deprecation("8.15.0", "TextureGCSystem.run is deprecated, please use the GCSystem instead.");
|
|
2309
|
+
this._checkCount = value;
|
|
2310
|
+
}
|
|
2311
|
+
/**
|
|
2312
|
+
* Maximum idle frames before a texture is destroyed by garbage collection.
|
|
2313
|
+
* @see TextureGCSystem.defaultMaxIdle
|
|
2314
|
+
* @deprecated since 8.15.0
|
|
2315
|
+
*/
|
|
2316
|
+
get maxIdle() {
|
|
2317
|
+
return this._renderer.gc.maxUnusedTime / 1e3 * 60;
|
|
2318
|
+
}
|
|
2319
|
+
set maxIdle(value) {
|
|
2320
|
+
deprecation("8.15.0", "TextureGCSystem.run is deprecated, please use the GCSystem instead.");
|
|
2321
|
+
this._renderer.gc.maxUnusedTime = value / 60 * 1e3;
|
|
2322
|
+
}
|
|
2323
|
+
/**
|
|
2324
|
+
* Frames between two garbage collections.
|
|
2325
|
+
* @see TextureGCSystem.defaultCheckCountMax
|
|
2326
|
+
* @deprecated since 8.15.0
|
|
2327
|
+
*/
|
|
2328
|
+
// eslint-disable-next-line dot-notation
|
|
2329
|
+
get checkCountMax() {
|
|
2330
|
+
return Math.floor(this._renderer.gc["_frequency"] / 1e3);
|
|
2331
|
+
}
|
|
2332
|
+
set checkCountMax(_value) {
|
|
2333
|
+
deprecation("8.15.0", "TextureGCSystem.run is deprecated, please use the GCSystem instead.");
|
|
2334
|
+
}
|
|
2335
|
+
/**
|
|
2336
|
+
* Current garbage collection mode.
|
|
2337
|
+
* @see TextureGCSystem.defaultMode
|
|
2338
|
+
* @deprecated since 8.15.0
|
|
2339
|
+
*/
|
|
2340
|
+
get active() {
|
|
2341
|
+
return this._renderer.gc.enabled;
|
|
2342
|
+
}
|
|
2343
|
+
set active(value) {
|
|
2344
|
+
deprecation("8.15.0", "TextureGCSystem.run is deprecated, please use the GCSystem instead.");
|
|
2345
|
+
this._renderer.gc.enabled = value;
|
|
2346
|
+
}
|
|
2347
|
+
/** @param renderer - The renderer this System works for. */
|
|
2348
|
+
constructor(renderer) {
|
|
2349
|
+
this._renderer = renderer;
|
|
2350
|
+
this._checkCount = 0;
|
|
2351
|
+
}
|
|
2352
|
+
init(options) {
|
|
2353
|
+
if (options.textureGCActive !== _TextureGCSystem2.defaultOptions.textureGCActive) {
|
|
2354
|
+
this.active = options.textureGCActive;
|
|
2355
|
+
}
|
|
2356
|
+
if (options.textureGCMaxIdle !== _TextureGCSystem2.defaultOptions.textureGCMaxIdle) {
|
|
2357
|
+
this.maxIdle = options.textureGCMaxIdle;
|
|
2358
|
+
}
|
|
2359
|
+
if (options.textureGCCheckCountMax !== _TextureGCSystem2.defaultOptions.textureGCCheckCountMax) {
|
|
2360
|
+
this.checkCountMax = options.textureGCCheckCountMax;
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
/**
|
|
2364
|
+
* Checks to see when the last time a texture was used.
|
|
2365
|
+
* If the texture has not been used for a specified amount of time, it will be removed from the GPU.
|
|
2366
|
+
* @deprecated since 8.15.0
|
|
2367
|
+
*/
|
|
2368
|
+
run() {
|
|
2369
|
+
deprecation("8.15.0", "TextureGCSystem.run is deprecated, please use the GCSystem instead.");
|
|
2370
|
+
this._renderer.gc.run();
|
|
2371
|
+
}
|
|
2372
|
+
destroy() {
|
|
2373
|
+
this._renderer = null;
|
|
2374
|
+
}
|
|
2375
|
+
};
|
|
2376
|
+
_TextureGCSystem.extension = {
|
|
2377
|
+
type: [
|
|
2378
|
+
ExtensionType.WebGLSystem,
|
|
2379
|
+
ExtensionType.WebGPUSystem
|
|
2380
|
+
],
|
|
2381
|
+
name: "textureGC"
|
|
2382
|
+
};
|
|
2383
|
+
_TextureGCSystem.defaultOptions = {
|
|
2384
|
+
/**
|
|
2385
|
+
* If set to true, this will enable the garbage collector on the GPU.
|
|
2386
|
+
* @default true
|
|
2387
|
+
*/
|
|
2388
|
+
textureGCActive: true,
|
|
2389
|
+
/**
|
|
2390
|
+
* @deprecated since 8.3.0
|
|
2391
|
+
* @see {@link TextureGCSystemOptions.textureGCMaxIdle}
|
|
2392
|
+
*/
|
|
2393
|
+
textureGCAMaxIdle: null,
|
|
2394
|
+
/**
|
|
2395
|
+
* The maximum idle frames before a texture is destroyed by garbage collection.
|
|
2396
|
+
* @default 60 * 60
|
|
2397
|
+
*/
|
|
2398
|
+
textureGCMaxIdle: 60 * 60,
|
|
2399
|
+
/**
|
|
2400
|
+
* Frames between two garbage collections.
|
|
2401
|
+
* @default 600
|
|
2402
|
+
*/
|
|
2403
|
+
textureGCCheckCountMax: 600
|
|
2404
|
+
};
|
|
2405
|
+
let TextureGCSystem = _TextureGCSystem;
|
|
2406
|
+
const _RenderTarget = class _RenderTarget2 {
|
|
2407
|
+
/**
|
|
2408
|
+
* @param [descriptor] - Options for creating a render target.
|
|
2409
|
+
*/
|
|
2410
|
+
constructor(descriptor = {}) {
|
|
2411
|
+
this.uid = uid$1("renderTarget");
|
|
2412
|
+
this.colorTextures = [];
|
|
2413
|
+
this.dirtyId = 0;
|
|
2414
|
+
this.isRoot = false;
|
|
2415
|
+
this._size = new Float32Array(2);
|
|
2416
|
+
this._managedColorTextures = false;
|
|
2417
|
+
descriptor = { ..._RenderTarget2.defaultOptions, ...descriptor };
|
|
2418
|
+
this.stencil = descriptor.stencil;
|
|
2419
|
+
this.depth = descriptor.depth;
|
|
2420
|
+
this.isRoot = descriptor.isRoot;
|
|
2421
|
+
if (typeof descriptor.colorTextures === "number") {
|
|
2422
|
+
this._managedColorTextures = true;
|
|
2423
|
+
for (let i = 0; i < descriptor.colorTextures; i++) {
|
|
2424
|
+
this.colorTextures.push(
|
|
2425
|
+
new TextureSource({
|
|
2426
|
+
width: descriptor.width,
|
|
2427
|
+
height: descriptor.height,
|
|
2428
|
+
resolution: descriptor.resolution,
|
|
2429
|
+
antialias: descriptor.antialias
|
|
2430
|
+
})
|
|
2431
|
+
);
|
|
2432
|
+
}
|
|
2433
|
+
} else {
|
|
2434
|
+
this.colorTextures = [...descriptor.colorTextures.map((texture) => texture.source)];
|
|
2435
|
+
const colorSource = this.colorTexture.source;
|
|
2436
|
+
this.resize(colorSource.width, colorSource.height, colorSource._resolution);
|
|
2437
|
+
}
|
|
2438
|
+
this.colorTexture.source.on("resize", this.onSourceResize, this);
|
|
2439
|
+
if (descriptor.depthStencilTexture || this.stencil) {
|
|
2440
|
+
if (descriptor.depthStencilTexture instanceof Texture || descriptor.depthStencilTexture instanceof TextureSource) {
|
|
2441
|
+
this.depthStencilTexture = descriptor.depthStencilTexture.source;
|
|
2442
|
+
} else {
|
|
2443
|
+
this.ensureDepthStencilTexture();
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
get size() {
|
|
2448
|
+
const _size = this._size;
|
|
2449
|
+
_size[0] = this.pixelWidth;
|
|
2450
|
+
_size[1] = this.pixelHeight;
|
|
2451
|
+
return _size;
|
|
2452
|
+
}
|
|
2453
|
+
get width() {
|
|
2454
|
+
return this.colorTexture.source.width;
|
|
2455
|
+
}
|
|
2456
|
+
get height() {
|
|
2457
|
+
return this.colorTexture.source.height;
|
|
2458
|
+
}
|
|
2459
|
+
get pixelWidth() {
|
|
2460
|
+
return this.colorTexture.source.pixelWidth;
|
|
2461
|
+
}
|
|
2462
|
+
get pixelHeight() {
|
|
2463
|
+
return this.colorTexture.source.pixelHeight;
|
|
2464
|
+
}
|
|
2465
|
+
get resolution() {
|
|
2466
|
+
return this.colorTexture.source._resolution;
|
|
2467
|
+
}
|
|
2468
|
+
get colorTexture() {
|
|
2469
|
+
return this.colorTextures[0];
|
|
2470
|
+
}
|
|
2471
|
+
onSourceResize(source2) {
|
|
2472
|
+
this.resize(source2.width, source2.height, source2._resolution, true);
|
|
2473
|
+
}
|
|
2474
|
+
/**
|
|
2475
|
+
* This will ensure a depthStencil texture is created for this render target.
|
|
2476
|
+
* Most likely called by the mask system to make sure we have stencil buffer added.
|
|
2477
|
+
* @internal
|
|
2478
|
+
*/
|
|
2479
|
+
ensureDepthStencilTexture() {
|
|
2480
|
+
if (!this.depthStencilTexture) {
|
|
2481
|
+
this.depthStencilTexture = new TextureSource({
|
|
2482
|
+
width: this.width,
|
|
2483
|
+
height: this.height,
|
|
2484
|
+
resolution: this.resolution,
|
|
2485
|
+
format: "depth24plus-stencil8",
|
|
2486
|
+
autoGenerateMipmaps: false,
|
|
2487
|
+
antialias: false,
|
|
2488
|
+
mipLevelCount: 1
|
|
2489
|
+
// sampleCount: handled by the render target system..
|
|
2490
|
+
});
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
resize(width, height, resolution = this.resolution, skipColorTexture = false) {
|
|
2494
|
+
this.dirtyId++;
|
|
2495
|
+
this.colorTextures.forEach((colorTexture, i) => {
|
|
2496
|
+
if (skipColorTexture && i === 0) return;
|
|
2497
|
+
colorTexture.source.resize(width, height, resolution);
|
|
2498
|
+
});
|
|
2499
|
+
if (this.depthStencilTexture) {
|
|
2500
|
+
this.depthStencilTexture.source.resize(width, height, resolution);
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
destroy() {
|
|
2504
|
+
this.colorTexture.source.off("resize", this.onSourceResize, this);
|
|
2505
|
+
if (this._managedColorTextures) {
|
|
2506
|
+
this.colorTextures.forEach((texture) => {
|
|
2507
|
+
texture.destroy();
|
|
2508
|
+
});
|
|
2509
|
+
}
|
|
2510
|
+
if (this.depthStencilTexture) {
|
|
2511
|
+
this.depthStencilTexture.destroy();
|
|
2512
|
+
delete this.depthStencilTexture;
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
};
|
|
2516
|
+
_RenderTarget.defaultOptions = {
|
|
2517
|
+
/** the width of the RenderTarget */
|
|
2518
|
+
width: 0,
|
|
2519
|
+
/** the height of the RenderTarget */
|
|
2520
|
+
height: 0,
|
|
2521
|
+
/** the resolution of the RenderTarget */
|
|
2522
|
+
resolution: 1,
|
|
2523
|
+
/** an array of textures, or a number indicating how many color textures there should be */
|
|
2524
|
+
colorTextures: 1,
|
|
2525
|
+
/** should this render target have a stencil buffer? */
|
|
2526
|
+
stencil: false,
|
|
2527
|
+
/** should this render target have a depth buffer? */
|
|
2528
|
+
depth: false,
|
|
2529
|
+
/** should this render target be antialiased? */
|
|
2530
|
+
antialias: false,
|
|
2531
|
+
// save on perf by default!
|
|
2532
|
+
/** is this a root element, true if this is gl context owners render target */
|
|
2533
|
+
isRoot: false
|
|
2534
|
+
};
|
|
2535
|
+
let RenderTarget = _RenderTarget;
|
|
2536
|
+
const canvasCache = /* @__PURE__ */ new Map();
|
|
2537
|
+
GlobalResourceRegistry.register(canvasCache);
|
|
2538
|
+
function getCanvasTexture(canvas, options) {
|
|
2539
|
+
if (!canvasCache.has(canvas)) {
|
|
2540
|
+
const texture = new Texture({
|
|
2541
|
+
source: new CanvasSource({
|
|
2542
|
+
resource: canvas,
|
|
2543
|
+
...options
|
|
2544
|
+
})
|
|
2545
|
+
});
|
|
2546
|
+
const onDestroy = () => {
|
|
2547
|
+
if (canvasCache.get(canvas) === texture) {
|
|
2548
|
+
canvasCache.delete(canvas);
|
|
2549
|
+
}
|
|
2550
|
+
};
|
|
2551
|
+
texture.once("destroy", onDestroy);
|
|
2552
|
+
texture.source.once("destroy", onDestroy);
|
|
2553
|
+
canvasCache.set(canvas, texture);
|
|
2554
|
+
}
|
|
2555
|
+
return canvasCache.get(canvas);
|
|
2556
|
+
}
|
|
2557
|
+
const _ViewSystem = class _ViewSystem2 {
|
|
2558
|
+
/**
|
|
2559
|
+
* Whether CSS dimensions of canvas view should be resized to screen dimensions automatically.
|
|
2560
|
+
* This is only supported for HTMLCanvasElement and will be ignored if the canvas is an OffscreenCanvas.
|
|
2561
|
+
* @type {boolean}
|
|
2562
|
+
*/
|
|
2563
|
+
get autoDensity() {
|
|
2564
|
+
return this.texture.source.autoDensity;
|
|
2565
|
+
}
|
|
2566
|
+
set autoDensity(value) {
|
|
2567
|
+
this.texture.source.autoDensity = value;
|
|
2568
|
+
}
|
|
2569
|
+
/** The resolution / device pixel ratio of the renderer. */
|
|
2570
|
+
get resolution() {
|
|
2571
|
+
return this.texture.source._resolution;
|
|
2572
|
+
}
|
|
2573
|
+
set resolution(value) {
|
|
2574
|
+
this.texture.source.resize(
|
|
2575
|
+
this.texture.source.width,
|
|
2576
|
+
this.texture.source.height,
|
|
2577
|
+
value
|
|
2578
|
+
);
|
|
2579
|
+
}
|
|
2580
|
+
/**
|
|
2581
|
+
* initiates the view system
|
|
2582
|
+
* @param options - the options for the view
|
|
2583
|
+
*/
|
|
2584
|
+
init(options) {
|
|
2585
|
+
options = {
|
|
2586
|
+
..._ViewSystem2.defaultOptions,
|
|
2587
|
+
...options
|
|
2588
|
+
};
|
|
2589
|
+
if (options.view) {
|
|
2590
|
+
deprecation(v8_0_0, "ViewSystem.view has been renamed to ViewSystem.canvas");
|
|
2591
|
+
options.canvas = options.view;
|
|
2592
|
+
}
|
|
2593
|
+
this.screen = new Rectangle(0, 0, options.width, options.height);
|
|
2594
|
+
this.canvas = options.canvas || DOMAdapter.get().createCanvas();
|
|
2595
|
+
this.antialias = !!options.antialias;
|
|
2596
|
+
this.texture = getCanvasTexture(this.canvas, options);
|
|
2597
|
+
this.renderTarget = new RenderTarget({
|
|
2598
|
+
colorTextures: [this.texture],
|
|
2599
|
+
depth: !!options.depth,
|
|
2600
|
+
isRoot: true
|
|
2601
|
+
});
|
|
2602
|
+
this.texture.source.transparent = options.backgroundAlpha < 1;
|
|
2603
|
+
this.resolution = options.resolution;
|
|
2604
|
+
}
|
|
2605
|
+
/**
|
|
2606
|
+
* Resizes the screen and canvas to the specified dimensions.
|
|
2607
|
+
* @param desiredScreenWidth - The new width of the screen.
|
|
2608
|
+
* @param desiredScreenHeight - The new height of the screen.
|
|
2609
|
+
* @param resolution
|
|
2610
|
+
*/
|
|
2611
|
+
resize(desiredScreenWidth, desiredScreenHeight, resolution) {
|
|
2612
|
+
this.texture.source.resize(desiredScreenWidth, desiredScreenHeight, resolution);
|
|
2613
|
+
this.screen.width = this.texture.frame.width;
|
|
2614
|
+
this.screen.height = this.texture.frame.height;
|
|
2615
|
+
}
|
|
2616
|
+
/**
|
|
2617
|
+
* Destroys this System and optionally removes the canvas from the dom.
|
|
2618
|
+
* @param {options | false} options - The options for destroying the view, or "false".
|
|
2619
|
+
* @example
|
|
2620
|
+
* viewSystem.destroy();
|
|
2621
|
+
* viewSystem.destroy(true);
|
|
2622
|
+
* viewSystem.destroy({ removeView: true });
|
|
2623
|
+
*/
|
|
2624
|
+
destroy(options = false) {
|
|
2625
|
+
const removeView = typeof options === "boolean" ? options : !!options?.removeView;
|
|
2626
|
+
if (removeView && this.canvas.parentNode) {
|
|
2627
|
+
this.canvas.parentNode.removeChild(this.canvas);
|
|
2628
|
+
}
|
|
2629
|
+
this.texture.destroy();
|
|
2630
|
+
}
|
|
2631
|
+
};
|
|
2632
|
+
_ViewSystem.extension = {
|
|
2633
|
+
type: [
|
|
2634
|
+
ExtensionType.WebGLSystem,
|
|
2635
|
+
ExtensionType.WebGPUSystem,
|
|
2636
|
+
ExtensionType.CanvasSystem
|
|
2637
|
+
],
|
|
2638
|
+
name: "view",
|
|
2639
|
+
priority: 0
|
|
2640
|
+
};
|
|
2641
|
+
_ViewSystem.defaultOptions = {
|
|
2642
|
+
/**
|
|
2643
|
+
* {@link WebGLOptions.width}
|
|
2644
|
+
* @default 800
|
|
2645
|
+
*/
|
|
2646
|
+
width: 800,
|
|
2647
|
+
/**
|
|
2648
|
+
* {@link WebGLOptions.height}
|
|
2649
|
+
* @default 600
|
|
2650
|
+
*/
|
|
2651
|
+
height: 600,
|
|
2652
|
+
/**
|
|
2653
|
+
* {@link WebGLOptions.autoDensity}
|
|
2654
|
+
* @default false
|
|
2655
|
+
*/
|
|
2656
|
+
autoDensity: false,
|
|
2657
|
+
/**
|
|
2658
|
+
* {@link WebGLOptions.antialias}
|
|
2659
|
+
* @default false
|
|
2660
|
+
*/
|
|
2661
|
+
antialias: false
|
|
2662
|
+
};
|
|
2663
|
+
let ViewSystem = _ViewSystem;
|
|
2664
|
+
const SharedSystems = [
|
|
2665
|
+
BackgroundSystem,
|
|
2666
|
+
GlobalUniformSystem,
|
|
2667
|
+
HelloSystem,
|
|
2668
|
+
ViewSystem,
|
|
2669
|
+
RenderGroupSystem,
|
|
2670
|
+
GCSystem,
|
|
2671
|
+
TextureGCSystem,
|
|
2672
|
+
GenerateTextureSystem,
|
|
2673
|
+
ExtractSystem,
|
|
2674
|
+
RendererInitHook,
|
|
2675
|
+
RenderableGCSystem,
|
|
2676
|
+
SchedulerSystem
|
|
2677
|
+
];
|
|
2678
|
+
const SharedRenderPipes = [
|
|
2679
|
+
BlendModePipe,
|
|
2680
|
+
BatcherPipe,
|
|
2681
|
+
SpritePipe,
|
|
2682
|
+
RenderGroupPipe,
|
|
2683
|
+
AlphaMaskPipe,
|
|
2684
|
+
StencilMaskPipe,
|
|
2685
|
+
ColorMaskPipe,
|
|
2686
|
+
CustomRenderPipe
|
|
2687
|
+
];
|
|
2688
|
+
function calculateProjection(pm, x, y, width, height, flipY) {
|
|
2689
|
+
const sign = flipY ? 1 : -1;
|
|
2690
|
+
pm.identity();
|
|
2691
|
+
pm.a = 1 / width * 2;
|
|
2692
|
+
pm.d = sign * (1 / height * 2);
|
|
2693
|
+
pm.tx = -1 - x * pm.a;
|
|
2694
|
+
pm.ty = -sign - y * pm.d;
|
|
2695
|
+
return pm;
|
|
2696
|
+
}
|
|
2697
|
+
function isRenderingToScreen(renderTarget) {
|
|
2698
|
+
const resource = renderTarget.colorTexture.source.resource;
|
|
2699
|
+
return globalThis.HTMLCanvasElement && resource instanceof HTMLCanvasElement && document.body.contains(resource);
|
|
2700
|
+
}
|
|
2701
|
+
class RenderTargetSystem {
|
|
2702
|
+
constructor(renderer) {
|
|
2703
|
+
this.rootViewPort = new Rectangle();
|
|
2704
|
+
this.viewport = new Rectangle();
|
|
2705
|
+
this.mipLevel = 0;
|
|
2706
|
+
this.layer = 0;
|
|
2707
|
+
this.onRenderTargetChange = new SystemRunner("onRenderTargetChange");
|
|
2708
|
+
this.projectionMatrix = new Matrix();
|
|
2709
|
+
this.defaultClearColor = [0, 0, 0, 0];
|
|
2710
|
+
this._renderSurfaceToRenderTargetHash = /* @__PURE__ */ new Map();
|
|
2711
|
+
this._gpuRenderTargetHash = /* @__PURE__ */ Object.create(null);
|
|
2712
|
+
this._renderTargetStack = [];
|
|
2713
|
+
this._renderer = renderer;
|
|
2714
|
+
renderer.gc.addCollection(this, "_gpuRenderTargetHash", "hash");
|
|
2715
|
+
}
|
|
2716
|
+
/** called when dev wants to finish a render pass */
|
|
2717
|
+
finishRenderPass() {
|
|
2718
|
+
this.adaptor.finishRenderPass(this.renderTarget);
|
|
2719
|
+
}
|
|
2720
|
+
/**
|
|
2721
|
+
* called when the renderer starts to render a scene.
|
|
2722
|
+
* @param options
|
|
2723
|
+
* @param options.target - the render target to render to
|
|
2724
|
+
* @param options.clear - the clear mode to use. Can be true or a CLEAR number 'COLOR | DEPTH | STENCIL' 0b111
|
|
2725
|
+
* @param options.clearColor - the color to clear to
|
|
2726
|
+
* @param options.frame - the frame to render to
|
|
2727
|
+
* @param options.mipLevel - the mip level to render to
|
|
2728
|
+
* @param options.layer - The layer of the render target to render to. Used for array or 3D textures, or when rendering
|
|
2729
|
+
* to a specific layer of a layered render target. Optional.
|
|
2730
|
+
*/
|
|
2731
|
+
renderStart({
|
|
2732
|
+
target,
|
|
2733
|
+
clear,
|
|
2734
|
+
clearColor,
|
|
2735
|
+
frame,
|
|
2736
|
+
mipLevel,
|
|
2737
|
+
layer
|
|
2738
|
+
}) {
|
|
2739
|
+
this._renderTargetStack.length = 0;
|
|
2740
|
+
this.push(
|
|
2741
|
+
target,
|
|
2742
|
+
clear,
|
|
2743
|
+
clearColor,
|
|
2744
|
+
frame,
|
|
2745
|
+
mipLevel ?? 0,
|
|
2746
|
+
layer ?? 0
|
|
2747
|
+
);
|
|
2748
|
+
this.rootViewPort.copyFrom(this.viewport);
|
|
2749
|
+
this.rootRenderTarget = this.renderTarget;
|
|
2750
|
+
this.renderingToScreen = isRenderingToScreen(this.rootRenderTarget);
|
|
2751
|
+
this.adaptor.prerender?.(this.rootRenderTarget);
|
|
2752
|
+
}
|
|
2753
|
+
postrender() {
|
|
2754
|
+
this.adaptor.postrender?.(this.rootRenderTarget);
|
|
2755
|
+
}
|
|
2756
|
+
/**
|
|
2757
|
+
* Binding a render surface! This is the main function of the render target system.
|
|
2758
|
+
* It will take the RenderSurface (which can be a texture, canvas, or render target) and bind it to the renderer.
|
|
2759
|
+
* Once bound all draw calls will be rendered to the render surface.
|
|
2760
|
+
*
|
|
2761
|
+
* If a frame is not provided and the render surface is a {@link Texture}, the frame of the texture will be used.
|
|
2762
|
+
*
|
|
2763
|
+
* IMPORTANT:
|
|
2764
|
+
* - `frame` is treated as **base mip (mip 0) pixel space**.
|
|
2765
|
+
* - When `mipLevel > 0`, the viewport derived from `frame` is scaled by \(2^{mipLevel}\) and clamped to the
|
|
2766
|
+
* mip dimensions. This keeps "render the same region" semantics consistent across mip levels.
|
|
2767
|
+
* - When `renderSurface` is a {@link Texture}, `renderer.render({ container, target: texture, mipLevel })` will
|
|
2768
|
+
* render into
|
|
2769
|
+
* the underlying {@link TextureSource} (Pixi will create/use a {@link RenderTarget} for the source) using the
|
|
2770
|
+
* texture's frame to define the region (in mip 0 space).
|
|
2771
|
+
* @param renderSurface - the render surface to bind
|
|
2772
|
+
* @param clear - the clear mode to use. Can be true or a CLEAR number 'COLOR | DEPTH | STENCIL' 0b111
|
|
2773
|
+
* @param clearColor - the color to clear to
|
|
2774
|
+
* @param frame - the frame to render to
|
|
2775
|
+
* @param mipLevel - the mip level to render to
|
|
2776
|
+
* @param layer - the layer (or slice) of the render surface to render to. For array textures,
|
|
2777
|
+
* 3D textures, or cubemaps, this specifies the target layer or face. Defaults to 0 (the first layer/face).
|
|
2778
|
+
* Ignored for surfaces that do not support layers.
|
|
2779
|
+
* @returns the render target that was bound
|
|
2780
|
+
*/
|
|
2781
|
+
bind(renderSurface, clear = true, clearColor, frame, mipLevel = 0, layer = 0) {
|
|
2782
|
+
const renderTarget = this.getRenderTarget(renderSurface);
|
|
2783
|
+
const didChange = this.renderTarget !== renderTarget;
|
|
2784
|
+
this.renderTarget = renderTarget;
|
|
2785
|
+
this.renderSurface = renderSurface;
|
|
2786
|
+
const gpuRenderTarget = this.getGpuRenderTarget(renderTarget);
|
|
2787
|
+
if (renderTarget.pixelWidth !== gpuRenderTarget.width || renderTarget.pixelHeight !== gpuRenderTarget.height) {
|
|
2788
|
+
this.adaptor.resizeGpuRenderTarget(renderTarget);
|
|
2789
|
+
gpuRenderTarget.width = renderTarget.pixelWidth;
|
|
2790
|
+
gpuRenderTarget.height = renderTarget.pixelHeight;
|
|
2791
|
+
}
|
|
2792
|
+
const source2 = renderTarget.colorTexture;
|
|
2793
|
+
const viewport = this.viewport;
|
|
2794
|
+
const arrayLayerCount = source2.arrayLayerCount || 1;
|
|
2795
|
+
if ((layer | 0) !== layer) {
|
|
2796
|
+
layer |= 0;
|
|
2797
|
+
}
|
|
2798
|
+
if (layer < 0 || layer >= arrayLayerCount) {
|
|
2799
|
+
throw new Error(`[RenderTargetSystem] layer ${layer} is out of bounds (arrayLayerCount=${arrayLayerCount}).`);
|
|
2800
|
+
}
|
|
2801
|
+
this.mipLevel = mipLevel | 0;
|
|
2802
|
+
this.layer = layer | 0;
|
|
2803
|
+
const pixelWidth = Math.max(source2.pixelWidth >> mipLevel, 1);
|
|
2804
|
+
const pixelHeight = Math.max(source2.pixelHeight >> mipLevel, 1);
|
|
2805
|
+
if (!frame && renderSurface instanceof Texture) {
|
|
2806
|
+
frame = renderSurface.frame;
|
|
2807
|
+
}
|
|
2808
|
+
if (frame) {
|
|
2809
|
+
const resolution = source2._resolution;
|
|
2810
|
+
const scale = 1 << Math.max(mipLevel | 0, 0);
|
|
2811
|
+
const baseX = frame.x * resolution + 0.5 | 0;
|
|
2812
|
+
const baseY = frame.y * resolution + 0.5 | 0;
|
|
2813
|
+
const baseW = frame.width * resolution + 0.5 | 0;
|
|
2814
|
+
const baseH = frame.height * resolution + 0.5 | 0;
|
|
2815
|
+
let x = Math.floor(baseX / scale);
|
|
2816
|
+
let y = Math.floor(baseY / scale);
|
|
2817
|
+
let w = Math.ceil(baseW / scale);
|
|
2818
|
+
let h = Math.ceil(baseH / scale);
|
|
2819
|
+
x = Math.min(Math.max(x, 0), pixelWidth - 1);
|
|
2820
|
+
y = Math.min(Math.max(y, 0), pixelHeight - 1);
|
|
2821
|
+
w = Math.min(Math.max(w, 1), pixelWidth - x);
|
|
2822
|
+
h = Math.min(Math.max(h, 1), pixelHeight - y);
|
|
2823
|
+
viewport.x = x;
|
|
2824
|
+
viewport.y = y;
|
|
2825
|
+
viewport.width = w;
|
|
2826
|
+
viewport.height = h;
|
|
2827
|
+
} else {
|
|
2828
|
+
viewport.x = 0;
|
|
2829
|
+
viewport.y = 0;
|
|
2830
|
+
viewport.width = pixelWidth;
|
|
2831
|
+
viewport.height = pixelHeight;
|
|
2832
|
+
}
|
|
2833
|
+
calculateProjection(
|
|
2834
|
+
this.projectionMatrix,
|
|
2835
|
+
0,
|
|
2836
|
+
0,
|
|
2837
|
+
viewport.width / source2.resolution,
|
|
2838
|
+
viewport.height / source2.resolution,
|
|
2839
|
+
!renderTarget.isRoot
|
|
2840
|
+
);
|
|
2841
|
+
this.adaptor.startRenderPass(renderTarget, clear, clearColor, viewport, mipLevel, layer);
|
|
2842
|
+
if (didChange) {
|
|
2843
|
+
this.onRenderTargetChange.emit(renderTarget);
|
|
2844
|
+
}
|
|
2845
|
+
return renderTarget;
|
|
2846
|
+
}
|
|
2847
|
+
clear(target, clear = CLEAR.ALL, clearColor, mipLevel = this.mipLevel, layer = this.layer) {
|
|
2848
|
+
if (!clear) return;
|
|
2849
|
+
if (target) {
|
|
2850
|
+
target = this.getRenderTarget(target);
|
|
2851
|
+
}
|
|
2852
|
+
this.adaptor.clear(
|
|
2853
|
+
target || this.renderTarget,
|
|
2854
|
+
clear,
|
|
2855
|
+
clearColor,
|
|
2856
|
+
this.viewport,
|
|
2857
|
+
mipLevel,
|
|
2858
|
+
layer
|
|
2859
|
+
);
|
|
2860
|
+
}
|
|
2861
|
+
contextChange() {
|
|
2862
|
+
this._gpuRenderTargetHash = /* @__PURE__ */ Object.create(null);
|
|
2863
|
+
}
|
|
2864
|
+
/**
|
|
2865
|
+
* Push a render surface to the renderer. This will bind the render surface to the renderer,
|
|
2866
|
+
* @param renderSurface - the render surface to push
|
|
2867
|
+
* @param clear - the clear mode to use. Can be true or a CLEAR number 'COLOR | DEPTH | STENCIL' 0b111
|
|
2868
|
+
* @param clearColor - the color to clear to
|
|
2869
|
+
* @param frame - the frame to use when rendering to the render surface
|
|
2870
|
+
* @param mipLevel - the mip level to render to
|
|
2871
|
+
* @param layer - The layer of the render surface to render to. For array textures or cube maps, this specifies
|
|
2872
|
+
* which layer or face to target. Defaults to 0 (the first layer).
|
|
2873
|
+
*/
|
|
2874
|
+
push(renderSurface, clear = CLEAR.ALL, clearColor, frame, mipLevel = 0, layer = 0) {
|
|
2875
|
+
const renderTarget = this.bind(renderSurface, clear, clearColor, frame, mipLevel, layer);
|
|
2876
|
+
this._renderTargetStack.push({
|
|
2877
|
+
renderTarget,
|
|
2878
|
+
frame,
|
|
2879
|
+
mipLevel,
|
|
2880
|
+
layer
|
|
2881
|
+
});
|
|
2882
|
+
return renderTarget;
|
|
2883
|
+
}
|
|
2884
|
+
/** Pops the current render target from the renderer and restores the previous render target. */
|
|
2885
|
+
pop() {
|
|
2886
|
+
this._renderTargetStack.pop();
|
|
2887
|
+
const currentRenderTargetData = this._renderTargetStack[this._renderTargetStack.length - 1];
|
|
2888
|
+
this.bind(
|
|
2889
|
+
currentRenderTargetData.renderTarget,
|
|
2890
|
+
false,
|
|
2891
|
+
null,
|
|
2892
|
+
currentRenderTargetData.frame,
|
|
2893
|
+
currentRenderTargetData.mipLevel,
|
|
2894
|
+
currentRenderTargetData.layer
|
|
2895
|
+
);
|
|
2896
|
+
}
|
|
2897
|
+
/**
|
|
2898
|
+
* Gets the render target from the provide render surface. Eg if its a texture,
|
|
2899
|
+
* it will return the render target for the texture.
|
|
2900
|
+
* If its a render target, it will return the same render target.
|
|
2901
|
+
* @param renderSurface - the render surface to get the render target for
|
|
2902
|
+
* @returns the render target for the render surface
|
|
2903
|
+
*/
|
|
2904
|
+
getRenderTarget(renderSurface) {
|
|
2905
|
+
if (renderSurface.isTexture) {
|
|
2906
|
+
renderSurface = renderSurface.source;
|
|
2907
|
+
}
|
|
2908
|
+
return this._renderSurfaceToRenderTargetHash.get(renderSurface) ?? this._initRenderTarget(renderSurface);
|
|
2909
|
+
}
|
|
2910
|
+
/**
|
|
2911
|
+
* Copies a render surface to another texture.
|
|
2912
|
+
*
|
|
2913
|
+
* NOTE:
|
|
2914
|
+
* for sourceRenderSurfaceTexture, The render target must be something that is written too by the renderer
|
|
2915
|
+
*
|
|
2916
|
+
* The following is not valid:
|
|
2917
|
+
* @example
|
|
2918
|
+
* const canvas = document.createElement('canvas')
|
|
2919
|
+
* canvas.width = 200;
|
|
2920
|
+
* canvas.height = 200;
|
|
2921
|
+
*
|
|
2922
|
+
* const ctx = canvas2.getContext('2d')!
|
|
2923
|
+
* ctx.fillStyle = 'red'
|
|
2924
|
+
* ctx.fillRect(0, 0, 200, 200);
|
|
2925
|
+
*
|
|
2926
|
+
* const texture = RenderTexture.create({
|
|
2927
|
+
* width: 200,
|
|
2928
|
+
* height: 200,
|
|
2929
|
+
* })
|
|
2930
|
+
* const renderTarget = renderer.renderTarget.getRenderTarget(canvas2);
|
|
2931
|
+
*
|
|
2932
|
+
* renderer.renderTarget.copyToTexture(renderTarget,texture, {x:0,y:0},{width:200,height:200},{x:0,y:0});
|
|
2933
|
+
*
|
|
2934
|
+
* The best way to copy a canvas is to create a texture from it. Then render with that.
|
|
2935
|
+
*
|
|
2936
|
+
* Parsing in a RenderTarget canvas context (with a 2d context)
|
|
2937
|
+
* @param sourceRenderSurfaceTexture - the render surface to copy from
|
|
2938
|
+
* @param {Texture} destinationTexture - the texture to copy to
|
|
2939
|
+
* @param {object} originSrc - the origin of the copy
|
|
2940
|
+
* @param {number} originSrc.x - the x origin of the copy
|
|
2941
|
+
* @param {number} originSrc.y - the y origin of the copy
|
|
2942
|
+
* @param {object} size - the size of the copy
|
|
2943
|
+
* @param {number} size.width - the width of the copy
|
|
2944
|
+
* @param {number} size.height - the height of the copy
|
|
2945
|
+
* @param {object} originDest - the destination origin (top left to paste from!)
|
|
2946
|
+
* @param {number} originDest.x - the x origin of the paste
|
|
2947
|
+
* @param {number} originDest.y - the y origin of the paste
|
|
2948
|
+
*/
|
|
2949
|
+
copyToTexture(sourceRenderSurfaceTexture, destinationTexture, originSrc, size, originDest) {
|
|
2950
|
+
if (originSrc.x < 0) {
|
|
2951
|
+
size.width += originSrc.x;
|
|
2952
|
+
originDest.x -= originSrc.x;
|
|
2953
|
+
originSrc.x = 0;
|
|
2954
|
+
}
|
|
2955
|
+
if (originSrc.y < 0) {
|
|
2956
|
+
size.height += originSrc.y;
|
|
2957
|
+
originDest.y -= originSrc.y;
|
|
2958
|
+
originSrc.y = 0;
|
|
2959
|
+
}
|
|
2960
|
+
const { pixelWidth, pixelHeight } = sourceRenderSurfaceTexture;
|
|
2961
|
+
size.width = Math.min(size.width, pixelWidth - originSrc.x);
|
|
2962
|
+
size.height = Math.min(size.height, pixelHeight - originSrc.y);
|
|
2963
|
+
return this.adaptor.copyToTexture(
|
|
2964
|
+
sourceRenderSurfaceTexture,
|
|
2965
|
+
destinationTexture,
|
|
2966
|
+
originSrc,
|
|
2967
|
+
size,
|
|
2968
|
+
originDest
|
|
2969
|
+
);
|
|
2970
|
+
}
|
|
2971
|
+
/**
|
|
2972
|
+
* ensures that we have a depth stencil buffer available to render to
|
|
2973
|
+
* This is used by the mask system to make sure we have a stencil buffer.
|
|
2974
|
+
*/
|
|
2975
|
+
ensureDepthStencil() {
|
|
2976
|
+
if (!this.renderTarget.stencil) {
|
|
2977
|
+
this.renderTarget.stencil = true;
|
|
2978
|
+
this.adaptor.startRenderPass(this.renderTarget, false, null, this.viewport, 0, this.layer);
|
|
2979
|
+
}
|
|
2980
|
+
}
|
|
2981
|
+
/** nukes the render target system */
|
|
2982
|
+
destroy() {
|
|
2983
|
+
this._renderer = null;
|
|
2984
|
+
this._renderSurfaceToRenderTargetHash.forEach((renderTarget, key) => {
|
|
2985
|
+
if (renderTarget !== key) {
|
|
2986
|
+
renderTarget.destroy();
|
|
2987
|
+
}
|
|
2988
|
+
});
|
|
2989
|
+
this._renderSurfaceToRenderTargetHash.clear();
|
|
2990
|
+
this._gpuRenderTargetHash = /* @__PURE__ */ Object.create(null);
|
|
2991
|
+
}
|
|
2992
|
+
_initRenderTarget(renderSurface) {
|
|
2993
|
+
let renderTarget = null;
|
|
2994
|
+
if (CanvasSource.test(renderSurface)) {
|
|
2995
|
+
renderSurface = getCanvasTexture(renderSurface).source;
|
|
2996
|
+
}
|
|
2997
|
+
if (renderSurface instanceof RenderTarget) {
|
|
2998
|
+
renderTarget = renderSurface;
|
|
2999
|
+
} else if (renderSurface instanceof TextureSource) {
|
|
3000
|
+
renderTarget = new RenderTarget({
|
|
3001
|
+
colorTextures: [renderSurface]
|
|
3002
|
+
});
|
|
3003
|
+
if (renderSurface.source instanceof CanvasSource) {
|
|
3004
|
+
renderTarget.isRoot = true;
|
|
3005
|
+
}
|
|
3006
|
+
renderSurface.once("destroy", () => {
|
|
3007
|
+
renderTarget.destroy();
|
|
3008
|
+
this._renderSurfaceToRenderTargetHash.delete(renderSurface);
|
|
3009
|
+
const gpuRenderTarget = this._gpuRenderTargetHash[renderTarget.uid];
|
|
3010
|
+
if (gpuRenderTarget) {
|
|
3011
|
+
this._gpuRenderTargetHash[renderTarget.uid] = null;
|
|
3012
|
+
this.adaptor.destroyGpuRenderTarget(gpuRenderTarget);
|
|
3013
|
+
}
|
|
3014
|
+
});
|
|
3015
|
+
}
|
|
3016
|
+
this._renderSurfaceToRenderTargetHash.set(renderSurface, renderTarget);
|
|
3017
|
+
return renderTarget;
|
|
3018
|
+
}
|
|
3019
|
+
getGpuRenderTarget(renderTarget) {
|
|
3020
|
+
return this._gpuRenderTargetHash[renderTarget.uid] || (this._gpuRenderTargetHash[renderTarget.uid] = this.adaptor.initGpuRenderTarget(renderTarget));
|
|
3021
|
+
}
|
|
3022
|
+
resetState() {
|
|
3023
|
+
this.renderTarget = null;
|
|
3024
|
+
this.renderSurface = null;
|
|
3025
|
+
}
|
|
3026
|
+
}
|
|
3027
|
+
export {
|
|
3028
|
+
AlphaMaskPipe as A,
|
|
3029
|
+
BlendModePipe as B,
|
|
3030
|
+
CustomRenderPipe as C,
|
|
3031
|
+
RenderTargetSystem as R,
|
|
3032
|
+
SharedSystems as S,
|
|
3033
|
+
BatcherPipe as a,
|
|
3034
|
+
SpritePipe as b,
|
|
3035
|
+
RenderGroupPipe as c,
|
|
3036
|
+
SharedRenderPipes as d
|
|
3037
|
+
};
|