fuzzyworld

nordic-skating / local-littlejs / files / src / adapters / three-to-littlejs.ts

Branch local-littlejs · default branch · all branches

three-to-littlejs.ts

Blame: the commit that last changed each line · file · history

39d08b63 2026-10-20 Pavel Safronov1/**
2 * Adapter utilities for converting Three.js concepts to LittleJS
3 */
4
5import * as THREE from 'three'
6
7export class ThreeToLittleJSAdapter {
8 /**
9 * Convert a Three.js Color to a number suitable for LittleJS
10 */
11 static convertColor(threeColor: THREE.Color): number {
12 const r = Math.round(threeColor.r * 255)
13 const g = Math.round(threeColor.g * 255)
14 const b = Math.round(threeColor.b * 255)
15 return (r << 16) | (g << 8) | b
16 }
17
18 /**
19 * Convert a Three.js Vector3 to a LittleJS position array
20 */
21 static convertVector3(vec: THREE.Vector3): [number, number, number] {
22 return [vec.x, vec.y, vec.z]
23 }
24
25 /**
26 * Convert a Three.js Vector4 to a LittleJS array
27 */
28 static convertVector4(vec: THREE.Vector4): [number, number, number, number] {
29 return [vec.x, vec.y, vec.z, vec.w]
30 }
31
32 /**
33 * Create a basic LittleJS material equivalent from Three.js material properties
34 */
35 static createBasicMaterial(options: {
36 color?: THREE.Color,
37 transparent?: boolean,
38 opacity?: number,
39 emissive?: THREE.Color,
40 emissiveIntensity?: number
41 }): any {
42 const result: any = {}
43
44 if (options.color) {
45 result.color = this.convertColor(options.color)
46 }
47
48 if (options.transparent !== undefined) {
49 result.transparent = options.transparent
50 }
51
52 if (options.opacity !== undefined) {
53 result.opacity = options.opacity
54 }
55
56 if (options.emissive) {
57 result.emissive = this.convertColor(options.emissive)
58 result.emissiveIntensity = options.emissiveIntensity || 1.0
59 }
60
61 return result
62 }
63
64 /**
65 * Adapt a Three.js shader uniform object to LittleJS format
66 */
67 static adaptUniforms(threeUniforms: Record<string, any>): Record<string, any> {
68 const adapted: Record<string, any> = {}
69
70 for (const [name, uniform] of Object.entries(threeUniforms)) {
71 if (uniform.value instanceof THREE.Color) {
72 adapted[name] = this.convertColor(uniform.value)
73 } else if (uniform.value instanceof THREE.Vector3) {
74 adapted[name] = this.convertVector3(uniform.value)
75 } else if (uniform.value instanceof THREE.Vector4) {
76 adapted[name] = this.convertVector4(uniform.value)
77 } else if (typeof uniform.value === 'number') {
78 adapted[name] = uniform.value
79 }
80 }
81
82 return adapted
83 }
84}