@needle-tools/gltf-build-pipeline 3.0.2 → 4.0.0-14560b0

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.
@@ -0,0 +1,148 @@
1
+ import { createRequire as __createRequire } from 'node:module'; const require = __createRequire(import.meta.url);
2
+
3
+ // ../modules/mesh-baker/src/asset-pipeline/texture-mip-node.worker.js
4
+ import { parentPort } from "node:worker_threads";
5
+
6
+ // ../modules/mesh-baker/src/asset-pipeline/texture-sampling.js
7
+ var mipChainCache = /* @__PURE__ */ new WeakMap();
8
+ var SRGB8_TO_LINEAR = Array.from(
9
+ { length: 256 },
10
+ (_, value) => srgbToLinear(value / 255)
11
+ );
12
+ var LINEAR16_TO_SRGB8 = Uint8Array.from(
13
+ { length: 65536 },
14
+ (_, value) => Math.round(linearToSrgb(value / 65535) * 255)
15
+ );
16
+ function getTextureMipChain(texture, semantic = "linear") {
17
+ let semanticCache = mipChainCache.get(texture.data);
18
+ if (!semanticCache) {
19
+ semanticCache = /* @__PURE__ */ new Map();
20
+ mipChainCache.set(texture.data, semanticCache);
21
+ }
22
+ const cacheKey = textureMipCacheKey(texture, semantic);
23
+ const cached = semanticCache.get(cacheKey);
24
+ if (cached) return cached;
25
+ const levels = [texture];
26
+ let source = texture;
27
+ while (source.width > 1 || source.height > 1) {
28
+ source = downsampleMip(source, semantic);
29
+ levels.push(source);
30
+ }
31
+ semanticCache.set(cacheKey, levels);
32
+ return levels;
33
+ }
34
+ function textureMipCacheKey(texture, semantic) {
35
+ return `${semantic}:${texture.width}x${texture.height}`;
36
+ }
37
+ function downsampleMip(source, semantic) {
38
+ const width = Math.max(1, Math.floor(source.width / 2));
39
+ const height = Math.max(1, Math.floor(source.height / 2));
40
+ const data = new Uint8Array(width * height * 4);
41
+ if (semantic === "normal") downsampleNormalMip(source, width, height, data);
42
+ else if (semantic === "srgb") downsampleSrgbMip(source, width, height, data);
43
+ else downsampleLinearMip(source, width, height, data);
44
+ return { width, height, data };
45
+ }
46
+ function downsampleNormalMip(source, width, height, data) {
47
+ for (let y = 0; y < height; y++) {
48
+ const sourceY0 = y * 2;
49
+ const sourceY1 = Math.min(source.height - 1, sourceY0 + 1);
50
+ for (let x = 0; x < width; x++) {
51
+ const sourceX0 = x * 2;
52
+ const sourceX1 = Math.min(source.width - 1, sourceX0 + 1);
53
+ const offset00 = (sourceY0 * source.width + sourceX0) * 4;
54
+ const offset10 = (sourceY0 * source.width + sourceX1) * 4;
55
+ const offset01 = (sourceY1 * source.width + sourceX0) * 4;
56
+ const offset11 = (sourceY1 * source.width + sourceX1) * 4;
57
+ const offset = (y * width + x) * 4;
58
+ let nx = (source.data[offset00] + source.data[offset10] + source.data[offset01] + source.data[offset11]) / 127.5 - 4;
59
+ let ny = (source.data[offset00 + 1] + source.data[offset10 + 1] + source.data[offset01 + 1] + source.data[offset11 + 1]) / 127.5 - 4;
60
+ let nz = (source.data[offset00 + 2] + source.data[offset10 + 2] + source.data[offset01 + 2] + source.data[offset11 + 2]) / 127.5 - 4;
61
+ const length = Math.hypot(nx, ny, nz);
62
+ if (length > 1e-12) {
63
+ nx /= length;
64
+ ny /= length;
65
+ nz /= length;
66
+ } else {
67
+ nx = 0;
68
+ ny = 0;
69
+ nz = 1;
70
+ }
71
+ data[offset] = Math.round((nx * 0.5 + 0.5) * 255);
72
+ data[offset + 1] = Math.round((ny * 0.5 + 0.5) * 255);
73
+ data[offset + 2] = Math.round((nz * 0.5 + 0.5) * 255);
74
+ data[offset + 3] = Math.round(
75
+ (source.data[offset00 + 3] + source.data[offset10 + 3] + source.data[offset01 + 3] + source.data[offset11 + 3]) * 0.25
76
+ );
77
+ }
78
+ }
79
+ }
80
+ function downsampleSrgbMip(source, width, height, data) {
81
+ for (let y = 0; y < height; y++) {
82
+ const sourceY0 = y * 2;
83
+ const sourceY1 = Math.min(source.height - 1, sourceY0 + 1);
84
+ for (let x = 0; x < width; x++) {
85
+ const sourceX0 = x * 2;
86
+ const sourceX1 = Math.min(source.width - 1, sourceX0 + 1);
87
+ const offset00 = (sourceY0 * source.width + sourceX0) * 4;
88
+ const offset10 = (sourceY0 * source.width + sourceX1) * 4;
89
+ const offset01 = (sourceY1 * source.width + sourceX0) * 4;
90
+ const offset11 = (sourceY1 * source.width + sourceX1) * 4;
91
+ const offset = (y * width + x) * 4;
92
+ for (let channel = 0; channel < 3; channel++) {
93
+ const average = (SRGB8_TO_LINEAR[source.data[offset00 + channel]] + SRGB8_TO_LINEAR[source.data[offset10 + channel]] + SRGB8_TO_LINEAR[source.data[offset01 + channel]] + SRGB8_TO_LINEAR[source.data[offset11 + channel]]) * 0.25;
94
+ data[offset + channel] = LINEAR16_TO_SRGB8[Math.round(Math.min(1, Math.max(0, average)) * 65535)];
95
+ }
96
+ data[offset + 3] = Math.round((source.data[offset00 + 3] + source.data[offset10 + 3] + source.data[offset01 + 3] + source.data[offset11 + 3]) * 0.25);
97
+ }
98
+ }
99
+ }
100
+ function downsampleLinearMip(source, width, height, data) {
101
+ for (let y = 0; y < height; y++) {
102
+ const sourceY0 = y * 2;
103
+ const sourceY1 = Math.min(source.height - 1, sourceY0 + 1);
104
+ for (let x = 0; x < width; x++) {
105
+ const sourceX0 = x * 2;
106
+ const sourceX1 = Math.min(source.width - 1, sourceX0 + 1);
107
+ const offset00 = (sourceY0 * source.width + sourceX0) * 4;
108
+ const offset10 = (sourceY0 * source.width + sourceX1) * 4;
109
+ const offset01 = (sourceY1 * source.width + sourceX0) * 4;
110
+ const offset11 = (sourceY1 * source.width + sourceX1) * 4;
111
+ const offset = (y * width + x) * 4;
112
+ for (let channel = 0; channel < 4; channel++) {
113
+ data[offset + channel] = Math.round((source.data[offset00 + channel] + source.data[offset10 + channel] + source.data[offset01 + channel] + source.data[offset11 + channel]) * 0.25);
114
+ }
115
+ }
116
+ }
117
+ }
118
+ function srgbToLinear(value) {
119
+ return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
120
+ }
121
+ function linearToSrgb(value) {
122
+ const clamped = Math.min(1, Math.max(0, value));
123
+ return clamped <= 31308e-7 ? clamped * 12.92 : 1.055 * clamped ** (1 / 2.4) - 0.055;
124
+ }
125
+
126
+ // ../modules/mesh-baker/src/asset-pipeline/texture-mip-worker-common.js
127
+ function buildTextureMipWorkerResult(jobs) {
128
+ return jobs.map((job) => getTextureMipChain(job, job.semantic).slice(1).map(({ width, height, data }) => ({ width, height, data })));
129
+ }
130
+ function textureMipResultTransferList(results) {
131
+ return (
132
+ /** @type {ArrayBuffer[]} */
133
+ results.flatMap((levels) => levels.map(({ data }) => data.buffer).filter((buffer) => buffer instanceof ArrayBuffer))
134
+ );
135
+ }
136
+
137
+ // ../modules/mesh-baker/src/asset-pipeline/texture-mip-node.worker.js
138
+ if (!parentPort) throw new Error("Texture mip worker requires a Node worker thread.");
139
+ parentPort.on("message", ({ jobs }) => {
140
+ try {
141
+ const results = buildTextureMipWorkerResult(jobs);
142
+ parentPort?.postMessage({ results }, textureMipResultTransferList(results));
143
+ } catch (error) {
144
+ parentPort?.postMessage({
145
+ error: error instanceof Error ? { message: error.message, stack: error.stack } : { message: String(error) }
146
+ });
147
+ }
148
+ });
Binary file