@carbonenginejs/runtime-utils 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +23 -0
- package/README.md +56 -0
- package/THIRD-PARTY-NOTICES.md +38 -0
- package/docs/README.md +81 -0
- package/docs/architecture.md +101 -0
- package/docs/concepts/foundation-consolidation.md +86 -0
- package/docs/const-kb.md +92 -0
- package/docs/core-types/DECORATOR-TODOS.md +25 -0
- package/docs/core-types/README.md +203 -0
- package/docs/reference/api.md +70 -0
- package/docs/reference/classes/README.md +115 -0
- package/package.json +132 -0
- package/src/arrays.js +5 -0
- package/src/audio/audioFormats.js +34 -0
- package/src/audio/index.js +1 -0
- package/src/box3.js +1385 -0
- package/src/bytes.js +56 -0
- package/src/compression.js +56 -0
- package/src/constants/index.js +6 -0
- package/src/constants.js +15 -0
- package/src/curve.js +419 -0
- package/src/d3d/dxgiFormats.js +46 -0
- package/src/d3d/index.js +2 -0
- package/src/d3d/primitiveTopology.js +11 -0
- package/src/document/CjsCarbonDocument.js +212 -0
- package/src/document/CjsClassRegistry.js +373 -0
- package/src/document/CjsDocumentDehydrator.js +142 -0
- package/src/document/CjsDocumentHydrator.js +156 -0
- package/src/document/CjsStructRegistry.js +348 -0
- package/src/document/hydrationAdapter.js +129 -0
- package/src/document/index.js +6 -0
- package/src/geometry/box.js +137 -0
- package/src/geometry/cylinder.js +244 -0
- package/src/geometry/helpers/LICENSE +15 -0
- package/src/geometry/helpers/earcut.js +766 -0
- package/src/geometry/helpers/misc.js +103 -0
- package/src/geometry/index.js +8 -0
- package/src/geometry/json.js +165 -0
- package/src/geometry/lathe.js +172 -0
- package/src/geometry/octahedron.js +0 -0
- package/src/geometry/plane.js +81 -0
- package/src/geometry/shape.js +95 -0
- package/src/geometry/sphere.js +123 -0
- package/src/geometry/torus.js +96 -0
- package/src/graphics/colorSpaces.js +22 -0
- package/src/graphics/index.js +4 -0
- package/src/graphics/pixelFormats.js +158 -0
- package/src/graphics/textureDimensions.js +22 -0
- package/src/graphics/trinityEnums.js +87 -0
- package/src/index.js +58 -0
- package/src/is.js +524 -0
- package/src/json.js +23 -0
- package/src/lifecycle/CjsLifecycleState.js +77 -0
- package/src/lifecycle/index.js +1 -0
- package/src/lne3.js +497 -0
- package/src/lookup.js +48 -0
- package/src/mat3.js +131 -0
- package/src/mat4.js +699 -0
- package/src/math/index.js +25 -0
- package/src/math/scalar.js +63 -0
- package/src/media/index.js +1 -0
- package/src/media/mediaTypes.js +50 -0
- package/src/mesh.js +394 -0
- package/src/model/CjsEventEmitter.js +333 -0
- package/src/model/CjsModel.js +1544 -0
- package/src/model/CjsModelState.js +72 -0
- package/src/model/index.js +4 -0
- package/src/model/sourceRecordUtils.js +54 -0
- package/src/noise.js +310 -0
- package/src/num.js +827 -0
- package/src/path.js +21 -0
- package/src/pln.js +762 -0
- package/src/pool.js +160 -0
- package/src/quat.js +144 -0
- package/src/ray3.js +1085 -0
- package/src/renderContext/formats.js +145 -0
- package/src/renderContext/index.js +5 -0
- package/src/renderContext/presentation.js +125 -0
- package/src/renderContext/resources.js +27 -0
- package/src/renderContext/upscaling.js +22 -0
- package/src/renderContext/window.js +20 -0
- package/src/runtime/CjsRuntimeState.js +50 -0
- package/src/schema/CjsSchema.js +1009 -0
- package/src/schema/index.js +17 -0
- package/src/shader/index.js +1 -0
- package/src/shader/shaderStages.js +37 -0
- package/src/sph3.js +754 -0
- package/src/tangent.js +288 -0
- package/src/text.js +40 -0
- package/src/tri3.js +650 -0
- package/src/types/carbonTypes.js +635 -0
- package/src/types/index.js +2 -0
- package/src/utils.js +58 -0
- package/src/validation.js +46 -0
- package/src/vec2.js +229 -0
- package/src/vec3.js +1172 -0
- package/src/vec4.js +347 -0
- package/src/vertex.js +108 -0
- package/src/webgpu/index.js +1 -0
- package/src/webgpu/textureFormats.js +121 -0
package/src/vec4.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import * as glVec4 from "gl-matrix/esm/vec4.js";
|
|
2
|
+
import { num } from "./num.js";
|
|
3
|
+
import { pool } from "./pool.js";
|
|
4
|
+
|
|
5
|
+
const vec4 = { ...glVec4 };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Vector 4
|
|
9
|
+
* @typedef {Float32Array} vec4
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Allocates a pooled vec4
|
|
15
|
+
* @returns {Float32Array|vec4}
|
|
16
|
+
*/
|
|
17
|
+
vec4.alloc = function()
|
|
18
|
+
{
|
|
19
|
+
return pool.allocF32(4);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Unallocates a pooled vec4
|
|
24
|
+
* @param {vec4|Float32Array} a
|
|
25
|
+
*/
|
|
26
|
+
vec4.unalloc = function(a)
|
|
27
|
+
{
|
|
28
|
+
pool.freeType(a);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
vec4.ZERO = vec4.fromValues(0, 0, 0, 0);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates a linear color vec4 with opaque alpha
|
|
35
|
+
* @returns {vec4}
|
|
36
|
+
*/
|
|
37
|
+
vec4.createLinear = function()
|
|
38
|
+
{
|
|
39
|
+
const out = vec4.create();
|
|
40
|
+
out[3] = 1;
|
|
41
|
+
return out;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Adds a scalar to a vec4
|
|
46
|
+
*
|
|
47
|
+
* @param {vec4} out
|
|
48
|
+
* @param {vec4} a
|
|
49
|
+
* @param {Number} s
|
|
50
|
+
* @returns {vec4} out
|
|
51
|
+
*/
|
|
52
|
+
vec4.addScalar = function(out, a, s)
|
|
53
|
+
{
|
|
54
|
+
out[0] = a[0] + s;
|
|
55
|
+
out[1] = a[1] + s;
|
|
56
|
+
out[2] = a[2] + s;
|
|
57
|
+
out[3] = a[3] + s;
|
|
58
|
+
return out;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Checks if all elements are 0
|
|
63
|
+
* @param {vec4} a
|
|
64
|
+
* @returns {boolean}
|
|
65
|
+
*/
|
|
66
|
+
vec4.isEmpty = function(a)
|
|
67
|
+
{
|
|
68
|
+
return a[0] === 0 && a[1] === 0 && a[2] === 0 && a[3] === 0;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Divides a vec4 by a scalar
|
|
73
|
+
*
|
|
74
|
+
* @param {vec4} out
|
|
75
|
+
* @param {vec4} a
|
|
76
|
+
* @param {Number} s
|
|
77
|
+
* @returns {vec4} out
|
|
78
|
+
*/
|
|
79
|
+
vec4.divideScalar = function(out, a, s)
|
|
80
|
+
{
|
|
81
|
+
return vec4.multiplyScalar(out, a, 1 / s);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Multiplies a vec4 by a scalar
|
|
87
|
+
*
|
|
88
|
+
* @param {vec4} out
|
|
89
|
+
* @param {vec4} a
|
|
90
|
+
* @param {Number} s
|
|
91
|
+
* @returns {vec4} out
|
|
92
|
+
*/
|
|
93
|
+
vec4.multiplyScalar = function(out, a, s)
|
|
94
|
+
{
|
|
95
|
+
out[0] = a[0] * s;
|
|
96
|
+
out[1] = a[1] * s;
|
|
97
|
+
out[2] = a[2] * s;
|
|
98
|
+
out[3] = a[3] * s;
|
|
99
|
+
return out;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Sets a vec4 from a scalar
|
|
104
|
+
*
|
|
105
|
+
* @param {vec4} out
|
|
106
|
+
* @param {Number} s
|
|
107
|
+
* @returns {vec4} out
|
|
108
|
+
*/
|
|
109
|
+
vec4.setScalar = function(out, s)
|
|
110
|
+
{
|
|
111
|
+
out[0] = s;
|
|
112
|
+
out[1] = s;
|
|
113
|
+
out[2] = s;
|
|
114
|
+
out[3] = s;
|
|
115
|
+
return out;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Subtracts a scalar from a vec4
|
|
120
|
+
*
|
|
121
|
+
* @param {vec4} out
|
|
122
|
+
* @param {vec4} a
|
|
123
|
+
* @param {Number} s
|
|
124
|
+
* @returns {vec4} out
|
|
125
|
+
*/
|
|
126
|
+
vec4.subtractScalar = function(out, a, s)
|
|
127
|
+
{
|
|
128
|
+
out[0] = a[0] - s;
|
|
129
|
+
out[1] = a[1] - s;
|
|
130
|
+
out[2] = a[2] - s;
|
|
131
|
+
out[3] = a[3] - s;
|
|
132
|
+
return out;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Converts from linear color to rgba
|
|
137
|
+
* @param {vec4} out
|
|
138
|
+
* @param {vec4} linear
|
|
139
|
+
* @param {boolean} [denormalizeAlpha]
|
|
140
|
+
* @returns {vec4}
|
|
141
|
+
*/
|
|
142
|
+
vec4.toRGBA = function(out, linear, denormalizeAlpha)
|
|
143
|
+
{
|
|
144
|
+
out[0] = num.colorFromLinear(linear[0]);
|
|
145
|
+
out[1] = num.colorFromLinear(linear[1]);
|
|
146
|
+
out[2] = num.colorFromLinear(linear[2]);
|
|
147
|
+
out[3] = denormalizeAlpha ? num.colorFromLinear(linear[3]) : linear[3];
|
|
148
|
+
return out;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Converts to linear color from rgba
|
|
153
|
+
* @param {vec4} out
|
|
154
|
+
* @param {vec4} rgba
|
|
155
|
+
* @param {boolean} [denormalizedAlpha]
|
|
156
|
+
* @returns {vec4} out
|
|
157
|
+
*/
|
|
158
|
+
vec4.fromRGBA = function(out, rgba, denormalizedAlpha)
|
|
159
|
+
{
|
|
160
|
+
out[0] = num.linearFromColor(rgba[0]);
|
|
161
|
+
out[1] = num.linearFromColor(rgba[1]);
|
|
162
|
+
out[2] = num.linearFromColor(rgba[2]);
|
|
163
|
+
out[3] = denormalizedAlpha ? num.linearFromColor(rgba[3]) : rgba[3];
|
|
164
|
+
return out;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Converts to linear color from rgb
|
|
169
|
+
* @param {vec4} out
|
|
170
|
+
* @param {vec3} rgb
|
|
171
|
+
* @param {Number} [linearAlpha=1]
|
|
172
|
+
* @returns {vec4} out
|
|
173
|
+
*/
|
|
174
|
+
vec4.fromRGB = function(out, rgb, linearAlpha = 1)
|
|
175
|
+
{
|
|
176
|
+
out[0] = num.linearFromColor(rgb[0]);
|
|
177
|
+
out[1] = num.linearFromColor(rgb[1]);
|
|
178
|
+
out[2] = num.linearFromColor(rgb[2]);
|
|
179
|
+
out[3] = linearAlpha;
|
|
180
|
+
return out;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Gets hex value with alpha from linear color
|
|
185
|
+
* @param {vec4} linear
|
|
186
|
+
* @returns {string} hex value
|
|
187
|
+
*/
|
|
188
|
+
vec4.toHexA = function(linear)
|
|
189
|
+
{
|
|
190
|
+
return "#" +
|
|
191
|
+
num.hexFromLinear(linear[0]) +
|
|
192
|
+
num.hexFromLinear(linear[1]) +
|
|
193
|
+
num.hexFromLinear(linear[2]) +
|
|
194
|
+
num.hexFromLinear(linear[3]);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Gets hex value from linear color
|
|
199
|
+
* @param {vec4} linear
|
|
200
|
+
* @returns {string} hex value
|
|
201
|
+
*/
|
|
202
|
+
vec4.toHex = function(linear)
|
|
203
|
+
{
|
|
204
|
+
return "#" +
|
|
205
|
+
num.hexFromLinear(linear[0]) +
|
|
206
|
+
num.hexFromLinear(linear[1]) +
|
|
207
|
+
num.hexFromLinear(linear[2]);
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Gets linear color from hex or hex with alpha
|
|
212
|
+
* @param {vec4} out
|
|
213
|
+
* @param {String} hex
|
|
214
|
+
* @param {Number} [defaultAlpha=1]
|
|
215
|
+
* @return {vec4}
|
|
216
|
+
*/
|
|
217
|
+
vec4.fromHex = function(out, hex, defaultAlpha = 1)
|
|
218
|
+
{
|
|
219
|
+
// Set empty color in case of error
|
|
220
|
+
out[0] = 0;
|
|
221
|
+
out[1] = 0;
|
|
222
|
+
out[2] = 0;
|
|
223
|
+
out[3] = defaultAlpha;
|
|
224
|
+
|
|
225
|
+
if (typeof hex !== "string" || !/^#(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i.test(hex))
|
|
226
|
+
{
|
|
227
|
+
throw new TypeError("Invalid hex");
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// RGB hex
|
|
231
|
+
if (hex.length === 4 || hex.length === 5)
|
|
232
|
+
{
|
|
233
|
+
out[0] = ("0x" + hex[1] + hex[1]) / 255;
|
|
234
|
+
out[1] = ("0x" + hex[2] + hex[2]) / 255;
|
|
235
|
+
out[2] = ("0x" + hex[3] + hex[3]) / 255;
|
|
236
|
+
if (hex.length === 5) out[3] = ("0x" + hex[4] + hex[4]) / 255;
|
|
237
|
+
}
|
|
238
|
+
// RGB hex
|
|
239
|
+
else if (hex.length === 7 || hex.length === 9)
|
|
240
|
+
{
|
|
241
|
+
out[0] = ("0x" + hex[1] + hex[2]) / 255;
|
|
242
|
+
out[1] = ("0x" + hex[3] + hex[4]) / 255;
|
|
243
|
+
out[2] = ("0x" + hex[5] + hex[6]) / 255;
|
|
244
|
+
if (hex.length === 9) out[3] = ("0x" + hex[7] + hex[8]) / 255;
|
|
245
|
+
}
|
|
246
|
+
return out;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Sets a vec4 from an array with an optional offset
|
|
251
|
+
* @param {vec3} out
|
|
252
|
+
* @param {TypedArray|Array} array
|
|
253
|
+
* @param {Number} [offset=0]
|
|
254
|
+
* @returns {vec3} out
|
|
255
|
+
*/
|
|
256
|
+
vec4.fromArray = function(out, array, offset=0)
|
|
257
|
+
{
|
|
258
|
+
out[0] = array[offset];
|
|
259
|
+
out[1] = array[offset + 1];
|
|
260
|
+
out[2] = array[offset + 2];
|
|
261
|
+
out[3] = array[offset + 3];
|
|
262
|
+
return out;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
vec4.setArray = vec4.fromArray;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Writes a vec4 to an array at an optional offset.
|
|
269
|
+
* @param {vec4} a
|
|
270
|
+
* @param {TypedArray|Array} array
|
|
271
|
+
* @param {Number} [offset=0]
|
|
272
|
+
* @returns {vec4} a
|
|
273
|
+
*/
|
|
274
|
+
vec4.toArray = function(a, array, offset=0)
|
|
275
|
+
{
|
|
276
|
+
array[offset] = a[0];
|
|
277
|
+
array[offset + 1] = a[1];
|
|
278
|
+
array[offset + 2] = a[2];
|
|
279
|
+
array[offset + 3] = a[3];
|
|
280
|
+
return a;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
export { vec4 };
|
|
285
|
+
|
|
286
|
+
export const {
|
|
287
|
+
add,
|
|
288
|
+
ceil,
|
|
289
|
+
clone,
|
|
290
|
+
copy,
|
|
291
|
+
create,
|
|
292
|
+
cross,
|
|
293
|
+
dist,
|
|
294
|
+
distance,
|
|
295
|
+
div,
|
|
296
|
+
divide,
|
|
297
|
+
dot,
|
|
298
|
+
equals,
|
|
299
|
+
exactEquals,
|
|
300
|
+
floor,
|
|
301
|
+
forEach,
|
|
302
|
+
fromValues,
|
|
303
|
+
inverse,
|
|
304
|
+
len,
|
|
305
|
+
length,
|
|
306
|
+
lerp,
|
|
307
|
+
max,
|
|
308
|
+
min,
|
|
309
|
+
mul,
|
|
310
|
+
multiply,
|
|
311
|
+
negate,
|
|
312
|
+
normalize,
|
|
313
|
+
random,
|
|
314
|
+
round,
|
|
315
|
+
scale,
|
|
316
|
+
scaleAndAdd,
|
|
317
|
+
set,
|
|
318
|
+
sqrDist,
|
|
319
|
+
sqrLen,
|
|
320
|
+
squaredDistance,
|
|
321
|
+
squaredLength,
|
|
322
|
+
str,
|
|
323
|
+
sub,
|
|
324
|
+
subtract,
|
|
325
|
+
transformMat4,
|
|
326
|
+
transformQuat,
|
|
327
|
+
zero,
|
|
328
|
+
createLinear,
|
|
329
|
+
alloc,
|
|
330
|
+
unalloc,
|
|
331
|
+
ZERO,
|
|
332
|
+
addScalar,
|
|
333
|
+
isEmpty,
|
|
334
|
+
divideScalar,
|
|
335
|
+
multiplyScalar,
|
|
336
|
+
setScalar,
|
|
337
|
+
subtractScalar,
|
|
338
|
+
toRGBA,
|
|
339
|
+
fromRGBA,
|
|
340
|
+
fromRGB,
|
|
341
|
+
toHexA,
|
|
342
|
+
toHex,
|
|
343
|
+
fromHex,
|
|
344
|
+
fromArray,
|
|
345
|
+
setArray,
|
|
346
|
+
toArray
|
|
347
|
+
} = vec4;
|
package/src/vertex.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy vertex helper container backed by the shared mesh/tangent math.
|
|
3
|
+
*
|
|
4
|
+
* The previous ccpwgl implementation is intentionally not preserved here:
|
|
5
|
+
* normal/tangent generation now delegates to the same helpers used by the GR2,
|
|
6
|
+
* OBJ, glTF, and STL format packages.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
generateBiNormals,
|
|
11
|
+
generateNormals,
|
|
12
|
+
generateTangentFrames,
|
|
13
|
+
generateTangents
|
|
14
|
+
} from "./mesh.js";
|
|
15
|
+
import { packTangentFrames } from "./tangent.js";
|
|
16
|
+
|
|
17
|
+
function facesForAreas(indices, areas)
|
|
18
|
+
{
|
|
19
|
+
if (!areas || areas.length === 0) return Array.from(indices);
|
|
20
|
+
|
|
21
|
+
const faces = [];
|
|
22
|
+
for (const area of areas)
|
|
23
|
+
{
|
|
24
|
+
const
|
|
25
|
+
start = area.start || 0,
|
|
26
|
+
count = area.count === undefined ? indices.length - start : area.count;
|
|
27
|
+
|
|
28
|
+
for (let i = start; i < start + count; i++)
|
|
29
|
+
{
|
|
30
|
+
faces.push(indices[i]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return faces;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Calculate area-weighted vertex normals.
|
|
38
|
+
*
|
|
39
|
+
* @param {ArrayLike<number>} indices Flat triangle indices.
|
|
40
|
+
* @param {ArrayLike<number>} positions Flat xyz positions.
|
|
41
|
+
* @returns {number[]} Flat xyz normals.
|
|
42
|
+
*/
|
|
43
|
+
export function calculateNormals(indices, positions)
|
|
44
|
+
{
|
|
45
|
+
return generateNormals(positions, indices);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Calculate xyz tangents from positions, normals, UVs, and triangle indices.
|
|
50
|
+
*
|
|
51
|
+
* @param {ArrayLike<number>} indices Flat triangle indices.
|
|
52
|
+
* @param {ArrayLike<number>} positions Flat xyz positions.
|
|
53
|
+
* @param {ArrayLike<number>} uvs Flat uv coordinates.
|
|
54
|
+
* @param {Array<object>} [areas] Optional index ranges to use.
|
|
55
|
+
* @param {ArrayLike<number>} [normals] Flat xyz normals; generated when absent.
|
|
56
|
+
* @returns {number[]} Flat xyz tangents.
|
|
57
|
+
*/
|
|
58
|
+
export function calculateTangents(indices, positions, uvs, areas, normals)
|
|
59
|
+
{
|
|
60
|
+
const faces = facesForAreas(indices, areas);
|
|
61
|
+
return generateTangents(
|
|
62
|
+
positions,
|
|
63
|
+
normals && normals.length ? normals : generateNormals(positions, faces),
|
|
64
|
+
uvs,
|
|
65
|
+
faces
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Calculate xyz binormals from normals and tangents.
|
|
71
|
+
*
|
|
72
|
+
* @param {ArrayLike<number>} normals Flat xyz normals.
|
|
73
|
+
* @param {ArrayLike<number>} tangents Flat xyz tangents.
|
|
74
|
+
* @param {object} [options] Generation options.
|
|
75
|
+
* @returns {number[]} Flat xyz binormals.
|
|
76
|
+
*/
|
|
77
|
+
export function calculateBiNormals(normals, tangents, options)
|
|
78
|
+
{
|
|
79
|
+
return generateBiNormals(normals, tangents, options);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Calculate GR2-style packed tangent frames.
|
|
84
|
+
*
|
|
85
|
+
* @param {ArrayLike<number>} indices Flat triangle indices.
|
|
86
|
+
* @param {ArrayLike<number>} positions Flat xyz positions.
|
|
87
|
+
* @param {ArrayLike<number>} uvs Flat uv coordinates.
|
|
88
|
+
* @param {Array<object>} [areas] Optional index ranges to use.
|
|
89
|
+
* @param {ArrayLike<number>} [normals] Flat xyz normals; generated when absent.
|
|
90
|
+
* @param {object} [options] Generation options.
|
|
91
|
+
* @returns {number[]} Flat xyzw packed tangent-frame values.
|
|
92
|
+
*/
|
|
93
|
+
export function calculatePackedTangents(indices, positions, uvs, areas, normals, options)
|
|
94
|
+
{
|
|
95
|
+
const
|
|
96
|
+
faces = facesForAreas(indices, areas),
|
|
97
|
+
normalValues = normals && normals.length ? normals : generateNormals(positions, faces),
|
|
98
|
+
frame = generateTangentFrames(positions, normalValues, uvs, faces, options);
|
|
99
|
+
|
|
100
|
+
return packTangentFrames(normalValues, frame.tangents, frame.binormals);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const vertex = Object.freeze({
|
|
104
|
+
calculateNormals,
|
|
105
|
+
calculateTangents,
|
|
106
|
+
calculateBiNormals,
|
|
107
|
+
calculatePackedTangents
|
|
108
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./textureFormats.js";
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { PixelFormat } from "../graphics/pixelFormats.js";
|
|
2
|
+
import { DxgiFormat } from "../d3d/dxgiFormats.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* WebGPU texture format tokens mirrored as stable strings.
|
|
6
|
+
*/
|
|
7
|
+
export const GpuTextureFormat = Object.freeze({
|
|
8
|
+
R8_UNORM: "r8unorm",
|
|
9
|
+
RG8_UNORM: "rg8unorm",
|
|
10
|
+
RGBA8_UNORM: "rgba8unorm",
|
|
11
|
+
RGBA8_UNORM_SRGB: "rgba8unorm-srgb",
|
|
12
|
+
BGRA8_UNORM: "bgra8unorm",
|
|
13
|
+
BGRA8_UNORM_SRGB: "bgra8unorm-srgb",
|
|
14
|
+
R16_FLOAT: "r16float",
|
|
15
|
+
RG16_FLOAT: "rg16float",
|
|
16
|
+
RGBA16_FLOAT: "rgba16float",
|
|
17
|
+
R32_FLOAT: "r32float",
|
|
18
|
+
RG32_FLOAT: "rg32float",
|
|
19
|
+
RGBA32_FLOAT: "rgba32float",
|
|
20
|
+
BC1_RGBA_UNORM: "bc1-rgba-unorm",
|
|
21
|
+
BC1_RGBA_UNORM_SRGB: "bc1-rgba-unorm-srgb",
|
|
22
|
+
BC2_RGBA_UNORM: "bc2-rgba-unorm",
|
|
23
|
+
BC2_RGBA_UNORM_SRGB: "bc2-rgba-unorm-srgb",
|
|
24
|
+
BC3_RGBA_UNORM: "bc3-rgba-unorm",
|
|
25
|
+
BC3_RGBA_UNORM_SRGB: "bc3-rgba-unorm-srgb",
|
|
26
|
+
BC4_R_UNORM: "bc4-r-unorm",
|
|
27
|
+
BC4_R_SNORM: "bc4-r-snorm",
|
|
28
|
+
BC5_RG_UNORM: "bc5-rg-unorm",
|
|
29
|
+
BC5_RG_SNORM: "bc5-rg-snorm",
|
|
30
|
+
BC6H_RGB_UFLOAT: "bc6h-rgb-ufloat",
|
|
31
|
+
BC6H_RGB_FLOAT: "bc6h-rgb-float",
|
|
32
|
+
BC7_RGBA_UNORM: "bc7-rgba-unorm",
|
|
33
|
+
BC7_RGBA_UNORM_SRGB: "bc7-rgba-unorm-srgb"
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Map canonical CarbonEngineJS pixel formats to WebGPU texture format strings.
|
|
38
|
+
*/
|
|
39
|
+
export const PixelFormatToWebGpu = Object.freeze({
|
|
40
|
+
[PixelFormat.R8_UNORM]: GpuTextureFormat.R8_UNORM,
|
|
41
|
+
[PixelFormat.RG8_UNORM]: GpuTextureFormat.RG8_UNORM,
|
|
42
|
+
[PixelFormat.RGBA8_UNORM]: GpuTextureFormat.RGBA8_UNORM,
|
|
43
|
+
[PixelFormat.RGBA8_UNORM_SRGB]: GpuTextureFormat.RGBA8_UNORM_SRGB,
|
|
44
|
+
[PixelFormat.BGRA8_UNORM]: GpuTextureFormat.BGRA8_UNORM,
|
|
45
|
+
[PixelFormat.BGRA8_UNORM_SRGB]: GpuTextureFormat.BGRA8_UNORM_SRGB,
|
|
46
|
+
[PixelFormat.R16_FLOAT]: GpuTextureFormat.R16_FLOAT,
|
|
47
|
+
[PixelFormat.RG16_FLOAT]: GpuTextureFormat.RG16_FLOAT,
|
|
48
|
+
[PixelFormat.RGBA16_FLOAT]: GpuTextureFormat.RGBA16_FLOAT,
|
|
49
|
+
[PixelFormat.R32_FLOAT]: GpuTextureFormat.R32_FLOAT,
|
|
50
|
+
[PixelFormat.RG32_FLOAT]: GpuTextureFormat.RG32_FLOAT,
|
|
51
|
+
[PixelFormat.RGBA32_FLOAT]: GpuTextureFormat.RGBA32_FLOAT,
|
|
52
|
+
[PixelFormat.BC1_RGBA_UNORM]: GpuTextureFormat.BC1_RGBA_UNORM,
|
|
53
|
+
[PixelFormat.BC1_RGBA_UNORM_SRGB]: GpuTextureFormat.BC1_RGBA_UNORM_SRGB,
|
|
54
|
+
[PixelFormat.BC2_RGBA_UNORM]: GpuTextureFormat.BC2_RGBA_UNORM,
|
|
55
|
+
[PixelFormat.BC2_RGBA_UNORM_SRGB]: GpuTextureFormat.BC2_RGBA_UNORM_SRGB,
|
|
56
|
+
[PixelFormat.BC3_RGBA_UNORM]: GpuTextureFormat.BC3_RGBA_UNORM,
|
|
57
|
+
[PixelFormat.BC3_RGBA_UNORM_SRGB]: GpuTextureFormat.BC3_RGBA_UNORM_SRGB,
|
|
58
|
+
[PixelFormat.BC4_R_UNORM]: GpuTextureFormat.BC4_R_UNORM,
|
|
59
|
+
[PixelFormat.BC4_R_SNORM]: GpuTextureFormat.BC4_R_SNORM,
|
|
60
|
+
[PixelFormat.BC5_RG_UNORM]: GpuTextureFormat.BC5_RG_UNORM,
|
|
61
|
+
[PixelFormat.BC5_RG_SNORM]: GpuTextureFormat.BC5_RG_SNORM,
|
|
62
|
+
[PixelFormat.BC6H_RGB_UFLOAT]: GpuTextureFormat.BC6H_RGB_UFLOAT,
|
|
63
|
+
[PixelFormat.BC6H_RGB_FLOAT]: GpuTextureFormat.BC6H_RGB_FLOAT,
|
|
64
|
+
[PixelFormat.BC7_RGBA_UNORM]: GpuTextureFormat.BC7_RGBA_UNORM,
|
|
65
|
+
[PixelFormat.BC7_RGBA_UNORM_SRGB]: GpuTextureFormat.BC7_RGBA_UNORM_SRGB
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Map selected DXGI formats to canonical CarbonEngineJS pixel formats.
|
|
70
|
+
*/
|
|
71
|
+
export const DxgiFormatToPixelFormat = Object.freeze({
|
|
72
|
+
[DxgiFormat.R32G32B32A32_FLOAT]: PixelFormat.RGBA32_FLOAT,
|
|
73
|
+
[DxgiFormat.R32G32_FLOAT]: PixelFormat.RG32_FLOAT,
|
|
74
|
+
[DxgiFormat.R32_FLOAT]: PixelFormat.R32_FLOAT,
|
|
75
|
+
[DxgiFormat.R16G16B16A16_FLOAT]: PixelFormat.RGBA16_FLOAT,
|
|
76
|
+
[DxgiFormat.R8G8_UNORM]: PixelFormat.RG8_UNORM,
|
|
77
|
+
[DxgiFormat.R8G8B8A8_UNORM]: PixelFormat.RGBA8_UNORM,
|
|
78
|
+
[DxgiFormat.R8G8B8A8_UNORM_SRGB]: PixelFormat.RGBA8_UNORM_SRGB,
|
|
79
|
+
[DxgiFormat.R16_FLOAT]: PixelFormat.R16_FLOAT,
|
|
80
|
+
[DxgiFormat.R8_UNORM]: PixelFormat.R8_UNORM,
|
|
81
|
+
[DxgiFormat.BC1_UNORM]: PixelFormat.BC1_RGBA_UNORM,
|
|
82
|
+
[DxgiFormat.BC1_UNORM_SRGB]: PixelFormat.BC1_RGBA_UNORM_SRGB,
|
|
83
|
+
[DxgiFormat.BC2_UNORM]: PixelFormat.BC2_RGBA_UNORM,
|
|
84
|
+
[DxgiFormat.BC2_UNORM_SRGB]: PixelFormat.BC2_RGBA_UNORM_SRGB,
|
|
85
|
+
[DxgiFormat.BC3_UNORM]: PixelFormat.BC3_RGBA_UNORM,
|
|
86
|
+
[DxgiFormat.BC3_UNORM_SRGB]: PixelFormat.BC3_RGBA_UNORM_SRGB,
|
|
87
|
+
[DxgiFormat.BC4_UNORM]: PixelFormat.BC4_R_UNORM,
|
|
88
|
+
[DxgiFormat.BC4_SNORM]: PixelFormat.BC4_R_SNORM,
|
|
89
|
+
[DxgiFormat.BC5_UNORM]: PixelFormat.BC5_RG_UNORM,
|
|
90
|
+
[DxgiFormat.BC5_SNORM]: PixelFormat.BC5_RG_SNORM,
|
|
91
|
+
[DxgiFormat.B8G8R8A8_UNORM]: PixelFormat.BGRA8_UNORM,
|
|
92
|
+
[DxgiFormat.B8G8R8X8_UNORM]: PixelFormat.BGRX8_UNORM,
|
|
93
|
+
[DxgiFormat.B8G8R8A8_UNORM_SRGB]: PixelFormat.BGRA8_UNORM_SRGB,
|
|
94
|
+
[DxgiFormat.B8G8R8X8_UNORM_SRGB]: PixelFormat.BGRX8_UNORM_SRGB,
|
|
95
|
+
[DxgiFormat.BC6H_UF16]: PixelFormat.BC6H_RGB_UFLOAT,
|
|
96
|
+
[DxgiFormat.BC6H_SF16]: PixelFormat.BC6H_RGB_FLOAT,
|
|
97
|
+
[DxgiFormat.BC7_UNORM]: PixelFormat.BC7_RGBA_UNORM,
|
|
98
|
+
[DxgiFormat.BC7_UNORM_SRGB]: PixelFormat.BC7_RGBA_UNORM_SRGB
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Convert a pixel format token to a WebGPU texture format token.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} format Pixel format token.
|
|
105
|
+
* @returns {string} WebGPU texture format token or empty string.
|
|
106
|
+
*/
|
|
107
|
+
export function toWebGpuTextureFormat(format)
|
|
108
|
+
{
|
|
109
|
+
return PixelFormatToWebGpu[format] || "";
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Convert a DXGI numeric format to a pixel format token.
|
|
114
|
+
*
|
|
115
|
+
* @param {number} format DXGI_FORMAT numeric value.
|
|
116
|
+
* @returns {string} Pixel format token or unknown.
|
|
117
|
+
*/
|
|
118
|
+
export function dxgiToPixelFormat(format)
|
|
119
|
+
{
|
|
120
|
+
return DxgiFormatToPixelFormat[format] || PixelFormat.UNKNOWN;
|
|
121
|
+
}
|