Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Take the renderer as a parameter in update-part-vertices

Nguyen Xuan An54686797570a
diff --git a/changes/430.md b/changes/430.md
index e1d2e5d0..2425d34f 100644
--- a/changes/430.md
+++ b/changes/430.md
@@ -1,2 +1,2 @@
- Cloth steps live: `createCloth(positions, triangles, options)` returns a stepper with `step(dt)`, runtime `pin`/`unpin` with moving targets, `pinForce`, per-particle extra mass and tearing (`tearStretch`, `tearForce`, a readable `broken` list). `bakeCloth` is now a loop over it and records the same frames as before (closes #430).
-- New addon `@codepawlhq/67js/addons/update-part-vertices` adds `renderer.updatePartVertices(part, positions, normals)`, which rewrites a rigid part's vertex buffers with `bufferSubData` instead of `replaceParts`. The core bundle does not change.
+- New addon `@codepawlhq/67js/addons/update-part-vertices` exports `updatePartVertices(renderer, part, positions, normals)`, which rewrites a rigid part's vertex buffers with `bufferSubData` instead of `replaceParts`. The core bundle does not change.
diff --git a/docs/physics.md b/docs/physics.md
index 008a20d2..fcb2ce96 100644
--- a/docs/physics.md
+++ b/docs/physics.md
@@ -503,12 +503,12 @@ function frame(deltaSeconds) {
## Deforming a drawn part in place
-`renderer.updatePartVertices(part, positions, normals)` writes new vertex positions, and normals when given, into the buffers a rigid part already uses, with `bufferSubData`. It needs no `replaceParts`, no new geometry object and no new draw batch. Import `@codepawlhq/67js/addons/update-part-vertices` first; a scene that never deforms a mesh does not carry the code.
+`updatePartVertices(renderer, part, positions, normals)` writes new vertex positions, and normals when given, into the buffers a rigid part already uses, with `bufferSubData`. It needs no `replaceParts`, no new geometry object and no new draw batch. It comes from `@codepawlhq/67js/addons/update-part-vertices`, takes the renderer as its first argument and imports nothing from the renderer, so a scene that never deforms a mesh does not carry it.
```js
-import '@codepawlhq/67js/addons/update-part-vertices';
+import { updatePartVertices } from '@codepawlhq/67js/addons/update-part-vertices';
-renderer.updatePartVertices(sheetPart, sheetPositions, sheetNormals);
+updatePartVertices(renderer, sheetPart, sheetPositions, sheetNormals);
renderer.render(frame);
```
diff --git a/src/update-part-vertices.js b/src/update-part-vertices.js
index 8833887b..dd102b9b 100644
--- a/src/update-part-vertices.js
+++ b/src/update-part-vertices.js
@@ -1,8 +1,7 @@
-// Gives the renderer updatePartVertices: new vertex positions (and normals) for a part that already drew, written into
-// its existing GPU buffers with bufferSubData. A deforming mesh such as cloth skips replaceParts, which would build a
-// new geometry object and upload every buffer again. Importing this module adds the method to the renderer class (the
-// pattern of texture-update.js), so a scene that never deforms a mesh does not carry the code.
-import { Renderer67 } from './renderer.js';
+// updatePartVertices(renderer, part, positions, normals): new vertex positions (and normals) for a part that already
+// drew, written into its existing GPU buffers with bufferSubData. A deforming mesh such as cloth skips replaceParts,
+// which would build a new geometry object and upload every buffer again. The renderer is a parameter, so this module
+// imports nothing from the renderer and a scene that never deforms a mesh does not carry it.
function checkVertices(name, values, expectedLength, label) {
if (!(values instanceof Float32Array) || values.length !== expectedLength) {
@@ -35,7 +34,7 @@ function boundingSphereOf(positions) {
* geometry is shared by every part that uses it. Skinned, morphed, crowd and level-of-detail parts are refused.
* Returns the part.
*/
-export function updateRendererPartVertices(renderer, part, positions, normals) {
+export function updatePartVertices(renderer, part, positions, normals) {
const location = renderer.partLocations.get(part);
if (!location) throw new Error(`Unknown part identity: ${part?.name}`);
const geometry = location.geometry;
@@ -57,7 +56,3 @@ export function updateRendererPartVertices(renderer, part, positions, normals) {
renderer.shadowsDirty = true;
return part;
}
-
-Renderer67.prototype.updatePartVertices = function updatePartVertices(part, positions, normals) {
- return updateRendererPartVertices(this, part, positions, normals);
-};
diff --git a/tests/update-part-vertices.test.js b/tests/update-part-vertices.test.js
index 8342b1cc..17ba6093 100644
--- a/tests/update-part-vertices.test.js
+++ b/tests/update-part-vertices.test.js
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { Renderer67 } from '../src/renderer.js';
-import '../src/update-part-vertices.js';
+import { updatePartVertices } from '../src/update-part-vertices.js';
/** A WebGL2 stand-in that records buffer writes, with one rigid part already uploaded. */
function fakeRenderer() {
@@ -33,7 +33,7 @@ test('updatePartVertices rewrites the drawn buffers without a new batch or geome
const batchesBefore = batch;
const positions = new Float32Array([0, 0, 5, 1, 0, 5, 0, 1, 5]);
const normals = new Float32Array([1, 0, 0, 1, 0, 0, 1, 0, 0]);
- renderer.updatePartVertices(part, positions, normals);
+ updatePartVertices(renderer, part, positions, normals);
assert.deepEqual(calls, [
['bindBuffer', 'positionBuffer'], ['bufferSubData', 0, Array.from(positions)],
['bindBuffer', 'normalBuffer'], ['bufferSubData', 0, Array.from(normals)],
@@ -47,15 +47,15 @@ test('updatePartVertices rewrites the drawn buffers without a new batch or geome
test('updatePartVertices leaves normals alone when none are given', () => {
const { renderer, part, calls } = fakeRenderer();
- renderer.updatePartVertices(part, new Float32Array(9));
+ updatePartVertices(renderer, part, new Float32Array(9));
assert.deepEqual(calls.map(call => call[0]), ['bindBuffer', 'bufferSubData']);
});
test('updatePartVertices refuses a changed vertex count, an unknown part and a skinned part', () => {
const { renderer, part } = fakeRenderer();
- assert.throws(() => renderer.updatePartVertices(part, new Float32Array(12)), /3 per vertex/);
- assert.throws(() => renderer.updatePartVertices(part, [0, 0, 0, 0, 0, 0, 0, 0, 0]), TypeError);
- assert.throws(() => renderer.updatePartVertices({ name: 'other' }, new Float32Array(9)), /Unknown part/);
+ assert.throws(() => updatePartVertices(renderer, part, new Float32Array(12)), /3 per vertex/);
+ assert.throws(() => updatePartVertices(renderer, part, [0, 0, 0, 0, 0, 0, 0, 0, 0]), TypeError);
+ assert.throws(() => updatePartVertices(renderer, { name: 'other' }, new Float32Array(9)), /Unknown part/);
renderer.partLocations.get(part).skin = {};
- assert.throws(() => renderer.updatePartVertices(part, new Float32Array(9)), /rigid/);
+ assert.throws(() => updatePartVertices(renderer, part, new Float32Array(9)), /rigid/);
});