// Generates procedural reflections and soft particle textures (this looks so much better than p1). import * as THREE from "three"; function cubemapFaceDirection( face: number, s: number, t: number, ): [number, number, number] { let x = 0; let y = 0; let z = 0; switch (face) { case 0: x = 1; y = -t; z = -s; break; case 1: x = -1; y = -t; z = s; break; case 2: x = s; y = 1; z = t; break; case 3: x = s; y = -1; z = -t; break; case 4: x = s; y = -t; z = 1; break; default: x = -s; y = -t; z = -1; break; } const invLen = 1 / Math.hypot(x, y, z); return [x * invLen, y * invLen, z * invLen]; } function evalStudioRadiance( dx: number, dy: number, dz: number, roughness: number, ): [number, number, number] { const skyMix = Math.max(0, Math.min(1, dy * 0.5 + 0.5)); let r = 0.08 + 0.2 * skyMix; let g = 0.1 + 0.28 * skyMix; let b = 0.14 + 0.4 * skyMix; const lobePow = 2 + Math.pow(1 - roughness, 2.2) * 34; const lobeScale = 0.45 + 0.55 * (1 - roughness * 0.65); const dKey = Math.max(0, dx * 0.44 + dy * 0.82 + dz * 0.36); const k = Math.pow(dKey, lobePow) * 1.15 * lobeScale; r += k * 1.0; g += k * 0.96; b += k * 0.88; const dRim = Math.max(0, -dx * 0.72 + dy * 0.3 + -dz * 0.63); const rm = Math.pow(dRim, lobePow * 0.75) * 0.75 * lobeScale; r += rm * 0.42; g += rm * 0.68; b += rm * 1.0; const dFill = Math.max(0, -dx * 0.35 + dy * 0.64 + dz * 0.68); const fl = Math.pow(dFill, lobePow * 0.55) * 0.52 * lobeScale; r += fl * 0.74; g += fl * 0.84; b += fl * 0.98; const ground = Math.max(0, -dy); const gb = Math.pow(ground, 2) * 0.14; r += gb; g += gb * 1.05; b += gb * 1.15; return [Math.min(1, r), Math.min(1, g), Math.min(1, b)]; } function buildStudioFaceDataTextures( size: number, roughness: number, ): THREE.DataTexture[] { const faces: THREE.DataTexture[] = []; for (let face = 0; face < 6; face++) { const data = new Uint8Array(size * size * 4); for (let y = 0; y < size; y++) { const t = ((y + 0.5) / size) * 2 - 1; for (let x = 0; x < size; x++) { const s = ((x + 0.5) / size) * 2 - 1; const [dx, dy, dz] = cubemapFaceDirection(face, s, t); const [lr, lg, lb] = evalStudioRadiance(dx, dy, dz, roughness); const idx = (y * size + x) * 4; data[idx] = Math.round(Math.pow(lr, 1 / 2.2) * 255); data[idx + 1] = Math.round(Math.pow(lg, 1 / 2.2) * 255); data[idx + 2] = Math.round(Math.pow(lb, 1 / 2.2) * 255); data[idx + 3] = 255; } } const faceTex = new THREE.DataTexture( data, size, size, THREE.RGBAFormat, THREE.UnsignedByteType, ); faceTex.colorSpace = THREE.SRGBColorSpace; faces.push(faceTex); } return faces; } export function createStudioPMREMCubeTexture(): THREE.CubeTexture { const baseSize = 64; const maxLod = 5; const baseFaces = buildStudioFaceDataTextures(baseSize, 0); const cube = new THREE.CubeTexture(baseFaces); cube.mapping = THREE.CubeReflectionMapping; cube.format = THREE.RGBAFormat; cube.type = THREE.UnsignedByteType; cube.colorSpace = THREE.SRGBColorSpace; cube.generateMipmaps = false; cube.minFilter = THREE.LinearMipmapLinearFilter; cube.magFilter = THREE.LinearFilter; cube.mipmaps = [32, 16, 8, 4, 2, 1].map((sz, idx) => { const r = Math.min(1, (idx + 1) / maxLod); const roughness = 1 - Math.sqrt(Math.max(0, 1 - r)); return { width: sz, image: buildStudioFaceDataTextures(sz, roughness), }; }) as unknown as THREE.CubeTexture[]; cube.isPMREMTexture = true; cube.needsUpdate = true; return cube; } let cachedSoftParticleTex: THREE.CanvasTexture | null = null; export function getSoftParticleTexture(): THREE.CanvasTexture { if (cachedSoftParticleTex) return cachedSoftParticleTex; const size = 64; const canvas = document.createElement("canvas"); canvas.width = size; canvas.height = size; const ctx = canvas.getContext("2d"); if (ctx) { const half = size / 2; const grad = ctx.createRadialGradient(half, half, 0, half, half, half); grad.addColorStop(0.0, "rgba(255, 255, 255, 1.0)"); grad.addColorStop(0.25, "rgba(255, 255, 255, 0.72)"); grad.addColorStop(0.55, "rgba(255, 255, 255, 0.24)"); grad.addColorStop(0.85, "rgba(255, 255, 255, 0.05)"); grad.addColorStop(1.0, "rgba(255, 255, 255, 0.0)"); ctx.fillStyle = grad; ctx.fillRect(0, 0, size, size); } const tex = new THREE.CanvasTexture(canvas); tex.generateMipmaps = false; tex.minFilter = THREE.LinearFilter; tex.magFilter = THREE.LinearFilter; tex.needsUpdate = true; cachedSoftParticleTex = tex; return tex; }