video / src /jiangnan /engine /assets.ts
Surf
暮归江南 · 步骤8 补充:扫描货物按镜头观感放大、摊位铺满、提升货物天光;README 记录 SimReady 目录调研结论与验证
a8359b3
Raw History Blame Contribute Delete
7.52 kB
// Scanned / artist-made props from NVIDIA SimReady Central (Palatial NV Asset Library, CC-BY-4.0),
// converted offline from USD to small GLBs (tools/jiangnan/usd2glb.py) and dressed onto the
// market stalls and quay as instanced meshes. The procedural goods stay as a fallback when the
// files cannot be loaded (offline / blocked), so the film never shows an empty stall.
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { patch } from './materials'
import { mulberry32 } from './rng'
import type { StallSpot } from './props'
export interface AssetDef {
id: string
name: string
/** where this prop may be placed */
slot: 'table' | 'ground' | 'shelf'
/** uniform scale applied to the real-world metres of the asset (1 = true size) */
scale?: number
/** how many of them per stall slot (random in [min,max]) */
count: [number, number]
}
/** Palatial NV Asset Library IDs (https://huggingface.co/datasets/PalatialSim/palatial-nv-asset-library), CC-BY-4.0 */
export const ASSETS: AssetDef[] = [
{ id: 'D00006', name: '竹篮', slot: 'table', count: [1, 2], scale: 1.3 },
{ id: 'D00016', name: '提篮', slot: 'table', count: [1, 2], scale: 1.3 },
{ id: 'D00323', name: '果篮', slot: 'table', count: [1, 2], scale: 1.4 },
{ id: 'D00395', name: '蛋篮', slot: 'table', count: [1, 1], scale: 1.4 },
{ id: 'D00952', name: '木碗', slot: 'table', count: [2, 4], scale: 1.5 },
{ id: 'D00854', name: '茶壶', slot: 'table', count: [1, 1], scale: 1.4 },
{ id: 'D00855', name: '茶杯', slot: 'table', count: [3, 5], scale: 1.5 },
{ id: 'D00945', name: '铁锅', slot: 'table', count: [1, 1], scale: 1.2 },
{ id: 'D00953', name: '木箱', slot: 'ground', count: [1, 2], scale: 1.3 },
{ id: 'D00954', name: '木盒', slot: 'table', count: [1, 1], scale: 1.3 },
{ id: 'D00902', name: '陶瓶', slot: 'table', count: [1, 3], scale: 1.5 },
{ id: 'D00645', name: '大白菜', slot: 'table', count: [3, 6], scale: 1.4 },
{ id: 'D00638', name: '茄子', slot: 'table', count: [4, 7], scale: 1.5 },
{ id: 'D00668', name: '米缸', slot: 'ground', count: [1, 2], scale: 1.6 },
{ id: 'D00839', name: '陶罐', slot: 'table', count: [1, 3], scale: 1.6 },
{ id: 'D00930', name: '面粉袋', slot: 'ground', count: [1, 3], scale: 1.8 },
{ id: 'D00479', name: '馒头', slot: 'table', count: [6, 10], scale: 1.6 },
]
export interface AssetsOut {
group: THREE.Group
loaded: string[]
failed: string[]
triangles: number
}
const base = () => new URL('models/', document.baseURI).href
async function loadGlb(loader: GLTFLoader, id: string) {
const g = await loader.loadAsync(`${base()}${id}.glb`)
const meshes: THREE.Mesh[] = []
g.scene.traverse((o) => { if ((o as THREE.Mesh).isMesh) meshes.push(o as THREE.Mesh) })
if (!meshes.length) throw new Error('empty')
// merge into one geometry (all our conversions bake transforms, so geometries are already world-space)
const geo = meshes.length === 1 ? meshes[0].geometry : meshes[0].geometry
const src = meshes[0].material as THREE.MeshStandardMaterial
const mat = patch(new THREE.MeshStandardMaterial({
map: src.map, normalMap: src.normalMap, roughnessMap: src.roughnessMap, metalnessMap: src.metalnessMap,
color: src.color, roughness: src.roughness, metalness: Math.min(src.metalness, 0.6),
}), { key: 'asset-' + id })
if (mat.map) mat.map.colorSpace = THREE.SRGBColorSpace
// the stalls stand in the shade of the house fronts at sunset: lift the sky light a little so
// the scanned goods read as warm objects rather than silhouettes
mat.envMapIntensity = 1.6
geo.computeBoundingBox()
return { geo, mat, box: geo.boundingBox! }
}
/**
* Load the catalogue and scatter the props over the stall spots. Resolves even when some
* (or all) files fail; the caller hides the procedural goods only for stalls that got assets.
*/
export async function createAssets(stalls: StallSpot[], onProgress?: (p: number) => void): Promise<AssetsOut> {
const group = new THREE.Group()
const out: AssetsOut = { group, loaded: [], failed: [], triangles: 0 }
const loader = new GLTFLoader()
const rng = mulberry32(2026)
const models = new Map<string, Awaited<ReturnType<typeof loadGlb>>>()
let done = 0
await Promise.all(ASSETS.map(async (a) => {
try { models.set(a.id, await loadGlb(loader, a.id)); out.loaded.push(a.id) } catch { out.failed.push(a.id) }
onProgress?.(++done / ASSETS.length)
}))
if (!models.size) return out
// plan instances: per stall, a handful of table props and ground props, never overlapping badly
const placements = new Map<string, THREE.Matrix4[]>()
const tmp = new THREE.Matrix4(), rot = new THREE.Matrix4(), scl = new THREE.Matrix4(), tr = new THREE.Matrix4()
const put = (id: string, stallM: THREE.Matrix4, x: number, y: number, z: number, yaw: number, s: number) => {
const m = models.get(id)!
// sit the asset on its bounding-box floor, centred on x/z
const cx = (m.box.min.x + m.box.max.x) / 2, cz = (m.box.min.z + m.box.max.z) / 2
tr.makeTranslation(x, y, z)
rot.makeRotationY(yaw)
scl.makeScale(s, s, s)
tmp.copy(stallM).multiply(tr).multiply(rot).multiply(scl).multiply(new THREE.Matrix4().makeTranslation(-cx, -m.box.min.y, -cz))
if (!placements.has(id)) placements.set(id, [])
placements.get(id)!.push(tmp.clone())
}
for (const st of stalls) {
st.dressed = true
const table = ASSETS.filter((a) => a.slot === 'table' && models.has(a.id))
const ground = ASSETS.filter((a) => a.slot === 'ground' && models.has(a.id))
// table top is 2.2 × 1.0 m at y = 0.82 (see props.ts)
const cells: [number, number][] = []
for (let i = -3; i <= 3; i++) for (let j = -1; j <= 1; j++) cells.push([i * 0.3, j * 0.3])
for (let k = cells.length - 1; k > 0; k--) { const r = Math.floor(rng() * (k + 1)); [cells[k], cells[r]] = [cells[r], cells[k]] }
// 3–4 kinds per stall, so each stall reads differently
const kinds = [...table].sort(() => rng() - 0.5).slice(0, 4 + Math.floor(rng() * 2))
let c = 0
for (const a of kinds) {
const n = a.count[0] + Math.floor(rng() * (a.count[1] - a.count[0] + 1))
for (let i = 0; i < n && c < cells.length; i++) {
const [x, z] = cells[c++]
put(a.id, st.m, x + (rng() - 0.5) * 0.08, 0.82, z + (rng() - 0.5) * 0.08, rng() * Math.PI * 2, (a.scale ?? 1) * (0.95 + rng() * 0.1))
}
}
// ground: beside / under the table, toward the street
const gk = [...ground].sort(() => rng() - 0.5).slice(0, 2)
let gx = -1.15
for (const a of gk) {
const n = a.count[0] + Math.floor(rng() * (a.count[1] - a.count[0] + 1))
for (let i = 0; i < n; i++) {
put(a.id, st.m, gx + rng() * 0.2, 0, 0.75 + rng() * 0.25, rng() * Math.PI * 2, (a.scale ?? 1) * (0.95 + rng() * 0.1))
gx += 0.55
if (gx > 1.2) break
}
}
}
for (const [id, ms] of placements) {
const m = models.get(id)!
const inst = new THREE.InstancedMesh(m.geo, m.mat, ms.length)
ms.forEach((mat, i) => inst.setMatrixAt(i, mat))
inst.instanceMatrix.needsUpdate = true
inst.castShadow = true
inst.receiveShadow = true
inst.layers.set(1) // main view only (too small to matter in the water reflection)
inst.frustumCulled = true
inst.computeBoundingSphere()
group.add(inst)
out.triangles += (m.geo.index ? m.geo.index.count / 3 : m.geo.attributes.position.count / 3) * ms.length
}
return out
}