@xprite/lighter 0.1.1 → 0.1.2

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.
@@ -4,6 +4,8 @@
4
4
  * https://www.moonfell-rpg.com/Pixel-Art-Lighter-v3.5.22.html
5
5
  */
6
6
  import { type PixelBuffer } from "./base.js";
7
+ import { segmentLightingMapSource } from "./lighting-map-source.js";
8
+ import { type PaintedLightEstimate } from "./normal-map-light-estimate.js";
7
9
  export declare enum NormalMapPreset {
8
10
  Architecture = "architecture",
9
11
  Character = "character",
@@ -31,11 +33,32 @@ export declare const NORMAL_MAP_LIMITS: Readonly<{
31
33
  }>;
32
34
  export declare function defaultNormalMapOptions(preset?: NormalMapPreset): NormalMapOptions;
33
35
  export declare function normalizeNormalMapOptions(input: Partial<NormalMapOptions>): NormalMapOptions;
36
+ export interface GenerateGeometryOptions extends Partial<NormalMapOptions> {
37
+ /**
38
+ * When true, also include the pre-quantization continuous unit Float32 normals
39
+ * (`continuousDirections`) before per-cluster averaging and 8-azimuth facet quantization.
40
+ * Does not modify `normalMap` or `directions`. Default false.
41
+ */
42
+ readonly includeContinuousDirections?: boolean;
43
+ }
44
+ export interface NormalMapGeometry {
45
+ readonly normalMap: PixelBuffer;
46
+ readonly directions: Float32Array;
47
+ /** Pre-quantization continuous unit Float32 normals (present only when `includeContinuousDirections: true`). */
48
+ readonly continuousDirections?: Float32Array;
49
+ readonly heightField: Float32Array;
50
+ readonly silhouetteDistance: Float32Array;
51
+ readonly thinOutline: Uint8Array;
52
+ readonly lightSignal: Float32Array;
53
+ readonly paintedLight: PaintedLightEstimate | null;
54
+ readonly segmented: ReturnType<typeof segmentLightingMapSource>;
55
+ }
34
56
  /** Cache source segmentation, silhouette distance and geometry between parameter edits. */
35
57
  export declare function createNormalMapGenerator(albedo: PixelBuffer, shading?: PixelBuffer): {
36
58
  setShading(source: PixelBuffer | null): void;
37
59
  suggestPaintedLightAngle(): number | null;
38
60
  generate(input?: Partial<NormalMapOptions>): PixelBuffer;
61
+ generateGeometry(input?: GenerateGeometryOptions): NormalMapGeometry;
39
62
  };
40
63
  export declare function generateNormalMap(albedo: PixelBuffer, input?: Partial<NormalMapOptions>): PixelBuffer;
41
64
  //# sourceMappingURL=normal-map.d.ts.map
@@ -1 +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;AAqDD,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,CAgWA;AAED,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,WAAW,EACnB,KAAK,GAAE,OAAO,CAAC,gBAAgB,CAAM,GACpC,WAAW,CAEb"}
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;AAChE,OAAO,EAIL,wBAAwB,EACzB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAwB,KAAK,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AAGjG,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;AAqDD,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,CAgB5F;AAED,MAAM,WAAW,uBAAwB,SAAQ,OAAO,CAAC,gBAAgB,CAAC;IACxE;;;;OAIG;IACH,QAAQ,CAAC,2BAA2B,CAAC,EAAE,OAAO,CAAC;CAChD;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC;IAChC,QAAQ,CAAC,UAAU,EAAE,YAAY,CAAC;IAClC,gHAAgH;IAChH,QAAQ,CAAC,oBAAoB,CAAC,EAAE,YAAY,CAAC;IAC7C,QAAQ,CAAC,WAAW,EAAE,YAAY,CAAC;IACnC,QAAQ,CAAC,kBAAkB,EAAE,YAAY,CAAC;IAC1C,QAAQ,CAAC,WAAW,EAAE,UAAU,CAAC;IACjC,QAAQ,CAAC,WAAW,EAAE,YAAY,CAAC;IACnC,QAAQ,CAAC,YAAY,EAAE,oBAAoB,GAAG,IAAI,CAAC;IACnD,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC,OAAO,wBAAwB,CAAC,CAAC;CACjE;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;IACzD,gBAAgB,CAAC,KAAK,CAAC,EAAE,uBAAuB,GAAG,iBAAiB,CAAC;CACtE,CAwYA;AAED,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,WAAW,EACnB,KAAK,GAAE,OAAO,CAAC,gBAAgB,CAAM,GACpC,WAAW,CAEb"}
@@ -96,7 +96,10 @@ export function normalizeNormalMapOptions(input) {
96
96
  for (const key of Object.keys(NORMAL_MAP_LIMITS)) {
97
97
  const value = input[key];
98
98
  if (typeof value === "number" && Number.isFinite(value))
99
- result[key] = Math.round(Math.max(0, Math.min(NORMAL_MAP_LIMITS[key], value)));
99
+ result[key] =
100
+ key === "paintedLightAngle"
101
+ ? ((Math.round(value) % 360) + 360) % 360
102
+ : Math.round(Math.max(0, Math.min(NORMAL_MAP_LIMITS[key], value)));
100
103
  }
101
104
  for (const key of ["mergeOutlines", "invertY"])
102
105
  if (typeof input[key] === "boolean")
@@ -105,7 +108,8 @@ export function normalizeNormalMapOptions(input) {
105
108
  }
106
109
  /** Cache source segmentation, silhouette distance and geometry between parameter edits. */
107
110
  export function createNormalMapGenerator(albedo, shading) {
108
- const { w, h, N, a, fg, lum, labels, lo, hi, adjacent, queue, clusters, foregroundArea } = segmentLightingMapSource(albedo, LightingMapRegionMode.Perceptual);
111
+ const segmented = segmentLightingMapSource(albedo, LightingMapRegionMode.Perceptual);
112
+ const { w, h, N, a, fg, lum, labels, lo, hi, adjacent, queue, clusters, foregroundArea } = segmented;
109
113
  const spriteScale = Math.min(FACET_MAX_SPRITE_SCALE, Math.sqrt(foregroundArea) / FACET_REFERENCE_SIDE);
110
114
  const distance = new Float32Array(N);
111
115
  distance.fill(N);
@@ -141,6 +145,7 @@ export function createNormalMapGenerator(albedo, shading) {
141
145
  let cachedField = null;
142
146
  let cachedKey = "";
143
147
  let cachedDirections = null;
148
+ let cachedContinuousDirections = null;
144
149
  let shadingSource;
145
150
  let lightSignal;
146
151
  let paintedEstimate;
@@ -159,144 +164,148 @@ export function createNormalMapGenerator(albedo, shading) {
159
164
  lightSignal = undefined;
160
165
  paintedEstimate = undefined;
161
166
  cachedKey = "";
167
+ cachedContinuousDirections = null;
162
168
  };
163
169
  if (shading)
164
170
  setShading(shading);
165
- return {
166
- setShading,
167
- suggestPaintedLightAngle() {
168
- return estimatePainted()?.angle ?? null;
169
- },
170
- generate(input = {}) {
171
- const options = normalizeNormalMapOptions(input);
172
- const experimental = input.experimental ?? {};
173
- let influence = options.paintedLightInfluence / PERCENT;
174
- let angleDeg = options.paintedLightAngle;
175
- if (influence > 0 && input.paintedLightAngle === undefined) {
176
- const estimate = estimatePainted();
177
- if (estimate) {
178
- angleDeg = estimate.angle;
179
- influence *= Math.min(1, Math.max(0, (estimate.fit - ESTIMATE_FADE_LOW) / (ESTIMATE_FADE_HIGH - ESTIMATE_FADE_LOW)));
180
- }
181
- else
182
- influence = 0;
171
+ const generateInternal = (input = {}, includeContinuous = false) => {
172
+ const options = normalizeNormalMapOptions(input);
173
+ const experimental = input.experimental ?? {};
174
+ let influence = options.paintedLightInfluence / PERCENT;
175
+ let angleDeg = options.paintedLightAngle;
176
+ if (influence > 0 && input.paintedLightAngle === undefined) {
177
+ const estimate = estimatePainted();
178
+ if (estimate) {
179
+ angleDeg = estimate.angle;
180
+ influence *= Math.min(1, Math.max(0, (estimate.fit - ESTIMATE_FADE_LOW) / (ESTIMATE_FADE_HIGH - ESTIMATE_FADE_LOW)));
183
181
  }
184
- const key = JSON.stringify([
185
- options.preset,
186
- options.shapeStrength,
187
- options.smoothing,
188
- options.facets,
189
- options.mergeOutlines,
190
- influence,
191
- influence ? angleDeg : 0,
192
- ]);
193
- let directions = cachedDirections;
194
- if (!directions || cachedKey !== key) {
195
- const preset = options.preset;
196
- const profile = HEIGHT_PROFILES[preset];
197
- const scale = options.shapeStrength / PERCENT;
198
- const fieldKey = JSON.stringify([preset, options.smoothing, influence, experimental]);
199
- let field = cachedField;
200
- if (!field || cachedFieldKey !== fieldKey) {
201
- const distField = experimental.chamferDistance
202
- ? chamferDistance(fg, w, h)
203
- : distance;
204
- const groove = experimental.edgeGroove ?? 0;
205
- const edgeDist = groove > 0 ? colorEdgeDistance(a, fg, w, h) : null;
206
- // silhouette ramp exponent scales from the small-sprite sqrt profile to a
207
- // linear ramp once the sprite is large enough for facets to stay accurate
208
- const pow = experimental.bevelPow ??
209
- Math.min(BEVEL_POW_LINEAR, Math.max(BEVEL_POW_SQRT, BEVEL_POW_SQRT + (spriteScale - BEVEL_POW_PIVOT) * 0.5));
210
- const mid = experimental.midLum ? (lo + hi) / 2 : null;
211
- const lumTerm = experimental.oklab ? new Float32Array(N) : null;
212
- if (lumTerm)
213
- for (let i = 0; i < N; i++)
214
- lumTerm[i] = fg[i]
215
- ? oklabLightness(a[i * 4], a[i * 4 + 1], a[i * 4 + 2])
216
- : 0;
217
- let nextField = new Float32Array(N);
182
+ else
183
+ influence = 0;
184
+ }
185
+ const key = JSON.stringify([
186
+ options.preset,
187
+ options.shapeStrength,
188
+ options.smoothing,
189
+ options.facets,
190
+ options.mergeOutlines,
191
+ influence,
192
+ influence ? angleDeg : 0,
193
+ experimental,
194
+ ]);
195
+ let directions = cachedDirections;
196
+ const needsDirections = !directions || cachedKey !== key;
197
+ const needsContinuousOnly = !needsDirections && includeContinuous && !cachedContinuousDirections;
198
+ if (needsDirections || needsContinuousOnly) {
199
+ const preset = options.preset;
200
+ const profile = HEIGHT_PROFILES[preset];
201
+ const scale = options.shapeStrength / PERCENT;
202
+ const fieldKey = JSON.stringify([preset, options.smoothing, influence, experimental]);
203
+ let field = cachedField;
204
+ if (!field || cachedFieldKey !== fieldKey) {
205
+ const distField = experimental.chamferDistance
206
+ ? chamferDistance(fg, w, h)
207
+ : distance;
208
+ const groove = experimental.edgeGroove ?? 0;
209
+ const edgeDist = groove > 0 ? colorEdgeDistance(a, fg, w, h) : null;
210
+ // silhouette ramp exponent scales from the small-sprite sqrt profile to a
211
+ // linear ramp once the sprite is large enough for facets to stay accurate
212
+ const pow = experimental.bevelPow ??
213
+ Math.min(BEVEL_POW_LINEAR, Math.max(BEVEL_POW_SQRT, BEVEL_POW_SQRT + (spriteScale - BEVEL_POW_PIVOT) * 0.5));
214
+ const mid = experimental.midLum ? (lo + hi) / 2 : null;
215
+ const lumTerm = experimental.oklab ? new Float32Array(N) : null;
216
+ if (lumTerm)
218
217
  for (let i = 0; i < N; i++)
219
- if (fg[i]) {
220
- const convex = Math.pow(Math.min(distField[i], profile.cap) / profile.cap, pow) *
221
- profile.silhouette * Math.sqrt(profile.cap);
222
- const lumPart = lumTerm
223
- ? lumTerm[i] * profile.luminance * (1 - influence)
224
- : mid !== null
225
- ? (lum[i] - mid) * profile.luminance * (1 - influence)
226
- : normalized(i) * profile.luminance * (1 - influence);
227
- const dip = edgeDist ? groove * Math.min(1, edgeDist[i] / 2) : 0;
228
- nextField[i] = convex + lumPart + dip;
229
- }
230
- for (let pass = 0; pass < options.smoothing; pass++) {
231
- let blurred;
232
- if (experimental.maskedBlur) {
233
- blurred = separableMaskedBlur(nextField, fg, w, h);
234
- }
235
- else {
236
- blurred = new Float32Array(N);
237
- for (let i = 0; i < N; i++)
238
- if (fg[i]) {
239
- let sum = nextField[i] * CENTER_SMOOTHING_WEIGHT, count = CENTER_SMOOTHING_WEIGHT;
240
- for (const n of adjacent(i))
241
- if (fg[n]) {
242
- sum += nextField[n];
243
- count++;
244
- }
245
- blurred[i] = sum / count;
246
- }
247
- }
248
- nextField = blurred;
218
+ lumTerm[i] = fg[i]
219
+ ? oklabLightness(a[i * 4], a[i * 4 + 1], a[i * 4 + 2])
220
+ : 0;
221
+ let nextField = new Float32Array(N);
222
+ for (let i = 0; i < N; i++)
223
+ if (fg[i]) {
224
+ const convex = Math.pow(Math.min(distField[i], profile.cap) / profile.cap, pow) *
225
+ profile.silhouette * Math.sqrt(profile.cap);
226
+ const lumPart = lumTerm
227
+ ? lumTerm[i] * profile.luminance * (1 - influence)
228
+ : mid !== null
229
+ ? (lum[i] - mid) * profile.luminance * (1 - influence)
230
+ : normalized(i) * profile.luminance * (1 - influence);
231
+ const dip = edgeDist ? groove * Math.min(1, edgeDist[i] / 2) : 0;
232
+ nextField[i] = convex + lumPart + dip;
233
+ }
234
+ for (let pass = 0; pass < options.smoothing; pass++) {
235
+ let blurred;
236
+ if (experimental.maskedBlur) {
237
+ blurred = separableMaskedBlur(nextField, fg, w, h);
238
+ }
239
+ else {
240
+ blurred = new Float32Array(N);
241
+ for (let i = 0; i < N; i++)
242
+ if (fg[i]) {
243
+ let sum = nextField[i] * CENTER_SMOOTHING_WEIGHT, count = CENTER_SMOOTHING_WEIGHT;
244
+ for (const n of adjacent(i))
245
+ if (fg[n]) {
246
+ sum += nextField[n];
247
+ count++;
248
+ }
249
+ blurred[i] = sum / count;
250
+ }
249
251
  }
250
- cachedFieldKey = fieldKey;
251
- field = cachedField = nextField;
252
+ nextField = blurred;
252
253
  }
253
- const dirs = new Float32Array(N * 3);
254
- const unit = (v) => {
255
- const len = Math.hypot(...v) || 1;
256
- return v.map((n) => n / len);
257
- };
258
- const quantize = (v) => {
259
- v = unit(v);
260
- const tilt = Math.hypot(v[0], v[1]);
261
- if (tilt < FRONT_FACING_LIMIT)
262
- return [0, 0, 1];
263
- const angle = Math.round(Math.atan2(v[1], v[0]) / AZIMUTH_STEP) * AZIMUTH_STEP;
264
- const elevation = preset === NormalMapPreset.Architecture
265
- ? Math.min(TILT_LEVEL_MAX, ARCHITECTURE_TILT * scale)
266
- : tilt < TILT_LOW
267
- ? TILT_LEVEL_LOW
268
- : tilt < TILT_MEDIUM
269
- ? TILT_LEVEL_MEDIUM
270
- : tilt < TILT_HIGH
271
- ? TILT_LEVEL_HIGH
272
- : TILT_LEVEL_MAX;
273
- return [
274
- Math.cos(angle) * elevation,
275
- Math.sin(angle) * elevation,
276
- Math.sqrt(1 - elevation * elevation),
277
- ];
278
- };
279
- const heightField = field;
280
- if (influence)
281
- lightSignal ??= paintedLightSignal(w, h, fg, foregroundArea, lum, shadingSource);
282
- const angle = angleDeg * RADIANS_PER_DEGREE;
283
- const lightX = Math.cos(angle) * influence * PAINTED_LIGHT_TILT_GAIN;
284
- const lightY = Math.sin(angle) * influence * PAINTED_LIGHT_TILT_GAIN;
285
- const normalAt = (pixel) => {
286
- const x = pixel % w;
287
- const y = (pixel / w) | 0;
288
- const center = heightField[pixel];
289
- const at = (neighbor) => (fg[neighbor] ? heightField[neighbor] : center);
290
- const dx = (x < w - 1 ? at(pixel + 1) : center) - (x ? at(pixel - 1) : center);
291
- const dy = (y < h - 1 ? at(pixel + w) : center) - (y ? at(pixel - w) : center);
292
- if (!influence)
293
- return unit([-dx * scale, dy * scale, 1]);
294
- const tilt = lightSignal[pixel];
295
- return unit([-dx * scale + lightX * tilt, dy * scale + lightY * tilt, 1]);
296
- };
297
- for (let i = 0; i < N; i++)
298
- if (fg[i])
299
- dirs.set(normalAt(i), i * 3);
254
+ cachedFieldKey = fieldKey;
255
+ field = cachedField = nextField;
256
+ }
257
+ const dirs = new Float32Array(N * 3);
258
+ const unit = (v) => {
259
+ const len = Math.hypot(...v) || 1;
260
+ return v.map((n) => n / len);
261
+ };
262
+ const quantize = (v) => {
263
+ v = unit(v);
264
+ const tilt = Math.hypot(v[0], v[1]);
265
+ if (tilt < FRONT_FACING_LIMIT)
266
+ return [0, 0, 1];
267
+ const angle = Math.round(Math.atan2(v[1], v[0]) / AZIMUTH_STEP) * AZIMUTH_STEP;
268
+ const elevation = preset === NormalMapPreset.Architecture
269
+ ? Math.min(TILT_LEVEL_MAX, ARCHITECTURE_TILT * scale)
270
+ : tilt < TILT_LOW
271
+ ? TILT_LEVEL_LOW
272
+ : tilt < TILT_MEDIUM
273
+ ? TILT_LEVEL_MEDIUM
274
+ : tilt < TILT_HIGH
275
+ ? TILT_LEVEL_HIGH
276
+ : TILT_LEVEL_MAX;
277
+ return [
278
+ Math.cos(angle) * elevation,
279
+ Math.sin(angle) * elevation,
280
+ Math.sqrt(1 - elevation * elevation),
281
+ ];
282
+ };
283
+ const heightField = field;
284
+ if (influence)
285
+ lightSignal ??= paintedLightSignal(w, h, fg, foregroundArea, lum, shadingSource);
286
+ const angle = angleDeg * RADIANS_PER_DEGREE;
287
+ const lightX = Math.cos(angle) * influence * PAINTED_LIGHT_TILT_GAIN;
288
+ const lightY = Math.sin(angle) * influence * PAINTED_LIGHT_TILT_GAIN;
289
+ const normalAt = (pixel) => {
290
+ const x = pixel % w;
291
+ const y = (pixel / w) | 0;
292
+ const center = heightField[pixel];
293
+ const at = (neighbor) => (fg[neighbor] ? heightField[neighbor] : center);
294
+ const dx = (x < w - 1 ? at(pixel + 1) : center) - (x ? at(pixel - 1) : center);
295
+ const dy = (y < h - 1 ? at(pixel + w) : center) - (y ? at(pixel - w) : center);
296
+ if (!influence)
297
+ return unit([-dx * scale, dy * scale, 1]);
298
+ const tilt = lightSignal[pixel];
299
+ return unit([-dx * scale + lightX * tilt, dy * scale + lightY * tilt, 1]);
300
+ };
301
+ for (let i = 0; i < N; i++)
302
+ if (fg[i])
303
+ dirs.set(normalAt(i), i * 3);
304
+ if (needsContinuousOnly) {
305
+ cachedContinuousDirections = dirs;
306
+ }
307
+ else {
308
+ cachedContinuousDirections = includeContinuous ? dirs.slice() : null;
300
309
  const facetStrength = options.facets / PERCENT;
301
310
  // Size eligibility by the occupied footprint, then size each candidate by its own region.
302
311
  const minFacetArea = Math.max(FACET_MIN_REGION_AREA, Math.round((FACET_AREA_BASE - options.facets * FACET_AREA_SCALE) * spriteScale));
@@ -415,21 +424,52 @@ export function createNormalMapGenerator(albedo, shading) {
415
424
  cachedKey = key;
416
425
  directions = cachedDirections = dirs;
417
426
  }
418
- const output = new Uint8ClampedArray(N * 4);
419
- const gain = options.normalStrength / PERCENT;
420
- const ySign = options.invertY ? -1 : 1;
421
- for (let i = 0; i < N; i++) {
422
- const vector = i * 3, at = i * 4;
423
- const x = fg[i] ? directions[vector] * gain : 0;
424
- const y = fg[i] ? directions[vector + 1] * gain : 0;
425
- const z = fg[i] ? directions[vector + 2] : 1;
426
- const length = Math.sqrt(x * x + y * y + z * z) || 1;
427
- output[at] = Math.round((x / length + 1) * NORMAL_CHANNEL_SCALE);
428
- output[at + 1] = Math.round(((y / length) * ySign + 1) * NORMAL_CHANNEL_SCALE);
429
- output[at + 2] = Math.round((z / length + 1) * NORMAL_CHANNEL_SCALE);
430
- output[at + 3] = a[at + 3];
431
- }
432
- return { width: w, height: h, data: output };
427
+ }
428
+ const activeDirections = directions;
429
+ const output = new Uint8ClampedArray(N * 4);
430
+ const gain = options.normalStrength / PERCENT;
431
+ const ySign = options.invertY ? -1 : 1;
432
+ for (let i = 0; i < N; i++) {
433
+ const vector = i * 3, at = i * 4;
434
+ const x = fg[i] ? activeDirections[vector] * gain : 0;
435
+ const y = fg[i] ? activeDirections[vector + 1] * gain : 0;
436
+ const z = fg[i] ? activeDirections[vector + 2] : 1;
437
+ const length = Math.sqrt(x * x + y * y + z * z) || 1;
438
+ output[at] = Math.round((x / length + 1) * NORMAL_CHANNEL_SCALE);
439
+ output[at + 1] = Math.round(((y / length) * ySign + 1) * NORMAL_CHANNEL_SCALE);
440
+ output[at + 2] = Math.round((z / length + 1) * NORMAL_CHANNEL_SCALE);
441
+ output[at + 3] = a[at + 3];
442
+ }
443
+ return { width: w, height: h, data: output };
444
+ };
445
+ return {
446
+ setShading,
447
+ suggestPaintedLightAngle() {
448
+ return estimatePainted()?.angle ?? null;
449
+ },
450
+ generate(input = {}) {
451
+ return generateInternal(input, false);
452
+ },
453
+ generateGeometry(input = {}) {
454
+ const includeContinuous = input.includeContinuousDirections === true;
455
+ const normalMap = generateInternal(input, includeContinuous);
456
+ lightSignal ??=
457
+ foregroundArea > 0
458
+ ? paintedLightSignal(w, h, fg, foregroundArea, lum, shadingSource)
459
+ : new Float32Array(N);
460
+ return {
461
+ normalMap,
462
+ directions: cachedDirections ?? new Float32Array(N * 3),
463
+ ...(includeContinuous
464
+ ? { continuousDirections: cachedContinuousDirections ?? new Float32Array(N * 3) }
465
+ : {}),
466
+ heightField: cachedField ?? new Float32Array(N),
467
+ silhouetteDistance: distance,
468
+ thinOutline: thin,
469
+ lightSignal,
470
+ paintedLight: estimatePainted() ?? null,
471
+ segmented,
472
+ };
433
473
  },
434
474
  };
435
475
  }
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@xprite/lighter",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Generate normal maps, light masks, and relighted renders from pixel art.",
5
5
  "license": "MIT",
6
6
  "author": "rhinoc",
7
+ "packageManager": "pnpm@11.1.3",
7
8
  "repository": {
8
9
  "type": "git",
9
10
  "url": "git+https://github.com/rhinoc/xprite-lighter.git"
@@ -30,18 +31,19 @@
30
31
  "LICENSE",
31
32
  "README.md"
32
33
  ],
33
- "devDependencies": {
34
- "@types/node": "^26.6.6",
35
- "pngjs": "^7.0.0",
36
- "typescript": "^6.0.3",
37
- "vitest": "^5.0.2"
38
- },
39
34
  "scripts": {
40
35
  "build": "rm -rf dist && tsc -p tsconfig.build.json",
41
36
  "typecheck": "tsc -p tsconfig.json",
42
37
  "test": "vitest run",
38
+ "prepublishOnly": "pnpm run build",
43
39
  "bench": "node bench/run.mjs && python3 bench/run-relit.py --out bench/run",
44
40
  "demo": "python3 -m http.server 8734",
45
41
  "bench:all": "for m in object character terrain architecture spritelab spritefusion naive; do node bench/run.mjs bench bench/run-$m $m && python3 bench/run-relit.py --out bench/run-$m; done"
42
+ },
43
+ "devDependencies": {
44
+ "@types/node": "^26.6.6",
45
+ "pngjs": "^7.0.0",
46
+ "typescript": "^6.0.3",
47
+ "vitest": "^5.0.2"
46
48
  }
47
- }
49
+ }