Download web/js/scene/prims.js from paralaif/tiny-scene-three: direct link, hf CLI and curl.
- Browser
- Download file 4.16 kB
-
https://huggingface.co/paralaif/tiny-scene-three/resolve/main/web/js/scene/prims.js
- Command line
-
hf download hf://paralaif/tiny-scene-three/web/js/scene/prims.js
-
curl -L -o prims.js https://huggingface.co/paralaif/tiny-scene-three/resolve/main/web/js/scene/prims.js
4.16 kB
| // Low-poly building blocks shared by every object builder. | |
| import * as THREE from 'three'; | |
| export const PALETTE = { | |
| red: 0xd63c33, blue: 0x2f6fde, green: 0x3fa34d, yellow: 0xf2c230, orange: 0xf08a24, purple: 0x8a4fd3, | |
| pink: 0xf284b6, white: 0xf4f4f0, black: 0x2a2a30, gray: 0x8d9199, brown: 0x8a5a3b, gold: 0xe2b23a, | |
| silver: 0xc4c9d1, cyan: 0x2ec4d6, | |
| }; | |
| const geos = new Map(); | |
| const mats = new Map(); | |
| const geo = (key, make) => geos.get(key) ?? (geos.set(key, make()), geos.get(key)); | |
| export function mat(color, o) { | |
| o = o ?? {}; | |
| const key = `${color}|${o.emissive ?? ''}|${o.opacity ?? ''}|${o.metal ?? ''}|${o.basic ?? ''}|${o.side ?? ''}|${o.glow ?? ''}`; | |
| if (mats.has(key)) return mats.get(key); | |
| const m = o.basic | |
| ? new THREE.MeshBasicMaterial({ color }) | |
| : new THREE.MeshStandardMaterial({ | |
| color, flatShading: true, roughness: o.metal ? 0.38 : 0.86, metalness: o.metal ? 0.35 : 0, | |
| emissive: o.emissive ?? 0x000000, emissiveIntensity: o.emissive ? (o.glow ?? 1) : 0, | |
| }); | |
| if (o.opacity != null) { m.transparent = true; m.opacity = o.opacity; m.depthWrite = false; } | |
| if (o.side) m.side = THREE.DoubleSide; | |
| mats.set(key, m); | |
| return m; | |
| } | |
| function mk(g, color, o) { | |
| const m = new THREE.Mesh(g, mat(color, o)); | |
| m.castShadow = !(o && (o.opacity != null || o.noShadow)); | |
| m.receiveShadow = true; | |
| return m; | |
| } | |
| export const box = (w, h, d, c, o) => mk(geo(`b${w},${h},${d}`, () => new THREE.BoxGeometry(w, h, d)), c, o); | |
| export const ball = (r, c, o) => mk(geo(`s${r}`, () => new THREE.IcosahedronGeometry(r, 1)), c, o); | |
| export const orb = (r, c, o) => mk(geo(`o${r}`, () => new THREE.SphereGeometry(r, 14, 10)), c, o); | |
| export const cyl = (rt, rb, h, c, o, seg = 10) => | |
| mk(geo(`c${rt},${rb},${h},${seg}`, () => new THREE.CylinderGeometry(rt, rb, h, seg)), c, o); | |
| export const cone = (r, h, c, o, seg = 10) => mk(geo(`k${r},${h},${seg}`, () => new THREE.ConeGeometry(r, h, seg)), c, o); | |
| export const torus = (R, r, c, o) => mk(geo(`t${R},${r}`, () => new THREE.TorusGeometry(R, r, 6, 14)), c, o); | |
| export const rockGeo = (r, c, o) => mk(geo(`d${r}`, () => new THREE.DodecahedronGeometry(r, 0)), c, o); | |
| /** Position/rotate/scale helper that returns the object so calls can be nested. */ | |
| export function at(obj, x = 0, y = 0, z = 0, rx = 0, ry = 0, rz = 0, s) { | |
| obj.position.set(x, y, z); | |
| obj.rotation.set(rx, ry, rz); | |
| if (s != null) Array.isArray(s) ? obj.scale.set(...s) : obj.scale.setScalar(s); | |
| return obj; | |
| } | |
| /** A pivot: children rotate around (x, y, z). */ | |
| export function joint(x, y, z, ...children) { | |
| const g = new THREE.Group(); | |
| g.position.set(x, y, z); | |
| for (const c of children) g.add(c); | |
| return g; | |
| } | |
| export function group(...children) { | |
| const g = new THREE.Group(); | |
| for (const c of children) g.add(c); | |
| return g; | |
| } | |
| const _c = new THREE.Color(); | |
| /** Lighter (k > 0) or darker (k < 0) version of a hex colour. */ | |
| export function tint(hex, k) { | |
| _c.setHex(hex); | |
| const hsl = {}; | |
| _c.getHSL(hsl); | |
| _c.setHSL(hsl.h, hsl.s, Math.min(0.97, Math.max(0.04, hsl.l + k))); | |
| return _c.getHex(); | |
| } | |
| export const eyes = (parent, x, y, z, r = 0.05, c = 0x151518) => { | |
| parent.add(at(orb(r, c), x, y, z), at(orb(r, c), -x, y, z)); | |
| }; | |
| /** Deterministic random numbers so a scene looks the same every time it is rebuilt. */ | |
| export function rng(seed) { | |
| let s = (seed >>> 0) || 1; | |
| return () => { | |
| s ^= s << 13; s >>>= 0; s ^= s >> 17; s ^= s << 5; s >>>= 0; | |
| return s / 4294967296; | |
| }; | |
| } | |
| export const hash = (str) => { let h = 2166136261; for (const ch of str) h = Math.imul(h ^ ch.charCodeAt(0), 16777619); return h >>> 0; }; | |
| /** | |
| * Wrap finished parts into a rig the animator understands. | |
| * parts: { body, head, tail, legs[], arms[], wings[], wheels[], spinners[], fins[] } | |
| */ | |
| export function rig(root, kind, dims, parts = {}) { | |
| const all = []; | |
| for (const v of Object.values(parts)) for (const p of Array.isArray(v) ? v : [v]) if (p) all.push(p); | |
| for (const p of all) p.userData.rest = { p: p.position.clone(), r: p.rotation.clone(), s: p.scale.clone() }; | |
| root.userData.rig = { kind, h: 1, r: 0.5, top: 1, seat: [0, 1, 0], ...dims, ...parts, all }; | |
| return root; | |
| } | |