// Shell-texture fur. Each furred part is a normal mesh whose geometry holds N stacked // copies of the base surface ("shells"). The shader pushes each shell out along the // normal and cuts strands with a 3D cell pattern, so the cat looks genuinely fluffy. import * as THREE from 'three'; export const quality = { shells: 14, low: false }; export function setQuality(low: boolean) { quality.low = low; quality.shells = low ? 9 : 14; } const shellCache = new Map(); export function shellGeometry(key: string, make: () => THREE.BufferGeometry): THREE.BufferGeometry { const k = key + ':' + quality.shells; const hit = shellCache.get(k); if (hit) return hit; const base = make().toNonIndexed(); base.computeVertexNormals(); const n = quality.shells; const pos = base.getAttribute('position') as THREE.BufferAttribute; const nor = base.getAttribute('normal') as THREE.BufferAttribute; const vc = pos.count; const P = new Float32Array(vc * 3 * n), N = new Float32Array(vc * 3 * n), S = new Float32Array(vc * n); for (let s = 0; s < n; s++) { P.set(pos.array as Float32Array, s * vc * 3); N.set(nor.array as Float32Array, s * vc * 3); S.fill(s / (n - 1), s * vc, (s + 1) * vc); } const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.BufferAttribute(P, 3)); g.setAttribute('normal', new THREE.BufferAttribute(N, 3)); g.setAttribute('aShell', new THREE.BufferAttribute(S, 1)); g.computeBoundingSphere(); if (g.boundingSphere) g.boundingSphere.radius *= 1.4; shellCache.set(k, g); base.dispose(); return g; } export const furSphere = () => shellGeometry('sphere', () => new THREE.SphereGeometry(1, quality.low ? 16 : 22, quality.low ? 12 : 16)); export const furCone = () => shellGeometry('cone', () => new THREE.ConeGeometry(1, 1.6, quality.low ? 10 : 14, 3)); /** Lighting + fog shared by all custom shaders. Updated once per frame by the scene. */ export const sharedLight = { uSunDir: { value: new THREE.Vector3(0.4, 0.85, 0.35).normalize() }, uSunColor: { value: new THREE.Color(1.0, 0.95, 0.88) }, uSky: { value: new THREE.Color(0.75, 0.82, 1.0) }, uGround: { value: new THREE.Color(0.95, 0.75, 0.7) }, uFogColor: { value: new THREE.Color('#ffd9ec') }, uFogNear: { value: 40 }, uFogFar: { value: 120 }, uTime: { value: 0 }, }; export interface FurOptions { top: THREE.ColorRepresentation; bottom: THREE.ColorRepresentation; axis?: THREE.Vector3; // local direction that gets the "top" color split?: number; // -1..1 threshold along axis stripes?: number; // tabby stripe strength 0..1 stripeAxis?: THREE.Vector3; length?: number; // fur length in local units density?: number; // strand density drift?: { value: THREE.Vector3 }; // shared per-creature wind/gravity vector (world units) glow?: number; // magic emissive (Golden Fluff, Sparkle) } const vert = /* glsl */` attribute float aShell; uniform float uLen; uniform vec3 uDrift; uniform float uTime; varying vec3 vBase; varying vec3 vN; varying float vShell; varying vec3 vWorld; varying vec3 vLocalN; void main(){ vShell = aShell; vBase = position; vLocalN = normal; vec3 p = position + normal * aShell * uLen; vec4 w = modelMatrix * vec4(p, 1.0); float s2 = aShell * aShell; w.xyz += uDrift * s2 + vec3(sin(uTime*2.3 + w.y*3.0), 0.0, cos(uTime*1.9 + w.x*3.0)) * 0.006 * s2; vWorld = w.xyz; vN = normalize(mat3(modelMatrix) * normal); gl_Position = projectionMatrix * viewMatrix * w; }`; const frag = /* glsl */` uniform vec3 uTop; uniform vec3 uBottom; uniform vec3 uAxis; uniform float uSplit; uniform float uStripes; uniform vec3 uStripeAxis; uniform float uDensity; uniform float uGlow; uniform vec3 uSunDir; uniform vec3 uSunColor; uniform vec3 uSky; uniform vec3 uGround; uniform vec3 uFogColor; uniform float uFogNear; uniform float uFogFar; uniform float uTime; varying vec3 vBase; varying vec3 vN; varying float vShell; varying vec3 vWorld; varying vec3 vLocalN; float h31(vec3 p){ p = fract(p*0.3183099 + 0.1); p *= 17.0; return fract(p.x*p.y*p.z*(p.x+p.y+p.z)); } float strand(vec3 p, float shell){ vec3 c = floor(p); vec3 f = fract(p) - 0.5; float r = h31(c); float len = 0.55 + 0.45 * r; // strand length varies if (shell > len) return 0.0; float rad = (1.0 - shell/len) * 0.62 + 0.08; return step(length(f.xy + vec2(r-0.5, h31(c+7.1)-0.5)*0.35) , rad); } void main(){ if (vShell > 0.001) { vec3 p = vBase * uDensity; float a = max(strand(p, vShell), strand(p*1.73 + 3.1, vShell*1.1)); if (a < 0.5) discard; } float t = dot(normalize(vLocalN), uAxis); float m = smoothstep(uSplit - 0.18, uSplit + 0.18, t); vec3 col = mix(uBottom, uTop, m); // soft tabby stripes on the top colour only float st = sin(dot(vBase, uStripeAxis) * 9.0 + sin(vBase.x*7.0)*0.8); col *= 1.0 - uStripes * m * smoothstep(0.35, 0.95, st) * 0.28; // fake ambient occlusion through the fur, brighter tips col *= mix(0.58, 1.08, vShell); vec3 N = normalize(vN); float diff = clamp((dot(N, uSunDir) + 0.45) / 1.45, 0.0, 1.0); vec3 amb = mix(uGround, uSky, N.y*0.5+0.5) * 0.55; vec3 V = normalize(cameraPosition - vWorld); float rim = pow(1.0 - clamp(dot(N, V), 0.0, 1.0), 2.5) * (0.25 + 0.5*vShell); vec3 c = col * (amb + uSunColor * diff * 0.78) + rim * vec3(1.0, 0.92, 0.85) * 0.45; c += uGlow * vec3(1.0, 0.85, 0.35) * (0.35 + 0.25*sin(uTime*4.0 + vBase.y*6.0)); float fog = smoothstep(uFogNear, uFogFar, length(cameraPosition - vWorld)); gl_FragColor = vec4(mix(c, uFogColor, fog), 1.0); #include }`; export function furMaterial(o: FurOptions): THREE.ShaderMaterial { return new THREE.ShaderMaterial({ vertexShader: vert, fragmentShader: frag, uniforms: { ...sharedLight, uTop: { value: new THREE.Color(o.top) }, uBottom: { value: new THREE.Color(o.bottom) }, uAxis: { value: (o.axis ?? new THREE.Vector3(0, 1, 0)).clone().normalize() }, uSplit: { value: o.split ?? -0.1 }, uStripes: { value: o.stripes ?? 0 }, uStripeAxis: { value: (o.stripeAxis ?? new THREE.Vector3(0, 0, 1)).clone() }, uLen: { value: o.length ?? 0.22 }, uDensity: { value: o.density ?? 26 }, uDrift: o.drift ?? { value: new THREE.Vector3(0, -0.02, 0) }, uGlow: { value: o.glow ?? 0 }, }, }); } /** Simple soft-lit material for non-fur parts that still matches the fur lighting. */ const softVert = /* glsl */` varying vec3 vN; varying vec3 vWorld; #ifdef USE_INSTANCING_COLOR #endif void main(){ vec4 w = modelMatrix * vec4(position,1.0); vWorld = w.xyz; vN = normalize(mat3(modelMatrix)*normal); gl_Position = projectionMatrix * viewMatrix * w; }`; const softFrag = /* glsl */` uniform vec3 uColor; uniform float uGloss; uniform float uEmissive; uniform vec3 uSunDir; uniform vec3 uSunColor; uniform vec3 uSky; uniform vec3 uGround; uniform vec3 uFogColor; uniform float uFogNear; uniform float uFogFar; varying vec3 vN; varying vec3 vWorld; void main(){ vec3 N = normalize(vN); vec3 V = normalize(cameraPosition - vWorld); float diff = clamp((dot(N,uSunDir)+0.4)/1.4,0.0,1.0); vec3 amb = mix(uGround,uSky,N.y*0.5+0.5)*0.55; vec3 c = uColor*(amb + uSunColor*diff*0.8); vec3 H = normalize(uSunDir + V); c += uGloss * pow(max(dot(N,H),0.0), 40.0) * vec3(1.0); c += uGloss * 0.6 * step(0.985, max(dot(N, normalize(V + vec3(0.25,0.35,0.0))),0.0)); c += uColor * uEmissive; float rim = pow(1.0 - clamp(dot(N,V),0.0,1.0), 3.0)*0.18; c += rim; float fog = smoothstep(uFogNear, uFogFar, length(cameraPosition - vWorld)); gl_FragColor = vec4(mix(c,uFogColor,fog),1.0); #include }`; const softCache = new Map(); export function softMaterial(color: THREE.ColorRepresentation, gloss = 0, emissive = 0): THREE.ShaderMaterial { const key = new THREE.Color(color).getHexString() + gloss + ':' + emissive; const hit = softCache.get(key); if (hit) return hit; const m = new THREE.ShaderMaterial({ vertexShader: softVert, fragmentShader: softFrag, uniforms: { ...sharedLight, uColor: { value: new THREE.Color(color) }, uGloss: { value: gloss }, uEmissive: { value: emissive } }, }); softCache.set(key, m); return m; }