@xprite/lighter 0.1.0 → 0.1.1
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.
|
@@ -1 +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;
|
|
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;AAiBD,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"}
|
|
@@ -6,7 +6,8 @@ const DEFAULT_AMBIENT = 15;
|
|
|
6
6
|
const PERCENT = 100;
|
|
7
7
|
const DEFAULT_MASK_STRENGTH = PERCENT;
|
|
8
8
|
const MIN_DISTANCE = 0.000001;
|
|
9
|
-
|
|
9
|
+
// A fixed reference distance in source pixels keeps the light independent of canvas padding.
|
|
10
|
+
const LIGHT_FALLOFF_DISTANCE_SQUARED = 32 ** 2;
|
|
10
11
|
const DELIGHT_ELEVATION = 45;
|
|
11
12
|
const DELIGHT_AMBIENT = 0.15;
|
|
12
13
|
const SRGB_DECODE_LIMIT = 0.04045;
|
|
@@ -110,7 +111,6 @@ export function renderPreparedNormalMapLighting(source, options) {
|
|
|
110
111
|
const ambient = limit(options.ambient, NORMAL_LIGHT_LIMITS.ambient, DEFAULT_AMBIENT) / PERCENT;
|
|
111
112
|
const maskStrength = limit(options.maskStrength, NORMAL_LIGHT_LIMITS.maskStrength, DEFAULT_MASK_STRENGTH) / PERCENT;
|
|
112
113
|
const light = options.color.map(linear);
|
|
113
|
-
const radius = Math.max(1, Math.max(source.width, source.height) * LIGHT_RADIUS_FACTOR);
|
|
114
114
|
const x = Number.isFinite(options.position.x) ? options.position.x : source.width / 2;
|
|
115
115
|
const y = Number.isFinite(options.position.y) ? options.position.y : source.height / 2;
|
|
116
116
|
const ySign = options.invertY ? -1 : 1;
|
|
@@ -129,7 +129,7 @@ export function renderPreparedNormalMapLighting(source, options) {
|
|
|
129
129
|
source.directions[vector + 2] * height) /
|
|
130
130
|
Math.sqrt(distanceSquared));
|
|
131
131
|
const reception = source.mask ? 1 - maskStrength * (1 - source.mask[i]) : 1;
|
|
132
|
-
const direct = (power * diffuse * reception) / (1 + distanceSquared /
|
|
132
|
+
const direct = (power * diffuse * reception) / (1 + distanceSquared / LIGHT_FALLOFF_DISTANCE_SQUARED);
|
|
133
133
|
const gain = delight ? delight(vector) : 1;
|
|
134
134
|
for (let channel = 0; channel < 3; channel++)
|
|
135
135
|
data[at + channel] = encoded(source.colors[vector + channel] * gain * (ambient + direct * light[channel]));
|
package/dist/normal-map.d.ts.map
CHANGED
|
@@ -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,EA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,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"}
|
package/dist/normal-map.js
CHANGED
|
@@ -47,6 +47,8 @@ const TILT_LEVEL_HIGH = 0.72;
|
|
|
47
47
|
const TILT_LEVEL_MAX = 0.9;
|
|
48
48
|
const ARCHITECTURE_TILT = 0.6;
|
|
49
49
|
const ARCHITECTURE_FLAT_LIMIT = 0.06;
|
|
50
|
+
const ARCHITECTURE_EDGE_TILT = 0.35;
|
|
51
|
+
const ARCHITECTURE_EDGE_MIN_LENGTH = 12;
|
|
50
52
|
const FACET_AREA_BASE = 180;
|
|
51
53
|
const FACET_AREA_SCALE = 1.1;
|
|
52
54
|
const FACET_REFERENCE_SIDE = 64;
|
|
@@ -408,6 +410,8 @@ export function createNormalMapGenerator(albedo, shading) {
|
|
|
408
410
|
}
|
|
409
411
|
}
|
|
410
412
|
}
|
|
413
|
+
if (preset === NormalMapPreset.Architecture && scale > 0)
|
|
414
|
+
alignStraightArchitectureEdges(dirs, fg, w, h, spriteScale, scale);
|
|
411
415
|
cachedKey = key;
|
|
412
416
|
directions = cachedDirections = dirs;
|
|
413
417
|
}
|
|
@@ -432,6 +436,62 @@ export function createNormalMapGenerator(albedo, shading) {
|
|
|
432
436
|
export function generateNormalMap(albedo, input = {}) {
|
|
433
437
|
return createNormalMapGenerator(albedo).generate(input);
|
|
434
438
|
}
|
|
439
|
+
// A straight exterior edge is a stronger geometry cue than the colors of individual bricks.
|
|
440
|
+
function alignStraightArchitectureEdges(directions, foreground, width, height, spriteScale, strength) {
|
|
441
|
+
const depth = Math.max(1, Math.round(spriteScale * 2));
|
|
442
|
+
const minLength = Math.max(ARCHITECTURE_EDGE_MIN_LENGTH, depth * 4);
|
|
443
|
+
const distance = new Uint8Array(foreground.length).fill(depth);
|
|
444
|
+
const edgeX = new Int8Array(foreground.length);
|
|
445
|
+
const edgeY = new Int8Array(foreground.length);
|
|
446
|
+
for (const horizontal of [true, false]) {
|
|
447
|
+
const [lineCount, lineLength] = horizontal ? [height, width] : [width, height];
|
|
448
|
+
const [lineStride, pixelStride] = horizontal ? [width, 1] : [1, width];
|
|
449
|
+
for (const side of [-1, 1]) {
|
|
450
|
+
for (let line = 0; line < lineCount; line++) {
|
|
451
|
+
const start = line * lineStride;
|
|
452
|
+
const outside = line + side < 0 || line + side >= lineCount;
|
|
453
|
+
let runStart = 0;
|
|
454
|
+
for (let offset = 0; offset <= lineLength; offset++) {
|
|
455
|
+
const pixel = start + offset * pixelStride;
|
|
456
|
+
if (offset < lineLength &&
|
|
457
|
+
foreground[pixel] &&
|
|
458
|
+
(outside || !foreground[pixel + side * lineStride]))
|
|
459
|
+
continue;
|
|
460
|
+
if (offset - runStart >= minLength) {
|
|
461
|
+
for (let along = runStart; along < offset; along++) {
|
|
462
|
+
for (let step = 0; step < depth; step++) {
|
|
463
|
+
const inside = line - step * side;
|
|
464
|
+
const p = inside * lineStride + along * pixelStride;
|
|
465
|
+
if (inside < 0 || inside >= lineCount || !foreground[p])
|
|
466
|
+
break;
|
|
467
|
+
if (step > distance[p])
|
|
468
|
+
continue;
|
|
469
|
+
if (step < distance[p]) {
|
|
470
|
+
distance[p] = step;
|
|
471
|
+
edgeX[p] = edgeY[p] = 0;
|
|
472
|
+
}
|
|
473
|
+
if (horizontal)
|
|
474
|
+
edgeY[p] -= side;
|
|
475
|
+
else
|
|
476
|
+
edgeX[p] += side;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
runStart = offset + 1;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
for (let p = 0; p < foreground.length; p++) {
|
|
486
|
+
const length = Math.hypot(edgeX[p], edgeY[p]);
|
|
487
|
+
if (!length)
|
|
488
|
+
continue;
|
|
489
|
+
const tilt = ARCHITECTURE_EDGE_TILT * strength * (1 - distance[p] / depth);
|
|
490
|
+
directions[p * 3] = (edgeX[p] * tilt) / length;
|
|
491
|
+
directions[p * 3 + 1] = (edgeY[p] * tilt) / length;
|
|
492
|
+
directions[p * 3 + 2] = Math.sqrt(1 - tilt * tilt);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
435
495
|
function colorEdgeDistance(a, fg, w, h) {
|
|
436
496
|
const seeds = new Uint8Array(w * h);
|
|
437
497
|
for (let y = 0; y < h; y++)
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xprite/lighter",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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",
|
|
8
7
|
"repository": {
|
|
9
8
|
"type": "git",
|
|
10
9
|
"url": "git+https://github.com/rhinoc/xprite-lighter.git"
|
|
@@ -31,19 +30,18 @@
|
|
|
31
30
|
"LICENSE",
|
|
32
31
|
"README.md"
|
|
33
32
|
],
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^26.6.6",
|
|
35
|
+
"pngjs": "^7.0.0",
|
|
36
|
+
"typescript": "^6.0.3",
|
|
37
|
+
"vitest": "^5.0.2"
|
|
38
|
+
},
|
|
34
39
|
"scripts": {
|
|
35
40
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
36
41
|
"typecheck": "tsc -p tsconfig.json",
|
|
37
42
|
"test": "vitest run",
|
|
38
|
-
"prepublishOnly": "pnpm run build",
|
|
39
43
|
"bench": "node bench/run.mjs && python3 bench/run-relit.py --out bench/run",
|
|
40
44
|
"demo": "python3 -m http.server 8734",
|
|
41
45
|
"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"
|
|
48
46
|
}
|
|
49
|
-
}
|
|
47
|
+
}
|