@xprite/lighter 0.0.0-stage → 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/README.md +178 -2
- package/dist/base.d.ts +21 -0
- package/dist/base.d.ts.map +1 -0
- package/dist/base.js +34 -0
- package/dist/color-lab.d.ts +4 -0
- package/dist/color-lab.d.ts.map +1 -0
- package/dist/color-lab.js +17 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/light-mask.d.ts +24 -0
- package/dist/light-mask.d.ts.map +1 -0
- package/dist/light-mask.js +75 -0
- package/dist/lighting-map-source.d.ts +33 -0
- package/dist/lighting-map-source.d.ts.map +1 -0
- package/dist/lighting-map-source.js +123 -0
- package/dist/normal-map-light-estimate.d.ts +10 -0
- package/dist/normal-map-light-estimate.d.ts.map +1 -0
- package/dist/normal-map-light-estimate.js +298 -0
- package/dist/normal-map-lighting.d.ts +42 -0
- package/dist/normal-map-lighting.d.ts.map +1 -0
- package/dist/normal-map-lighting.js +156 -0
- package/dist/normal-map-painted-light.d.ts +4 -0
- package/dist/normal-map-painted-light.d.ts.map +1 -0
- package/dist/normal-map-painted-light.js +63 -0
- package/dist/normal-map.d.ts +41 -0
- package/dist/normal-map.d.ts.map +1 -0
- package/dist/normal-map.js +597 -0
- package/package.json +47 -4
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { UINT8_MAX } from "./base.js";
|
|
2
|
+
import { lightingMapLuminance } from "./lighting-map-source.js";
|
|
3
|
+
const RGBA_CHANNELS = 4;
|
|
4
|
+
const ALPHA_CHANNEL = 3;
|
|
5
|
+
const SOLID_ALPHA = 128;
|
|
6
|
+
const NEUTRAL_SHADE = 0.5;
|
|
7
|
+
const MIN_VISIBLE_CHANNEL = 20;
|
|
8
|
+
const CONTOUR_DEPTH = 4;
|
|
9
|
+
const DISTANCE_CAP = CONTOUR_DEPTH + 3;
|
|
10
|
+
const SMOOTH_CENTER_WEIGHT = 2;
|
|
11
|
+
const MIN_SLOPE = 0.15;
|
|
12
|
+
const MIN_COMPONENT_PIXELS = 24;
|
|
13
|
+
const MIN_FIT_PIXELS = 40;
|
|
14
|
+
const MIN_GROUP_PIXELS = 8;
|
|
15
|
+
const CHROMA_STEPS = 16;
|
|
16
|
+
const CHROMA_BUCKETS = CHROMA_STEPS + 1;
|
|
17
|
+
const TONE_STEPS = 32;
|
|
18
|
+
const MIN_TONE_PIXELS = 2;
|
|
19
|
+
const MIN_TONES = 3;
|
|
20
|
+
const MIN_CONTRAST = 0.025;
|
|
21
|
+
const MIN_FIT = 0.3;
|
|
22
|
+
const MIN_OPPOSITE_MARGIN = 0.12;
|
|
23
|
+
const MIN_COMPONENT_FIT = 0.25;
|
|
24
|
+
const MAX_HOLDOUT_DRIFT = 45;
|
|
25
|
+
const MAX_COMPONENT_DRIFT = 75;
|
|
26
|
+
const HOLDOUT_COUNT = 4;
|
|
27
|
+
const MIN_TILE_SIZE = 4;
|
|
28
|
+
const TILES_PER_SPRITE = 5;
|
|
29
|
+
const BLOCK_ROW_STEP = 7;
|
|
30
|
+
const FULL_TURN = 360;
|
|
31
|
+
const HALF_TURN = FULL_TURN / 2;
|
|
32
|
+
const ANGLE_STEP = 5;
|
|
33
|
+
const ANGLE_COUNT = FULL_TURN / ANGLE_STEP;
|
|
34
|
+
const ELEVATIONS = [25, 45, 65];
|
|
35
|
+
const RAD_PER_DEGREE = Math.PI / 180;
|
|
36
|
+
const MAX_ESTIMATE_PIXELS = 1_048_576;
|
|
37
|
+
const MAX_SAMPLES = 8_192;
|
|
38
|
+
const FIT_EPSILON = 1e-9;
|
|
39
|
+
const CARDINAL_STEPS = [
|
|
40
|
+
[-1, 0],
|
|
41
|
+
[1, 0],
|
|
42
|
+
[0, -1],
|
|
43
|
+
[0, 1],
|
|
44
|
+
];
|
|
45
|
+
const CHAMFER_STEPS = [
|
|
46
|
+
[-1, 0, 1],
|
|
47
|
+
[0, -1, 1],
|
|
48
|
+
[-1, -1, Math.SQRT2],
|
|
49
|
+
[1, -1, Math.SQRT2],
|
|
50
|
+
];
|
|
51
|
+
function silhouette(source) {
|
|
52
|
+
const { width, height, data } = source;
|
|
53
|
+
const count = width * height;
|
|
54
|
+
const mask = new Uint8Array(count);
|
|
55
|
+
let area = 0;
|
|
56
|
+
let left = width;
|
|
57
|
+
let top = height;
|
|
58
|
+
for (let i = 0; i < count; i++) {
|
|
59
|
+
if (data[i * RGBA_CHANNELS + ALPHA_CHANNEL] < SOLID_ALPHA)
|
|
60
|
+
continue;
|
|
61
|
+
mask[i] = 1;
|
|
62
|
+
area++;
|
|
63
|
+
left = Math.min(left, i % width);
|
|
64
|
+
top = Math.min(top, Math.floor(i / width));
|
|
65
|
+
}
|
|
66
|
+
const indexAt = (x, y) => x >= 0 && x < width && y >= 0 && y < height ? y * width + x : -1;
|
|
67
|
+
const distance = new Float32Array(count);
|
|
68
|
+
const labels = new Int32Array(count).fill(-1);
|
|
69
|
+
const componentSizes = [];
|
|
70
|
+
const queue = new Int32Array(count);
|
|
71
|
+
for (let i = 0; i < count; i++) {
|
|
72
|
+
if (!mask[i])
|
|
73
|
+
continue;
|
|
74
|
+
const x = i % width;
|
|
75
|
+
const y = Math.floor(i / width);
|
|
76
|
+
distance[i] = CARDINAL_STEPS.some(([dx, dy]) => !mask[indexAt(x + dx, y + dy)])
|
|
77
|
+
? 1
|
|
78
|
+
: DISTANCE_CAP;
|
|
79
|
+
if (labels[i] >= 0)
|
|
80
|
+
continue;
|
|
81
|
+
const label = componentSizes.length;
|
|
82
|
+
queue[0] = i;
|
|
83
|
+
labels[i] = label;
|
|
84
|
+
let length = 1;
|
|
85
|
+
for (let head = 0; head < length; head++) {
|
|
86
|
+
const p = queue[head];
|
|
87
|
+
for (const [dx, dy] of CARDINAL_STEPS) {
|
|
88
|
+
const next = indexAt((p % width) + dx, Math.floor(p / width) + dy);
|
|
89
|
+
if (!mask[next] || labels[next] >= 0)
|
|
90
|
+
continue;
|
|
91
|
+
labels[next] = label;
|
|
92
|
+
queue[length++] = next;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
componentSizes.push(length);
|
|
96
|
+
}
|
|
97
|
+
// Two bounded chamfer passes cover the contour band used for fitting.
|
|
98
|
+
for (const sign of [1, -1]) {
|
|
99
|
+
for (let step = 0; step < count; step++) {
|
|
100
|
+
const i = sign === 1 ? step : count - 1 - step;
|
|
101
|
+
if (!mask[i] || distance[i] === 1)
|
|
102
|
+
continue;
|
|
103
|
+
const x = i % width;
|
|
104
|
+
const y = Math.floor(i / width);
|
|
105
|
+
for (const [dx, dy, cost] of CHAMFER_STEPS) {
|
|
106
|
+
const next = indexAt(x + dx * sign, y + dy * sign);
|
|
107
|
+
if (mask[next])
|
|
108
|
+
distance[i] = Math.min(distance[i], distance[next] + cost);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const smooth = new Float32Array(count);
|
|
113
|
+
for (let i = 0; i < count; i++) {
|
|
114
|
+
if (!mask[i])
|
|
115
|
+
continue;
|
|
116
|
+
let sum = distance[i] * SMOOTH_CENTER_WEIGHT;
|
|
117
|
+
let weight = SMOOTH_CENTER_WEIGHT;
|
|
118
|
+
const x = i % width;
|
|
119
|
+
const y = Math.floor(i / width);
|
|
120
|
+
for (const [dx, dy] of CARDINAL_STEPS) {
|
|
121
|
+
const next = indexAt(x + dx, y + dy);
|
|
122
|
+
if (!mask[next])
|
|
123
|
+
continue;
|
|
124
|
+
sum += distance[next];
|
|
125
|
+
weight++;
|
|
126
|
+
}
|
|
127
|
+
smooth[i] = sum / weight;
|
|
128
|
+
}
|
|
129
|
+
return { area, left, top, mask, distance, smooth, labels, componentSizes, indexAt };
|
|
130
|
+
}
|
|
131
|
+
function fitDirection(samples) {
|
|
132
|
+
if (samples.length < MIN_FIT_PIXELS)
|
|
133
|
+
return null;
|
|
134
|
+
const byGroup = new Map();
|
|
135
|
+
for (const sample of samples) {
|
|
136
|
+
const group = byGroup.get(sample.group);
|
|
137
|
+
if (group)
|
|
138
|
+
group.push(sample);
|
|
139
|
+
else
|
|
140
|
+
byGroup.set(sample.group, [sample]);
|
|
141
|
+
}
|
|
142
|
+
const groups = [...byGroup.values()].map((samples) => {
|
|
143
|
+
const count = samples.length;
|
|
144
|
+
const mean = samples.reduce((sum, sample) => sum + sample.luminance, 0) / count;
|
|
145
|
+
const residuals = samples.map((sample) => sample.luminance - mean);
|
|
146
|
+
return { samples, residuals, weight: 1 / Math.sqrt(count) };
|
|
147
|
+
});
|
|
148
|
+
const observedVariance = groups.reduce((sum, group) => sum + group.weight * group.residuals.reduce((s, r) => s + r * r, 0), 0);
|
|
149
|
+
if (observedVariance <= FIT_EPSILON)
|
|
150
|
+
return null;
|
|
151
|
+
const scores = new Float64Array(ANGLE_COUNT).fill(-1);
|
|
152
|
+
let winner = 0;
|
|
153
|
+
for (let slot = 0; slot < ANGLE_COUNT; slot++) {
|
|
154
|
+
const angle = slot * ANGLE_STEP * RAD_PER_DEGREE;
|
|
155
|
+
for (const elevation of ELEVATIONS) {
|
|
156
|
+
const height = elevation * RAD_PER_DEGREE;
|
|
157
|
+
const lx = Math.cos(angle) * Math.cos(height);
|
|
158
|
+
const ly = Math.sin(angle) * Math.cos(height);
|
|
159
|
+
const lz = Math.sin(height);
|
|
160
|
+
let predictedVariance = 0;
|
|
161
|
+
let covariance = 0;
|
|
162
|
+
for (const { samples, residuals, weight } of groups) {
|
|
163
|
+
let sum = 0;
|
|
164
|
+
let squared = 0;
|
|
165
|
+
let cross = 0;
|
|
166
|
+
for (let i = 0; i < samples.length; i++) {
|
|
167
|
+
const { nx, ny, nz } = samples[i];
|
|
168
|
+
const prediction = Math.max(0, nx * lx + ny * ly + nz * lz);
|
|
169
|
+
sum += prediction;
|
|
170
|
+
squared += prediction * prediction;
|
|
171
|
+
cross += prediction * residuals[i];
|
|
172
|
+
}
|
|
173
|
+
predictedVariance += weight * (squared - (sum * sum) / samples.length);
|
|
174
|
+
covariance += weight * cross;
|
|
175
|
+
}
|
|
176
|
+
if (predictedVariance > FIT_EPSILON)
|
|
177
|
+
scores[slot] = Math.max(scores[slot], covariance / Math.sqrt(predictedVariance * observedVariance));
|
|
178
|
+
}
|
|
179
|
+
if (scores[slot] > scores[winner])
|
|
180
|
+
winner = slot;
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
angle: winner * ANGLE_STEP,
|
|
184
|
+
fit: scores[winner],
|
|
185
|
+
oppositeMargin: scores[winner] - scores[(winner + ANGLE_COUNT / 2) % ANGLE_COUNT],
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function angularDistance(a, b) {
|
|
189
|
+
return Math.abs(((a - b + FULL_TURN + HALF_TURN) % FULL_TURN) - HALF_TURN);
|
|
190
|
+
}
|
|
191
|
+
export function estimatePaintedLightAngle(source, shading) {
|
|
192
|
+
const estimate = estimatePaintedLight(source, shading);
|
|
193
|
+
return estimate?.angle ?? null;
|
|
194
|
+
}
|
|
195
|
+
export function estimatePaintedLight(source, shading) {
|
|
196
|
+
const { width, height, data } = source;
|
|
197
|
+
if (width * height > MAX_ESTIMATE_PIXELS)
|
|
198
|
+
return null;
|
|
199
|
+
if (shading && (shading.width !== width || shading.height !== height))
|
|
200
|
+
return null;
|
|
201
|
+
const { area, left, top, mask, distance, smooth, labels, componentSizes, indexAt } = silhouette(source);
|
|
202
|
+
if (area < MIN_FIT_PIXELS)
|
|
203
|
+
return null;
|
|
204
|
+
const tileSize = Math.max(MIN_TILE_SIZE, Math.floor(Math.sqrt(area) / TILES_PER_SPRITE));
|
|
205
|
+
const samples = [];
|
|
206
|
+
for (let i = 0; i < mask.length; i++) {
|
|
207
|
+
const component = labels[i];
|
|
208
|
+
if (!mask[i] || distance[i] > CONTOUR_DEPTH || componentSizes[component] < MIN_COMPONENT_PIXELS)
|
|
209
|
+
continue;
|
|
210
|
+
const x = i % width;
|
|
211
|
+
const y = Math.floor(i / width);
|
|
212
|
+
const sampleAt = (x, y) => smooth[indexAt(x, y)] ?? 0;
|
|
213
|
+
const nx = (sampleAt(x - 1, y) - sampleAt(x + 1, y)) / 2;
|
|
214
|
+
const ny = (sampleAt(x, y + 1) - sampleAt(x, y - 1)) / 2;
|
|
215
|
+
const length = Math.hypot(nx, ny, 1);
|
|
216
|
+
if (Math.hypot(nx, ny) / length < MIN_SLOPE)
|
|
217
|
+
continue;
|
|
218
|
+
const at = i * RGBA_CHANNELS;
|
|
219
|
+
let group = component;
|
|
220
|
+
let luminance = lightingMapLuminance(data, at);
|
|
221
|
+
if (shading) {
|
|
222
|
+
const alpha = shading.data[at + ALPHA_CHANNEL] / UINT8_MAX;
|
|
223
|
+
luminance = NEUTRAL_SHADE + alpha * (lightingMapLuminance(shading.data, at) - NEUTRAL_SHADE);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (Math.max(data[at], data[at + 1], data[at + 2]) < MIN_VISIBLE_CHANNEL)
|
|
227
|
+
continue;
|
|
228
|
+
const sum = data[at] + data[at + 1] + data[at + 2];
|
|
229
|
+
const red = Math.round((data[at] / sum) * CHROMA_STEPS);
|
|
230
|
+
const green = Math.round((data[at + 1] / sum) * CHROMA_STEPS);
|
|
231
|
+
group = (component * CHROMA_BUCKETS + red) * CHROMA_BUCKETS + green;
|
|
232
|
+
}
|
|
233
|
+
const block = (Math.floor((y - top) / tileSize) * BLOCK_ROW_STEP + Math.floor((x - left) / tileSize)) %
|
|
234
|
+
HOLDOUT_COUNT;
|
|
235
|
+
samples.push({
|
|
236
|
+
nx: nx / length,
|
|
237
|
+
ny: ny / length,
|
|
238
|
+
nz: 1 / length,
|
|
239
|
+
luminance,
|
|
240
|
+
component,
|
|
241
|
+
group,
|
|
242
|
+
block,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
const bounded = samples.length <= MAX_SAMPLES
|
|
246
|
+
? samples
|
|
247
|
+
: Array.from({ length: MAX_SAMPLES }, (_, i) => samples[Math.floor((i * samples.length) / MAX_SAMPLES)]);
|
|
248
|
+
const byGroup = new Map();
|
|
249
|
+
for (const sample of bounded) {
|
|
250
|
+
const group = byGroup.get(sample.group);
|
|
251
|
+
if (group)
|
|
252
|
+
group.push(sample);
|
|
253
|
+
else
|
|
254
|
+
byGroup.set(sample.group, [sample]);
|
|
255
|
+
}
|
|
256
|
+
const supported = [];
|
|
257
|
+
let contrast = 0;
|
|
258
|
+
for (const group of byGroup.values()) {
|
|
259
|
+
if (group.length < MIN_GROUP_PIXELS)
|
|
260
|
+
continue;
|
|
261
|
+
const tones = new Uint16Array(TONE_STEPS + 1);
|
|
262
|
+
for (const sample of group)
|
|
263
|
+
tones[Math.round(sample.luminance * TONE_STEPS)]++;
|
|
264
|
+
if (tones.filter((tone) => tone >= MIN_TONE_PIXELS).length < MIN_TONES)
|
|
265
|
+
continue;
|
|
266
|
+
const mean = group.reduce((sum, sample) => sum + sample.luminance, 0) / group.length;
|
|
267
|
+
for (const sample of group) {
|
|
268
|
+
contrast += (sample.luminance - mean) ** 2;
|
|
269
|
+
supported.push(sample);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (supported.length < MIN_FIT_PIXELS || contrast < supported.length * MIN_CONTRAST ** 2)
|
|
273
|
+
return null;
|
|
274
|
+
const candidate = fitDirection(supported);
|
|
275
|
+
if (!candidate || candidate.fit < MIN_FIT || candidate.oppositeMargin < MIN_OPPOSITE_MARGIN)
|
|
276
|
+
return null;
|
|
277
|
+
for (let block = 0; block < HOLDOUT_COUNT; block++) {
|
|
278
|
+
const fit = fitDirection(supported.filter((sample) => sample.block !== block));
|
|
279
|
+
if (!fit || angularDistance(fit.angle, candidate.angle) > MAX_HOLDOUT_DRIFT)
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
const byComponent = new Map();
|
|
283
|
+
for (const sample of supported) {
|
|
284
|
+
const component = byComponent.get(sample.component);
|
|
285
|
+
if (component)
|
|
286
|
+
component.push(sample);
|
|
287
|
+
else
|
|
288
|
+
byComponent.set(sample.component, [sample]);
|
|
289
|
+
}
|
|
290
|
+
for (const component of byComponent.values()) {
|
|
291
|
+
const fit = fitDirection(component);
|
|
292
|
+
if (fit &&
|
|
293
|
+
fit.fit >= MIN_COMPONENT_FIT &&
|
|
294
|
+
angularDistance(fit.angle, candidate.angle) > MAX_COMPONENT_DRIFT)
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
return { angle: candidate.angle, fit: candidate.fit };
|
|
298
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type PixelBuffer, type Point, type Rgba } from "./base.js";
|
|
2
|
+
export interface NormalMapLightOptions {
|
|
3
|
+
position: Point;
|
|
4
|
+
height: number;
|
|
5
|
+
brightness: number;
|
|
6
|
+
ambient: number;
|
|
7
|
+
maskStrength: number;
|
|
8
|
+
color: Rgba;
|
|
9
|
+
invertY: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Divide out a directional light already painted into the sprite before shading.
|
|
12
|
+
* `angle` is the image-plane azimuth in degrees (0 = right, 90 = up), as returned by
|
|
13
|
+
* `estimatePaintedLight`; `influence` 0–100 blends from no correction to full division.
|
|
14
|
+
*/
|
|
15
|
+
delight?: {
|
|
16
|
+
angle: number;
|
|
17
|
+
influence: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export declare const NORMAL_LIGHT_LIMITS: Readonly<{
|
|
21
|
+
height: 600;
|
|
22
|
+
brightness: 500;
|
|
23
|
+
ambient: 100;
|
|
24
|
+
maskStrength: 100;
|
|
25
|
+
}>;
|
|
26
|
+
export declare function defaultNormalMapLightOptions(width: number, height: number): NormalMapLightOptions;
|
|
27
|
+
export interface PreparedNormalMapLighting {
|
|
28
|
+
readonly width: number;
|
|
29
|
+
readonly height: number;
|
|
30
|
+
readonly colors: Float32Array;
|
|
31
|
+
readonly directions: Float32Array;
|
|
32
|
+
readonly alpha: Uint8Array;
|
|
33
|
+
readonly mask: Float32Array | null;
|
|
34
|
+
}
|
|
35
|
+
/** Decode invariant color and vector data once for repeated light positions. */
|
|
36
|
+
export declare function prepareNormalMapLighting(albedo: PixelBuffer, normals: PixelBuffer | null, lightMask?: PixelBuffer | null): PreparedNormalMapLighting;
|
|
37
|
+
/** Lambert point lighting in linear color; normal channels are raw vector data. */
|
|
38
|
+
export declare function renderPreparedNormalMapLighting(source: PreparedNormalMapLighting, options: NormalMapLightOptions): PixelBuffer;
|
|
39
|
+
/** Return the sprite with a painted directional light divided out, as flat-lit albedo. */
|
|
40
|
+
export declare function removePaintedLight(source: PreparedNormalMapLighting, delight: NonNullable<NormalMapLightOptions["delight"]>, invertY?: boolean): PixelBuffer;
|
|
41
|
+
export declare function renderNormalMapLighting(albedo: PixelBuffer, normals: PixelBuffer | null, options: NormalMapLightOptions, lightMask?: PixelBuffer | null): PixelBuffer;
|
|
42
|
+
//# sourceMappingURL=normal-map-lighting.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normal-map-lighting.d.ts","sourceRoot":"","sources":["../src/normal-map-lighting.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,KAAK,WAAW,EAAE,KAAK,KAAK,EAAE,KAAK,IAAI,EAAE,MAAM,WAAW,CAAC;AAGlG,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,KAAK,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,IAAI,CAAC;IACZ,OAAO,EAAE,OAAO,CAAC;IACjB;;;;OAIG;IACH,OAAO,CAAC,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;CAChD;AAgBD,eAAO,MAAM,mBAAmB;;;;;EAK9B,CAAC;AAEH,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,qBAAqB,CAUjG;AAsBD,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,YAAY,CAAC;IAClC,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,IAAI,CAAC;CACpC;AAGD,gFAAgF;AAChF,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,WAAW,GAAG,IAAI,EAC3B,SAAS,GAAE,WAAW,GAAG,IAAW,GACnC,yBAAyB,CAoC3B;AAgCD,mFAAmF;AACnF,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,yBAAyB,EACjC,OAAO,EAAE,qBAAqB,GAC7B,WAAW,CAyCb;AAED,0FAA0F;AAC1F,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,yBAAyB,EACjC,OAAO,EAAE,WAAW,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC,EACtD,OAAO,UAAQ,GACd,WAAW,CAab;AAED,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,WAAW,GAAG,IAAI,EAC3B,OAAO,EAAE,qBAAqB,EAC9B,SAAS,GAAE,WAAW,GAAG,IAAW,GACnC,WAAW,CAKb"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { assertPixelBuffer, UINT8_MAX } from "./base.js";
|
|
2
|
+
import { lightingMapLuminance } from "./lighting-map-source.js";
|
|
3
|
+
const DEFAULT_LIGHT_HEIGHT = 25;
|
|
4
|
+
const DEFAULT_LIGHT_BRIGHTNESS = 250;
|
|
5
|
+
const DEFAULT_AMBIENT = 15;
|
|
6
|
+
const PERCENT = 100;
|
|
7
|
+
const DEFAULT_MASK_STRENGTH = PERCENT;
|
|
8
|
+
const MIN_DISTANCE = 0.000001;
|
|
9
|
+
const LIGHT_RADIUS_FACTOR = 0.75;
|
|
10
|
+
const DELIGHT_ELEVATION = 45;
|
|
11
|
+
const DELIGHT_AMBIENT = 0.15;
|
|
12
|
+
const SRGB_DECODE_LIMIT = 0.04045;
|
|
13
|
+
const SRGB_ENCODE_LIMIT = 0.0031308;
|
|
14
|
+
const SRGB_LINEAR_FACTOR = 12.92;
|
|
15
|
+
const SRGB_OFFSET = 0.055;
|
|
16
|
+
const SRGB_SCALE = 1.055;
|
|
17
|
+
const SRGB_GAMMA = 2.4;
|
|
18
|
+
export const NORMAL_LIGHT_LIMITS = Object.freeze({
|
|
19
|
+
height: 600,
|
|
20
|
+
brightness: 500,
|
|
21
|
+
ambient: PERCENT,
|
|
22
|
+
maskStrength: PERCENT,
|
|
23
|
+
});
|
|
24
|
+
export function defaultNormalMapLightOptions(width, height) {
|
|
25
|
+
return {
|
|
26
|
+
position: { x: width / 2, y: height / 2 },
|
|
27
|
+
height: DEFAULT_LIGHT_HEIGHT,
|
|
28
|
+
brightness: DEFAULT_LIGHT_BRIGHTNESS,
|
|
29
|
+
ambient: DEFAULT_AMBIENT,
|
|
30
|
+
maskStrength: DEFAULT_MASK_STRENGTH,
|
|
31
|
+
color: [UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX],
|
|
32
|
+
invertY: false,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const linear = (value) => {
|
|
36
|
+
const channel = value / UINT8_MAX;
|
|
37
|
+
return channel <= SRGB_DECODE_LIMIT
|
|
38
|
+
? channel / SRGB_LINEAR_FACTOR
|
|
39
|
+
: ((channel + SRGB_OFFSET) / SRGB_SCALE) ** SRGB_GAMMA;
|
|
40
|
+
};
|
|
41
|
+
const encoded = (value) => Math.round(UINT8_MAX *
|
|
42
|
+
Math.max(0, Math.min(1, value <= SRGB_ENCODE_LIMIT
|
|
43
|
+
? value * SRGB_LINEAR_FACTOR
|
|
44
|
+
: SRGB_SCALE * value ** (1 / SRGB_GAMMA) - SRGB_OFFSET)));
|
|
45
|
+
const LINEAR_CHANNELS = Float32Array.from({ length: UINT8_MAX + 1 }, (_, value) => linear(value));
|
|
46
|
+
/** Decode invariant color and vector data once for repeated light positions. */
|
|
47
|
+
export function prepareNormalMapLighting(albedo, normals, lightMask = null) {
|
|
48
|
+
assertPixelBuffer(albedo);
|
|
49
|
+
if (normals) {
|
|
50
|
+
assertPixelBuffer(normals);
|
|
51
|
+
if (normals.width !== albedo.width || normals.height !== albedo.height)
|
|
52
|
+
throw new RangeError("Normal map dimensions must match the sprite");
|
|
53
|
+
}
|
|
54
|
+
if (lightMask) {
|
|
55
|
+
assertPixelBuffer(lightMask);
|
|
56
|
+
if (lightMask.width !== albedo.width || lightMask.height !== albedo.height)
|
|
57
|
+
throw new RangeError("Light mask dimensions must match the sprite");
|
|
58
|
+
}
|
|
59
|
+
const count = albedo.width * albedo.height;
|
|
60
|
+
const colors = new Float32Array(count * 3);
|
|
61
|
+
const directions = new Float32Array(count * 3);
|
|
62
|
+
const alpha = new Uint8Array(count);
|
|
63
|
+
const mask = lightMask ? new Float32Array(count) : null;
|
|
64
|
+
for (let i = 0; i < count; i++) {
|
|
65
|
+
const at = i * 4, vector = i * 3;
|
|
66
|
+
alpha[i] = albedo.data[at + 3];
|
|
67
|
+
if (mask && lightMask) {
|
|
68
|
+
const coverage = lightMask.data[at + 3] / UINT8_MAX;
|
|
69
|
+
mask[i] = 1 - coverage * (1 - lightingMapLuminance(lightMask.data, at));
|
|
70
|
+
}
|
|
71
|
+
for (let c = 0; c < 3; c++)
|
|
72
|
+
colors[vector + c] = LINEAR_CHANNELS[albedo.data[at + c]];
|
|
73
|
+
const valid = normals && normals.data[at + 3] > 0;
|
|
74
|
+
const nx = valid ? (normals.data[at] / UINT8_MAX) * 2 - 1 : 0;
|
|
75
|
+
const ny = valid ? (normals.data[at + 1] / UINT8_MAX) * 2 - 1 : 0;
|
|
76
|
+
const nz = valid ? (normals.data[at + 2] / UINT8_MAX) * 2 - 1 : 1;
|
|
77
|
+
const length = Math.hypot(nx, ny, nz) || 1;
|
|
78
|
+
directions[vector] = nx / length;
|
|
79
|
+
directions[vector + 1] = ny / length;
|
|
80
|
+
directions[vector + 2] = nz / length;
|
|
81
|
+
}
|
|
82
|
+
return { width: albedo.width, height: albedo.height, colors, directions, alpha, mask };
|
|
83
|
+
}
|
|
84
|
+
const limit = (value, max, fallback) => Number.isFinite(value) ? Math.max(0, Math.min(max, value)) : fallback;
|
|
85
|
+
/**
|
|
86
|
+
* Per-pixel gain that divides out a directional light painted at `angle` (+Y up azimuth),
|
|
87
|
+
* assuming the art was shaded as ambient + Lambert. Returns null when there is nothing to undo.
|
|
88
|
+
*/
|
|
89
|
+
function paintedLightGain(source, delight, ySign) {
|
|
90
|
+
if (!delight || !Number.isFinite(delight.angle))
|
|
91
|
+
return null;
|
|
92
|
+
const strength = limit(delight.influence, PERCENT, 0) / PERCENT;
|
|
93
|
+
if (!strength)
|
|
94
|
+
return null;
|
|
95
|
+
const azimuth = delight.angle * (Math.PI / 180);
|
|
96
|
+
const elevation = DELIGHT_ELEVATION * (Math.PI / 180);
|
|
97
|
+
const lx = Math.cos(azimuth) * Math.cos(elevation);
|
|
98
|
+
const ly = Math.sin(azimuth) * Math.cos(elevation) * ySign;
|
|
99
|
+
const lz = Math.sin(elevation);
|
|
100
|
+
const { directions } = source;
|
|
101
|
+
return (vector) => {
|
|
102
|
+
const facing = Math.max(0, directions[vector] * lx + directions[vector + 1] * ly + directions[vector + 2] * lz);
|
|
103
|
+
return 1 + strength * (1 / (DELIGHT_AMBIENT + (1 - DELIGHT_AMBIENT) * facing) - 1);
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** Lambert point lighting in linear color; normal channels are raw vector data. */
|
|
107
|
+
export function renderPreparedNormalMapLighting(source, options) {
|
|
108
|
+
const height = Math.max(MIN_DISTANCE, limit(options.height, NORMAL_LIGHT_LIMITS.height, DEFAULT_LIGHT_HEIGHT));
|
|
109
|
+
const power = limit(options.brightness, NORMAL_LIGHT_LIMITS.brightness, DEFAULT_LIGHT_BRIGHTNESS) / PERCENT;
|
|
110
|
+
const ambient = limit(options.ambient, NORMAL_LIGHT_LIMITS.ambient, DEFAULT_AMBIENT) / PERCENT;
|
|
111
|
+
const maskStrength = limit(options.maskStrength, NORMAL_LIGHT_LIMITS.maskStrength, DEFAULT_MASK_STRENGTH) / PERCENT;
|
|
112
|
+
const light = options.color.map(linear);
|
|
113
|
+
const radius = Math.max(1, Math.max(source.width, source.height) * LIGHT_RADIUS_FACTOR);
|
|
114
|
+
const x = Number.isFinite(options.position.x) ? options.position.x : source.width / 2;
|
|
115
|
+
const y = Number.isFinite(options.position.y) ? options.position.y : source.height / 2;
|
|
116
|
+
const ySign = options.invertY ? -1 : 1;
|
|
117
|
+
const data = new Uint8ClampedArray(source.width * source.height * 4);
|
|
118
|
+
const delight = paintedLightGain(source, options.delight, ySign);
|
|
119
|
+
for (let i = 0; i < source.alpha.length; i++) {
|
|
120
|
+
const at = i * 4, vector = i * 3;
|
|
121
|
+
data[at + 3] = source.alpha[i];
|
|
122
|
+
if (!source.alpha[i])
|
|
123
|
+
continue;
|
|
124
|
+
const dx = x - (i % source.width);
|
|
125
|
+
const dy = Math.floor(i / source.width) - y;
|
|
126
|
+
const distanceSquared = dx * dx + dy * dy + height * height;
|
|
127
|
+
const diffuse = Math.max(0, (source.directions[vector] * dx +
|
|
128
|
+
source.directions[vector + 1] * dy * ySign +
|
|
129
|
+
source.directions[vector + 2] * height) /
|
|
130
|
+
Math.sqrt(distanceSquared));
|
|
131
|
+
const reception = source.mask ? 1 - maskStrength * (1 - source.mask[i]) : 1;
|
|
132
|
+
const direct = (power * diffuse * reception) / (1 + distanceSquared / (radius * radius));
|
|
133
|
+
const gain = delight ? delight(vector) : 1;
|
|
134
|
+
for (let channel = 0; channel < 3; channel++)
|
|
135
|
+
data[at + channel] = encoded(source.colors[vector + channel] * gain * (ambient + direct * light[channel]));
|
|
136
|
+
}
|
|
137
|
+
return { width: source.width, height: source.height, data };
|
|
138
|
+
}
|
|
139
|
+
/** Return the sprite with a painted directional light divided out, as flat-lit albedo. */
|
|
140
|
+
export function removePaintedLight(source, delight, invertY = false) {
|
|
141
|
+
const gain = paintedLightGain(source, delight, invertY ? -1 : 1);
|
|
142
|
+
const data = new Uint8ClampedArray(source.width * source.height * 4);
|
|
143
|
+
for (let i = 0; i < source.alpha.length; i++) {
|
|
144
|
+
const at = i * 4, vector = i * 3;
|
|
145
|
+
data[at + 3] = source.alpha[i];
|
|
146
|
+
if (!source.alpha[i])
|
|
147
|
+
continue;
|
|
148
|
+
const value = gain ? gain(vector) : 1;
|
|
149
|
+
for (let channel = 0; channel < 3; channel++)
|
|
150
|
+
data[at + channel] = encoded(source.colors[vector + channel] * value);
|
|
151
|
+
}
|
|
152
|
+
return { width: source.width, height: source.height, data };
|
|
153
|
+
}
|
|
154
|
+
export function renderNormalMapLighting(albedo, normals, options, lightMask = null) {
|
|
155
|
+
return renderPreparedNormalMapLighting(prepareNormalMapLighting(albedo, normals, lightMask), options);
|
|
156
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type PixelBuffer } from "./base.js";
|
|
2
|
+
/** Signed luminance relative to the surrounding foreground, independent of the light angle. */
|
|
3
|
+
export declare function paintedLightSignal(width: number, height: number, foreground: Uint8Array, foregroundArea: number, luminance: Float32Array, shading?: PixelBuffer): Float32Array;
|
|
4
|
+
//# sourceMappingURL=normal-map-painted-light.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normal-map-painted-light.d.ts","sourceRoot":"","sources":["../src/normal-map-painted-light.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAUxD,+FAA+F;AAC/F,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,UAAU,EACtB,cAAc,EAAE,MAAM,EACtB,SAAS,EAAE,YAAY,EACvB,OAAO,CAAC,EAAE,WAAW,GACpB,YAAY,CAwDd"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { UINT8_MAX } from "./base.js";
|
|
2
|
+
import { lightingMapLuminance } from "./lighting-map-source.js";
|
|
3
|
+
const RGBA_CHANNELS = 4;
|
|
4
|
+
const ALPHA_CHANNEL = 3;
|
|
5
|
+
const NEUTRAL_SHADING = 0.5;
|
|
6
|
+
const RADIUS_SCALE = 4;
|
|
7
|
+
const MIN_RADIUS = 2;
|
|
8
|
+
const MAX_RADIUS = 12;
|
|
9
|
+
/** Signed luminance relative to the surrounding foreground, independent of the light angle. */
|
|
10
|
+
export function paintedLightSignal(width, height, foreground, foregroundArea, luminance, shading) {
|
|
11
|
+
const radius = Math.min(MAX_RADIUS, Math.max(MIN_RADIUS, Math.round(Math.sqrt(foregroundArea) / RADIUS_SCALE)));
|
|
12
|
+
const rowSums = new Float32Array(foreground.length);
|
|
13
|
+
const rowCounts = new Uint8Array(foreground.length);
|
|
14
|
+
const signal = new Float32Array(foreground.length);
|
|
15
|
+
const brightness = (pixel) => {
|
|
16
|
+
if (!shading)
|
|
17
|
+
return luminance[pixel];
|
|
18
|
+
const at = pixel * RGBA_CHANNELS;
|
|
19
|
+
const alpha = shading.data[at + ALPHA_CHANNEL] / UINT8_MAX;
|
|
20
|
+
return NEUTRAL_SHADING + alpha * (lightingMapLuminance(shading.data, at) - NEUTRAL_SHADING);
|
|
21
|
+
};
|
|
22
|
+
for (let y = 0; y < height; y++) {
|
|
23
|
+
const start = y * width;
|
|
24
|
+
let sum = 0;
|
|
25
|
+
let count = 0;
|
|
26
|
+
for (let x = -radius; x < width; x++) {
|
|
27
|
+
const next = x + radius;
|
|
28
|
+
if (next < width && foreground[start + next]) {
|
|
29
|
+
sum += brightness(start + next);
|
|
30
|
+
count++;
|
|
31
|
+
}
|
|
32
|
+
const previous = x - radius - 1;
|
|
33
|
+
if (previous >= 0 && foreground[start + previous]) {
|
|
34
|
+
sum -= brightness(start + previous);
|
|
35
|
+
count--;
|
|
36
|
+
}
|
|
37
|
+
if (x >= 0) {
|
|
38
|
+
rowSums[start + x] = sum;
|
|
39
|
+
rowCounts[start + x] = count;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (let x = 0; x < width; x++) {
|
|
44
|
+
let sum = 0;
|
|
45
|
+
let count = 0;
|
|
46
|
+
for (let y = -radius; y < height; y++) {
|
|
47
|
+
const next = y + radius;
|
|
48
|
+
if (next < height) {
|
|
49
|
+
sum += rowSums[next * width + x];
|
|
50
|
+
count += rowCounts[next * width + x];
|
|
51
|
+
}
|
|
52
|
+
const previous = y - radius - 1;
|
|
53
|
+
if (previous >= 0) {
|
|
54
|
+
sum -= rowSums[previous * width + x];
|
|
55
|
+
count -= rowCounts[previous * width + x];
|
|
56
|
+
}
|
|
57
|
+
const pixel = y * width + x;
|
|
58
|
+
if (y >= 0 && foreground[pixel] && count)
|
|
59
|
+
signal[pixel] = brightness(pixel) - sum / count;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return signal;
|
|
63
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapted from Pixel Art Lighter v3.5.22 by Red Hat Sprite, MIT.
|
|
3
|
+
* Copyright (c) 2026 Red Hat Sprite. Full notice: LICENSES/pixel-art-lighter-MIT.txt.
|
|
4
|
+
* https://www.moonfell-rpg.com/Pixel-Art-Lighter-v3.5.22.html
|
|
5
|
+
*/
|
|
6
|
+
import { type PixelBuffer } from "./base.js";
|
|
7
|
+
export declare enum NormalMapPreset {
|
|
8
|
+
Architecture = "architecture",
|
|
9
|
+
Character = "character",
|
|
10
|
+
Object = "object",
|
|
11
|
+
Terrain = "terrain"
|
|
12
|
+
}
|
|
13
|
+
export interface NormalMapOptions {
|
|
14
|
+
preset: NormalMapPreset;
|
|
15
|
+
shapeStrength: number;
|
|
16
|
+
normalStrength: number;
|
|
17
|
+
smoothing: number;
|
|
18
|
+
facets: number;
|
|
19
|
+
mergeOutlines: boolean;
|
|
20
|
+
invertY: boolean;
|
|
21
|
+
paintedLightAngle: number;
|
|
22
|
+
paintedLightInfluence: number;
|
|
23
|
+
}
|
|
24
|
+
export declare const NORMAL_MAP_LIMITS: Readonly<{
|
|
25
|
+
shapeStrength: 200;
|
|
26
|
+
normalStrength: 300;
|
|
27
|
+
smoothing: 6;
|
|
28
|
+
facets: 100;
|
|
29
|
+
paintedLightAngle: number;
|
|
30
|
+
paintedLightInfluence: 100;
|
|
31
|
+
}>;
|
|
32
|
+
export declare function defaultNormalMapOptions(preset?: NormalMapPreset): NormalMapOptions;
|
|
33
|
+
export declare function normalizeNormalMapOptions(input: Partial<NormalMapOptions>): NormalMapOptions;
|
|
34
|
+
/** Cache source segmentation, silhouette distance and geometry between parameter edits. */
|
|
35
|
+
export declare function createNormalMapGenerator(albedo: PixelBuffer, shading?: PixelBuffer): {
|
|
36
|
+
setShading(source: PixelBuffer | null): void;
|
|
37
|
+
suggestPaintedLightAngle(): number | null;
|
|
38
|
+
generate(input?: Partial<NormalMapOptions>): PixelBuffer;
|
|
39
|
+
};
|
|
40
|
+
export declare function generateNormalMap(albedo: PixelBuffer, input?: Partial<NormalMapOptions>): PixelBuffer;
|
|
41
|
+
//# sourceMappingURL=normal-map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normal-map.d.ts","sourceRoot":"","sources":["../src/normal-map.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAqB,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAUhE,oBAAY,eAAe;IACzB,YAAY,iBAAiB;IAC7B,SAAS,cAAc;IACvB,MAAM,WAAW;IACjB,OAAO,YAAY;CACpB;AACD,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,eAAe,CAAC;IACxB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,EAAE,OAAO,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;CAC/B;AAmDD,eAAO,MAAM,iBAAiB;;;;;;;EAO5B,CAAC;AAUH,wBAAgB,uBAAuB,CAAC,MAAM,kBAAyB,GAAG,gBAAgB,CAUzF;AACD,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,gBAAgB,CAa5F;AAED,2FAA2F;AAC3F,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,WAAW,EACnB,OAAO,CAAC,EAAE,WAAW,GACpB;IACD,UAAU,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,GAAG,IAAI,CAAC;IAC7C,wBAAwB,IAAI,MAAM,GAAG,IAAI,CAAC;IAC1C,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,WAAW,CAAC;CAC1D,CA6VA;AAED,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,WAAW,EACnB,KAAK,GAAE,OAAO,CAAC,gBAAgB,CAAM,GACpC,WAAW,CAEb"}
|