bg2e-js 2.3.11 → 2.3.13
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/bg2e-js.js +356 -326
- package/dist/bg2e-js.js.map +1 -1
- package/package.json +56 -56
- package/src/app/AppController.ts +39 -39
- package/src/app/Bg2KeyboardEvent.ts +54 -54
- package/src/app/Bg2MouseEvent.ts +82 -82
- package/src/app/Bg2TouchEvent.ts +18 -18
- package/src/app/Canvas.ts +108 -108
- package/src/app/EventBase.ts +10 -10
- package/src/app/MainLoop.ts +273 -273
- package/src/app/index.ts +24 -24
- package/src/base/Color.ts +134 -134
- package/src/base/Environment.ts +183 -183
- package/src/base/Light.ts +192 -192
- package/src/base/Material.ts +620 -620
- package/src/base/PolyList.ts +365 -365
- package/src/base/Texture.ts +620 -620
- package/src/base/index.ts +81 -81
- package/src/db/Bg2LoaderPlugin.ts +143 -143
- package/src/db/DBPluginApi.ts +48 -48
- package/src/db/Loader.ts +116 -116
- package/src/db/LoaderPlugin.ts +34 -34
- package/src/db/MtlParser.ts +7 -7
- package/src/db/ObjLoaderPlugin.ts +54 -54
- package/src/db/ObjParser.ts +252 -252
- package/src/db/ObjWriterPlugin.ts +18 -18
- package/src/db/VitscnjLoaderPlugin.ts +112 -112
- package/src/db/Writer.ts +52 -52
- package/src/db/WriterPlugin.ts +22 -22
- package/src/db/index.ts +44 -44
- package/src/debug/DebugRenderer.ts +173 -173
- package/src/debug/WebGLTextureViewer.ts +75 -75
- package/src/debug/index.ts +6 -6
- package/src/index.html +11 -11
- package/src/index.ts +33 -33
- package/src/manipulation/SelectionBuffer.ts +81 -81
- package/src/manipulation/SelectionHighlight.ts +105 -84
- package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
- package/src/manipulation/SelectionManager.ts +196 -188
- package/src/manipulation/SelectionMode.ts +6 -6
- package/src/math/Mat3.ts +259 -259
- package/src/math/Mat4.ts +710 -710
- package/src/math/MatrixStrategy.ts +25 -25
- package/src/math/Quat.ts +65 -65
- package/src/math/Vec.ts +753 -753
- package/src/math/constants.ts +46 -46
- package/src/math/functions.ts +103 -103
- package/src/math/index.ts +74 -74
- package/src/phsics/joint.ts +137 -137
- package/src/primitives/arrow.ts +57 -57
- package/src/primitives/cone.ts +138 -138
- package/src/primitives/cube.ts +60 -60
- package/src/primitives/cylinder.ts +216 -216
- package/src/primitives/index.ts +13 -13
- package/src/primitives/plane.ts +31 -31
- package/src/primitives/sphere.ts +809 -809
- package/src/react/useBg2e.ts +69 -69
- package/src/render/BRDFIntegrationMap.ts +4 -4
- package/src/render/Environment.ts +135 -135
- package/src/render/FrameBuffer.ts +35 -35
- package/src/render/MaterialRenderer.ts +34 -34
- package/src/render/Pipeline.ts +108 -108
- package/src/render/PolyListRenderer.ts +47 -47
- package/src/render/RenderBuffer.ts +197 -197
- package/src/render/RenderQueue.ts +198 -198
- package/src/render/RenderState.ts +116 -116
- package/src/render/Renderer.ts +248 -248
- package/src/render/SceneAppController.ts +250 -250
- package/src/render/SceneRenderer.ts +387 -387
- package/src/render/Shader.ts +32 -32
- package/src/render/ShadowRenderer.ts +176 -176
- package/src/render/SkyCube.ts +105 -105
- package/src/render/SkySphere.ts +117 -117
- package/src/render/TextureMergerRenderer.ts +70 -70
- package/src/render/TextureRenderer.ts +34 -34
- package/src/render/index.ts +67 -67
- package/src/render/webgl/FrameBuffer.ts +9 -9
- package/src/render/webgl/MaterialRenderer.ts +112 -112
- package/src/render/webgl/Pipeline.ts +88 -88
- package/src/render/webgl/PolyListRenderer.ts +260 -260
- package/src/render/webgl/RenderBuffer.ts +226 -226
- package/src/render/webgl/Renderer.ts +262 -262
- package/src/render/webgl/SceneRenderer.ts +67 -67
- package/src/render/webgl/ShaderProgram.ts +424 -424
- package/src/render/webgl/ShadowRenderer.ts +6 -6
- package/src/render/webgl/SkyCube.ts +15 -15
- package/src/render/webgl/SkySphere.ts +15 -15
- package/src/render/webgl/State.ts +152 -152
- package/src/render/webgl/TextureRenderer.ts +167 -167
- package/src/render/webgl/VertexBuffer.ts +137 -137
- package/src/render/webgl/index.ts +35 -35
- package/src/scene/Camera.ts +458 -458
- package/src/scene/Chain.ts +44 -44
- package/src/scene/ChainJoint.ts +58 -58
- package/src/scene/Component.ts +177 -177
- package/src/scene/ComponentMap.ts +106 -106
- package/src/scene/Drawable.ts +154 -154
- package/src/scene/EnvironmentComponent.ts +141 -141
- package/src/scene/FindNodeVisitor.ts +59 -59
- package/src/scene/LightComponent.ts +154 -154
- package/src/scene/MatrixState.ts +46 -46
- package/src/scene/Node.ts +328 -328
- package/src/scene/NodeVisitor.ts +15 -15
- package/src/scene/OrbitCameraController.ts +450 -450
- package/src/scene/SmoothOrbitCameraController.ts +99 -99
- package/src/scene/Transform.ts +73 -73
- package/src/scene/index.ts +60 -60
- package/src/shaders/BasicDiffuseColorShader.ts +111 -111
- package/src/shaders/BasicPBRLightShader.ts +276 -276
- package/src/shaders/DebugRenderShader.ts +97 -97
- package/src/shaders/DepthRenderShader.ts +127 -127
- package/src/shaders/IrradianceMapCubeShader.ts +115 -115
- package/src/shaders/PBRLightIBLShader.ts +486 -486
- package/src/shaders/PickSelectionShader.ts +101 -101
- package/src/shaders/PresentDebugFramebufferShader.ts +118 -118
- package/src/shaders/PresentTextureShader.ts +99 -99
- package/src/shaders/SelectionHighlightShader.ts +143 -127
- package/src/shaders/ShaderFunction.ts +318 -318
- package/src/shaders/SkyCubeShader.ts +93 -93
- package/src/shaders/SkySphereShader.ts +102 -102
- package/src/shaders/SpecularMapCubeShader.ts +164 -164
- package/src/shaders/TextureMergerShader.ts +171 -171
- package/src/shaders/index.ts +36 -36
- package/src/shaders/webgl/color_correction.glsl +47 -47
- package/src/shaders/webgl/constants.glsl +6 -6
- package/src/shaders/webgl/index.ts +70 -70
- package/src/shaders/webgl/normal_map.glsl +9 -9
- package/src/shaders/webgl/pbr.glsl +173 -173
- package/src/shaders/webgl/uniforms.glsl +91 -91
- package/src/shaders/webgl_shader_lib.ts +213 -213
- package/src/tools/BinaryResourceProvider.ts +14 -14
- package/src/tools/ImageResourceProvider.ts +66 -66
- package/src/tools/MaterialModifier.ts +446 -446
- package/src/tools/Resource.ts +203 -203
- package/src/tools/ResourceProvider.ts +69 -69
- package/src/tools/TextResourceProvider.ts +24 -24
- package/src/tools/TextureCache.ts +51 -51
- package/src/tools/TextureResourceDatabase.ts +100 -100
- package/src/tools/UserAgent.ts +362 -362
- package/src/tools/VideoResourceProvider.ts +50 -50
- package/src/tools/WriteStrategy.ts +22 -22
- package/src/tools/base64.ts +11 -11
- package/src/tools/crypto.ts +19 -19
- package/src/tools/endiantess.ts +13 -13
- package/src/tools/image.ts +18 -18
- package/src/tools/index.ts +41 -41
- package/src/tools/processType.ts +39 -39
- package/src/vite-env.d.ts +12 -12
package/src/base/Texture.ts
CHANGED
|
@@ -1,620 +1,620 @@
|
|
|
1
|
-
|
|
2
|
-
import Vec from '../math/Vec';
|
|
3
|
-
import Resource from '../tools/Resource';
|
|
4
|
-
import { generateImageHash } from '../tools/image';
|
|
5
|
-
import Color from './Color';
|
|
6
|
-
import Canvas from '../app/Canvas';
|
|
7
|
-
|
|
8
|
-
export enum TextureDataType {
|
|
9
|
-
NONE = 0,
|
|
10
|
-
IMAGE = 1,
|
|
11
|
-
IMAGE_DATA = 2,
|
|
12
|
-
CUBEMAP = 3,
|
|
13
|
-
CUBEMAP_DATA = 4,
|
|
14
|
-
VIDEO = 5,
|
|
15
|
-
PROCEDURAL = 6,
|
|
16
|
-
RENDER_TARGET = 7
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export enum TextureWrap {
|
|
20
|
-
REPEAT = 0,
|
|
21
|
-
CLAMP = 1,
|
|
22
|
-
MIRRORED_REPEAT = 2
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export enum TextureFilter {
|
|
26
|
-
NEAREST_MIPMAP_NEAREST = 0,
|
|
27
|
-
LINEAR_MIPMAP_NEAREST = 1,
|
|
28
|
-
NEAREST_MIPMAP_LINEAR = 2,
|
|
29
|
-
LINEAR_MIPMAP_LINEAR = 3,
|
|
30
|
-
NEAREST = 4,
|
|
31
|
-
LINEAR = 5
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export enum TextureTarget {
|
|
35
|
-
TEXTURE_2D = 0,
|
|
36
|
-
CUBE_MAP = 1
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export enum ProceduralTextureFunction {
|
|
40
|
-
PLAIN_COLOR = 0,
|
|
41
|
-
RANDOM_NOISE = 1,
|
|
42
|
-
DYNAMIC_CUBEMAP = 2,
|
|
43
|
-
FROM_BASE64 = 3,
|
|
44
|
-
CANVAS_2D = 4
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export enum TextureRenderTargetAttachment {
|
|
48
|
-
COLOR_ATTACHMENT_0 = 0,
|
|
49
|
-
COLOR_ATTACHMENT_1 = 1,
|
|
50
|
-
COLOR_ATTACHMENT_2 = 2,
|
|
51
|
-
COLOR_ATTACHMENT_3 = 3,
|
|
52
|
-
COLOR_ATTACHMENT_4 = 4,
|
|
53
|
-
COLOR_ATTACHMENT_5 = 5,
|
|
54
|
-
COLOR_ATTACHMENT_6 = 6,
|
|
55
|
-
COLOR_ATTACHMENT_7 = 7,
|
|
56
|
-
COLOR_ATTACHMENT_8 = 8,
|
|
57
|
-
COLOR_ATTACHMENT_9 = 9,
|
|
58
|
-
COLOR_ATTACHMENT_10 = 10,
|
|
59
|
-
COLOR_ATTACHMENT_11 = 11,
|
|
60
|
-
COLOR_ATTACHMENT_12 = 12,
|
|
61
|
-
COLOR_ATTACHMENT_13 = 13,
|
|
62
|
-
COLOR_ATTACHMENT_14 = 14,
|
|
63
|
-
COLOR_ATTACHMENT_15 = 15,
|
|
64
|
-
DEPTH_ATTACHMENT = 100,
|
|
65
|
-
STENCIL_ATTACHMENT = 200
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export enum TextureComponentFormat {
|
|
69
|
-
UNSIGNED_BYTE = 0,
|
|
70
|
-
FLOAT32 = 1
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export enum TextureChannel {
|
|
74
|
-
R = 1,
|
|
75
|
-
G = 2,
|
|
76
|
-
B = 3,
|
|
77
|
-
A = 4
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Mapeos de enums a nombres para compatibilidad
|
|
81
|
-
export const TextureDataTypeName = Object.freeze({
|
|
82
|
-
[TextureDataType.NONE]: "NONE",
|
|
83
|
-
[TextureDataType.IMAGE]: "IMAGE",
|
|
84
|
-
[TextureDataType.IMAGE_DATA]: "IMAGE_DATA",
|
|
85
|
-
[TextureDataType.CUBEMAP]: "CUBEMAP",
|
|
86
|
-
[TextureDataType.CUBEMAP_DATA]: "CUBEMAP_DATA",
|
|
87
|
-
[TextureDataType.VIDEO]: "VIDEO",
|
|
88
|
-
[TextureDataType.PROCEDURAL]: "PROCEDURAL",
|
|
89
|
-
[TextureDataType.RENDER_TARGET]: "RENDER_TARGET"
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
export const TextureWrapName = Object.freeze({
|
|
93
|
-
[TextureWrap.REPEAT]: "REPEAT",
|
|
94
|
-
[TextureWrap.CLAMP]: "CLAMP",
|
|
95
|
-
[TextureWrap.MIRRORED_REPEAT]: "MIRRORED_REPEAT"
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
export const TextureFilterName = Object.freeze({
|
|
99
|
-
[TextureFilter.NEAREST_MIPMAP_NEAREST]: "NEAREST_MIPMAP_NEAREST",
|
|
100
|
-
[TextureFilter.LINEAR_MIPMAP_NEAREST]: "LINEAR_MIPMAP_NEAREST",
|
|
101
|
-
[TextureFilter.NEAREST_MIPMAP_LINEAR]: "NEAREST_MIPMAP_LINEAR",
|
|
102
|
-
[TextureFilter.LINEAR_MIPMAP_LINEAR]: "LINEAR_MIPMAP_LINEAR",
|
|
103
|
-
[TextureFilter.NEAREST]: "NEAREST",
|
|
104
|
-
[TextureFilter.LINEAR]: "LINEAR"
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
export const TextureTargetName = Object.freeze({
|
|
108
|
-
[TextureTarget.TEXTURE_2D]: "TEXTURE_2D",
|
|
109
|
-
[TextureTarget.CUBE_MAP]: "CUBE_MAP"
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
export const ProceduralTextureFunctionName = Object.freeze({
|
|
113
|
-
[ProceduralTextureFunction.PLAIN_COLOR]: "PLAIN_COLOR",
|
|
114
|
-
[ProceduralTextureFunction.RANDOM_NOISE]: "RANDOM_NOISE",
|
|
115
|
-
[ProceduralTextureFunction.DYNAMIC_CUBEMAP]: "DYNAMIC_CUBEMAP",
|
|
116
|
-
[ProceduralTextureFunction.FROM_BASE64]: "FROM_BASE64",
|
|
117
|
-
[ProceduralTextureFunction.CANVAS_2D]: "CANVAS_2D"
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
export const TextureRenderTargetAttachmentNames = Object.freeze({
|
|
121
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_0]: "COLOR_ATTACHMENT_0",
|
|
122
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_1]: "COLOR_ATTACHMENT_1",
|
|
123
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_2]: "COLOR_ATTACHMENT_2",
|
|
124
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_3]: "COLOR_ATTACHMENT_3",
|
|
125
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_4]: "COLOR_ATTACHMENT_4",
|
|
126
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_5]: "COLOR_ATTACHMENT_5",
|
|
127
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_6]: "COLOR_ATTACHMENT_6",
|
|
128
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_7]: "COLOR_ATTACHMENT_7",
|
|
129
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_8]: "COLOR_ATTACHMENT_8",
|
|
130
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_9]: "COLOR_ATTACHMENT_9",
|
|
131
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_10]: "COLOR_ATTACHMENT_10",
|
|
132
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_11]: "COLOR_ATTACHMENT_11",
|
|
133
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_12]: "COLOR_ATTACHMENT_12",
|
|
134
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_13]: "COLOR_ATTACHMENT_13",
|
|
135
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_14]: "COLOR_ATTACHMENT_14",
|
|
136
|
-
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_15]: "COLOR_ATTACHMENT_15",
|
|
137
|
-
[TextureRenderTargetAttachment.DEPTH_ATTACHMENT]: "DEPTH_ATTACHMENT",
|
|
138
|
-
[TextureRenderTargetAttachment.STENCIL_ATTACHMENT]: "STENCIL_ATTACHMENT"
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
export const TextureComponentFormatNames = Object.freeze({
|
|
142
|
-
[TextureComponentFormat.UNSIGNED_BYTE]: "UNSIGNED_BYTE",
|
|
143
|
-
[TextureComponentFormat.FLOAT32]: "FLOAT32"
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
export const TextureChannelNames = Object.freeze({
|
|
147
|
-
[TextureChannel.R]: "R",
|
|
148
|
-
[TextureChannel.G]: "G",
|
|
149
|
-
[TextureChannel.B]: "B",
|
|
150
|
-
[TextureChannel.A]: "A"
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
interface ExtendedHTMLImageElement extends HTMLImageElement {
|
|
155
|
-
_hash?: string;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
interface RenderTargetImageData {
|
|
159
|
-
currentSize: Vec;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
interface ProceduralCanvasParameters {
|
|
163
|
-
canvas?: HTMLCanvasElement;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
interface ProceduralBase64Parameters {
|
|
167
|
-
imageData?: string;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
type TextureImageData = ExtendedHTMLImageElement | RenderTargetImageData | null;
|
|
171
|
-
|
|
172
|
-
const g_loadedImages: Record<string, Promise<HTMLImageElement> | null> = {};
|
|
173
|
-
let g_resource: Resource | null = null;
|
|
174
|
-
const g_loadPromises: Record<string, Promise<HTMLImageElement> | null> = {};
|
|
175
|
-
const loadImageFromFile = async (fileUrl: string): Promise<HTMLImageElement> => {
|
|
176
|
-
if (!g_resource) {
|
|
177
|
-
g_resource = new Resource();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (g_loadPromises[fileUrl]) {
|
|
181
|
-
console.log(`Image already loaded or loading: ${fileUrl}`);
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
console.log(`Loading image: ${fileUrl}`);
|
|
185
|
-
g_loadPromises[fileUrl] = new Promise(async (resolve, reject) => {
|
|
186
|
-
const image = await g_resource?.load(fileUrl);
|
|
187
|
-
// Flip image Y coord
|
|
188
|
-
const canvas = document.createElement("canvas");
|
|
189
|
-
canvas.width = image.naturalWidth;
|
|
190
|
-
canvas.height = image.naturalHeight;
|
|
191
|
-
|
|
192
|
-
const ctx = canvas.getContext('2d');
|
|
193
|
-
ctx!.fillStyle = '#00000000';
|
|
194
|
-
ctx!.clearRect(0, 0, canvas.width, canvas.height);
|
|
195
|
-
ctx!.fillRect(0, 0, canvas.width, canvas.height);
|
|
196
|
-
ctx!.scale(1, -1);
|
|
197
|
-
ctx!.drawImage(image, 0, 0, canvas.width, -canvas.height);
|
|
198
|
-
const flipImage = new Image();
|
|
199
|
-
const loadFlipImage = () => {
|
|
200
|
-
return new Promise<void>(resolve => {
|
|
201
|
-
flipImage.onload = () => {
|
|
202
|
-
(flipImage as any)._hash = generateImageHash(flipImage);
|
|
203
|
-
resolve();
|
|
204
|
-
}
|
|
205
|
-
flipImage.src = canvas.toDataURL();
|
|
206
|
-
})
|
|
207
|
-
}
|
|
208
|
-
await loadFlipImage();
|
|
209
|
-
|
|
210
|
-
resolve(flipImage);
|
|
211
|
-
})
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return g_loadPromises[fileUrl];
|
|
215
|
-
}
|
|
216
|
-
const loadBase64Image = async (base64Img: string): Promise<HTMLImageElement> => {
|
|
217
|
-
const loadImage = () => {
|
|
218
|
-
return new Promise<HTMLImageElement>(resolve => {
|
|
219
|
-
const image = new Image();
|
|
220
|
-
image.onload = () => {
|
|
221
|
-
resolve(image);
|
|
222
|
-
}
|
|
223
|
-
image.src = base64Img;
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const image = await loadImage();
|
|
228
|
-
|
|
229
|
-
// Flip image Y coord
|
|
230
|
-
const canvas = document.createElement("canvas");
|
|
231
|
-
canvas.width = image.naturalWidth;
|
|
232
|
-
canvas.height = image.naturalHeight;
|
|
233
|
-
|
|
234
|
-
const ctx = canvas.getContext('2d');
|
|
235
|
-
ctx!.fillRect(0, 0, canvas.width, canvas.height);
|
|
236
|
-
ctx!.scale(1, -1);
|
|
237
|
-
ctx!.drawImage(image, 0, 0, canvas.width, -canvas.height);
|
|
238
|
-
const flipImage = new Image();
|
|
239
|
-
const loadFlipImage = () => {
|
|
240
|
-
return new Promise<void>(resolve => {
|
|
241
|
-
flipImage.onload = () => {
|
|
242
|
-
(flipImage as any)._hash = generateImageHash(flipImage);
|
|
243
|
-
resolve();
|
|
244
|
-
}
|
|
245
|
-
flipImage.src = canvas.toDataURL("image/png");
|
|
246
|
-
})
|
|
247
|
-
}
|
|
248
|
-
await loadFlipImage();
|
|
249
|
-
|
|
250
|
-
return flipImage;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
export function textureWrapString(wrap: TextureWrap): string {
|
|
254
|
-
return TextureWrapName[wrap] || "UNKNOWN";
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export function textureDataTypeString(dataType: TextureDataType): string {
|
|
258
|
-
return TextureDataTypeName[dataType] || "UNKNOWN";
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
export function textureFilterString(filter: TextureFilter): string {
|
|
262
|
-
return TextureFilterName[filter] || "UNKNOWN";
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export function textureTargetString(target: TextureTarget): string {
|
|
266
|
-
return TextureTargetName[target] || "UNKNOWN";
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function proceduralTextureFunctionString(func: ProceduralTextureFunction): string {
|
|
270
|
-
return ProceduralTextureFunctionName[func] || "UNKNOWN";
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
export function textureRenderTargetAttachmentString(attachment: TextureRenderTargetAttachment): string {
|
|
274
|
-
return TextureRenderTargetAttachmentNames[attachment] || "UNKNOWN";
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export function textureComponentFormatString(format: TextureComponentFormat): string {
|
|
278
|
-
return TextureComponentFormatNames[format] || "UNKNOWN";
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
export function textureChannelString(channel: TextureChannel): string {
|
|
282
|
-
return TextureChannelNames[channel] || "UNKNOWN";
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
export default class Texture {
|
|
286
|
-
private _canvas: Canvas;
|
|
287
|
-
private _dirty: boolean;
|
|
288
|
-
private _dataType: TextureDataType;
|
|
289
|
-
private _wrapModeX: TextureWrap;
|
|
290
|
-
private _wrapModeY: TextureWrap;
|
|
291
|
-
private _magFilter: TextureFilter;
|
|
292
|
-
private _minFilter: TextureFilter;
|
|
293
|
-
private _target: TextureTarget;
|
|
294
|
-
private _size: Vec;
|
|
295
|
-
private _fileName: string;
|
|
296
|
-
private _proceduralFunction: ProceduralTextureFunction;
|
|
297
|
-
private _proceduralParameters: any;
|
|
298
|
-
private _renderTargetAttachment: TextureRenderTargetAttachment;
|
|
299
|
-
private _componentFormat: TextureComponentFormat;
|
|
300
|
-
private _imageData: TextureImageData;
|
|
301
|
-
private _references: number;
|
|
302
|
-
private _name: string;
|
|
303
|
-
_renderer?: any;
|
|
304
|
-
|
|
305
|
-
constructor(canvas: Canvas | null = null) {
|
|
306
|
-
const c = canvas || Canvas.FirstCanvas();
|
|
307
|
-
if (!c) {
|
|
308
|
-
throw new Error("Error creating Texture: no Canvas available.");
|
|
309
|
-
}
|
|
310
|
-
this._canvas = c;
|
|
311
|
-
|
|
312
|
-
// This flag allows to the renderer to know if the texture object
|
|
313
|
-
// has been updated. In this case, the renderer texture must to
|
|
314
|
-
// be regenerated.
|
|
315
|
-
this._dirty = true;
|
|
316
|
-
|
|
317
|
-
this._dataType = TextureDataType.NONE;
|
|
318
|
-
this._wrapModeX = TextureWrap.REPEAT;
|
|
319
|
-
this._wrapModeY = TextureWrap.REPEAT;
|
|
320
|
-
this._magFilter = TextureFilter.LINEAR;
|
|
321
|
-
this._minFilter = TextureFilter.LINEAR;
|
|
322
|
-
this._target = TextureTarget.TEXTURE_2D;
|
|
323
|
-
this._size = new Vec(64, 64);
|
|
324
|
-
this._fileName = "";
|
|
325
|
-
this._proceduralFunction = ProceduralTextureFunction.PLAIN_COLOR;
|
|
326
|
-
this._proceduralParameters = {};
|
|
327
|
-
this._renderTargetAttachment = TextureRenderTargetAttachment.COLOR_ATTACHMENT_0;
|
|
328
|
-
this._componentFormat = TextureComponentFormat.UNSIGNED_BYTE;
|
|
329
|
-
|
|
330
|
-
// This attribute is generated from the previous attributes, for example,
|
|
331
|
-
// calling loadImageData() after setting the fileName attribute
|
|
332
|
-
this._imageData = null;
|
|
333
|
-
|
|
334
|
-
// Reference counter, to know if a texture can be purged
|
|
335
|
-
this._references = 0;
|
|
336
|
-
|
|
337
|
-
// Object name, form debugging purposes
|
|
338
|
-
this._name = "";
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
get canvas(): Canvas {
|
|
342
|
-
return this._canvas;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
get references(): number {
|
|
346
|
-
return this._references;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
incReferences(): void {
|
|
350
|
-
this._references++;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
decReferences(): void {
|
|
354
|
-
this._references--;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
clone(): Texture {
|
|
358
|
-
const copy = new Texture(this.canvas);
|
|
359
|
-
copy.assign(this);
|
|
360
|
-
return copy;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
assign(other: Texture): void {
|
|
364
|
-
this._dataType = other._dataType;
|
|
365
|
-
this._wrapModeX = other._wrapModeX;
|
|
366
|
-
this._wrapModeY = other._wrapModeY;
|
|
367
|
-
this._magFilter = other._magFilter;
|
|
368
|
-
this._minFilter = other._minFilter;
|
|
369
|
-
this._target = other._target;
|
|
370
|
-
this._size = new Vec(other._size);
|
|
371
|
-
this._fileName = other._fileName;
|
|
372
|
-
this._proceduralFunction = other._proceduralFunction;
|
|
373
|
-
this._proceduralParameters = other._proceduralParameters;
|
|
374
|
-
this._imageData = other._imageData;
|
|
375
|
-
this._renderTargetAttachment = other._renderTargetAttachment;
|
|
376
|
-
this._componentFormat = other._componentFormat;
|
|
377
|
-
|
|
378
|
-
this._dirty = true;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
get dirty(): boolean {
|
|
382
|
-
return this._dirty;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
setUpdated(updated: boolean = true): void {
|
|
386
|
-
this._dirty = !updated;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
get dataType(): TextureDataType { return this._dataType; }
|
|
390
|
-
set dataType(v: TextureDataType) {
|
|
391
|
-
this._dataType = v;
|
|
392
|
-
if (!this.isPowerOfTwo) {
|
|
393
|
-
this.wrapModeXY = TextureWrap.CLAMP;
|
|
394
|
-
}
|
|
395
|
-
this.magFilter = TextureFilter.LINEAR;
|
|
396
|
-
this.minFilter = TextureFilter.LINEAR;
|
|
397
|
-
this._dirty = true;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
get isPowerOfTwo(): boolean {
|
|
401
|
-
const pot = (n: number) => n!==0 && (n & (n - 1)) === 0;
|
|
402
|
-
return pot(this.size[0]) && pot(this.size[1]);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
get wrapModeX(): TextureWrap { return this._wrapModeX; }
|
|
406
|
-
set wrapModeX(v: TextureWrap) { this._wrapModeX = v; this._dirty = true; }
|
|
407
|
-
get wrapModeY(): TextureWrap { return this._wrapModeY; }
|
|
408
|
-
set wrapModeY(v: TextureWrap) { this._wrapModeY = v; this._dirty = true; }
|
|
409
|
-
set wrapModeXY(xy: TextureWrap) {
|
|
410
|
-
this.wrapModeX = xy;
|
|
411
|
-
this.wrapModeY = xy;
|
|
412
|
-
this._dirty = true;
|
|
413
|
-
}
|
|
414
|
-
get magFilter(): TextureFilter { return this._magFilter; }
|
|
415
|
-
set magFilter(v: TextureFilter) {
|
|
416
|
-
if (v === TextureFilter.LINEAR || v === TextureFilter.NEAREST) {
|
|
417
|
-
this._magFilter = v;
|
|
418
|
-
this._dirty = true;
|
|
419
|
-
}
|
|
420
|
-
else {
|
|
421
|
-
console.warn(`Unsupported texture magnification filter: ${TextureFilter[v]}. Command ignored.`);
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
get minFilter(): TextureFilter { return this._minFilter; }
|
|
425
|
-
set minFilter(v: TextureFilter) { this._minFilter = v; this._dirty = true; }
|
|
426
|
-
get target(): TextureTarget { return this._target; }
|
|
427
|
-
set target(v: TextureTarget) { this._target = v; this._dirty = true; }
|
|
428
|
-
get size(): Vec { return this._size; }
|
|
429
|
-
set size(v: Vec | number[]) {
|
|
430
|
-
if (!v.length) {
|
|
431
|
-
throw new Error("Invalid parameter specified setting texture size.");
|
|
432
|
-
}
|
|
433
|
-
this._size = new Vec(v[0],v[1]);
|
|
434
|
-
this._dirty = true;
|
|
435
|
-
}
|
|
436
|
-
get fileName(): string { return this._fileName; }
|
|
437
|
-
set fileName(v: string) { this._fileName = v; this._dirty = true; this._imageData = null; this._name = v; }
|
|
438
|
-
get proceduralFunction(): ProceduralTextureFunction { return this._proceduralFunction; }
|
|
439
|
-
set proceduralFunction(v: ProceduralTextureFunction) { this._proceduralFunction = v; this._dirty = true; }
|
|
440
|
-
get proceduralParameters(): any { return this._proceduralParameters; }
|
|
441
|
-
set proceduralParameters(v: any) {
|
|
442
|
-
if (typeof(v) !== 'object' || !v) {
|
|
443
|
-
throw new Error("Invalid parameter specified setting procedural texture parameters.");
|
|
444
|
-
}
|
|
445
|
-
this._proceduralParameters = v;
|
|
446
|
-
this._dirty = true;
|
|
447
|
-
}
|
|
448
|
-
get renderTargetAttachment(): TextureRenderTargetAttachment { return this._renderTargetAttachment; }
|
|
449
|
-
set renderTargetAttachment(att: TextureRenderTargetAttachment) { this._renderTargetAttachment = att; this._dirty = true; }
|
|
450
|
-
get componentFormat(): TextureComponentFormat { return this._componentFormat; }
|
|
451
|
-
set componentFormat(fmt: TextureComponentFormat) { this._componentFormat = fmt; this._dirty = true; }
|
|
452
|
-
|
|
453
|
-
get mipmapRequired(): boolean {
|
|
454
|
-
return this._minFilter === TextureFilter.NEAREST_MIPMAP_NEAREST ||
|
|
455
|
-
this._minFilter === TextureFilter.LINEAR_MIPMAP_NEAREST ||
|
|
456
|
-
this._minFilter === TextureFilter.NEAREST_MIPMAP_LINEAR ||
|
|
457
|
-
this._minFilter === TextureFilter.LINEAR_MIPMAP_LINEAR ||
|
|
458
|
-
this._magFilter === TextureFilter.NEAREST_MIPMAP_NEAREST ||
|
|
459
|
-
this._magFilter === TextureFilter.LINEAR_MIPMAP_NEAREST ||
|
|
460
|
-
this._magFilter === TextureFilter.NEAREST_MIPMAP_LINEAR ||
|
|
461
|
-
this._magFilter === TextureFilter.LINEAR_MIPMAP_LINEAR;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
// If imageData === undefined it's because the function loadImageData() has not been called
|
|
465
|
-
get imageData(): TextureImageData {
|
|
466
|
-
return this._imageData;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
// The this._renderer variable is initialized by the texture renderer
|
|
470
|
-
get renderer(): any {
|
|
471
|
-
return this._renderer;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
get name(): string {
|
|
475
|
-
return this._name;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
set name(n: string) {
|
|
479
|
-
this._name = n;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
destroy(): void {
|
|
483
|
-
if (this.renderer) {
|
|
484
|
-
this.renderer.destroy();
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
async deserialize(sceneData: any): Promise<void> {
|
|
489
|
-
this._dataType = sceneData.dataType !== undefined ? TextureDataType[sceneData.dataType as keyof typeof TextureDataType] : TextureDataType.NONE;
|
|
490
|
-
this._wrapModeX = sceneData.wrapModeX !== undefined ? TextureWrap[sceneData.wrapModeX as keyof typeof TextureWrap] : TextureWrap.REPEAT;
|
|
491
|
-
this._wrapModeY = sceneData.wrapModeY !== undefined ? TextureWrap[sceneData.wrapModeY as keyof typeof TextureWrap] : TextureWrap.REPEAT;
|
|
492
|
-
this._magFilter = sceneData.magFilter !== undefined ? TextureFilter[sceneData.magFilter as keyof typeof TextureFilter] : TextureFilter.LINEAR;
|
|
493
|
-
this._minFilter = sceneData.minFilter !== undefined ? TextureFilter[sceneData.minFilter as keyof typeof TextureFilter] : TextureFilter.LINEAR;
|
|
494
|
-
this._target = sceneData.target !== undefined ? TextureTarget[sceneData.target as keyof typeof TextureTarget] : TextureTarget.TEXTURE_2D;
|
|
495
|
-
this._size = sceneData.size?.length === 2 ? new Vec(sceneData.size[0], sceneData.size[1]) : new Vec(64, 64);
|
|
496
|
-
this._fileName = sceneData.fileName !== undefined ? sceneData.fileName : "";
|
|
497
|
-
this._proceduralFunction = sceneData.proceduralFunction !== undefined ? ProceduralTextureFunction[sceneData.proceduralFunction as keyof typeof ProceduralTextureFunction] : ProceduralTextureFunction.PLAIN_COLOR;
|
|
498
|
-
this._proceduralParameters = sceneData.proceduralParameters !== undefined ? sceneData.proceduralParameters : {};
|
|
499
|
-
this._renderTargetAttachment = sceneData.renderTargetAttachment !== undefined ? sceneData.renderTargetAttachment : TextureRenderTargetAttachment.COLOR_ATTACHMENT_0;
|
|
500
|
-
this._componentFormat = sceneData.componentFormat !== undefined ? sceneData.componentFormat : TextureComponentFormat.UNSIGNED_BYTE;
|
|
501
|
-
this._name = sceneData.name !== undefined ? sceneData.name : this._name;
|
|
502
|
-
this._dirty = true;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
async serialize(sceneData: any): Promise<void> {
|
|
506
|
-
sceneData.dataType = TextureDataType[this.dataType];
|
|
507
|
-
sceneData.wrapModeX = TextureWrap[this.wrapModeX];
|
|
508
|
-
sceneData.wrapModeY = TextureWrap[this.wrapModeY];
|
|
509
|
-
sceneData.magFilter = TextureFilter[this.magFilter];
|
|
510
|
-
sceneData.minFilter = TextureFilter[this.minFilter];
|
|
511
|
-
sceneData.target = TextureTarget[this.target];
|
|
512
|
-
sceneData.size = this.size;
|
|
513
|
-
sceneData.fileName = this.fileName;
|
|
514
|
-
sceneData.proceduralFunction = ProceduralTextureFunction[this.proceduralFunction];
|
|
515
|
-
sceneData.proceduralParameters = this.proceduralParameters;
|
|
516
|
-
sceneData.renderTargetAttachment = TextureRenderTargetAttachment[this.renderTargetAttachment];
|
|
517
|
-
sceneData.componentFormat = TextureComponentFormat[this.componentFormat];
|
|
518
|
-
sceneData.name = this._name;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
async loadImageData(refresh: boolean = false): Promise<void> {
|
|
522
|
-
if (this.fileName) {
|
|
523
|
-
if (g_loadedImages[this.fileName] && refresh) {
|
|
524
|
-
delete g_loadedImages[this.fileName];
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
let loadPromise = g_loadedImages[this.fileName];
|
|
528
|
-
if (!loadPromise) {
|
|
529
|
-
loadPromise = loadImageFromFile(this.fileName);
|
|
530
|
-
g_loadedImages[this.fileName] = loadPromise;
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
console.debug(`Texture: loadImageData(): image already loaded or is loading: ${this.fileName}`)
|
|
534
|
-
}
|
|
535
|
-
this._imageData = await loadPromise;
|
|
536
|
-
|
|
537
|
-
this._size = new Vec((this._imageData as HTMLImageElement).width, (this._imageData as HTMLImageElement).height);
|
|
538
|
-
|
|
539
|
-
this._dirty = true;
|
|
540
|
-
}
|
|
541
|
-
else if (this.dataType === TextureDataType.RENDER_TARGET) {
|
|
542
|
-
// This object will store data to determine if the
|
|
543
|
-
// texture must to be resized, and other information
|
|
544
|
-
// from the framebuffer
|
|
545
|
-
this._imageData = {
|
|
546
|
-
currentSize: new Vec(this.size)
|
|
547
|
-
};
|
|
548
|
-
this._dirty = true;
|
|
549
|
-
}
|
|
550
|
-
else if (this.proceduralFunction === ProceduralTextureFunction.PLAIN_COLOR) {
|
|
551
|
-
if (this._imageData && refresh === false) {
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
if ((!Array.isArray(this.proceduralParameters) && !(this.proceduralParameters instanceof Vec)) || this.proceduralParameters.length<3) {
|
|
556
|
-
throw new Error("Error generating procedural plain color texture. invalid 'proceduralParameters' set.")
|
|
557
|
-
}
|
|
558
|
-
const color = new Color(this.proceduralParameters);
|
|
559
|
-
const canvas = document.createElement('canvas');
|
|
560
|
-
canvas.width = this.size.x;
|
|
561
|
-
canvas.height = this.size.y;
|
|
562
|
-
const ctx = canvas.getContext('2d');
|
|
563
|
-
ctx!.fillStyle = color.hexColor;
|
|
564
|
-
ctx!.fillRect(0, 0, this.size.x, this.size.y);
|
|
565
|
-
|
|
566
|
-
const loadProceduralImage = () => {
|
|
567
|
-
return new Promise<void>(resolve => {
|
|
568
|
-
this._imageData = new Image();
|
|
569
|
-
this._imageData.onload = () => {
|
|
570
|
-
resolve();
|
|
571
|
-
}
|
|
572
|
-
this._imageData.src = canvas.toDataURL("image/png");
|
|
573
|
-
//document.body.appendChild(canvas);
|
|
574
|
-
})
|
|
575
|
-
}
|
|
576
|
-
await loadProceduralImage();
|
|
577
|
-
|
|
578
|
-
this._dirty = true;
|
|
579
|
-
}
|
|
580
|
-
else if (this.proceduralFunction === ProceduralTextureFunction.FROM_BASE64) {
|
|
581
|
-
if (this._imageData && refresh === false) {
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
if (!/;base64/i.test(this.proceduralParameters?.imageData)) {
|
|
586
|
-
throw new Error("Error generating procedural texture from base64 string. Invalid base64 image");
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
this._imageData = await loadBase64Image(this.proceduralParameters.imageData);
|
|
590
|
-
this._size = new Vec(this._imageData.width, this._imageData.height);
|
|
591
|
-
this._dirty = true;
|
|
592
|
-
}
|
|
593
|
-
else if (this.proceduralFunction == ProceduralTextureFunction.CANVAS_2D) {
|
|
594
|
-
if (this._imageData && refresh === false) {
|
|
595
|
-
return;
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
const canvas = this.proceduralParameters?.canvas;
|
|
599
|
-
if (!/canvas/i.test(canvas?.tagName)) {
|
|
600
|
-
throw new Error("Error generating procedural texture from HTML canvas. Invalid 'canvas' parameter.");
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
const imageData = canvas.toDataURL();
|
|
604
|
-
this._imageData = await loadBase64Image(imageData);
|
|
605
|
-
this._size = new Vec(this._imageData.width, this._imageData.height);
|
|
606
|
-
this._dirty = true;
|
|
607
|
-
}
|
|
608
|
-
else {
|
|
609
|
-
// TODO: load other classes of procedural image data
|
|
610
|
-
throw new Error("Texture: loadImageData(): not implemented");
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
async updateImageData(): Promise<void> {
|
|
615
|
-
// TODO: improve this function. It is possible to optimize the
|
|
616
|
-
// texture refresh in some cases
|
|
617
|
-
this.loadImageData(true);
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
};
|
|
1
|
+
|
|
2
|
+
import Vec from '../math/Vec';
|
|
3
|
+
import Resource from '../tools/Resource';
|
|
4
|
+
import { generateImageHash } from '../tools/image';
|
|
5
|
+
import Color from './Color';
|
|
6
|
+
import Canvas from '../app/Canvas';
|
|
7
|
+
|
|
8
|
+
export enum TextureDataType {
|
|
9
|
+
NONE = 0,
|
|
10
|
+
IMAGE = 1,
|
|
11
|
+
IMAGE_DATA = 2,
|
|
12
|
+
CUBEMAP = 3,
|
|
13
|
+
CUBEMAP_DATA = 4,
|
|
14
|
+
VIDEO = 5,
|
|
15
|
+
PROCEDURAL = 6,
|
|
16
|
+
RENDER_TARGET = 7
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export enum TextureWrap {
|
|
20
|
+
REPEAT = 0,
|
|
21
|
+
CLAMP = 1,
|
|
22
|
+
MIRRORED_REPEAT = 2
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export enum TextureFilter {
|
|
26
|
+
NEAREST_MIPMAP_NEAREST = 0,
|
|
27
|
+
LINEAR_MIPMAP_NEAREST = 1,
|
|
28
|
+
NEAREST_MIPMAP_LINEAR = 2,
|
|
29
|
+
LINEAR_MIPMAP_LINEAR = 3,
|
|
30
|
+
NEAREST = 4,
|
|
31
|
+
LINEAR = 5
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export enum TextureTarget {
|
|
35
|
+
TEXTURE_2D = 0,
|
|
36
|
+
CUBE_MAP = 1
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export enum ProceduralTextureFunction {
|
|
40
|
+
PLAIN_COLOR = 0,
|
|
41
|
+
RANDOM_NOISE = 1,
|
|
42
|
+
DYNAMIC_CUBEMAP = 2,
|
|
43
|
+
FROM_BASE64 = 3,
|
|
44
|
+
CANVAS_2D = 4
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export enum TextureRenderTargetAttachment {
|
|
48
|
+
COLOR_ATTACHMENT_0 = 0,
|
|
49
|
+
COLOR_ATTACHMENT_1 = 1,
|
|
50
|
+
COLOR_ATTACHMENT_2 = 2,
|
|
51
|
+
COLOR_ATTACHMENT_3 = 3,
|
|
52
|
+
COLOR_ATTACHMENT_4 = 4,
|
|
53
|
+
COLOR_ATTACHMENT_5 = 5,
|
|
54
|
+
COLOR_ATTACHMENT_6 = 6,
|
|
55
|
+
COLOR_ATTACHMENT_7 = 7,
|
|
56
|
+
COLOR_ATTACHMENT_8 = 8,
|
|
57
|
+
COLOR_ATTACHMENT_9 = 9,
|
|
58
|
+
COLOR_ATTACHMENT_10 = 10,
|
|
59
|
+
COLOR_ATTACHMENT_11 = 11,
|
|
60
|
+
COLOR_ATTACHMENT_12 = 12,
|
|
61
|
+
COLOR_ATTACHMENT_13 = 13,
|
|
62
|
+
COLOR_ATTACHMENT_14 = 14,
|
|
63
|
+
COLOR_ATTACHMENT_15 = 15,
|
|
64
|
+
DEPTH_ATTACHMENT = 100,
|
|
65
|
+
STENCIL_ATTACHMENT = 200
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export enum TextureComponentFormat {
|
|
69
|
+
UNSIGNED_BYTE = 0,
|
|
70
|
+
FLOAT32 = 1
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export enum TextureChannel {
|
|
74
|
+
R = 1,
|
|
75
|
+
G = 2,
|
|
76
|
+
B = 3,
|
|
77
|
+
A = 4
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Mapeos de enums a nombres para compatibilidad
|
|
81
|
+
export const TextureDataTypeName = Object.freeze({
|
|
82
|
+
[TextureDataType.NONE]: "NONE",
|
|
83
|
+
[TextureDataType.IMAGE]: "IMAGE",
|
|
84
|
+
[TextureDataType.IMAGE_DATA]: "IMAGE_DATA",
|
|
85
|
+
[TextureDataType.CUBEMAP]: "CUBEMAP",
|
|
86
|
+
[TextureDataType.CUBEMAP_DATA]: "CUBEMAP_DATA",
|
|
87
|
+
[TextureDataType.VIDEO]: "VIDEO",
|
|
88
|
+
[TextureDataType.PROCEDURAL]: "PROCEDURAL",
|
|
89
|
+
[TextureDataType.RENDER_TARGET]: "RENDER_TARGET"
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
export const TextureWrapName = Object.freeze({
|
|
93
|
+
[TextureWrap.REPEAT]: "REPEAT",
|
|
94
|
+
[TextureWrap.CLAMP]: "CLAMP",
|
|
95
|
+
[TextureWrap.MIRRORED_REPEAT]: "MIRRORED_REPEAT"
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
export const TextureFilterName = Object.freeze({
|
|
99
|
+
[TextureFilter.NEAREST_MIPMAP_NEAREST]: "NEAREST_MIPMAP_NEAREST",
|
|
100
|
+
[TextureFilter.LINEAR_MIPMAP_NEAREST]: "LINEAR_MIPMAP_NEAREST",
|
|
101
|
+
[TextureFilter.NEAREST_MIPMAP_LINEAR]: "NEAREST_MIPMAP_LINEAR",
|
|
102
|
+
[TextureFilter.LINEAR_MIPMAP_LINEAR]: "LINEAR_MIPMAP_LINEAR",
|
|
103
|
+
[TextureFilter.NEAREST]: "NEAREST",
|
|
104
|
+
[TextureFilter.LINEAR]: "LINEAR"
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
export const TextureTargetName = Object.freeze({
|
|
108
|
+
[TextureTarget.TEXTURE_2D]: "TEXTURE_2D",
|
|
109
|
+
[TextureTarget.CUBE_MAP]: "CUBE_MAP"
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
export const ProceduralTextureFunctionName = Object.freeze({
|
|
113
|
+
[ProceduralTextureFunction.PLAIN_COLOR]: "PLAIN_COLOR",
|
|
114
|
+
[ProceduralTextureFunction.RANDOM_NOISE]: "RANDOM_NOISE",
|
|
115
|
+
[ProceduralTextureFunction.DYNAMIC_CUBEMAP]: "DYNAMIC_CUBEMAP",
|
|
116
|
+
[ProceduralTextureFunction.FROM_BASE64]: "FROM_BASE64",
|
|
117
|
+
[ProceduralTextureFunction.CANVAS_2D]: "CANVAS_2D"
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
export const TextureRenderTargetAttachmentNames = Object.freeze({
|
|
121
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_0]: "COLOR_ATTACHMENT_0",
|
|
122
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_1]: "COLOR_ATTACHMENT_1",
|
|
123
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_2]: "COLOR_ATTACHMENT_2",
|
|
124
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_3]: "COLOR_ATTACHMENT_3",
|
|
125
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_4]: "COLOR_ATTACHMENT_4",
|
|
126
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_5]: "COLOR_ATTACHMENT_5",
|
|
127
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_6]: "COLOR_ATTACHMENT_6",
|
|
128
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_7]: "COLOR_ATTACHMENT_7",
|
|
129
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_8]: "COLOR_ATTACHMENT_8",
|
|
130
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_9]: "COLOR_ATTACHMENT_9",
|
|
131
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_10]: "COLOR_ATTACHMENT_10",
|
|
132
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_11]: "COLOR_ATTACHMENT_11",
|
|
133
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_12]: "COLOR_ATTACHMENT_12",
|
|
134
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_13]: "COLOR_ATTACHMENT_13",
|
|
135
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_14]: "COLOR_ATTACHMENT_14",
|
|
136
|
+
[TextureRenderTargetAttachment.COLOR_ATTACHMENT_15]: "COLOR_ATTACHMENT_15",
|
|
137
|
+
[TextureRenderTargetAttachment.DEPTH_ATTACHMENT]: "DEPTH_ATTACHMENT",
|
|
138
|
+
[TextureRenderTargetAttachment.STENCIL_ATTACHMENT]: "STENCIL_ATTACHMENT"
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
export const TextureComponentFormatNames = Object.freeze({
|
|
142
|
+
[TextureComponentFormat.UNSIGNED_BYTE]: "UNSIGNED_BYTE",
|
|
143
|
+
[TextureComponentFormat.FLOAT32]: "FLOAT32"
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
export const TextureChannelNames = Object.freeze({
|
|
147
|
+
[TextureChannel.R]: "R",
|
|
148
|
+
[TextureChannel.G]: "G",
|
|
149
|
+
[TextureChannel.B]: "B",
|
|
150
|
+
[TextureChannel.A]: "A"
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
interface ExtendedHTMLImageElement extends HTMLImageElement {
|
|
155
|
+
_hash?: string;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
interface RenderTargetImageData {
|
|
159
|
+
currentSize: Vec;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface ProceduralCanvasParameters {
|
|
163
|
+
canvas?: HTMLCanvasElement;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
interface ProceduralBase64Parameters {
|
|
167
|
+
imageData?: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
type TextureImageData = ExtendedHTMLImageElement | RenderTargetImageData | null;
|
|
171
|
+
|
|
172
|
+
const g_loadedImages: Record<string, Promise<HTMLImageElement> | null> = {};
|
|
173
|
+
let g_resource: Resource | null = null;
|
|
174
|
+
const g_loadPromises: Record<string, Promise<HTMLImageElement> | null> = {};
|
|
175
|
+
const loadImageFromFile = async (fileUrl: string): Promise<HTMLImageElement> => {
|
|
176
|
+
if (!g_resource) {
|
|
177
|
+
g_resource = new Resource();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (g_loadPromises[fileUrl]) {
|
|
181
|
+
console.log(`Image already loaded or loading: ${fileUrl}`);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
console.log(`Loading image: ${fileUrl}`);
|
|
185
|
+
g_loadPromises[fileUrl] = new Promise(async (resolve, reject) => {
|
|
186
|
+
const image = await g_resource?.load(fileUrl);
|
|
187
|
+
// Flip image Y coord
|
|
188
|
+
const canvas = document.createElement("canvas");
|
|
189
|
+
canvas.width = image.naturalWidth;
|
|
190
|
+
canvas.height = image.naturalHeight;
|
|
191
|
+
|
|
192
|
+
const ctx = canvas.getContext('2d');
|
|
193
|
+
ctx!.fillStyle = '#00000000';
|
|
194
|
+
ctx!.clearRect(0, 0, canvas.width, canvas.height);
|
|
195
|
+
ctx!.fillRect(0, 0, canvas.width, canvas.height);
|
|
196
|
+
ctx!.scale(1, -1);
|
|
197
|
+
ctx!.drawImage(image, 0, 0, canvas.width, -canvas.height);
|
|
198
|
+
const flipImage = new Image();
|
|
199
|
+
const loadFlipImage = () => {
|
|
200
|
+
return new Promise<void>(resolve => {
|
|
201
|
+
flipImage.onload = () => {
|
|
202
|
+
(flipImage as any)._hash = generateImageHash(flipImage);
|
|
203
|
+
resolve();
|
|
204
|
+
}
|
|
205
|
+
flipImage.src = canvas.toDataURL();
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
await loadFlipImage();
|
|
209
|
+
|
|
210
|
+
resolve(flipImage);
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return g_loadPromises[fileUrl];
|
|
215
|
+
}
|
|
216
|
+
const loadBase64Image = async (base64Img: string): Promise<HTMLImageElement> => {
|
|
217
|
+
const loadImage = () => {
|
|
218
|
+
return new Promise<HTMLImageElement>(resolve => {
|
|
219
|
+
const image = new Image();
|
|
220
|
+
image.onload = () => {
|
|
221
|
+
resolve(image);
|
|
222
|
+
}
|
|
223
|
+
image.src = base64Img;
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const image = await loadImage();
|
|
228
|
+
|
|
229
|
+
// Flip image Y coord
|
|
230
|
+
const canvas = document.createElement("canvas");
|
|
231
|
+
canvas.width = image.naturalWidth;
|
|
232
|
+
canvas.height = image.naturalHeight;
|
|
233
|
+
|
|
234
|
+
const ctx = canvas.getContext('2d');
|
|
235
|
+
ctx!.fillRect(0, 0, canvas.width, canvas.height);
|
|
236
|
+
ctx!.scale(1, -1);
|
|
237
|
+
ctx!.drawImage(image, 0, 0, canvas.width, -canvas.height);
|
|
238
|
+
const flipImage = new Image();
|
|
239
|
+
const loadFlipImage = () => {
|
|
240
|
+
return new Promise<void>(resolve => {
|
|
241
|
+
flipImage.onload = () => {
|
|
242
|
+
(flipImage as any)._hash = generateImageHash(flipImage);
|
|
243
|
+
resolve();
|
|
244
|
+
}
|
|
245
|
+
flipImage.src = canvas.toDataURL("image/png");
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
await loadFlipImage();
|
|
249
|
+
|
|
250
|
+
return flipImage;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function textureWrapString(wrap: TextureWrap): string {
|
|
254
|
+
return TextureWrapName[wrap] || "UNKNOWN";
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function textureDataTypeString(dataType: TextureDataType): string {
|
|
258
|
+
return TextureDataTypeName[dataType] || "UNKNOWN";
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function textureFilterString(filter: TextureFilter): string {
|
|
262
|
+
return TextureFilterName[filter] || "UNKNOWN";
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function textureTargetString(target: TextureTarget): string {
|
|
266
|
+
return TextureTargetName[target] || "UNKNOWN";
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function proceduralTextureFunctionString(func: ProceduralTextureFunction): string {
|
|
270
|
+
return ProceduralTextureFunctionName[func] || "UNKNOWN";
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function textureRenderTargetAttachmentString(attachment: TextureRenderTargetAttachment): string {
|
|
274
|
+
return TextureRenderTargetAttachmentNames[attachment] || "UNKNOWN";
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function textureComponentFormatString(format: TextureComponentFormat): string {
|
|
278
|
+
return TextureComponentFormatNames[format] || "UNKNOWN";
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function textureChannelString(channel: TextureChannel): string {
|
|
282
|
+
return TextureChannelNames[channel] || "UNKNOWN";
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export default class Texture {
|
|
286
|
+
private _canvas: Canvas;
|
|
287
|
+
private _dirty: boolean;
|
|
288
|
+
private _dataType: TextureDataType;
|
|
289
|
+
private _wrapModeX: TextureWrap;
|
|
290
|
+
private _wrapModeY: TextureWrap;
|
|
291
|
+
private _magFilter: TextureFilter;
|
|
292
|
+
private _minFilter: TextureFilter;
|
|
293
|
+
private _target: TextureTarget;
|
|
294
|
+
private _size: Vec;
|
|
295
|
+
private _fileName: string;
|
|
296
|
+
private _proceduralFunction: ProceduralTextureFunction;
|
|
297
|
+
private _proceduralParameters: any;
|
|
298
|
+
private _renderTargetAttachment: TextureRenderTargetAttachment;
|
|
299
|
+
private _componentFormat: TextureComponentFormat;
|
|
300
|
+
private _imageData: TextureImageData;
|
|
301
|
+
private _references: number;
|
|
302
|
+
private _name: string;
|
|
303
|
+
_renderer?: any;
|
|
304
|
+
|
|
305
|
+
constructor(canvas: Canvas | null = null) {
|
|
306
|
+
const c = canvas || Canvas.FirstCanvas();
|
|
307
|
+
if (!c) {
|
|
308
|
+
throw new Error("Error creating Texture: no Canvas available.");
|
|
309
|
+
}
|
|
310
|
+
this._canvas = c;
|
|
311
|
+
|
|
312
|
+
// This flag allows to the renderer to know if the texture object
|
|
313
|
+
// has been updated. In this case, the renderer texture must to
|
|
314
|
+
// be regenerated.
|
|
315
|
+
this._dirty = true;
|
|
316
|
+
|
|
317
|
+
this._dataType = TextureDataType.NONE;
|
|
318
|
+
this._wrapModeX = TextureWrap.REPEAT;
|
|
319
|
+
this._wrapModeY = TextureWrap.REPEAT;
|
|
320
|
+
this._magFilter = TextureFilter.LINEAR;
|
|
321
|
+
this._minFilter = TextureFilter.LINEAR;
|
|
322
|
+
this._target = TextureTarget.TEXTURE_2D;
|
|
323
|
+
this._size = new Vec(64, 64);
|
|
324
|
+
this._fileName = "";
|
|
325
|
+
this._proceduralFunction = ProceduralTextureFunction.PLAIN_COLOR;
|
|
326
|
+
this._proceduralParameters = {};
|
|
327
|
+
this._renderTargetAttachment = TextureRenderTargetAttachment.COLOR_ATTACHMENT_0;
|
|
328
|
+
this._componentFormat = TextureComponentFormat.UNSIGNED_BYTE;
|
|
329
|
+
|
|
330
|
+
// This attribute is generated from the previous attributes, for example,
|
|
331
|
+
// calling loadImageData() after setting the fileName attribute
|
|
332
|
+
this._imageData = null;
|
|
333
|
+
|
|
334
|
+
// Reference counter, to know if a texture can be purged
|
|
335
|
+
this._references = 0;
|
|
336
|
+
|
|
337
|
+
// Object name, form debugging purposes
|
|
338
|
+
this._name = "";
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
get canvas(): Canvas {
|
|
342
|
+
return this._canvas;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
get references(): number {
|
|
346
|
+
return this._references;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
incReferences(): void {
|
|
350
|
+
this._references++;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
decReferences(): void {
|
|
354
|
+
this._references--;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
clone(): Texture {
|
|
358
|
+
const copy = new Texture(this.canvas);
|
|
359
|
+
copy.assign(this);
|
|
360
|
+
return copy;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
assign(other: Texture): void {
|
|
364
|
+
this._dataType = other._dataType;
|
|
365
|
+
this._wrapModeX = other._wrapModeX;
|
|
366
|
+
this._wrapModeY = other._wrapModeY;
|
|
367
|
+
this._magFilter = other._magFilter;
|
|
368
|
+
this._minFilter = other._minFilter;
|
|
369
|
+
this._target = other._target;
|
|
370
|
+
this._size = new Vec(other._size);
|
|
371
|
+
this._fileName = other._fileName;
|
|
372
|
+
this._proceduralFunction = other._proceduralFunction;
|
|
373
|
+
this._proceduralParameters = other._proceduralParameters;
|
|
374
|
+
this._imageData = other._imageData;
|
|
375
|
+
this._renderTargetAttachment = other._renderTargetAttachment;
|
|
376
|
+
this._componentFormat = other._componentFormat;
|
|
377
|
+
|
|
378
|
+
this._dirty = true;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
get dirty(): boolean {
|
|
382
|
+
return this._dirty;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
setUpdated(updated: boolean = true): void {
|
|
386
|
+
this._dirty = !updated;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
get dataType(): TextureDataType { return this._dataType; }
|
|
390
|
+
set dataType(v: TextureDataType) {
|
|
391
|
+
this._dataType = v;
|
|
392
|
+
if (!this.isPowerOfTwo) {
|
|
393
|
+
this.wrapModeXY = TextureWrap.CLAMP;
|
|
394
|
+
}
|
|
395
|
+
this.magFilter = TextureFilter.LINEAR;
|
|
396
|
+
this.minFilter = TextureFilter.LINEAR;
|
|
397
|
+
this._dirty = true;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
get isPowerOfTwo(): boolean {
|
|
401
|
+
const pot = (n: number) => n!==0 && (n & (n - 1)) === 0;
|
|
402
|
+
return pot(this.size[0]) && pot(this.size[1]);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
get wrapModeX(): TextureWrap { return this._wrapModeX; }
|
|
406
|
+
set wrapModeX(v: TextureWrap) { this._wrapModeX = v; this._dirty = true; }
|
|
407
|
+
get wrapModeY(): TextureWrap { return this._wrapModeY; }
|
|
408
|
+
set wrapModeY(v: TextureWrap) { this._wrapModeY = v; this._dirty = true; }
|
|
409
|
+
set wrapModeXY(xy: TextureWrap) {
|
|
410
|
+
this.wrapModeX = xy;
|
|
411
|
+
this.wrapModeY = xy;
|
|
412
|
+
this._dirty = true;
|
|
413
|
+
}
|
|
414
|
+
get magFilter(): TextureFilter { return this._magFilter; }
|
|
415
|
+
set magFilter(v: TextureFilter) {
|
|
416
|
+
if (v === TextureFilter.LINEAR || v === TextureFilter.NEAREST) {
|
|
417
|
+
this._magFilter = v;
|
|
418
|
+
this._dirty = true;
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
console.warn(`Unsupported texture magnification filter: ${TextureFilter[v]}. Command ignored.`);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
get minFilter(): TextureFilter { return this._minFilter; }
|
|
425
|
+
set minFilter(v: TextureFilter) { this._minFilter = v; this._dirty = true; }
|
|
426
|
+
get target(): TextureTarget { return this._target; }
|
|
427
|
+
set target(v: TextureTarget) { this._target = v; this._dirty = true; }
|
|
428
|
+
get size(): Vec { return this._size; }
|
|
429
|
+
set size(v: Vec | number[]) {
|
|
430
|
+
if (!v.length) {
|
|
431
|
+
throw new Error("Invalid parameter specified setting texture size.");
|
|
432
|
+
}
|
|
433
|
+
this._size = new Vec(v[0],v[1]);
|
|
434
|
+
this._dirty = true;
|
|
435
|
+
}
|
|
436
|
+
get fileName(): string { return this._fileName; }
|
|
437
|
+
set fileName(v: string) { this._fileName = v; this._dirty = true; this._imageData = null; this._name = v; }
|
|
438
|
+
get proceduralFunction(): ProceduralTextureFunction { return this._proceduralFunction; }
|
|
439
|
+
set proceduralFunction(v: ProceduralTextureFunction) { this._proceduralFunction = v; this._dirty = true; }
|
|
440
|
+
get proceduralParameters(): any { return this._proceduralParameters; }
|
|
441
|
+
set proceduralParameters(v: any) {
|
|
442
|
+
if (typeof(v) !== 'object' || !v) {
|
|
443
|
+
throw new Error("Invalid parameter specified setting procedural texture parameters.");
|
|
444
|
+
}
|
|
445
|
+
this._proceduralParameters = v;
|
|
446
|
+
this._dirty = true;
|
|
447
|
+
}
|
|
448
|
+
get renderTargetAttachment(): TextureRenderTargetAttachment { return this._renderTargetAttachment; }
|
|
449
|
+
set renderTargetAttachment(att: TextureRenderTargetAttachment) { this._renderTargetAttachment = att; this._dirty = true; }
|
|
450
|
+
get componentFormat(): TextureComponentFormat { return this._componentFormat; }
|
|
451
|
+
set componentFormat(fmt: TextureComponentFormat) { this._componentFormat = fmt; this._dirty = true; }
|
|
452
|
+
|
|
453
|
+
get mipmapRequired(): boolean {
|
|
454
|
+
return this._minFilter === TextureFilter.NEAREST_MIPMAP_NEAREST ||
|
|
455
|
+
this._minFilter === TextureFilter.LINEAR_MIPMAP_NEAREST ||
|
|
456
|
+
this._minFilter === TextureFilter.NEAREST_MIPMAP_LINEAR ||
|
|
457
|
+
this._minFilter === TextureFilter.LINEAR_MIPMAP_LINEAR ||
|
|
458
|
+
this._magFilter === TextureFilter.NEAREST_MIPMAP_NEAREST ||
|
|
459
|
+
this._magFilter === TextureFilter.LINEAR_MIPMAP_NEAREST ||
|
|
460
|
+
this._magFilter === TextureFilter.NEAREST_MIPMAP_LINEAR ||
|
|
461
|
+
this._magFilter === TextureFilter.LINEAR_MIPMAP_LINEAR;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// If imageData === undefined it's because the function loadImageData() has not been called
|
|
465
|
+
get imageData(): TextureImageData {
|
|
466
|
+
return this._imageData;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// The this._renderer variable is initialized by the texture renderer
|
|
470
|
+
get renderer(): any {
|
|
471
|
+
return this._renderer;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
get name(): string {
|
|
475
|
+
return this._name;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
set name(n: string) {
|
|
479
|
+
this._name = n;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
destroy(): void {
|
|
483
|
+
if (this.renderer) {
|
|
484
|
+
this.renderer.destroy();
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
async deserialize(sceneData: any): Promise<void> {
|
|
489
|
+
this._dataType = sceneData.dataType !== undefined ? TextureDataType[sceneData.dataType as keyof typeof TextureDataType] : TextureDataType.NONE;
|
|
490
|
+
this._wrapModeX = sceneData.wrapModeX !== undefined ? TextureWrap[sceneData.wrapModeX as keyof typeof TextureWrap] : TextureWrap.REPEAT;
|
|
491
|
+
this._wrapModeY = sceneData.wrapModeY !== undefined ? TextureWrap[sceneData.wrapModeY as keyof typeof TextureWrap] : TextureWrap.REPEAT;
|
|
492
|
+
this._magFilter = sceneData.magFilter !== undefined ? TextureFilter[sceneData.magFilter as keyof typeof TextureFilter] : TextureFilter.LINEAR;
|
|
493
|
+
this._minFilter = sceneData.minFilter !== undefined ? TextureFilter[sceneData.minFilter as keyof typeof TextureFilter] : TextureFilter.LINEAR;
|
|
494
|
+
this._target = sceneData.target !== undefined ? TextureTarget[sceneData.target as keyof typeof TextureTarget] : TextureTarget.TEXTURE_2D;
|
|
495
|
+
this._size = sceneData.size?.length === 2 ? new Vec(sceneData.size[0], sceneData.size[1]) : new Vec(64, 64);
|
|
496
|
+
this._fileName = sceneData.fileName !== undefined ? sceneData.fileName : "";
|
|
497
|
+
this._proceduralFunction = sceneData.proceduralFunction !== undefined ? ProceduralTextureFunction[sceneData.proceduralFunction as keyof typeof ProceduralTextureFunction] : ProceduralTextureFunction.PLAIN_COLOR;
|
|
498
|
+
this._proceduralParameters = sceneData.proceduralParameters !== undefined ? sceneData.proceduralParameters : {};
|
|
499
|
+
this._renderTargetAttachment = sceneData.renderTargetAttachment !== undefined ? sceneData.renderTargetAttachment : TextureRenderTargetAttachment.COLOR_ATTACHMENT_0;
|
|
500
|
+
this._componentFormat = sceneData.componentFormat !== undefined ? sceneData.componentFormat : TextureComponentFormat.UNSIGNED_BYTE;
|
|
501
|
+
this._name = sceneData.name !== undefined ? sceneData.name : this._name;
|
|
502
|
+
this._dirty = true;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
async serialize(sceneData: any): Promise<void> {
|
|
506
|
+
sceneData.dataType = TextureDataType[this.dataType];
|
|
507
|
+
sceneData.wrapModeX = TextureWrap[this.wrapModeX];
|
|
508
|
+
sceneData.wrapModeY = TextureWrap[this.wrapModeY];
|
|
509
|
+
sceneData.magFilter = TextureFilter[this.magFilter];
|
|
510
|
+
sceneData.minFilter = TextureFilter[this.minFilter];
|
|
511
|
+
sceneData.target = TextureTarget[this.target];
|
|
512
|
+
sceneData.size = this.size;
|
|
513
|
+
sceneData.fileName = this.fileName;
|
|
514
|
+
sceneData.proceduralFunction = ProceduralTextureFunction[this.proceduralFunction];
|
|
515
|
+
sceneData.proceduralParameters = this.proceduralParameters;
|
|
516
|
+
sceneData.renderTargetAttachment = TextureRenderTargetAttachment[this.renderTargetAttachment];
|
|
517
|
+
sceneData.componentFormat = TextureComponentFormat[this.componentFormat];
|
|
518
|
+
sceneData.name = this._name;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async loadImageData(refresh: boolean = false): Promise<void> {
|
|
522
|
+
if (this.fileName) {
|
|
523
|
+
if (g_loadedImages[this.fileName] && refresh) {
|
|
524
|
+
delete g_loadedImages[this.fileName];
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
let loadPromise = g_loadedImages[this.fileName];
|
|
528
|
+
if (!loadPromise) {
|
|
529
|
+
loadPromise = loadImageFromFile(this.fileName);
|
|
530
|
+
g_loadedImages[this.fileName] = loadPromise;
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
console.debug(`Texture: loadImageData(): image already loaded or is loading: ${this.fileName}`)
|
|
534
|
+
}
|
|
535
|
+
this._imageData = await loadPromise;
|
|
536
|
+
|
|
537
|
+
this._size = new Vec((this._imageData as HTMLImageElement).width, (this._imageData as HTMLImageElement).height);
|
|
538
|
+
|
|
539
|
+
this._dirty = true;
|
|
540
|
+
}
|
|
541
|
+
else if (this.dataType === TextureDataType.RENDER_TARGET) {
|
|
542
|
+
// This object will store data to determine if the
|
|
543
|
+
// texture must to be resized, and other information
|
|
544
|
+
// from the framebuffer
|
|
545
|
+
this._imageData = {
|
|
546
|
+
currentSize: new Vec(this.size)
|
|
547
|
+
};
|
|
548
|
+
this._dirty = true;
|
|
549
|
+
}
|
|
550
|
+
else if (this.proceduralFunction === ProceduralTextureFunction.PLAIN_COLOR) {
|
|
551
|
+
if (this._imageData && refresh === false) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if ((!Array.isArray(this.proceduralParameters) && !(this.proceduralParameters instanceof Vec)) || this.proceduralParameters.length<3) {
|
|
556
|
+
throw new Error("Error generating procedural plain color texture. invalid 'proceduralParameters' set.")
|
|
557
|
+
}
|
|
558
|
+
const color = new Color(this.proceduralParameters);
|
|
559
|
+
const canvas = document.createElement('canvas');
|
|
560
|
+
canvas.width = this.size.x;
|
|
561
|
+
canvas.height = this.size.y;
|
|
562
|
+
const ctx = canvas.getContext('2d');
|
|
563
|
+
ctx!.fillStyle = color.hexColor;
|
|
564
|
+
ctx!.fillRect(0, 0, this.size.x, this.size.y);
|
|
565
|
+
|
|
566
|
+
const loadProceduralImage = () => {
|
|
567
|
+
return new Promise<void>(resolve => {
|
|
568
|
+
this._imageData = new Image();
|
|
569
|
+
this._imageData.onload = () => {
|
|
570
|
+
resolve();
|
|
571
|
+
}
|
|
572
|
+
this._imageData.src = canvas.toDataURL("image/png");
|
|
573
|
+
//document.body.appendChild(canvas);
|
|
574
|
+
})
|
|
575
|
+
}
|
|
576
|
+
await loadProceduralImage();
|
|
577
|
+
|
|
578
|
+
this._dirty = true;
|
|
579
|
+
}
|
|
580
|
+
else if (this.proceduralFunction === ProceduralTextureFunction.FROM_BASE64) {
|
|
581
|
+
if (this._imageData && refresh === false) {
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
if (!/;base64/i.test(this.proceduralParameters?.imageData)) {
|
|
586
|
+
throw new Error("Error generating procedural texture from base64 string. Invalid base64 image");
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
this._imageData = await loadBase64Image(this.proceduralParameters.imageData);
|
|
590
|
+
this._size = new Vec(this._imageData.width, this._imageData.height);
|
|
591
|
+
this._dirty = true;
|
|
592
|
+
}
|
|
593
|
+
else if (this.proceduralFunction == ProceduralTextureFunction.CANVAS_2D) {
|
|
594
|
+
if (this._imageData && refresh === false) {
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const canvas = this.proceduralParameters?.canvas;
|
|
599
|
+
if (!/canvas/i.test(canvas?.tagName)) {
|
|
600
|
+
throw new Error("Error generating procedural texture from HTML canvas. Invalid 'canvas' parameter.");
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const imageData = canvas.toDataURL();
|
|
604
|
+
this._imageData = await loadBase64Image(imageData);
|
|
605
|
+
this._size = new Vec(this._imageData.width, this._imageData.height);
|
|
606
|
+
this._dirty = true;
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
// TODO: load other classes of procedural image data
|
|
610
|
+
throw new Error("Texture: loadImageData(): not implemented");
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
async updateImageData(): Promise<void> {
|
|
615
|
+
// TODO: improve this function. It is possible to optimize the
|
|
616
|
+
// texture refresh in some cases
|
|
617
|
+
this.loadImageData(true);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
};
|