Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Let streamed parts share a renderer with retained textured scenes

Nguyen Xuan Anabb4503e5323
diff --git a/docs/guide.md b/docs/guide.md
index 0c07fc8d..7393d696 100644
--- a/docs/guide.md
+++ b/docs/guide.md
@@ -187,7 +187,7 @@ The [retarget contract](/retarget.md) covers rest-pose assumptions and measured
4. Each frame, call `renderer.replaceParts([...sceneParts, ...player.partsAt(seconds)])` and render.
The [Spume contract](/spume.md) covers the cache format, whitewater options, placement and measured cost.
-The scene must stay rigid and untextured while frames are replaced.
+Textured, skinned and morphed parts may stay in the list unchanged while frames are replaced; adding or removing them needs a new renderer.
## Wheeled vehicles
diff --git a/docs/spume.md b/docs/spume.md
index c804de63..9bb10195 100644
--- a/docs/spume.md
+++ b/docs/spume.md
@@ -69,8 +69,8 @@ When a frame holds more points than `maxPoints`, the addon thins them evenly acr
## Rendering contract
-The player returns new geometry for each new frame. Pass it through `renderer.replaceParts`, which requires the whole scene to stay rigid and untextured.
-Skinned, morphed or textured parts cannot share a renderer with Spume playback yet.
+The player returns new geometry for each new frame. Pass it through `renderer.replaceParts` with the rest of the scene.
+Textured, skinned and morphed parts (trees, characters, imported assets) can share the renderer as long as they stay in the list unchanged: their batches are retained and only new geometry is uploaded and validated. Adding or removing such parts still needs a new renderer.
Whitewater parts set `castShadow: false`; the liquid casts shadows.
The `liquid` and `foam` tags draw after the opaque scene. Tune them with `frame.liquid` or `environment.liquid`:
diff --git a/src/renderer.js b/src/renderer.js
index b0747491..af585d00 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -27,6 +27,10 @@ function renderPass(material) {
return TRANSLUCENT_TAGS.has(material.tag) ? material.tag : 'opaque';
}
+function partNeedsFullUpload(part) {
+ return Boolean(part.skin || part.morph || part.geometry.morphTargets || part.geometry.skinIndices || hasMaterialMap(part.material));
+}
+
function batchPass(batch) {
return batch.pass ?? 'opaque';
}
@@ -540,21 +544,12 @@ export class Renderer67 {
if (!Array.isArray(parts) || new Set(parts).size !== parts.length) {
throw new TypeError('Scene parts must be an array with distinct object identities.');
}
- for (const location of this.partLocations.values()) {
- if (location.skin || location.morph || location.morphTargets || location.skinIndices || location.mapState.slice(0, 5).some(Boolean)) {
- throw new Error('replaceParts currently requires an existing rigid, untextured scene.');
- }
- }
- for (const part of parts) {
- if (part.skin || part.morph || part.geometry.morphTargets || part.geometry.skinIndices || hasMaterialMap(part.material)) {
- throw new Error('replaceParts currently requires rigid, untextured parts.');
- }
- }
const renderParts = this.groundPart ? [...parts, this.groundPart] : parts;
const groups = groupParts(renderParts, this.batching);
const geometries = new Set(renderParts.map(part => part.geometry));
+ // Uploaded geometry was validated when it arrived; rechecking a large static scene every frame dominated streamed playback.
for (const geometry of geometries) {
- validateReplacementGeometry(geometry);
+ if (!this.geometryBuffers.has(geometry)) validateReplacementGeometry(geometry);
}
const retained = new Set();
const replacements = groups.map(group => {
@@ -574,6 +569,18 @@ export class Renderer67 {
}
return { group, batch: null };
});
+ // Textured, skinned and morphed batches stay valid only while retained unchanged; uploading or dropping them is not supported here.
+ for (const item of replacements) {
+ if (!item.batch && item.group.some(partNeedsFullUpload)) {
+ throw new Error('replaceParts can add or change only rigid, untextured parts. Keep textured, skinned and morphed parts unchanged so their batches are retained.');
+ }
+ }
+ for (const [part, location] of this.partLocations) {
+ if (retained.has(location.batch)) continue;
+ if (location.skin || location.morph || location.morphTargets || location.skinIndices || location.mapState.slice(0, 5).some(Boolean)) {
+ throw new Error(`replaceParts can remove or regroup only existing rigid, untextured parts; ${part.name} is textured, skinned or morphed and needs a new renderer.`);
+ }
+ }
const newGeometryBytes = [...geometries].filter(geometry => !this.geometryBuffers.has(geometry)).reduce((total, geometry) => total + geometryBufferBytes(geometry), 0);
const newInstanceBytes = replacements.filter(item => !item.batch).reduce((total, item) => total + item.group.length * INSTANCE_STRIDE * 4, 0);
const frameBytes = estimateFrameBytes(this.canvas.width, this.canvas.height, this.samples, 2048, { hdr: this.hdr, bloom: !this.benchmark, depth: this.depth });
diff --git a/src/shaders.js b/src/shaders.js
index 628f16bd..01af1c4e 100644
--- a/src/shaders.js
+++ b/src/shaders.js
@@ -562,7 +562,10 @@ void main() {
if (environmentEnabled > 0.5) {
radiance += environmentLighting(normal, viewDirection, color, metallic, roughness, occlusion, ambientIntensity);
} else {
- radiance += studio(reflect(-viewDirection, normal), roughness) * reflectance * (1.0 - roughness * 0.55) * mix(1.0, 0.5 * daylight, worldMode) * occlusion * ambientIntensity;
+ // Outdoors the surroundings are sky and haze; reflecting the studio softboxes there lit grazing leaves white and cyan.
+ vec3 reflected = reflect(-viewDirection, normal);
+ vec3 surroundings = worldMode > 0.5 ? mix(fogColor * 0.8, vec3(0.20, 0.38, 0.52) * 1.6, smoothstep(-0.15, 0.7, reflected.y)) : studio(reflected, roughness);
+ radiance += surroundings * reflectance * (1.0 - roughness * 0.55) * mix(1.0, 0.5 * daylight, worldMode) * occlusion * ambientIntensity;
radiance += color * (1.0 - metallic) * mix(vec3(0.16, 0.20, 0.22), vec3(0.19, 0.25, 0.30) * max(daylight, 0.08), worldMode) * (0.45 + 0.55 * max(normal.y, 0.0)) * occlusion * ambientIntensity;
}
vec3 keyResponse = keyColor / mix(vec3(5.2, 4.7, 4.1), vec3(3.7, 3.4, 2.9), worldMode);
diff --git a/tests/renderer-topology.test.js b/tests/renderer-topology.test.js
index d30374da..ea276401 100644
--- a/tests/renderer-topology.test.js
+++ b/tests/renderer-topology.test.js
@@ -143,6 +143,28 @@ test('driver errors and throwing uploads clean staged resources without retiring
assert.equal(state.renderer.batches[0], batch);
});
+test('retained textured batches survive while streamed rigid parts change around them', () => {
+ const state = allocationRenderer();
+ const textured = part('textured', box());
+ textured.material.map = { image: { width: 1, height: 1 }, colorSpace: 'srgb' };
+ textured.geometry.uvs = new Float32Array(textured.geometry.positions.length / 3 * 2);
+ state.renderer.batches.push(state.renderer.uploadBatch([textured]));
+ const texturedBatch = state.renderer.batches[0];
+ const firstFrame = part('liquid frame 0', sphere(12, 8));
+ state.renderer.replaceParts([textured, firstFrame]);
+ assert.equal(state.renderer.batches[0], texturedBatch);
+ const secondFrame = part('liquid frame 1', sphere(10, 6));
+ state.renderer.replaceParts([textured, secondFrame]);
+ assert.equal(state.renderer.batches[0], texturedBatch, 'the textured batch is retained, not reuploaded');
+ assert.equal(state.renderer.partCount, 2);
+ assert.throws(() => state.renderer.replaceParts([secondFrame]), /existing rigid, untextured/);
+ const changedTexture = part('new textured', box());
+ changedTexture.material.map = textured.material.map;
+ changedTexture.geometry.uvs = new Float32Array(changedTexture.geometry.positions.length / 3 * 2);
+ assert.throws(() => state.renderer.replaceParts([textured, secondFrame, changedTexture]), /only rigid, untextured/);
+ assert.equal(state.renderer.batches[0], texturedBatch);
+});
+
test('committed uploaded resource ownership rejects replacement even when callers mutate live handles', () => {
const state = allocationRenderer();
const first = part('first', box());