@lightningjs/renderer 2.9.0-beta2 → 2.9.0-beta4
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/COPYING +1 -0
- package/LICENSE +202 -202
- package/NOTICE +3 -3
- package/README.md +147 -147
- package/dist/src/core/CoreNode.d.ts +29 -6
- package/dist/src/core/CoreNode.js +118 -79
- package/dist/src/core/CoreNode.js.map +1 -1
- package/dist/src/core/CoreTextNode.d.ts +2 -2
- package/dist/src/core/CoreTextNode.js +7 -4
- package/dist/src/core/CoreTextNode.js.map +1 -1
- package/dist/src/core/CoreTextureManager.d.ts +61 -15
- package/dist/src/core/CoreTextureManager.js +179 -104
- package/dist/src/core/CoreTextureManager.js.map +1 -1
- package/dist/src/core/Stage.d.ts +7 -0
- package/dist/src/core/Stage.js +34 -1
- package/dist/src/core/Stage.js.map +1 -1
- package/dist/src/core/TextureMemoryManager.js +2 -5
- package/dist/src/core/TextureMemoryManager.js.map +1 -1
- package/dist/src/core/lib/ImageWorker.js +31 -28
- package/dist/src/core/lib/ImageWorker.js.map +1 -1
- package/dist/src/core/lib/WebGlContextWrapper.d.ts +1 -0
- package/dist/src/core/lib/WebGlContextWrapper.js +2 -0
- package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
- package/dist/src/core/renderers/CoreContextTexture.d.ts +1 -0
- package/dist/src/core/renderers/CoreContextTexture.js +1 -0
- package/dist/src/core/renderers/CoreContextTexture.js.map +1 -1
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +1 -1
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +13 -5
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -1
- package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +6 -8
- package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -1
- package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +3 -1
- package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -1
- package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +1 -2
- package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +26 -24
- package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +0 -1
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +7 -17
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
- package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
- package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
- package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
- package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
- package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
- package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
- package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
- package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
- package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
- package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
- package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
- package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
- package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
- package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
- package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
- package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +12 -12
- package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
- package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
- package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +9 -5
- package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +2 -1
- package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
- package/dist/src/core/textures/ColorTexture.d.ts +1 -1
- package/dist/src/core/textures/ColorTexture.js +2 -1
- package/dist/src/core/textures/ColorTexture.js.map +1 -1
- package/dist/src/core/textures/ImageTexture.d.ts +8 -1
- package/dist/src/core/textures/ImageTexture.js +39 -1
- package/dist/src/core/textures/ImageTexture.js.map +1 -1
- package/dist/src/core/textures/NoiseTexture.d.ts +1 -1
- package/dist/src/core/textures/NoiseTexture.js +2 -1
- package/dist/src/core/textures/NoiseTexture.js.map +1 -1
- package/dist/src/core/textures/RenderTexture.d.ts +1 -1
- package/dist/src/core/textures/RenderTexture.js +2 -1
- package/dist/src/core/textures/RenderTexture.js.map +1 -1
- package/dist/src/core/textures/SubTexture.d.ts +1 -1
- package/dist/src/core/textures/SubTexture.js +21 -4
- package/dist/src/core/textures/SubTexture.js.map +1 -1
- package/dist/src/core/textures/Texture.d.ts +44 -22
- package/dist/src/core/textures/Texture.js +116 -36
- package/dist/src/core/textures/Texture.js.map +1 -1
- package/dist/src/main-api/Renderer.d.ts +18 -0
- package/dist/src/main-api/Renderer.js +6 -4
- package/dist/src/main-api/Renderer.js.map +1 -1
- package/dist/tsconfig.dist.tsbuildinfo +1 -1
- package/exports/canvas.ts +39 -39
- package/exports/index.ts +89 -89
- package/exports/inspector.ts +24 -24
- package/exports/utils.ts +44 -44
- package/exports/webgl.ts +38 -38
- package/package.json +1 -1
- package/scripts/please-use-pnpm.js +13 -13
- package/src/common/CommonTypes.ts +139 -139
- package/src/common/EventEmitter.ts +77 -77
- package/src/common/IAnimationController.ts +92 -92
- package/src/common/IEventEmitter.ts +28 -28
- package/src/core/CoreNode.test.ts +199 -95
- package/src/core/CoreNode.ts +2313 -2258
- package/src/core/CoreShaderManager.ts +292 -292
- package/src/core/CoreTextNode.ts +455 -450
- package/src/core/CoreTextureManager.ts +522 -432
- package/src/core/Stage.ts +700 -652
- package/src/core/TextureMemoryManager.ts +277 -279
- package/src/core/animations/AnimationManager.ts +38 -38
- package/src/core/animations/CoreAnimation.ts +340 -340
- package/src/core/animations/CoreAnimationController.ts +157 -157
- package/src/core/lib/ContextSpy.ts +41 -41
- package/src/core/lib/ImageWorker.ts +270 -267
- package/src/core/lib/Matrix3d.ts +244 -244
- package/src/core/lib/RenderCoords.ts +86 -86
- package/src/core/lib/WebGlContextWrapper.ts +1322 -1320
- package/src/core/lib/textureCompression.ts +152 -152
- package/src/core/lib/textureSvg.ts +78 -78
- package/src/core/lib/utils.ts +306 -306
- package/src/core/platform.ts +61 -61
- package/src/core/renderers/CoreContextTexture.ts +43 -42
- package/src/core/renderers/CoreRenderOp.ts +22 -22
- package/src/core/renderers/CoreRenderer.ts +114 -114
- package/src/core/renderers/CoreShader.ts +41 -41
- package/src/core/renderers/canvas/CanvasCoreRenderer.ts +364 -353
- package/src/core/renderers/canvas/CanvasCoreTexture.ts +150 -149
- package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
- package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
- package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
- package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -79
- package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -48
- package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +298 -290
- package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
- package/src/core/renderers/webgl/WebGlCoreRenderer.ts +805 -817
- package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
- package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
- package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
- package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
- package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
- package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
- package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
- package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
- package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
- package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
- package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
- package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
- package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
- package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
- package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
- package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
- package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
- package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
- package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
- package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
- package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
- package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
- package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
- package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
- package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
- package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
- package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
- package/src/core/text-rendering/TrFontManager.ts +183 -183
- package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -171
- package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
- package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
- package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
- package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
- package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +89 -89
- package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -507
- package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +798 -798
- package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +403 -403
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
- package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
- package/src/core/textures/ColorTexture.ts +102 -100
- package/src/core/textures/ImageTexture.ts +378 -337
- package/src/core/textures/NoiseTexture.ts +104 -101
- package/src/core/textures/RenderTexture.ts +85 -83
- package/src/core/textures/SubTexture.ts +171 -146
- package/src/core/textures/Texture.ts +423 -318
- package/src/core/utils.ts +227 -227
- package/src/env.d.ts +7 -7
- package/src/main-api/DynamicShaderController.ts +104 -104
- package/src/main-api/INode.ts +101 -101
- package/src/main-api/Inspector.ts +505 -505
- package/src/main-api/Renderer.ts +693 -670
- package/src/main-api/ShaderController.ts +80 -80
- package/src/main-api/utils.ts +45 -45
- package/src/utils.ts +248 -248
|
@@ -1,432 +1,522 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
-
* following copyright and licenses apply:
|
|
4
|
-
*
|
|
5
|
-
* Copyright 2023 Comcast Cable Communications Management, LLC.
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import { ImageWorkerManager } from './lib/ImageWorker.js';
|
|
21
|
-
import type { CoreRenderer } from './renderers/CoreRenderer.js';
|
|
22
|
-
import { ColorTexture } from './textures/ColorTexture.js';
|
|
23
|
-
import { ImageTexture } from './textures/ImageTexture.js';
|
|
24
|
-
import { NoiseTexture } from './textures/NoiseTexture.js';
|
|
25
|
-
import { SubTexture } from './textures/SubTexture.js';
|
|
26
|
-
import { RenderTexture } from './textures/RenderTexture.js';
|
|
27
|
-
import type { Texture } from './textures/Texture.js';
|
|
28
|
-
import { EventEmitter } from '../common/EventEmitter.js';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Augmentable map of texture class types
|
|
32
|
-
*
|
|
33
|
-
* @remarks
|
|
34
|
-
* This interface can be augmented by other modules/apps to add additional
|
|
35
|
-
* texture types. The ones included directly here are the ones that are
|
|
36
|
-
* included in the core library.
|
|
37
|
-
*/
|
|
38
|
-
export interface TextureMap {
|
|
39
|
-
ColorTexture: typeof ColorTexture;
|
|
40
|
-
ImageTexture: typeof ImageTexture;
|
|
41
|
-
NoiseTexture: typeof NoiseTexture;
|
|
42
|
-
SubTexture: typeof SubTexture;
|
|
43
|
-
RenderTexture: typeof RenderTexture;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface CreateImageBitmapSupport {
|
|
47
|
-
basic: boolean; // Supports createImageBitmap(image)
|
|
48
|
-
options: boolean; // Supports createImageBitmap(image, options)
|
|
49
|
-
full: boolean; // Supports createImageBitmap(image, sx, sy, sw, sh, options)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
|
|
53
|
-
? Props
|
|
54
|
-
: never;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Contains information about the texture manager's internal state
|
|
58
|
-
* for debugging purposes.
|
|
59
|
-
*/
|
|
60
|
-
export interface TextureManagerDebugInfo {
|
|
61
|
-
keyCacheSize: number;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type ResizeModeOptions =
|
|
65
|
-
| {
|
|
66
|
-
/**
|
|
67
|
-
* Specifies that the image should be resized to cover the specified dimensions.
|
|
68
|
-
*/
|
|
69
|
-
type: 'cover';
|
|
70
|
-
/**
|
|
71
|
-
* The horizontal clipping position
|
|
72
|
-
* To clip the left, set clipX to 0. To clip the right, set clipX to 1.
|
|
73
|
-
* clipX 0.5 will clip a equal amount from left and right
|
|
74
|
-
*
|
|
75
|
-
* @defaultValue 0.5
|
|
76
|
-
*/
|
|
77
|
-
clipX?: number;
|
|
78
|
-
/**
|
|
79
|
-
* The vertical clipping position
|
|
80
|
-
* To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
|
|
81
|
-
* clipY 0.5 will clip a equal amount from top and bottom
|
|
82
|
-
*
|
|
83
|
-
* @defaultValue 0.5
|
|
84
|
-
*/
|
|
85
|
-
clipY?: number;
|
|
86
|
-
}
|
|
87
|
-
| {
|
|
88
|
-
/**
|
|
89
|
-
* Specifies that the image should be resized to fit within the specified dimensions.
|
|
90
|
-
*/
|
|
91
|
-
type: 'contain';
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Universal options for all texture types
|
|
96
|
-
*
|
|
97
|
-
* @remarks
|
|
98
|
-
* Texture Options provide a way to specify options that are relevant to the
|
|
99
|
-
* texture loading process (including caching) and specifically for how a
|
|
100
|
-
* texture is rendered within a specific Node (or set of Nodes).
|
|
101
|
-
*
|
|
102
|
-
* They are not used in determining the cache key for a texture (except if
|
|
103
|
-
* the `cacheKey` option is provided explicitly to oveerride the default
|
|
104
|
-
* cache key for the texture instance) nor are they stored/referenced within
|
|
105
|
-
* the texture instance itself. Instead, the options are stored/referenced
|
|
106
|
-
* within individual Nodes. So a single texture instance can be used in
|
|
107
|
-
* multiple Nodes each using a different set of options.
|
|
108
|
-
*/
|
|
109
|
-
export interface TextureOptions {
|
|
110
|
-
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* @defaultValue `false`
|
|
121
|
-
*/
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
*
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
* @remarks
|
|
177
|
-
* This
|
|
178
|
-
*
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
this.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
0x00,
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
0x44,
|
|
259
|
-
|
|
260
|
-
0x00,
|
|
261
|
-
0x00,
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
]
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
+
* following copyright and licenses apply:
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { ImageWorkerManager } from './lib/ImageWorker.js';
|
|
21
|
+
import type { CoreRenderer } from './renderers/CoreRenderer.js';
|
|
22
|
+
import { ColorTexture } from './textures/ColorTexture.js';
|
|
23
|
+
import { ImageTexture } from './textures/ImageTexture.js';
|
|
24
|
+
import { NoiseTexture } from './textures/NoiseTexture.js';
|
|
25
|
+
import { SubTexture } from './textures/SubTexture.js';
|
|
26
|
+
import { RenderTexture } from './textures/RenderTexture.js';
|
|
27
|
+
import type { Texture } from './textures/Texture.js';
|
|
28
|
+
import { EventEmitter } from '../common/EventEmitter.js';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Augmentable map of texture class types
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* This interface can be augmented by other modules/apps to add additional
|
|
35
|
+
* texture types. The ones included directly here are the ones that are
|
|
36
|
+
* included in the core library.
|
|
37
|
+
*/
|
|
38
|
+
export interface TextureMap {
|
|
39
|
+
ColorTexture: typeof ColorTexture;
|
|
40
|
+
ImageTexture: typeof ImageTexture;
|
|
41
|
+
NoiseTexture: typeof NoiseTexture;
|
|
42
|
+
SubTexture: typeof SubTexture;
|
|
43
|
+
RenderTexture: typeof RenderTexture;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface CreateImageBitmapSupport {
|
|
47
|
+
basic: boolean; // Supports createImageBitmap(image)
|
|
48
|
+
options: boolean; // Supports createImageBitmap(image, options)
|
|
49
|
+
full: boolean; // Supports createImageBitmap(image, sx, sy, sw, sh, options)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
|
|
53
|
+
? Props
|
|
54
|
+
: never;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Contains information about the texture manager's internal state
|
|
58
|
+
* for debugging purposes.
|
|
59
|
+
*/
|
|
60
|
+
export interface TextureManagerDebugInfo {
|
|
61
|
+
keyCacheSize: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type ResizeModeOptions =
|
|
65
|
+
| {
|
|
66
|
+
/**
|
|
67
|
+
* Specifies that the image should be resized to cover the specified dimensions.
|
|
68
|
+
*/
|
|
69
|
+
type: 'cover';
|
|
70
|
+
/**
|
|
71
|
+
* The horizontal clipping position
|
|
72
|
+
* To clip the left, set clipX to 0. To clip the right, set clipX to 1.
|
|
73
|
+
* clipX 0.5 will clip a equal amount from left and right
|
|
74
|
+
*
|
|
75
|
+
* @defaultValue 0.5
|
|
76
|
+
*/
|
|
77
|
+
clipX?: number;
|
|
78
|
+
/**
|
|
79
|
+
* The vertical clipping position
|
|
80
|
+
* To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
|
|
81
|
+
* clipY 0.5 will clip a equal amount from top and bottom
|
|
82
|
+
*
|
|
83
|
+
* @defaultValue 0.5
|
|
84
|
+
*/
|
|
85
|
+
clipY?: number;
|
|
86
|
+
}
|
|
87
|
+
| {
|
|
88
|
+
/**
|
|
89
|
+
* Specifies that the image should be resized to fit within the specified dimensions.
|
|
90
|
+
*/
|
|
91
|
+
type: 'contain';
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Universal options for all texture types
|
|
96
|
+
*
|
|
97
|
+
* @remarks
|
|
98
|
+
* Texture Options provide a way to specify options that are relevant to the
|
|
99
|
+
* texture loading process (including caching) and specifically for how a
|
|
100
|
+
* texture is rendered within a specific Node (or set of Nodes).
|
|
101
|
+
*
|
|
102
|
+
* They are not used in determining the cache key for a texture (except if
|
|
103
|
+
* the `cacheKey` option is provided explicitly to oveerride the default
|
|
104
|
+
* cache key for the texture instance) nor are they stored/referenced within
|
|
105
|
+
* the texture instance itself. Instead, the options are stored/referenced
|
|
106
|
+
* within individual Nodes. So a single texture instance can be used in
|
|
107
|
+
* multiple Nodes each using a different set of options.
|
|
108
|
+
*/
|
|
109
|
+
export interface TextureOptions {
|
|
110
|
+
/**
|
|
111
|
+
* Flip the texture horizontally when rendering
|
|
112
|
+
*
|
|
113
|
+
* @defaultValue `false`
|
|
114
|
+
*/
|
|
115
|
+
flipX?: boolean;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Flip the texture vertically when rendering
|
|
119
|
+
*
|
|
120
|
+
* @defaultValue `false`
|
|
121
|
+
*/
|
|
122
|
+
flipY?: boolean;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* You can use resizeMode to determine the clipping automatically from the width
|
|
126
|
+
* and height of the source texture. This can be convenient if you are unsure about
|
|
127
|
+
* the exact image sizes but want the image to cover a specific area.
|
|
128
|
+
*
|
|
129
|
+
* The resize modes cover and contain are supported
|
|
130
|
+
*/
|
|
131
|
+
resizeMode?: ResizeModeOptions;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export class CoreTextureManager extends EventEmitter {
|
|
135
|
+
/**
|
|
136
|
+
* Map of textures by cache key
|
|
137
|
+
*/
|
|
138
|
+
keyCache: Map<string, Texture> = new Map();
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Map of cache keys by texture
|
|
142
|
+
*/
|
|
143
|
+
inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Map of texture constructors by their type name
|
|
147
|
+
*/
|
|
148
|
+
txConstructors: Partial<TextureMap> = {};
|
|
149
|
+
|
|
150
|
+
private downloadTextureSourceQueue: Array<Texture> = [];
|
|
151
|
+
private priorityQueue: Array<Texture> = [];
|
|
152
|
+
private uploadTextureQueue: Array<Texture> = [];
|
|
153
|
+
private initialized = false;
|
|
154
|
+
|
|
155
|
+
imageWorkerManager: ImageWorkerManager | null = null;
|
|
156
|
+
hasCreateImageBitmap = !!self.createImageBitmap;
|
|
157
|
+
imageBitmapSupported = {
|
|
158
|
+
basic: false,
|
|
159
|
+
options: false,
|
|
160
|
+
full: false,
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
hasWorker = !!self.Worker;
|
|
164
|
+
/**
|
|
165
|
+
* Renderer that this texture manager is associated with
|
|
166
|
+
*
|
|
167
|
+
* @remarks
|
|
168
|
+
* This MUST be set before the texture manager is used. Otherwise errors
|
|
169
|
+
* will occur when using the texture manager.
|
|
170
|
+
*/
|
|
171
|
+
renderer!: CoreRenderer;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The current frame time in milliseconds
|
|
175
|
+
*
|
|
176
|
+
* @remarks
|
|
177
|
+
* This is used to populate the `lastRenderableChangeTime` property of
|
|
178
|
+
* {@link Texture} instances when their renderable state changes.
|
|
179
|
+
*
|
|
180
|
+
* Set by stage via `updateFrameTime` method.
|
|
181
|
+
*/
|
|
182
|
+
frameTime = 0;
|
|
183
|
+
|
|
184
|
+
constructor(numImageWorkers: number) {
|
|
185
|
+
super();
|
|
186
|
+
this.validateCreateImageBitmap()
|
|
187
|
+
.then((result) => {
|
|
188
|
+
this.hasCreateImageBitmap =
|
|
189
|
+
result.basic || result.options || result.full;
|
|
190
|
+
this.imageBitmapSupported = result;
|
|
191
|
+
|
|
192
|
+
if (!this.hasCreateImageBitmap) {
|
|
193
|
+
console.warn(
|
|
194
|
+
'[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (
|
|
199
|
+
this.hasCreateImageBitmap &&
|
|
200
|
+
this.hasWorker &&
|
|
201
|
+
numImageWorkers > 0
|
|
202
|
+
) {
|
|
203
|
+
this.imageWorkerManager = new ImageWorkerManager(
|
|
204
|
+
numImageWorkers,
|
|
205
|
+
result,
|
|
206
|
+
);
|
|
207
|
+
} else {
|
|
208
|
+
console.warn(
|
|
209
|
+
'[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
this.initialized = true;
|
|
214
|
+
this.emit('initialized');
|
|
215
|
+
})
|
|
216
|
+
.catch((e) => {
|
|
217
|
+
console.warn(
|
|
218
|
+
'[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
// initialized without image worker manager and createImageBitmap
|
|
222
|
+
this.initialized = true;
|
|
223
|
+
this.emit('initialized');
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
this.registerTextureType('ImageTexture', ImageTexture);
|
|
227
|
+
this.registerTextureType('ColorTexture', ColorTexture);
|
|
228
|
+
this.registerTextureType('NoiseTexture', NoiseTexture);
|
|
229
|
+
this.registerTextureType('SubTexture', SubTexture);
|
|
230
|
+
this.registerTextureType('RenderTexture', RenderTexture);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private async validateCreateImageBitmap(): Promise<CreateImageBitmapSupport> {
|
|
234
|
+
// Test if createImageBitmap is supported using a simple 1x1 PNG image
|
|
235
|
+
// prettier-ignore
|
|
236
|
+
const pngBinaryData = new Uint8Array([
|
|
237
|
+
0x89, 0x50, 0x4e, 0x47,
|
|
238
|
+
0x0d, 0x0a, 0x1a, 0x0a, // PNG signature
|
|
239
|
+
0x00, 0x00, 0x00, 0x0d, // IHDR chunk length
|
|
240
|
+
0x49, 0x48, 0x44, 0x52, // "IHDR" chunk type
|
|
241
|
+
0x00, 0x00, 0x00, 0x01, // Width: 1
|
|
242
|
+
0x00, 0x00, 0x00, 0x01, // Height: 1
|
|
243
|
+
0x01, // Bit depth: 1
|
|
244
|
+
0x03, // Color type: Indexed
|
|
245
|
+
0x00, // Compression method: Deflate
|
|
246
|
+
0x00, // Filter method: None
|
|
247
|
+
0x00, // Interlace method: None
|
|
248
|
+
0x25, 0xdb, 0x56, 0xca, // CRC for IHDR
|
|
249
|
+
0x00, 0x00, 0x00, 0x03, // PLTE chunk length
|
|
250
|
+
0x50, 0x4c, 0x54, 0x45, // "PLTE" chunk type
|
|
251
|
+
0x00, 0x00, 0x00, // Palette entry: Black
|
|
252
|
+
0xa7, 0x7a, 0x3d, 0xda, // CRC for PLTE
|
|
253
|
+
0x00, 0x00, 0x00, 0x01, // tRNS chunk length
|
|
254
|
+
0x74, 0x52, 0x4e, 0x53, // "tRNS" chunk type
|
|
255
|
+
0x00, // Transparency for black: Fully transparent
|
|
256
|
+
0x40, 0xe6, 0xd8, 0x66, // CRC for tRNS
|
|
257
|
+
0x00, 0x00, 0x00, 0x0a, // IDAT chunk length
|
|
258
|
+
0x49, 0x44, 0x41, 0x54, // "IDAT" chunk type
|
|
259
|
+
0x08, 0xd7, // Deflate header
|
|
260
|
+
0x63, 0x60, 0x00, 0x00,
|
|
261
|
+
0x00, 0x02, 0x00, 0x01, // Zlib-compressed data
|
|
262
|
+
0xe2, 0x21, 0xbc, 0x33, // CRC for IDAT
|
|
263
|
+
0x00, 0x00, 0x00, 0x00, // IEND chunk length
|
|
264
|
+
0x49, 0x45, 0x4e, 0x44, // "IEND" chunk type
|
|
265
|
+
0xae, 0x42, 0x60, 0x82, // CRC for IEND
|
|
266
|
+
]);
|
|
267
|
+
|
|
268
|
+
const support: CreateImageBitmapSupport = {
|
|
269
|
+
basic: false,
|
|
270
|
+
options: false,
|
|
271
|
+
full: false,
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
// Test basic createImageBitmap support
|
|
275
|
+
const blob = new Blob([pngBinaryData], { type: 'image/png' });
|
|
276
|
+
const bitmap = await createImageBitmap(blob);
|
|
277
|
+
bitmap.close?.();
|
|
278
|
+
support.basic = true;
|
|
279
|
+
|
|
280
|
+
// Test createImageBitmap with options support
|
|
281
|
+
try {
|
|
282
|
+
const options = { premultiplyAlpha: 'none' as const };
|
|
283
|
+
const bitmapWithOptions = await createImageBitmap(blob, options);
|
|
284
|
+
bitmapWithOptions.close?.();
|
|
285
|
+
support.options = true;
|
|
286
|
+
} catch (e) {
|
|
287
|
+
/* ignore */
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Test createImageBitmap with full options support
|
|
291
|
+
try {
|
|
292
|
+
const bitmapWithFullOptions = await createImageBitmap(blob, 0, 0, 1, 1, {
|
|
293
|
+
premultiplyAlpha: 'none',
|
|
294
|
+
});
|
|
295
|
+
bitmapWithFullOptions.close?.();
|
|
296
|
+
support.full = true;
|
|
297
|
+
} catch (e) {
|
|
298
|
+
/* ignore */
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return support;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
registerTextureType<Type extends keyof TextureMap>(
|
|
305
|
+
textureType: Type,
|
|
306
|
+
textureClass: TextureMap[Type],
|
|
307
|
+
): void {
|
|
308
|
+
this.txConstructors[textureType] = textureClass;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Enqueue a texture for downloading its source image.
|
|
313
|
+
*/
|
|
314
|
+
enqueueDownloadTextureSource(texture: Texture): void {
|
|
315
|
+
if (!this.downloadTextureSourceQueue.includes(texture)) {
|
|
316
|
+
this.downloadTextureSourceQueue.push(texture);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Enqueue a texture for uploading to the GPU.
|
|
322
|
+
*
|
|
323
|
+
* @param texture - The texture to upload
|
|
324
|
+
*/
|
|
325
|
+
enqueueUploadTexture(texture: Texture): void {
|
|
326
|
+
if (this.uploadTextureQueue.includes(texture) === false) {
|
|
327
|
+
this.uploadTextureQueue.push(texture);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Create a texture
|
|
333
|
+
*
|
|
334
|
+
* @param textureType - The type of texture to create
|
|
335
|
+
* @param props - The properties to use for the texture
|
|
336
|
+
*/
|
|
337
|
+
createTexture<Type extends keyof TextureMap>(
|
|
338
|
+
textureType: Type,
|
|
339
|
+
props: ExtractProps<TextureMap[Type]>,
|
|
340
|
+
): InstanceType<TextureMap[Type]> {
|
|
341
|
+
let texture: Texture | undefined;
|
|
342
|
+
const TextureClass = this.txConstructors[textureType];
|
|
343
|
+
if (!TextureClass) {
|
|
344
|
+
throw new Error(`Texture type "${textureType}" is not registered`);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const cacheKey = TextureClass.makeCacheKey(props as any);
|
|
348
|
+
if (cacheKey && this.keyCache.has(cacheKey)) {
|
|
349
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
350
|
+
texture = this.keyCache.get(cacheKey)!;
|
|
351
|
+
} else {
|
|
352
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
|
|
353
|
+
texture = new TextureClass(this, props as any);
|
|
354
|
+
|
|
355
|
+
if (cacheKey) {
|
|
356
|
+
this.initTextureToCache(texture, cacheKey);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return texture as InstanceType<TextureMap[Type]>;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Override loadTexture to use the batched approach.
|
|
365
|
+
*
|
|
366
|
+
* @param texture - The texture to load
|
|
367
|
+
* @param immediate - Whether to prioritize the texture for immediate loading
|
|
368
|
+
*/
|
|
369
|
+
loadTexture(texture: Texture, priority?: boolean): void {
|
|
370
|
+
if (texture.state === 'loaded' || texture.state === 'loading') {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
texture.setSourceState('loading');
|
|
375
|
+
texture.setCoreCtxState('loading');
|
|
376
|
+
|
|
377
|
+
// if we're not initialized, just queue the texture into the priority queue
|
|
378
|
+
if (this.initialized === false) {
|
|
379
|
+
this.priorityQueue.push(texture);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// prioritize the texture for immediate loading
|
|
384
|
+
if (priority === true) {
|
|
385
|
+
texture
|
|
386
|
+
.getTextureData()
|
|
387
|
+
.then(() => {
|
|
388
|
+
this.uploadTexture(texture);
|
|
389
|
+
})
|
|
390
|
+
.catch((err) => {
|
|
391
|
+
console.error(err);
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// enqueue the texture for download and upload
|
|
396
|
+
this.enqueueDownloadTextureSource(texture);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Upload a texture to the GPU
|
|
401
|
+
*
|
|
402
|
+
* @param texture Texture to upload
|
|
403
|
+
*/
|
|
404
|
+
uploadTexture(texture: Texture): void {
|
|
405
|
+
const coreContext = texture.loadCtxTexture();
|
|
406
|
+
coreContext.load();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Process a limited number of downloads and uploads.
|
|
411
|
+
*
|
|
412
|
+
* @param maxItems - The maximum number of items to process
|
|
413
|
+
*/
|
|
414
|
+
processSome(maxItems = 0): void {
|
|
415
|
+
if (this.initialized === false) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
let itemsProcessed = 0;
|
|
420
|
+
|
|
421
|
+
// Process priority queue
|
|
422
|
+
while (
|
|
423
|
+
this.priorityQueue.length > 0 &&
|
|
424
|
+
(maxItems === 0 || itemsProcessed < maxItems)
|
|
425
|
+
) {
|
|
426
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
427
|
+
const texture = this.priorityQueue.shift()!;
|
|
428
|
+
texture.getTextureData().then(() => {
|
|
429
|
+
this.uploadTexture(texture);
|
|
430
|
+
});
|
|
431
|
+
itemsProcessed++;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// Process uploads
|
|
435
|
+
while (
|
|
436
|
+
this.uploadTextureQueue.length > 0 &&
|
|
437
|
+
(maxItems === 0 || itemsProcessed < maxItems)
|
|
438
|
+
) {
|
|
439
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
440
|
+
this.uploadTexture(this.uploadTextureQueue.shift()!);
|
|
441
|
+
itemsProcessed++;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Process downloads
|
|
445
|
+
while (
|
|
446
|
+
this.downloadTextureSourceQueue.length > 0 &&
|
|
447
|
+
(maxItems === 0 || itemsProcessed < maxItems)
|
|
448
|
+
) {
|
|
449
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
450
|
+
const texture = this.downloadTextureSourceQueue.shift()!;
|
|
451
|
+
queueMicrotask(() => {
|
|
452
|
+
texture.getTextureData().then(() => {
|
|
453
|
+
this.enqueueUploadTexture(texture);
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
itemsProcessed++;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
public hasUpdates(): boolean {
|
|
462
|
+
return (
|
|
463
|
+
this.downloadTextureSourceQueue.length > 0 ||
|
|
464
|
+
this.uploadTextureQueue.length > 0
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Initialize a texture to the cache
|
|
470
|
+
*
|
|
471
|
+
* @param texture Texture to cache
|
|
472
|
+
* @param cacheKey Cache key for the texture
|
|
473
|
+
*/
|
|
474
|
+
initTextureToCache(texture: Texture, cacheKey: string) {
|
|
475
|
+
const { keyCache, inverseKeyCache } = this;
|
|
476
|
+
keyCache.set(cacheKey, texture);
|
|
477
|
+
inverseKeyCache.set(texture, cacheKey);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Get a texture from the cache
|
|
482
|
+
*
|
|
483
|
+
* @param cacheKey
|
|
484
|
+
*/
|
|
485
|
+
getTextureFromCache(cacheKey: string): Texture | undefined {
|
|
486
|
+
return this.keyCache.get(cacheKey);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Remove a texture from the cache
|
|
491
|
+
*
|
|
492
|
+
* @remarks
|
|
493
|
+
* Called by Texture Cleanup when a texture is freed.
|
|
494
|
+
*
|
|
495
|
+
* @param texture
|
|
496
|
+
*/
|
|
497
|
+
removeTextureFromCache(texture: Texture) {
|
|
498
|
+
const { inverseKeyCache, keyCache } = this;
|
|
499
|
+
const cacheKey = inverseKeyCache.get(texture);
|
|
500
|
+
if (cacheKey) {
|
|
501
|
+
keyCache.delete(cacheKey);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Resolve a parent texture from the cache or fallback to the provided texture.
|
|
507
|
+
*
|
|
508
|
+
* @param texture - The provided texture to resolve.
|
|
509
|
+
* @returns The cached or provided texture.
|
|
510
|
+
*/
|
|
511
|
+
resolveParentTexture(texture: ImageTexture): Texture {
|
|
512
|
+
if (!texture?.props) {
|
|
513
|
+
return texture;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const cacheKey = ImageTexture.makeCacheKey(texture.props);
|
|
517
|
+
const cachedTexture = cacheKey
|
|
518
|
+
? this.getTextureFromCache(cacheKey)
|
|
519
|
+
: undefined;
|
|
520
|
+
return cachedTexture ?? texture;
|
|
521
|
+
}
|
|
522
|
+
}
|