@kruschdev/pascal-plugin-mit-assets 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/ASSETS.md +298 -0
- package/LICENSE +21 -0
- package/README.md +71 -2
- package/dist/agent-operations.d.ts +8 -0
- package/dist/agent-operations.js +305 -0
- package/dist/agent-tools.d.ts +7 -0
- package/dist/agent-tools.js +128 -0
- package/dist/audit.d.ts +78 -0
- package/dist/bin/pascal-mit-assets.js +5238 -0
- package/dist/bom.d.ts +123 -0
- package/dist/cad/appliance-mep-builders.d.ts +7 -0
- package/dist/cad/furniture-builders.d.ts +9 -0
- package/dist/cad/index.d.ts +48 -0
- package/dist/cad/index.js +23 -0
- package/dist/cad/kitchen-bath-builders.d.ts +9 -0
- package/dist/cad/outdoor-fenestration-builders.d.ts +8 -0
- package/dist/cad/primitives.d.ts +56 -0
- package/dist/cad/props-vehicles-builders.d.ts +8 -0
- package/dist/chunk-2qgndrap.js +5200 -0
- package/dist/chunk-6hzyh6qv.js +346 -0
- package/dist/chunk-f72r2bmp.js +84 -0
- package/dist/chunk-hqgm57dv.js +310 -0
- package/dist/chunk-kgwk6kan.js +150 -0
- package/dist/chunk-raafkn3w.js +286 -0
- package/dist/chunk-vzmjt9dz.js +4380 -0
- package/dist/chunk-xhkwv6t4.js +5080 -0
- package/dist/hdri/index.d.ts +1 -0
- package/dist/hdri/index.js +322 -0
- package/dist/hdri/sky.d.ts +26 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +198 -0
- package/dist/manifest.d.ts +46 -0
- package/dist/materials/index.d.ts +10 -0
- package/dist/materials/index.js +1464 -0
- package/dist/materials/ktx2.d.ts +10 -0
- package/dist/materials/noise.d.ts +89 -0
- package/dist/materials/patterns.d.ts +15 -0
- package/dist/nodes/mit-item.d.ts +76 -0
- package/dist/render.d.ts +10 -0
- package/dist/render.js +24 -0
- package/dist/scripts/build-assets.js +17129 -0
- package/dist/sfx/index.d.ts +1 -0
- package/dist/sfx/index.js +1223 -0
- package/dist/sfx/synth.d.ts +25 -0
- package/dist/sku-crosswalk.d.ts +46 -0
- package/dist/ui/chat-card.d.ts +7 -0
- package/dist/ui/host-panel.d.ts +1 -0
- package/dist/ui/inspector-card.d.ts +4 -0
- package/dist/ui/schedule-view.d.ts +1 -0
- package/package.json +135 -4
- package/src/agent-operations.ts +373 -0
- package/src/agent-tools.ts +136 -0
- package/src/audit.ts +356 -0
- package/src/bom.ts +512 -0
- package/src/cad/appliance-mep-builders.ts +1336 -0
- package/src/cad/furniture-builders.ts +1622 -0
- package/src/cad/index.ts +993 -0
- package/src/cad/kitchen-bath-builders.ts +1189 -0
- package/src/cad/outdoor-fenestration-builders.ts +1499 -0
- package/src/cad/primitives.ts +286 -0
- package/src/cad/props-vehicles-builders.ts +1065 -0
- package/src/hdri/index.ts +5 -0
- package/src/hdri/sky.ts +481 -0
- package/src/index.ts +127 -0
- package/src/manifest.ts +5296 -0
- package/src/materials/index.ts +56 -0
- package/src/materials/ktx2.ts +138 -0
- package/src/materials/noise.ts +417 -0
- package/src/materials/patterns.ts +1507 -0
- package/src/nodes/mit-item.ts +163 -0
- package/src/render.ts +25 -0
- package/src/sfx/index.ts +6 -0
- package/src/sfx/synth.ts +1495 -0
- package/src/sku-crosswalk.ts +3868 -0
- package/src/ui/chat-card.tsx +93 -0
- package/src/ui/host-panel.tsx +330 -0
- package/src/ui/inspector-card.tsx +213 -0
- package/src/ui/schedule-view.tsx +188 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/* Clean-room MIT procedural PBR materials & Khronos KTX2 encoder public entry (browser-safe). */
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
clamp,
|
|
5
|
+
clampByte,
|
|
6
|
+
createSeededRng,
|
|
7
|
+
fnv1a32,
|
|
8
|
+
fract,
|
|
9
|
+
hash2D,
|
|
10
|
+
hash2DVec,
|
|
11
|
+
hexToRgb,
|
|
12
|
+
hsvToRgb,
|
|
13
|
+
lerp,
|
|
14
|
+
mixRgb,
|
|
15
|
+
modulateRgb,
|
|
16
|
+
mulberry32,
|
|
17
|
+
periodicDomainWarpFbm2D,
|
|
18
|
+
periodicFbm2D,
|
|
19
|
+
periodicRidgedFbm2D,
|
|
20
|
+
periodicValueNoise2D,
|
|
21
|
+
periodicVoronoi2D,
|
|
22
|
+
quintic,
|
|
23
|
+
rgbToHsv,
|
|
24
|
+
scaleRgb,
|
|
25
|
+
smoothstep,
|
|
26
|
+
wrapInt,
|
|
27
|
+
type HsvTuple,
|
|
28
|
+
type RgbModulation,
|
|
29
|
+
type RgbTuple,
|
|
30
|
+
type VoronoiSample,
|
|
31
|
+
} from './noise'
|
|
32
|
+
|
|
33
|
+
export { synthesizeMaterialPack, type SynthesizedMaterialMaps } from './patterns'
|
|
34
|
+
|
|
35
|
+
export { encodeKtx2Rgba8, KTX2_WRITER_ID, type ZstdCompressor } from './ktx2'
|
|
36
|
+
|
|
37
|
+
export type MaterialChannel = 'albedo' | 'normal' | 'roughness' | 'ao' | 'metallic' | 'thumb'
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Infers the PBR texture channel (or thumbnail role) from a material asset path or filename.
|
|
41
|
+
* Matches Pascal's `.ktx2` and `.webp` naming conventions (`basecolor`, `diffuse`, `normal`,
|
|
42
|
+
* `roughness`, `specular`, `specularLevel`, `ao`, `ambientOcclusion`, `metallic`, `thumb`).
|
|
43
|
+
*/
|
|
44
|
+
export function inferChannelFromFilename(filename: string): MaterialChannel {
|
|
45
|
+
const clean = filename.split(/[?#]/, 1)[0] ?? filename
|
|
46
|
+
const base = (clean.split(/[\\/]/).pop() ?? clean).toLowerCase()
|
|
47
|
+
const stem = base.replace(/\.(ktx2|webp|png|jpe?g)$/i, '')
|
|
48
|
+
|
|
49
|
+
if (/(?:^|[._-])thumb(?:[._-]|$)/.test(stem)) return 'thumb'
|
|
50
|
+
if (/(?:^|[._-])normal(?:gl|dx)?(?:[._-]|$)/.test(stem)) return 'normal'
|
|
51
|
+
if (/(?:^|[._-])(roughness|specularlevel|specular)(?:[._-]|$)/.test(stem)) return 'roughness'
|
|
52
|
+
if (/(?:^|[._-])(ambientocclusion|ao)(?:[._-]|$)/.test(stem)) return 'ao'
|
|
53
|
+
if (/(?:^|[._-])(metallic|metalness)(?:[._-]|$)/.test(stem)) return 'metallic'
|
|
54
|
+
if (/(?:^|[._-])(basecolor|diffuse|albedo|color)(?:[._-]|$)/.test(stem)) return 'albedo'
|
|
55
|
+
return 'albedo'
|
|
56
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/* Clean-room MIT Khronos KTX2 RGBA8 encoder compatible with Three.js KTX2Loader (WebGL & WebGPU). */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
KHR_DF_CHANNEL_RGBSDA_ALPHA,
|
|
5
|
+
KHR_DF_CHANNEL_RGBSDA_BLUE,
|
|
6
|
+
KHR_DF_CHANNEL_RGBSDA_GREEN,
|
|
7
|
+
KHR_DF_CHANNEL_RGBSDA_RED,
|
|
8
|
+
KHR_DF_FLAG_ALPHA_STRAIGHT,
|
|
9
|
+
KHR_DF_KHR_DESCRIPTORTYPE_BASICFORMAT,
|
|
10
|
+
KHR_DF_MODEL_RGBSDA,
|
|
11
|
+
KHR_DF_PRIMARIES_BT709,
|
|
12
|
+
KHR_DF_SAMPLE_DATATYPE_LINEAR,
|
|
13
|
+
KHR_DF_TRANSFER_LINEAR,
|
|
14
|
+
KHR_DF_TRANSFER_SRGB,
|
|
15
|
+
KHR_DF_VENDORID_KHRONOS,
|
|
16
|
+
KHR_DF_VERSION,
|
|
17
|
+
KHR_SUPERCOMPRESSION_NONE,
|
|
18
|
+
KHR_SUPERCOMPRESSION_ZSTD,
|
|
19
|
+
KTX2Container,
|
|
20
|
+
VK_FORMAT_R8G8B8A8_SRGB,
|
|
21
|
+
VK_FORMAT_R8G8B8A8_UNORM,
|
|
22
|
+
write,
|
|
23
|
+
} from 'ktx-parse'
|
|
24
|
+
|
|
25
|
+
export const KTX2_WRITER_ID = '@kruschdev/pascal-plugin-mit-assets (MIT)'
|
|
26
|
+
|
|
27
|
+
export type ZstdCompressor = (bytes: Uint8Array) => Uint8Array
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Encodes an uncompressed RGBA8 pixel buffer into a spec-compliant Khronos KTX2 container
|
|
31
|
+
* (`VK_FORMAT_R8G8B8A8_SRGB` or `VK_FORMAT_R8G8B8A8_UNORM`) compatible with Three.js
|
|
32
|
+
* `KTX2Loader.createRawTexture()` in both WebGL and WebGPU.
|
|
33
|
+
*
|
|
34
|
+
* Accepts an optional browser-agnostic `compressZstd` callback so this module has zero `node:*` imports.
|
|
35
|
+
*/
|
|
36
|
+
export function encodeKtx2Rgba8(
|
|
37
|
+
rgba: Uint8Array | Uint8ClampedArray,
|
|
38
|
+
width: number,
|
|
39
|
+
height: number,
|
|
40
|
+
srgb: boolean,
|
|
41
|
+
compressZstd?: ZstdCompressor,
|
|
42
|
+
): Uint8Array {
|
|
43
|
+
if (!Number.isInteger(width) || width <= 0 || !Number.isInteger(height) || height <= 0) {
|
|
44
|
+
throw new RangeError(`Invalid KTX2 dimensions: ${width}x${height}`)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const expectedByteLength = width * height * 4
|
|
48
|
+
if (rgba.byteLength !== expectedByteLength) {
|
|
49
|
+
throw new RangeError(
|
|
50
|
+
`Invalid RGBA8 buffer byteLength: expected ${expectedByteLength} for ${width}x${height}, got ${rgba.byteLength}`,
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const rawBytes = new Uint8Array(rgba.buffer, rgba.byteOffset, rgba.byteLength)
|
|
55
|
+
const useZstd = typeof compressZstd === 'function'
|
|
56
|
+
const levelData = useZstd ? compressZstd(rawBytes) : rawBytes
|
|
57
|
+
|
|
58
|
+
const container = new KTX2Container()
|
|
59
|
+
container.vkFormat = srgb ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM
|
|
60
|
+
container.typeSize = 1
|
|
61
|
+
container.pixelWidth = width
|
|
62
|
+
container.pixelHeight = height
|
|
63
|
+
container.pixelDepth = 0
|
|
64
|
+
container.layerCount = 0
|
|
65
|
+
container.faceCount = 1
|
|
66
|
+
container.supercompressionScheme = useZstd ? KHR_SUPERCOMPRESSION_ZSTD : KHR_SUPERCOMPRESSION_NONE
|
|
67
|
+
container.globalData = null
|
|
68
|
+
|
|
69
|
+
// Khronos KTX2 Data Format Descriptor (DFD) v2 Basic Format for RGBA8 (4 bytes/texel, 1x1x1x1 block).
|
|
70
|
+
// `bitLength` is encoded as (bits - 1) = 7 per the Khronos DFD specification.
|
|
71
|
+
// For sRGB formats, RGB channels use the sRGB transfer function while Alpha is linear.
|
|
72
|
+
const alphaChannelType = srgb
|
|
73
|
+
? KHR_DF_CHANNEL_RGBSDA_ALPHA | KHR_DF_SAMPLE_DATATYPE_LINEAR
|
|
74
|
+
: KHR_DF_CHANNEL_RGBSDA_ALPHA
|
|
75
|
+
|
|
76
|
+
container.dataFormatDescriptor = [
|
|
77
|
+
{
|
|
78
|
+
vendorId: KHR_DF_VENDORID_KHRONOS,
|
|
79
|
+
descriptorType: KHR_DF_KHR_DESCRIPTORTYPE_BASICFORMAT,
|
|
80
|
+
versionNumber: KHR_DF_VERSION,
|
|
81
|
+
descriptorBlockSize: 24 + 4 * 16,
|
|
82
|
+
colorModel: KHR_DF_MODEL_RGBSDA,
|
|
83
|
+
colorPrimaries: KHR_DF_PRIMARIES_BT709,
|
|
84
|
+
transferFunction: srgb ? KHR_DF_TRANSFER_SRGB : KHR_DF_TRANSFER_LINEAR,
|
|
85
|
+
flags: KHR_DF_FLAG_ALPHA_STRAIGHT,
|
|
86
|
+
texelBlockDimension: [0, 0, 0, 0],
|
|
87
|
+
bytesPlane: [4, 0, 0, 0, 0, 0, 0, 0],
|
|
88
|
+
samples: [
|
|
89
|
+
{
|
|
90
|
+
bitOffset: 0,
|
|
91
|
+
bitLength: 7,
|
|
92
|
+
channelType: KHR_DF_CHANNEL_RGBSDA_RED,
|
|
93
|
+
samplePosition: [0, 0, 0, 0],
|
|
94
|
+
sampleLower: 0,
|
|
95
|
+
sampleUpper: 255,
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
bitOffset: 8,
|
|
99
|
+
bitLength: 7,
|
|
100
|
+
channelType: KHR_DF_CHANNEL_RGBSDA_GREEN,
|
|
101
|
+
samplePosition: [0, 0, 0, 0],
|
|
102
|
+
sampleLower: 0,
|
|
103
|
+
sampleUpper: 255,
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
bitOffset: 16,
|
|
107
|
+
bitLength: 7,
|
|
108
|
+
channelType: KHR_DF_CHANNEL_RGBSDA_BLUE,
|
|
109
|
+
samplePosition: [0, 0, 0, 0],
|
|
110
|
+
sampleLower: 0,
|
|
111
|
+
sampleUpper: 255,
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
bitOffset: 24,
|
|
115
|
+
bitLength: 7,
|
|
116
|
+
channelType: alphaChannelType,
|
|
117
|
+
samplePosition: [0, 0, 0, 0],
|
|
118
|
+
sampleLower: 0,
|
|
119
|
+
sampleUpper: 255,
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
container.keyValue = {
|
|
126
|
+
KTXwriter: KTX2_WRITER_ID,
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// `levels.length === 1` writes header `levelCount = 1`.
|
|
130
|
+
container.levels = [
|
|
131
|
+
{
|
|
132
|
+
levelData,
|
|
133
|
+
uncompressedByteLength: rawBytes.byteLength,
|
|
134
|
+
},
|
|
135
|
+
]
|
|
136
|
+
|
|
137
|
+
return write(container, { keepWriter: true })
|
|
138
|
+
}
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/* Clean-room MIT deterministic PRNG, seamless periodic 2D noise, Voronoi, and color utilities. */
|
|
2
|
+
|
|
3
|
+
export type RgbTuple = [number, number, number]
|
|
4
|
+
export type HsvTuple = [number, number, number]
|
|
5
|
+
|
|
6
|
+
export type VoronoiSample = {
|
|
7
|
+
/** Distance to closest feature point (in cell units). */
|
|
8
|
+
f1: number
|
|
9
|
+
/** Distance to second-closest feature point (in cell units). */
|
|
10
|
+
f2: number
|
|
11
|
+
/** Approximate distance to nearest Voronoi cell border (`f2 - f1`). */
|
|
12
|
+
edgeDist: number
|
|
13
|
+
/** Deterministic `[0, 1)` identifier for the winning cell. */
|
|
14
|
+
cellId: number
|
|
15
|
+
/** Wrapped integer X index of the winning cell. */
|
|
16
|
+
cellX: number
|
|
17
|
+
/** Wrapped integer Y index of the winning cell. */
|
|
18
|
+
cellY: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type RgbModulation = {
|
|
22
|
+
/** Hue shift in degrees (wraps modulo 360). */
|
|
23
|
+
hueShift?: number
|
|
24
|
+
/** Multiplicative saturation scale (default 1). */
|
|
25
|
+
satScale?: number
|
|
26
|
+
/** Additive saturation offset (default 0). */
|
|
27
|
+
satOffset?: number
|
|
28
|
+
/** Multiplicative value/brightness scale (default 1). */
|
|
29
|
+
valScale?: number
|
|
30
|
+
/** Additive value offset in `[-1, 1]` (default 0). */
|
|
31
|
+
valOffset?: number
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 32-bit unsigned FNV-1a string hash. */
|
|
35
|
+
export function fnv1a32(input: string): number {
|
|
36
|
+
let hash = 0x811c9dc5
|
|
37
|
+
for (let i = 0; i < input.length; i++) {
|
|
38
|
+
hash ^= input.charCodeAt(i)
|
|
39
|
+
hash = Math.imul(hash, 0x01000193)
|
|
40
|
+
}
|
|
41
|
+
return hash >>> 0
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Deterministic Mulberry32 PRNG returning floats in `[0, 1)`. */
|
|
45
|
+
export function mulberry32(seed: number): () => number {
|
|
46
|
+
let state = seed >>> 0
|
|
47
|
+
return () => {
|
|
48
|
+
state = (state + 0x6d2b79f5) >>> 0
|
|
49
|
+
let t = Math.imul(state ^ (state >>> 15), 1 | state)
|
|
50
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
|
|
51
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Create a deterministic PRNG from either a string or numeric seed. */
|
|
56
|
+
export function createSeededRng(seed: string | number): () => number {
|
|
57
|
+
const numeric = typeof seed === 'string' ? fnv1a32(seed) : seed >>> 0
|
|
58
|
+
return mulberry32(numeric)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Positive integer modulo over `[0, period)`. */
|
|
62
|
+
export function wrapInt(value: number, period: number): number {
|
|
63
|
+
const p = period > 0 ? period : 1
|
|
64
|
+
const r = value % p
|
|
65
|
+
return r < 0 ? r + p : r
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Fractional part in `[0, 1)`. */
|
|
69
|
+
export function fract(x: number): number {
|
|
70
|
+
return x - Math.floor(x)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Clamp a numeric value to `[min, max]`. */
|
|
74
|
+
export function clamp(value: number, min: number, max: number): number {
|
|
75
|
+
if (value < min) return min
|
|
76
|
+
if (value > max) return max
|
|
77
|
+
return value
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Clamp and round a numeric value to an integer byte `[0, 255]`. */
|
|
81
|
+
export function clampByte(value: number): number {
|
|
82
|
+
if (!(value > 0)) return 0
|
|
83
|
+
if (value >= 255) return 255
|
|
84
|
+
return Math.round(value)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Linear interpolation between `a` and `b`. */
|
|
88
|
+
export function lerp(a: number, b: number, t: number): number {
|
|
89
|
+
return a + (b - a) * t
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Hermite smoothstep interpolation. */
|
|
93
|
+
export function smoothstep(edge0: number, edge1: number, x: number): number {
|
|
94
|
+
if (edge0 === edge1) return x < edge0 ? 0 : 1
|
|
95
|
+
const t = clamp((x - edge0) / (edge1 - edge0), 0, 1)
|
|
96
|
+
return t * t * (3 - 2 * t)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** C2-continuous quintic fade curve for smooth value noise. */
|
|
100
|
+
export function quintic(t: number): number {
|
|
101
|
+
return t * t * t * (t * (t * 6 - 15) + 10)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Fast deterministic 2D integer hash returning `[0, 1)`. */
|
|
105
|
+
export function hash2D(ix: number, iy: number, seed: number): number {
|
|
106
|
+
let h = (Math.imul(ix, 374761393) + Math.imul(iy, 668265263) + Math.imul(seed, 1442695041)) | 0
|
|
107
|
+
h = Math.imul(h ^ (h >>> 13), 1274126177)
|
|
108
|
+
h = h ^ (h >>> 16)
|
|
109
|
+
return (h >>> 0) / 4294967296
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Fast deterministic 2D integer hash returning an uncorrelated `[0, 1)` pair. */
|
|
113
|
+
export function hash2DVec(ix: number, iy: number, seed: number): [number, number] {
|
|
114
|
+
return [hash2D(ix, iy, seed), hash2D(ix + 1013, iy - 1619, (seed ^ 0x9e3779b9) >>> 0)]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Seamless periodic tileable 2D value noise over normalized UV `(u, v) in [0, 1)^2`.
|
|
119
|
+
* Lattice indices wrap modulo integer `(periodX, periodY)` so `u=0` matches `u=1`
|
|
120
|
+
* and `v=0` matches `v=1` with C2 continuity and zero seams.
|
|
121
|
+
*/
|
|
122
|
+
export function periodicValueNoise2D(
|
|
123
|
+
u: number,
|
|
124
|
+
v: number,
|
|
125
|
+
periodX: number,
|
|
126
|
+
periodY: number,
|
|
127
|
+
seed: number,
|
|
128
|
+
): number {
|
|
129
|
+
const px = Math.max(1, Math.round(periodX))
|
|
130
|
+
const py = Math.max(1, Math.round(periodY))
|
|
131
|
+
|
|
132
|
+
const sx = fract(u) * px
|
|
133
|
+
const sy = fract(v) * py
|
|
134
|
+
|
|
135
|
+
const ix0 = Math.floor(sx)
|
|
136
|
+
const iy0 = Math.floor(sy)
|
|
137
|
+
const fx = quintic(sx - ix0)
|
|
138
|
+
const fy = quintic(sy - iy0)
|
|
139
|
+
|
|
140
|
+
const x0 = wrapInt(ix0, px)
|
|
141
|
+
const x1 = (x0 + 1) % px
|
|
142
|
+
const y0 = wrapInt(iy0, py)
|
|
143
|
+
const y1 = (y0 + 1) % py
|
|
144
|
+
|
|
145
|
+
const v00 = hash2D(x0, y0, seed)
|
|
146
|
+
const v10 = hash2D(x1, y0, seed)
|
|
147
|
+
const v01 = hash2D(x0, y1, seed)
|
|
148
|
+
const v11 = hash2D(x1, y1, seed)
|
|
149
|
+
|
|
150
|
+
const nx0 = v00 + (v10 - v00) * fx
|
|
151
|
+
const nx1 = v01 + (v11 - v01) * fx
|
|
152
|
+
return nx0 + (nx1 - nx0) * fy
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Seamless periodic tileable 2D fractal Brownian motion (fBM) over `[0, 1)^2`.
|
|
157
|
+
* Every octave scales period by an integer lacunarity so periodicity is strictly preserved.
|
|
158
|
+
*/
|
|
159
|
+
export function periodicFbm2D(
|
|
160
|
+
u: number,
|
|
161
|
+
v: number,
|
|
162
|
+
basePeriodX: number,
|
|
163
|
+
basePeriodY: number,
|
|
164
|
+
octaves: number,
|
|
165
|
+
seed: number,
|
|
166
|
+
persistence = 0.5,
|
|
167
|
+
lacunarity = 2,
|
|
168
|
+
): number {
|
|
169
|
+
let px = Math.max(1, Math.round(basePeriodX))
|
|
170
|
+
let py = Math.max(1, Math.round(basePeriodY))
|
|
171
|
+
const lac = Math.max(1, Math.round(lacunarity))
|
|
172
|
+
const count = Math.max(1, Math.round(octaves))
|
|
173
|
+
|
|
174
|
+
let sum = 0
|
|
175
|
+
let amp = 1
|
|
176
|
+
let norm = 0
|
|
177
|
+
let s = seed >>> 0
|
|
178
|
+
|
|
179
|
+
for (let i = 0; i < count; i++) {
|
|
180
|
+
sum += periodicValueNoise2D(u, v, px, py, s) * amp
|
|
181
|
+
norm += amp
|
|
182
|
+
amp *= persistence
|
|
183
|
+
px *= lac
|
|
184
|
+
py *= lac
|
|
185
|
+
s = (Math.imul(s ^ 0x85ebca6b, 0xc2b2ae35) + 0x27d4eb2f) >>> 0
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return norm > 0 ? sum / norm : 0.5
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Seamless periodic tileable 2D ridged multifractal noise over `[0, 1)^2`.
|
|
193
|
+
* Useful for rock strata, marble veins, soil clumps, and hammered creases.
|
|
194
|
+
*/
|
|
195
|
+
export function periodicRidgedFbm2D(
|
|
196
|
+
u: number,
|
|
197
|
+
v: number,
|
|
198
|
+
basePeriodX: number,
|
|
199
|
+
basePeriodY: number,
|
|
200
|
+
octaves: number,
|
|
201
|
+
seed: number,
|
|
202
|
+
persistence = 0.5,
|
|
203
|
+
lacunarity = 2,
|
|
204
|
+
): number {
|
|
205
|
+
let px = Math.max(1, Math.round(basePeriodX))
|
|
206
|
+
let py = Math.max(1, Math.round(basePeriodY))
|
|
207
|
+
const lac = Math.max(1, Math.round(lacunarity))
|
|
208
|
+
const count = Math.max(1, Math.round(octaves))
|
|
209
|
+
|
|
210
|
+
let sum = 0
|
|
211
|
+
let amp = 1
|
|
212
|
+
let norm = 0
|
|
213
|
+
let s = seed >>> 0
|
|
214
|
+
|
|
215
|
+
for (let i = 0; i < count; i++) {
|
|
216
|
+
const n = periodicValueNoise2D(u, v, px, py, s)
|
|
217
|
+
const ridge = 1 - Math.abs(2 * n - 1)
|
|
218
|
+
sum += ridge * ridge * amp
|
|
219
|
+
norm += amp
|
|
220
|
+
amp *= persistence
|
|
221
|
+
px *= lac
|
|
222
|
+
py *= lac
|
|
223
|
+
s = (Math.imul(s ^ 0x7feb352d, 0x846ca68b) + 0x1b873593) >>> 0
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return norm > 0 ? sum / norm : 0.5
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Seamless domain-warped periodic fBM over `[0, 1)^2`.
|
|
231
|
+
*/
|
|
232
|
+
export function periodicDomainWarpFbm2D(
|
|
233
|
+
u: number,
|
|
234
|
+
v: number,
|
|
235
|
+
periodX: number,
|
|
236
|
+
periodY: number,
|
|
237
|
+
octaves: number,
|
|
238
|
+
warpAmp: number,
|
|
239
|
+
seed: number,
|
|
240
|
+
): number {
|
|
241
|
+
const wu = periodicFbm2D(u, v, periodX, periodY, Math.max(2, octaves - 1), (seed + 101) >>> 0)
|
|
242
|
+
const wv = periodicFbm2D(u, v, periodX, periodY, Math.max(2, octaves - 1), (seed + 313) >>> 0)
|
|
243
|
+
const du = (wu - 0.5) * warpAmp
|
|
244
|
+
const dv = (wv - 0.5) * warpAmp
|
|
245
|
+
return periodicFbm2D(u + du, v + dv, periodX, periodY, octaves, (seed + 719) >>> 0)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Seamless periodic tileable 2D Voronoi cellular distance over `[0, 1)^2`.
|
|
250
|
+
* Evaluates a 3x3 toroidally wrapped neighborhood around `(u * cellsX, v * cellsY)`.
|
|
251
|
+
*/
|
|
252
|
+
export function periodicVoronoi2D(
|
|
253
|
+
u: number,
|
|
254
|
+
v: number,
|
|
255
|
+
cellsX: number,
|
|
256
|
+
cellsY: number,
|
|
257
|
+
seed: number,
|
|
258
|
+
jitter = 0.85,
|
|
259
|
+
): VoronoiSample {
|
|
260
|
+
const cx = Math.max(1, Math.round(cellsX))
|
|
261
|
+
const cy = Math.max(1, Math.round(cellsY))
|
|
262
|
+
const j = clamp(jitter, 0, 1)
|
|
263
|
+
|
|
264
|
+
const px = fract(u) * cx
|
|
265
|
+
const py = fract(v) * cy
|
|
266
|
+
const ix = Math.floor(px)
|
|
267
|
+
const iy = Math.floor(py)
|
|
268
|
+
const fx = px - ix
|
|
269
|
+
const fy = py - iy
|
|
270
|
+
|
|
271
|
+
let f1Sq = 1e9
|
|
272
|
+
let f2Sq = 1e9
|
|
273
|
+
let bestX = 0
|
|
274
|
+
let bestY = 0
|
|
275
|
+
|
|
276
|
+
const seedY = (seed ^ 0x9e3779b9) >>> 0
|
|
277
|
+
|
|
278
|
+
for (let oy = -1; oy <= 1; oy++) {
|
|
279
|
+
const ny = wrapInt(iy + oy, cy)
|
|
280
|
+
for (let ox = -1; ox <= 1; ox++) {
|
|
281
|
+
const nx = wrapInt(ix + ox, cx)
|
|
282
|
+
const rx = 0.5 + (hash2D(nx, ny, seed) - 0.5) * j
|
|
283
|
+
const ry = 0.5 + (hash2D(nx + 1013, ny - 1619, seedY) - 0.5) * j
|
|
284
|
+
const dx = ox + rx - fx
|
|
285
|
+
const dy = oy + ry - fy
|
|
286
|
+
const dSq = dx * dx + dy * dy
|
|
287
|
+
if (dSq < f1Sq) {
|
|
288
|
+
f2Sq = f1Sq
|
|
289
|
+
f1Sq = dSq
|
|
290
|
+
bestX = nx
|
|
291
|
+
bestY = ny
|
|
292
|
+
} else if (dSq < f2Sq) {
|
|
293
|
+
f2Sq = dSq
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const f1 = Math.sqrt(f1Sq)
|
|
299
|
+
const f2 = Math.sqrt(f2Sq)
|
|
300
|
+
const cellId = hash2D(bestX, bestY, (seed ^ 0x5bf03635) >>> 0)
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
f1,
|
|
304
|
+
f2,
|
|
305
|
+
edgeDist: Math.max(0, f2 - f1),
|
|
306
|
+
cellId,
|
|
307
|
+
cellX: bestX,
|
|
308
|
+
cellY: bestY,
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Parse a CSS hex string (`#RGB` or `#RRGGBB`, with or without `#`) to `[r, g, b]` in `0..255`. */
|
|
313
|
+
export function hexToRgb(hex: string): RgbTuple {
|
|
314
|
+
const clean = hex.trim().replace(/^#/, '')
|
|
315
|
+
if (/^[0-9a-fA-F]{3,4}$/.test(clean)) {
|
|
316
|
+
return [
|
|
317
|
+
Number.parseInt(clean[0]! + clean[0]!, 16),
|
|
318
|
+
Number.parseInt(clean[1]! + clean[1]!, 16),
|
|
319
|
+
Number.parseInt(clean[2]! + clean[2]!, 16),
|
|
320
|
+
]
|
|
321
|
+
}
|
|
322
|
+
if (/^[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$/.test(clean)) {
|
|
323
|
+
return [
|
|
324
|
+
Number.parseInt(clean.slice(0, 2), 16),
|
|
325
|
+
Number.parseInt(clean.slice(2, 4), 16),
|
|
326
|
+
Number.parseInt(clean.slice(4, 6), 16),
|
|
327
|
+
]
|
|
328
|
+
}
|
|
329
|
+
return [128, 128, 128]
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Convert `[r, g, b]` (`0..255`) to `[h, s, v]` where `h in [0, 360)`, `s, v in [0, 1]`. */
|
|
333
|
+
export function rgbToHsv(r: number, g: number, b: number): HsvTuple {
|
|
334
|
+
const rn = clamp(r / 255, 0, 1)
|
|
335
|
+
const gn = clamp(g / 255, 0, 1)
|
|
336
|
+
const bn = clamp(b / 255, 0, 1)
|
|
337
|
+
|
|
338
|
+
const max = Math.max(rn, gn, bn)
|
|
339
|
+
const min = Math.min(rn, gn, bn)
|
|
340
|
+
const d = max - min
|
|
341
|
+
|
|
342
|
+
let h = 0
|
|
343
|
+
if (d > 1e-6) {
|
|
344
|
+
if (max === rn) {
|
|
345
|
+
h = 60 * (((gn - bn) / d) % 6)
|
|
346
|
+
} else if (max === gn) {
|
|
347
|
+
h = 60 * ((bn - rn) / d + 2)
|
|
348
|
+
} else {
|
|
349
|
+
h = 60 * ((rn - gn) / d + 4)
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (h < 0) h += 360
|
|
353
|
+
|
|
354
|
+
const s = max > 1e-6 ? d / max : 0
|
|
355
|
+
return [h, s, max]
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Convert `[h, s, v]` (`h in [0, 360)`, `s, v in [0, 1]`) to `[r, g, b]` (`0..255`). */
|
|
359
|
+
export function hsvToRgb(h: number, s: number, v: number): RgbTuple {
|
|
360
|
+
const hh = ((h % 360) + 360) % 360
|
|
361
|
+
const ss = clamp(s, 0, 1)
|
|
362
|
+
const vv = clamp(v, 0, 1)
|
|
363
|
+
|
|
364
|
+
const c = vv * ss
|
|
365
|
+
const x = c * (1 - Math.abs(((hh / 60) % 2) - 1))
|
|
366
|
+
const m = vv - c
|
|
367
|
+
|
|
368
|
+
let r1 = 0
|
|
369
|
+
let g1 = 0
|
|
370
|
+
let b1 = 0
|
|
371
|
+
|
|
372
|
+
if (hh < 60) {
|
|
373
|
+
r1 = c
|
|
374
|
+
g1 = x
|
|
375
|
+
} else if (hh < 120) {
|
|
376
|
+
r1 = x
|
|
377
|
+
g1 = c
|
|
378
|
+
} else if (hh < 180) {
|
|
379
|
+
g1 = c
|
|
380
|
+
b1 = x
|
|
381
|
+
} else if (hh < 240) {
|
|
382
|
+
g1 = x
|
|
383
|
+
b1 = c
|
|
384
|
+
} else if (hh < 300) {
|
|
385
|
+
r1 = x
|
|
386
|
+
b1 = c
|
|
387
|
+
} else {
|
|
388
|
+
r1 = c
|
|
389
|
+
b1 = x
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return [clampByte((r1 + m) * 255), clampByte((g1 + m) * 255), clampByte((b1 + m) * 255)]
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Linearly blend two RGB triples. */
|
|
396
|
+
export function mixRgb(a: readonly [number, number, number], b: readonly [number, number, number], t: number): RgbTuple {
|
|
397
|
+
const k = clamp(t, 0, 1)
|
|
398
|
+
return [
|
|
399
|
+
clampByte(a[0] + (b[0] - a[0]) * k),
|
|
400
|
+
clampByte(a[1] + (b[1] - a[1]) * k),
|
|
401
|
+
clampByte(a[2] + (b[2] - a[2]) * k),
|
|
402
|
+
]
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** Scale an RGB color's luminance by `factor` and clamp to `[0, 255]`. */
|
|
406
|
+
export function scaleRgb(rgb: readonly [number, number, number], factor: number): RgbTuple {
|
|
407
|
+
return [clampByte(rgb[0] * factor), clampByte(rgb[1] * factor), clampByte(rgb[2] * factor)]
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Modulate an RGB color in HSV space for authentic natural pigment/tone variation. */
|
|
411
|
+
export function modulateRgb(rgb: readonly [number, number, number], mod: RgbModulation): RgbTuple {
|
|
412
|
+
const [h, s, v] = rgbToHsv(rgb[0], rgb[1], rgb[2])
|
|
413
|
+
const nextH = h + (mod.hueShift ?? 0)
|
|
414
|
+
const nextS = clamp(s * (mod.satScale ?? 1) + (mod.satOffset ?? 0), 0, 1)
|
|
415
|
+
const nextV = clamp(v * (mod.valScale ?? 1) + (mod.valOffset ?? 0), 0, 1)
|
|
416
|
+
return hsvToRgb(nextH, nextS, nextV)
|
|
417
|
+
}
|