video / src /jiangnan /engine /Film.ts
Surf
暮归江南 · 步骤8:集市货物改用 NVIDIA SimReady Central(Palatial NV Asset Library,CC-BY-4.0)的 18 件扫描模型,USD→GLB 转换器、目录检索脚本、实例化加载与程序化回退(GLB 文件经 Hub API 以 LFS 上传)
cdeed5c
Raw History Blame Contribute Delete
26.7 kB
// The film: builds the world once and renders any time t deterministically.
import * as THREE from 'three'
import { AssetsOut, createAssets } from './assets'
import { applyPose, cameraAt } from './camera'
import { TERRACE, TREE_POS } from './layout'
import { createLanterns, createLife, LanternSet, LifeOut } from './life'
import { U } from './materials'
import { createPost, Post } from './post'
import { createProps, PropsOut } from './props'
import { lerp, smoothstep } from './rng'
import { createSky, skyState } from './sky'
import { createFarMountains, createTerraces, createTerrain } from './terrain'
import { fadeBlack, shotAt } from './timeline'
import { createTrees } from './trees'
import { getTextures } from './textures'
import { createVillage, VillageOut } from './village'
import { createEmbankments, createWater, Water } from './water'
export type Quality = 'low' | 'medium' | 'high'
const tick = () => new Promise<void>((r) => setTimeout(r, 0))
export class Film {
renderer: THREE.WebGLRenderer
scene = new THREE.Scene()
camera = new THREE.PerspectiveCamera(40, 16 / 9, 0.4, 12000)
post!: Post
water!: Water
sky!: ReturnType<typeof createSky>
sun = new THREE.DirectionalLight(0xffffff, 3)
moon = new THREE.DirectionalLight(0x9fb4ff, 0)
hemi = new THREE.HemisphereLight(0x8899bb, 0x223311, 0.2)
lamps: THREE.PointLight[] = []
lanterns!: LanternSet
life!: LifeOut
props!: PropsOut
assets: AssetsOut | null = null
village!: VillageOut
pmrem: THREE.PMREMGenerator
envScene = new THREE.Scene()
envSky!: THREE.Mesh
envRT: THREE.WebGLRenderTarget | null = null
envKey = -1
envCache = new Map<number, THREE.WebGLRenderTarget>()
shadowShot = -1
shadowCenter = new THREE.Vector3()
shadowDir = new THREE.Vector3(0, 1, 0)
shadowSize = 90
lampLevel = new Map<number, number>()
width = 1280
height = 720
/** internal supersampling factor: the post chain renders at width*ss × height*ss, the final pass filters it down to the canvas */
ss = 1
postSamples = 4
shadowRes = 2048
/** reflection map resolution relative to the (supersampled) frame */
reflScale = 0.5
quality: Quality
ready = false
// ---- WebGL context loss bookkeeping (the usual cause of "the exported video is black")
contextLost = false
lostCount = 0
/** bumped on every context restore; GPU objects created under an older generation must not be deleted */
private glGen = 0
private postGen = 0
/** resolves once the context is back and the environment maps have been re-baked */
private restorePromise: Promise<void> | null = null
onContextEvent: ((state: 'lost' | 'restored') => void) | null = null
/**
* Self-healing level used by offline exports when a frame renders black on a live context
* (driver-specific shader / state faults): 0 = full pipeline, 1 = no god rays, 2 = no bloom,
* 3 = no water reflection pass, 4 = no animated figures / birds, 5 = no post-processing at all.
*/
heal = 0
static HEAL_LABELS = ['完整渲染', '关闭体积光', '关闭体积光 + 辉光', '关闭体积光 + 辉光 + 水面反射', '…+ 隐藏人物与鸟群', '关闭全部后期(直接渲染)']
/** shader programs that failed to compile / link on this GPU (material key + first error line) */
shaderErrors: string[] = []
/** QA: freeze the camera at this film time (everything else keeps animating) */
freezeCam: number | null = null
constructor(canvas: HTMLCanvasElement, quality: Quality = 'high') {
this.quality = quality
this.renderer = new THREE.WebGLRenderer({ canvas, antialias: false, powerPreference: 'high-performance', preserveDrawingBuffer: false, alpha: false, reversedDepthBuffer: true, stencil: false })
// see layers: 0 = everything reflected in the water, 1 = main view only
this.camera.layers.enable(1)
this.renderer.setPixelRatio(1)
this.renderer.toneMapping = THREE.ACESFilmicToneMapping
this.renderer.toneMappingExposure = 1
this.renderer.outputColorSpace = THREE.SRGBColorSpace
this.renderer.shadowMap.enabled = true
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap
this.renderer.shadowMap.autoUpdate = false
this.pmrem = new THREE.PMREMGenerator(this.renderer)
// record shader failures instead of only logging them: they are the usual reason a frame is
// black on one specific GPU / driver while every other machine is fine
this.renderer.debug.onShaderError = (gl, program, vs, fs) => {
const info = gl.getProgramInfoLog(program) || ''
const vl = gl.getShaderInfoLog(vs) || '', fl = gl.getShaderInfoLog(fs) || ''
const msg = `${(info || vl || fl).split('\n')[0]}`.slice(0, 200)
if (this.shaderErrors.length < 20) this.shaderErrors.push(msg)
console.error('[暮归江南] shader error:', msg)
}
this.shadowRes = quality === 'low' ? 1024 : quality === 'medium' ? 2048 : 3072
this.postSamples = quality === 'low' ? 0 : 4
this.reflScale = quality === 'low' ? 0.35 : quality === 'medium' ? 0.5 : 0.75
// Context loss: three.js already calls preventDefault() (so the browser will try to restore).
// We additionally track the state so exports can wait, retry, and never encode black frames.
canvas.addEventListener('webglcontextlost', () => {
this.contextLost = true
this.lostCount++
console.warn('[暮归江南] WebGL context lost')
this.onContextEvent?.('lost')
})
canvas.addEventListener('webglcontextrestored', () => {
console.warn('[暮归江南] WebGL context restored → re-baking')
this.glGen++
this.restorePromise = this.recoverFromContextLoss().finally(() => { this.restorePromise = null })
})
}
/** True when the GL context is currently lost. */
isContextLost() {
try { return this.contextLost || this.renderer.getContext().isContextLost() } catch { return true }
}
/** Wait until the context is usable again (restored + environment re-baked). Resolves false on timeout. */
async waitForContext(timeoutMs = 15000) {
const t0 = performance.now()
while (performance.now() - t0 < timeoutMs) {
if (!this.isContextLost() && !this.restorePromise) return true
if (this.restorePromise) { await this.restorePromise; continue }
await new Promise((r) => setTimeout(r, 100))
}
return !this.isContextLost() && !this.restorePromise
}
/** Everything that lived only on the GPU has to be produced again after a context restore. */
private async recoverFromContextLoss() {
if (!this.ready) { this.contextLost = false; return }
// baked environment maps are gone (they were only ever GPU textures)
for (const rt of this.envCache.values()) rt.dispose()
this.envCache.clear()
this.envKey = -1
this.envRT = null
this.pmrem.dispose()
this.pmrem = new THREE.PMREMGenerator(this.renderer)
// shadow map, reflection target and post chain are re-created lazily by three.js, but the
// shadow frustum and the post targets' sizes must be re-applied
this.shadowShot = -1
// the old post targets belonged to the dead context: do not try to delete them
this.rebuildPost(this.postSamples, false)
this.setSize(this.width, this.height, this.ss)
if (this.sun.shadow.map) this.sun.shadow.map = null
this.contextLost = false
await this.bakeEnvironment()
this.onContextEvent?.('restored')
}
/**
* Hard reset of the GPU side: deliberately lose and restore the context. Frees every GPU
* allocation (a renderer stuck in GL_OUT_OF_MEMORY / an invalid framebuffer keeps returning
* black frames without ever losing the context) and rebuilds everything through the normal
* restore path. Resolves when the film can render again.
*/
async resetContext() {
const gl = this.renderer.getContext()
const ext = gl.getExtension('WEBGL_lose_context')
if (!ext) return false
ext.loseContext()
await new Promise((r) => setTimeout(r, 300))
ext.restoreContext()
return this.waitForContext(20000)
}
/** Last GL error code (0 = none), for diagnostics. */
glError() {
try { return this.renderer.getContext().getError() } catch { return -1 }
}
/** Re-create the post-processing chain (e.g. with a different MSAA sample count). */
rebuildPost(samples: number, dispose = true) {
this.postSamples = samples
if (this.post && dispose && this.postGen === this.glGen) {
this.post.bloom.dispose()
this.post.composer.dispose()
}
this.postGen = this.glGen
this.post = createPost(this.renderer, this.scene, this.camera, Math.round(this.width * this.ss), Math.round(this.height * this.ss), samples)
;(this.post.grade.uniforms.uRes.value as THREE.Vector2).set(this.width, this.height)
this.post.grade.uniforms.uSS.value = this.ss
}
/** Change the sun shadow map resolution (1024 … 4096). */
setShadowRes(n: number) {
if (n === this.shadowRes) return
this.shadowRes = n
this.sun.shadow.mapSize.set(n, n)
if (this.sun.shadow.map) { this.sun.shadow.map.dispose(); this.sun.shadow.map = null }
this.shadowShot = -1
}
async init(progress: (p: number, label: string) => void) {
const s = this.scene
// a dummy fog so every built-in material compiles its fog chunk (replaced by our height fog)
s.fog = new THREE.Fog(0x000000, 1, 2)
progress(0.04, '绘制材质贴图')
await tick()
const T = getTextures() as unknown as Record<string, unknown>
// anisotropic filtering at the GPU maximum: tiles, flagstones and wood grain stay sharp at grazing angles
const aniso = Math.min(16, this.renderer.capabilities.getMaxAnisotropy())
for (const v of Object.values(T)) {
const list = Array.isArray(v) ? v : [v]
for (const t of list) if ((t as THREE.Texture)?.isTexture) { (t as THREE.Texture).anisotropy = aniso; (t as THREE.Texture).needsUpdate = true }
}
progress(0.12, '生成天空与山谷')
await tick()
this.sky = createSky(this.renderer.capabilities.reversedDepthBuffer)
s.add(this.sky.mesh)
s.add(createTerrain())
progress(0.3, '开垦梯田')
await tick()
s.add(createTerraces())
s.add(createFarMountains())
progress(0.42, '引来河水')
await tick()
this.water = createWater()
s.add(this.water.mesh)
s.add(createEmbankments())
progress(0.5, '修建粉墙黛瓦')
await tick()
this.village = createVillage(this.quality)
s.add(this.village.group)
progress(0.62, '搭石桥、架水车、摆集市')
await tick()
this.props = createProps()
s.add(this.props.group)
progress(0.66, '摆上竹篮、陶罐与瓜菜(SimReady 扫描模型)')
await tick()
try {
this.assets = await createAssets(this.props.stalls, (p) => progress(0.66 + p * 0.06, '摆上竹篮、陶罐与瓜菜(SimReady 扫描模型)'))
if (this.assets.loaded.length) {
s.add(this.assets.group)
// the scanned goods replace the procedural ones
for (const m of this.props.proceduralGoods) m.visible = false
}
if (this.assets.failed.length) console.warn('[暮归江南] 未能加载的模型:', this.assets.failed.join(', '))
} catch (e) { console.warn('[暮归江南] 扫描模型加载失败,使用程序化货物', e) }
progress(0.72, '种下古樟与竹林')
await tick()
const trees = createTrees(this.quality)
s.add(trees.group)
progress(0.86, '村民、牛车、白鹭与灯笼')
await tick()
this.lanterns = createLanterns(this.village, this.props)
s.add(this.lanterns.mesh, this.lanterns.glow)
this.life = createLife(this.village, this.props)
s.add(this.life.group)
// lights
this.sun.castShadow = true
this.sun.shadow.mapSize.set(this.shadowRes, this.shadowRes)
this.sun.shadow.bias = -0.0004
this.sun.shadow.normalBias = 0.04
s.add(this.sun, this.sun.target, this.moon, this.moon.target, this.hemi)
const nLamps = this.quality === 'low' ? 4 : this.quality === 'medium' ? 8 : 12
for (let i = 0; i < nLamps; i++) {
const l = new THREE.PointLight(0xff9a50, 0, 16, 2)
this.lamps.push(l)
s.add(l)
}
// environment (sky only) for image based lighting
const envSkyMat = this.sky.mesh.material as THREE.ShaderMaterial
this.envSky = new THREE.Mesh(new THREE.SphereGeometry(100, 32, 16), envSkyMat)
this.envScene.add(this.envSky)
const ground = new THREE.Mesh(new THREE.CircleGeometry(90, 24), new THREE.MeshBasicMaterial({ color: 0x0c140a }))
ground.rotation.x = -Math.PI / 2
ground.position.y = -6
this.envScene.add(ground)
this.post = createPost(this.renderer, this.scene, this.camera, this.width, this.height, this.postSamples)
progress(0.9, '烘焙天光')
await tick()
await this.bakeEnvironment((p) => progress(0.9 + p * 0.05, '烘焙天光'))
progress(0.95, '预热着色器')
await tick()
// warm-up: render every shot once (day and night states) at a tiny size so that every
// shader variant (main, reflection clipping, shadow depth, skinning…) is compiled now,
// not the first time a shot appears during playback
const W0 = this.width, H0 = this.height
this.setSize(96, 54)
const warm = [0.5, 12, 20, 28, 36, 44, 51, 58.6, 60, 62, 68, 80]
for (let i = 0; i < warm.length; i++) {
this.renderFrame(warm[i])
this.setSize(54, 96) // portrait framing variants
this.renderFrame(warm[i])
this.setSize(96, 54)
progress(0.95 + (i / warm.length) * 0.05, '预热着色器')
await tick()
}
this.shadowShot = -1
this.setSize(W0, H0)
progress(1, '完成')
this.ready = true
}
/**
* Output size (canvas) and internal supersampling factor. With ss > 1 the whole scene + post
* chain renders at w*ss × h*ss and the final grade pass filters it down to the canvas, so
* the exported frame is anti-aliased without MSAA and without a second CPU-side resample.
*/
setSize(w: number, h: number, ss = this.ss) {
this.width = w
this.height = h
this.ss = ss
this.renderer.setSize(w, h, false)
this.camera.aspect = w / h
this.camera.updateProjectionMatrix()
const iw = Math.round(w * ss), ih = Math.round(h * ss)
this.post?.setSize(iw, ih)
if (this.post) {
;(this.post.grade.uniforms.uRes.value as THREE.Vector2).set(w, h)
this.post.grade.uniforms.uSS.value = ss
}
this.water?.setSize(iw * this.reflScale, ih * this.reflScale)
}
/** Environment light from the sky. Pre-baked every 0.5 s of film time (no runtime hitches). */
private envFor(t: number) {
const key = Math.max(0, Math.min(Math.round(t * 2), 9999))
let rt = this.envCache.get(key)
if (!rt) {
// bake the sky as it is at the quantised time
const st = skyState(key / 2)
U.uSunDir.value.copy(st.sunDir)
U.uNight.value = st.night
this.sky.update(st)
const prevStar = this.sky.uniforms.uStarBoost.value
this.sky.uniforms.uStarBoost.value = 0
rt = this.pmrem.fromScene(this.envScene, 0.04, 0.1, 400, { size: 64 })
this.sky.uniforms.uStarBoost.value = prevStar
this.envCache.set(key, rt)
}
return rt
}
/** Bake all environment maps up-front (called during loading). */
async bakeEnvironment(progress?: (p: number) => void) {
const n = Math.ceil(86 * 2) + 1
for (let k = 0; k <= n; k++) {
this.envFor(k / 2)
if (k % 12 === 0) { progress?.(k / n); await tick() }
}
}
private updateEnv(t: number, state: ReturnType<typeof skyState>) {
const key = Math.round(t * 2)
if (key === this.envKey && this.envRT) return
this.envKey = key
const hit = this.envCache.has(Math.max(0, Math.min(Math.round(t * 2), 9999)))
this.envRT = this.envFor(t)
if (!hit) {
this.sky.update(state)
U.uSunDir.value.copy(state.sunDir)
U.uNight.value = state.night
}
this.scene.environment = this.envRT.texture
this.scene.environmentIntensity = lerp(1.6, 1.1, state.night)
}
/** Update every animated thing for film time t (seconds). */
update(t: number) {
const st = skyState(t)
U.uTime.value = t
U.uSunDir.value.copy(st.sunDir)
U.uNight.value = st.night
this.sky.update(st)
// fog / aerial perspective tinted by the sky
const fogAway = new THREE.Color(...st.hor).multiplyScalar(0.92)
const fogSun = new THREE.Color(...st.horSun).multiplyScalar(0.62)
U.uFogAway.value.copy(fogAway)
U.uFogSun.value.copy(fogSun)
U.uFogDensity.value = lerp(0.0008, 0.0016, smoothstep(40, 80, t))
U.uFogFalloff.value = lerp(0.032, 0.05, st.night)
U.uHaze.value = lerp(0.00011, 0.00016, st.night)
U.uWind.value = 0.8 + 0.25 * Math.sin(t * 0.21)
// sun & moon
const sunVis = smoothstep(-1.2, 1.5, st.elev)
this.sun.color.setRGB(st.sun[0], st.sun[1], st.sun[2])
this.sun.intensity = st.sunI * sunVis
U.uSunColor.value.copy(this.sun.color).multiplyScalar(this.sun.intensity * 0.5)
this.moon.intensity = st.night * 0.35
this.moon.position.copy(st.moonDir).multiplyScalar(500)
this.hemi.color.setRGB(st.zen[0] * 2, st.zen[1] * 2, st.zen[2] * 2)
this.hemi.groundColor.setRGB(0.05, 0.06, 0.03)
this.hemi.intensity = lerp(0.35, 0.5, st.night)
// water tint
const wu = this.water.uniforms
;(wu.uSunCol.value as THREE.Color).setRGB(st.sun[0], st.sun[1], st.sun[2])
wu.uSunI.value = st.sunI * sunVis
;(wu.uSkyAmb.value as THREE.Color).setRGB(st.zen[0] * 2 + st.hor[0] * 0.5, st.zen[1] * 2 + st.hor[1] * 0.5, st.zen[2] * 2 + st.hor[2] * 0.5)
// per-shot exposure, like a cinematographer riding the iris between set-ups
const SHOT_EXPOSURE = [1, 1, 1, 1, 1.35, 1.12, 1.15, 1.3, 1.18, 0.86, 1.2]
this.renderer.toneMappingExposure = st.exposure * SHOT_EXPOSURE[shotAt(t).id]
this.life.update(t)
return st
}
private placeLights(t: number, st: ReturnType<typeof skyState>) {
const shot = shotAt(t)
const cam = this.camera
const fwd = new THREE.Vector3()
cam.getWorldDirection(fwd)
// shadow frustum: fixed for the whole shot (no per-frame re-centering → no shimmering)
if (this.shadowShot !== shot.id) {
this.shadowShot = shot.id
// the sun direction used for shadows is frozen for the shot: a continuously rotating
// shadow camera re-samples every texel each frame, which reads as crawling / flicker
this.shadowDir.copy(skyState(shot.t0 + (shot.t1 - shot.t0) * 0.5).sunDir)
if (this.shadowDir.y < 0.02) this.shadowDir.y = 0.02
this.shadowDir.normalize()
const pose = cameraAt(shot.t0 + (shot.t1 - shot.t0) * 0.5, this.width / this.height)
const dir = pose.target.clone().sub(pose.pos).setY(0).normalize()
if (shot.id === 1 || shot.id === 10) { this.shadowCenter.set(-20, 0, -10); this.shadowSize = 380 } else if (shot.id === 7) { this.shadowCenter.set(TERRACE.cx - 60, 80, TERRACE.cz + 80); this.shadowSize = 320 } else if (shot.id === 6) { this.shadowCenter.set(TREE_POS.x + 6, 2, TREE_POS.z + 4); this.shadowSize = 60 } else {
this.shadowCenter.copy(pose.pos).addScaledVector(dir, 34)
this.shadowCenter.y = 2
this.shadowSize = shot.id === 5 || shot.id === 8 ? 75 : 70
}
}
const size = this.shadowSize
const sc = this.sun.shadow.camera as THREE.OrthographicCamera
if (sc.right !== size) {
sc.left = -size; sc.right = size; sc.top = size; sc.bottom = -size
sc.near = 1
sc.far = 2400
sc.updateProjectionMatrix()
}
// snap the frustum origin to whole shadow-map texels in light space
const texel = (size * 2) / this.sun.shadow.mapSize.x
const lightRot = new THREE.Matrix4().lookAt(this.shadowDir, new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 1, 0))
const inv = lightRot.clone().invert()
const c = this.shadowCenter.clone().applyMatrix4(inv)
c.x = Math.round(c.x / texel) * texel
c.y = Math.round(c.y / texel) * texel
c.applyMatrix4(lightRot)
this.sun.target.position.copy(c)
this.sun.position.copy(c).addScaledVector(this.shadowDir, 1000)
this.sun.target.updateMatrixWorld()
this.moon.target.position.copy(c)
this.moon.target.updateMatrixWorld()
// nearest lit lanterns become real point lights; intensities fade with distance so the
// set of lights can change without anything visibly popping
const cp = cam.position
const R = 26, R0 = 15
const cands: { i: number; d: number }[] = []
const list = this.lanterns.list
for (let i = 0; i < list.length; i++) {
const l = list[i]
if (t <= l.on) continue
const dx = l.p.x - cp.x, dy = l.p.y - cp.y, dz = l.p.z - cp.z
const d = Math.sqrt(dx * dx + dy * dy + dz * dz)
if (d > R) continue
const ahead = (dx * fwd.x + dy * fwd.y + dz * fwd.z) > -6
if (ahead) cands.push({ i, d })
}
cands.sort((a, b) => a.d - b.d)
// the fade-out distance is set by the first lantern that does NOT get a light, so a lantern
// swapping in or out of the set always does so at ~zero intensity
const cutoff = Math.min(R, cands[this.lamps.length]?.d ?? R)
this.lamps.forEach((lamp, k) => {
const c2 = cands[k]
if (!c2) { lamp.intensity = 0; return }
const l = list[c2.i]
const lt = t - l.on
const on = smoothstep(0, 0.4, lt)
const flick = 0.92 + 0.08 * Math.sin(t * 11 + l.seed * 50)
// fade out toward the edge of the radius, and for the last slots of the set
const edge = 1 - smoothstep(Math.min(R0, cutoff * 0.65), cutoff, c2.d)
lamp.position.set(l.p.x, l.p.y - 0.45, l.p.z)
lamp.intensity = 26 * on * flick * edge
})
}
/** Render the frame at film time t with the current canvas size. */
renderFrame(t: number) {
if (!this.sky || !this.water || !this.life) return
const st = this.update(t)
const shot = shotAt(t)
const pose = cameraAt(this.freezeCam ?? t, this.width / this.height)
applyPose(this.camera, pose)
this.sky.mesh.position.copy(this.camera.position)
this.updateEnv(t, st)
this.placeLights(t, st)
// god rays: sun position on screen
const sp = this.camera.position.clone().addScaledVector(st.sunDir, 1000).project(this.camera)
const onScreen = sp.z < 1 && Math.abs(sp.x) < 1.6 && Math.abs(sp.y) < 1.6
const camFwd = new THREE.Vector3()
this.camera.getWorldDirection(camFwd)
const facing = camFwd.dot(st.sunDir)
this.post.rays.uniforms.uSun.value.set(sp.x * 0.5 + 0.5, sp.y * 0.5 + 0.5)
this.post.rays.uniforms.uStrength.value = onScreen && facing > 0 && this.heal < 1 ? smoothstep(-1.5, 2, st.elev) * 0.45 : 0
this.post.rays.enabled = this.heal < 1
this.post.bloom.enabled = this.heal < 2
this.life.group.visible = this.heal < 4
;(this.post.rays.uniforms.uTint.value as THREE.Color).setRGB(st.sun[0], st.sun[1] * 0.95, st.sun[2] * 0.9)
this.post.bloom.strength = lerp(0.42, 0.75, st.night)
this.post.bloom.threshold = lerp(2.2, 0.85, smoothstep(0, 0.6, st.night))
this.post.grade.uniforms.uTime.value = t
this.post.grade.uniforms.uNight.value = st.night
this.post.grade.uniforms.uFade.value = fadeBlack(t)
this.renderer.shadowMap.needsUpdate = true
// the river is not in frame in the camphor and terrace shots: skip the whole reflection pass
const showWater = shot.id !== 6 && shot.id !== 7
this.water.mesh.visible = showWater
if (showWater && this.heal < 3) this.water.renderReflection(this.renderer, this.scene, this.camera)
if (this.heal >= 5) {
// last resort: plain forward render straight to the canvas (tone mapping only)
this.renderer.setRenderTarget(null)
this.renderer.render(this.scene, this.camera)
return
}
this.post.composer.render()
}
}
// ---------------------------------------------------------------------------
// Debug helpers: category-ID render for automated composition checks.
const CAT_COLORS: Record<string, number> = {
S: 0x6080ff, M: 0x304060, T: 0x806040, F: 0xc0c040, W: 0x00c0ff, H: 0xffffff, R: 0x202020, D: 0x8b4513,
K: 0x909090, Y: 0xffff00, V: 0x00a000, L: 0xff0000, P: 0xff00ff, C: 0x0000a0, O: 0x00ffa0, X: 0x404040,
}
function catOf(mat: THREE.Material): string | null {
const key = typeof mat.customProgramCacheKey === 'function' ? mat.customProgramCacheKey() : ''
const k = key.replace(/^jn-/, '').replace(/[fn]$/, '')
const map: Record<string, string> = {
wall: 'H', roof: 'R', proof: 'R', dark: 'R', pdark: 'R', wood: 'D', pwood: 'D', stone: 'K', pstone: 'K', pave: 'K', embank: 'K',
window: 'Y', terrain: 'T', terrace: 'F', far: 'M', camphorLeaf: 'V', treeLeaf: 'V', willow: 'V', bambooLeaf: 'V', reed: 'V',
forest0: 'V', forest1: 'V', culm: 'V', bark: 'V', bark2: 'V', lantern: 'L', cloth: 'C', hull: 'O', canopy: 'O', pred: 'D',
fruit: 'X', basket: 'X', jar: 'X', meat: 'X', awning: 'X', bamboo: 'D', bar: 'D', flock: 'P',
}
if (map[k]) return map[k]
if (mat.type === 'MeshStandardMaterial') return 'P'
return null
}
export function renderIds(film: Film, t: number) {
film.renderFrame(t)
const swaps: [THREE.Mesh, THREE.Material | THREE.Material[], boolean][] = []
const basic: Record<string, THREE.Material> = {}
film.scene.traverse((o) => {
const m = o as THREE.Mesh
if (!m.isMesh && !(o as THREE.Points).isPoints) return
const mat = m.material as THREE.Material
let cat: string | null
if (o === film.sky.mesh) cat = 'S'
else if (o === film.water.mesh) cat = 'W'
else if ((o as THREE.Points).isPoints || (mat as THREE.ShaderMaterial).isShaderMaterial) cat = null
else cat = catOf(mat)
swaps.push([m, m.material, m.visible])
if (!cat) { m.visible = false; return }
if (!basic[cat]) basic[cat] = new THREE.MeshBasicMaterial({ color: CAT_COLORS[cat], side: THREE.DoubleSide, alphaTest: 0.45, map: null })
let bm = basic[cat]
const std = mat as THREE.MeshStandardMaterial
if (std.alphaTest > 0 && std.map) {
const mb = new THREE.MeshBasicMaterial({ color: CAT_COLORS[cat], side: THREE.DoubleSide, alphaTest: 0.45, map: std.map })
mb.onBeforeCompile = (sh) => {
sh.fragmentShader = sh.fragmentShader.replace('#include <map_fragment>', 'diffuseColor.a *= texture2D(map, vMapUv).a;')
}
bm = mb
}
m.material = bm
})
const prevTM = film.renderer.toneMapping
film.renderer.toneMapping = THREE.NoToneMapping
film.renderer.setRenderTarget(null)
const fog = film.scene.fog
film.scene.fog = null
film.renderer.render(film.scene, film.camera)
film.scene.fog = fog
film.renderer.toneMapping = prevTM
for (const [m, mat, vis] of swaps) { m.material = mat; m.visible = vis }
}