// 날씨각 캐릭터 (career/shorts/nalssigak-shorts 에서 가져옴): 구름 캐릭터 = 맑은 날파 초록 털뭉치(셸 텍스처 퍼), 물방울 캐릭터 = 비 오는 날파 반투명 파란 젤리 import * as THREE from 'three'; import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js'; const eyeWhite = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.25 }); const pupilMat = new THREE.MeshStandardMaterial({ color: 0x1b2240, roughness: 0.15 }); function makeEyes(z, spread, size) { const g = new THREE.Group(); const pupils = []; for (const sx of [-1, 1]) { const w = new THREE.Mesh(new THREE.SphereGeometry(size, 32, 24), eyeWhite); w.scale.set(1, 1.15, 0.7); w.position.set(sx * spread, 0, z); const p = new THREE.Mesh(new THREE.SphereGeometry(size * 0.55, 24, 16), pupilMat); p.position.set(sx * spread, 0, z + size * 0.62); g.add(w, p); pupils.push(p); } g.userData.pupils = pupils; g.userData.base = pupils.map((p) => p.position.x); return g; } // 털 노이즈는 고정 시드라 어느 프레임을 먼저 그려도 같다 let noiseTex = null; function noise() { if (noiseTex) return noiseTex; const n = 128, data = new Uint8Array(n * n * 4); let seed = 1234567; const rnd = () => ((seed = (seed * 16807) % 2147483647) / 2147483647); for (let i = 0; i < n * n; i++) { const v = Math.floor(rnd() * 255); data.set([v, v, v, 255], i * 4); } noiseTex = new THREE.DataTexture(data, n, n, THREE.RGBAFormat); noiseTex.wrapS = noiseTex.wrapT = THREE.RepeatWrapping; noiseTex.magFilter = noiseTex.minFilter = THREE.NearestFilter; noiseTex.needsUpdate = true; return noiseTex; } const furVert = ` uniform float uLayer; varying vec2 vUv; varying vec3 vN; varying vec3 vView; varying float vL; void main(){ vUv = uv; vL = uLayer; vec3 p = position + normal * uLayer * 0.17; p.y -= uLayer * uLayer * 0.04; vN = normalize(normalMatrix * normal); vec4 mv = modelViewMatrix * vec4(p, 1.0); vView = -mv.xyz; gl_Position = projectionMatrix * mv; }`; const furFrag = ` uniform sampler2D uNoise; uniform vec3 uRoot; uniform vec3 uTip; uniform vec3 uWarm; uniform float uTint; uniform float uNight; varying vec2 vUv; varying vec3 vN; varying vec3 vView; varying float vL; void main(){ float n = texture2D(uNoise, vUv * vec2(70.0, 36.0)).r; if (vL > 0.0 && n < vL * 0.98) discard; vec3 N = normalize(vN); float d = max(dot(N, normalize(vec3(0.4, 0.7, 0.6))), 0.0); float rim = pow(1.0 - max(dot(N, normalize(vView)), 0.0), 2.2); vec3 c = mix(uRoot, uTip, vL); c = mix(c, uWarm * mix(0.7, 1.1, vL), uTint * 0.6); vec3 col = c * (0.42 + 0.8 * d) * mix(0.45, 1.0, vL) + rim * 0.35 * mix(uTip, uWarm, uTint); col *= mix(1.0, 0.62, uNight); gl_FragColor = vec4(col, 1.0); #include }`; let furGeo = null; function fur() { if (furGeo) return furGeo; const parts = [new THREE.SphereGeometry(0.78, 64, 48)]; for (let k = 0; k < 8; k++) { const a = (k / 8) * Math.PI * 2 + Math.PI / 8; const g = new THREE.SphereGeometry(0.44, 48, 32); g.translate(Math.cos(a) * 0.8, Math.sin(a) * 0.8, 0); parts.push(g); } for (let k = 0; k < 5; k++) { const a = (k / 5) * Math.PI * 2; const g = new THREE.SphereGeometry(0.36, 48, 32); g.translate(Math.cos(a) * 0.4, Math.sin(a) * 0.4, 0.48); parts.push(g); } furGeo = mergeGeometries(parts); return furGeo; } /** 구름 캐릭터. 돌려주는 값: { group, eyes, uniforms(uTint 0~1 노을, uNight 0~1 밤) } */ export function makeCloud(shells = 34) { const uniforms = { uNoise: { value: noise() }, uRoot: { value: new THREE.Color(0x1f8a1a) }, uTip: { value: new THREE.Color(0x8ef06a) }, uWarm: { value: new THREE.Color(0xff9a4a) }, uTint: { value: 0 }, uNight: { value: 0 } }; const group = new THREE.Group(); const body = new THREE.Group(); for (let i = 0; i < shells; i++) { const mat = new THREE.ShaderMaterial({ uniforms: { ...uniforms, uLayer: { value: i / (shells - 1) } }, vertexShader: furVert, fragmentShader: furFrag }); const m = new THREE.Mesh(fur(), mat); m.renderOrder = i; body.add(m); } group.add(body); const eyes = makeEyes(1.05, 0.13, 0.1); eyes.position.y = 0.06; group.add(eyes); return { group, eyes, uniforms }; } /** 물방울 캐릭터. 돌려주는 값: { group, eyes, arms, front, back } — front.color 를 바꾸면 전체 색이 바뀐다 */ export function makeDrop(envMap) { const group = new THREE.Group(); const body = new THREE.Group(); const front = new THREE.MeshPhysicalMaterial({ color: 0x7cbcff, transparent: true, opacity: 0.58, roughness: 0.06, metalness: 0, clearcoat: 1, clearcoatRoughness: 0.04, envMapIntensity: 1.6, iridescence: 0.25, depthWrite: false, envMap }); const back = front.clone(); back.side = THREE.BackSide; back.opacity = 0.32; const coreMat = new THREE.MeshPhysicalMaterial({ color: 0xbfe0ff, transparent: true, opacity: 0.45, roughness: 0.3, depthWrite: false, envMap }); const addJelly = (geo, parent) => { const b = new THREE.Mesh(geo, back); b.renderOrder = 1; const f = new THREE.Mesh(geo, front); f.renderOrder = 3; parent.add(b, f); }; const center = new THREE.SphereGeometry(0.72, 64, 48); center.scale(1, 0.95, 0.72); addJelly(center, body); const core = new THREE.Mesh(new THREE.SphereGeometry(0.42, 40, 30), coreMat); core.scale.set(1, 0.9, 0.6); core.position.set(0.12, -0.1, -0.1); core.renderOrder = 2; body.add(core); const arms = []; for (let k = 0; k < 6; k++) { const a = Math.PI / 2 + (k / 6) * Math.PI * 2 + (k % 2 ? 0.08 : -0.05); const arm = new THREE.Group(); const stem = new THREE.CapsuleGeometry(0.2 + (k % 3) * 0.02, 0.62, 12, 24); stem.rotateZ(-Math.PI / 2); stem.translate(0.78, 0, 0); stem.scale(1, 1, 0.8); addJelly(stem, arm); const tip = new THREE.SphereGeometry(0.27, 40, 30); tip.scale(1, 1, 0.8); tip.translate(1.18, 0, 0); addJelly(tip, arm); arm.rotation.z = a; arm.userData.a = a; arms.push(arm); body.add(arm); } group.add(body); const eyes = makeEyes(0.6, 0.15, 0.1); eyes.position.set(0.05, -0.02, 0); group.add(eyes); return { group, eyes, arms, front, back }; } export const JELLY = { blue: new THREE.Color(0x7cbcff), warm: new THREE.Color(0xffb27a), night: new THREE.Color(0x5b78c8) }; /** 숨쉬기·팔 흔들기: t 의 함수 */ export function animate(b, m, t, phase = 0) { b.group.rotation.set(Math.sin(t * 1.3 + phase) * 0.06, Math.sin(t * 0.9 + phase) * 0.18, Math.sin(t * 1.7 + phase) * 0.03); m.group.rotation.set(Math.sin(t * 1.3 + phase + 1.7) * 0.06, Math.sin(t * 0.9 + phase + 1.7) * 0.18, Math.sin(t * 1.7 + phase + 1.7) * 0.03); m.arms.forEach((arm, i) => { arm.rotation.z = arm.userData.a + Math.sin(t * 2.6 + i * 1.1) * 0.07; arm.scale.setScalar(1 + Math.sin(t * 3.1 + i) * 0.04); }); }