Line data Source code
1 : import { clamp } from 'es-toolkit'
2 :
3 : import { LIGHT_TYPES, createDefaultLight } from './types'
4 : import type { LightInfoEntry, LightInfoType } from './types'
5 : import type { Vector3 as Vec3 } from '@comfyorg/shared-frontend-utils/geometry'
6 :
7 2356 : const MIN_OUTER_CONE_ANGLE = 1
8 2356 : const MAX_CONE_ANGLE = 90
9 :
10 0 : function isLightType(value: unknown): value is LightInfoType {
11 0 : return (
12 : typeof value === 'string' &&
13 : (LIGHT_TYPES as readonly string[]).includes(value)
14 : )
15 : }
16 :
17 0 : function toVec3(value: unknown, fallback: Vec3): Vec3 {
18 0 : const v = value as Partial<Vec3> | null | undefined
19 0 : const num = (n: unknown, d: number) =>
20 : typeof n === 'number' && Number.isFinite(n) ? n : d
21 0 : return {
22 : x: num(v?.x, fallback.x),
23 : y: num(v?.y, fallback.y),
24 : z: num(v?.z, fallback.z)
25 : }
26 : }
27 :
28 0 : function toFinite(value: unknown, fallback: number): number {
29 0 : return typeof value === 'number' && Number.isFinite(value) ? value : fallback
30 : }
31 :
32 2356 : const HEX_COLOR = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
33 :
34 0 : function toColor(value: unknown, fallback: string): string {
35 0 : return typeof value === 'string' && HEX_COLOR.test(value) ? value : fallback
36 : }
37 :
38 0 : function coneAngles(
39 : raw: Record<string, unknown>,
40 : defaults: LightInfoEntry
41 : ): Pick<LightInfoEntry, 'innerConeAngle' | 'outerConeAngle'> {
42 0 : const outer = clamp(
43 : toFinite(raw.outerConeAngle, defaults.outerConeAngle ?? 45),
44 : MIN_OUTER_CONE_ANGLE,
45 : MAX_CONE_ANGLE
46 : )
47 : const inner = clamp(
48 : toFinite(raw.innerConeAngle, defaults.innerConeAngle ?? 30),
49 : 0,
50 : outer
51 : )
52 0 : return { innerConeAngle: inner, outerConeAngle: outer }
53 : }
54 :
55 0 : function typeSpecificFields(
56 : raw: Record<string, unknown>,
57 : defaults: LightInfoEntry
58 : ): Partial<LightInfoEntry> {
59 0 : const range = toFinite(raw.range, 0)
60 0 : return {
61 : ...(defaults.type !== 'point' && {
62 : target: toVec3(raw.target, defaults.target ?? { x: 0, y: 0, z: 0 })
63 : }),
64 : ...(defaults.type !== 'directional' && range > 0 && { range }),
65 : ...(defaults.type === 'spot' && coneAngles(raw, defaults))
66 : }
67 : }
68 :
69 0 : function normalizeLight(value: unknown): LightInfoEntry | null {
70 0 : if (typeof value !== 'object' || value === null) return null
71 0 : const raw = value as Record<string, unknown>
72 0 : if (!isLightType(raw.type)) return null
73 0 : const defaults = createDefaultLight(raw.type)
74 0 : return {
75 : type: raw.type,
76 : color: toColor(raw.color, defaults.color),
77 : intensity: Math.max(0, toFinite(raw.intensity, defaults.intensity)),
78 : position: toVec3(raw.position, defaults.position),
79 : ...typeSpecificFields(raw, defaults),
80 : radius: Math.max(0, toFinite(raw.radius, defaults.radius ?? 0)),
81 : ...(raw.castShadow === false && { castShadow: false })
82 : }
83 : }
84 :
85 0 : export function normalizeLightsValue(value: unknown): LightInfoEntry[] {
86 0 : if (!Array.isArray(value)) return []
87 0 : return value
88 : .map(normalizeLight)
89 : .filter((light): light is LightInfoEntry => light !== null)
90 : }
|