Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Count a growing curve like a trimmed one so closed curves can grow (#701)

Nguyen Xuan An9a8ddf00e9a3
diff --git a/src/modeling.js b/src/modeling.js
index 6990dd41..1cced501 100644
--- a/src/modeling.js
+++ b/src/modeling.js
@@ -383,7 +383,7 @@ export function curveSetVertexCount(splines, options = {}) {
function planCurveSet(splines, {
radius = 0.05, taper, profile, resolution = 12, radialSegments = 8,
-} , labelFirst) {
+}, labelFirst, growing = false) {
radius = finite(radius, 'radius');
if (radius <= 0) throw new RangeError('radius must be positive and closed must be boolean.');
integer(resolution, 1, 64, 'resolution');
@@ -398,8 +398,11 @@ function planCurveSet(splines, {
const taperFactors = taperFrom(taper);
const vertexCount = resolved.reduce((sum, spline) => {
const samples = sampleCountOf(spline, resolution);
- const rings = spline.trim ? samples + 3 : samples + (spline.closed ? 1 : 0);
- return sum + rings * sides * 2 + (spline.closed && !spline.trim ? 0 : 2 * (sides + 1));
+ // A growing curve sweeps every state open, with the ring count of growthState and caps, trim or not.
+ const trimmed = Boolean(spline.trim) || growing;
+ const extraRings = growing ? (spline.closed ? 1 : 0) : (spline.trim ? 3 : (spline.closed ? 1 : 0));
+ const rings = samples + extraRings;
+ return sum + rings * sides * 2 + (spline.closed && !trimmed ? 0 : 2 * (sides + 1));
}, 0);
return { resolved, vertexCount, radius, profilePoints, taperFactors, radialSegments, resolution };
}
@@ -464,7 +467,7 @@ function growthState(plan, fraction) {
// length. The motion weight 0 to 1 of grow.name blends two neighbouring steps, so seeking equals playing.
function growingCurve(splines, options, label) {
const grow = growFrom(options.grow);
- const plan = planCurveSet(splines, options, label);
+ const plan = planCurveSet(splines, options, label, true);
if (plan.vertexCount * grow.steps > MAX_GROW_MORPH_VERTICES) {
throw new RangeError(`grow needs ${plan.vertexCount * grow.steps} morph vertices; at most ${MAX_GROW_MORPH_VERTICES}. Lower grow.steps, resolution or radialSegments.`);
}
@@ -493,7 +496,7 @@ export function growthWeights(steps, growth) {
export function curveGeometryVertexCount(value) {
const { splines, label } = splinesOfGeometry(value);
if (value.grow !== undefined) growFrom(value.grow);
- return planCurveSet(splines, value, label).vertexCount;
+ return planCurveSet(splines, value, label, value.grow !== undefined).vertexCount;
}
/**
diff --git a/tests/curve-splines.test.js b/tests/curve-splines.test.js
index 6980178f..0eb1cfba 100644
--- a/tests/curve-splines.test.js
+++ b/tests/curve-splines.test.js
@@ -260,3 +260,19 @@ test('grow rejects bad fields with what to pass', () => {
[{ name: 'vine', bogus: 1 }, /Unknown grow field: bogus/],
]) assert.throws(() => createAgentScene(still(grow)), pattern);
});
+
+test('a closed curve grows to full length with no trim, and its first and last states share a vertex count', () => {
+ const closedShapes = {
+ polygon: { points: [[0, 0, 0], [2, 0, 0], [2, 2, 0], [0, 2, 0]], closed: true },
+ star: { shape: { type: 'star' } },
+ };
+ for (const [name, spline] of Object.entries(closedShapes)) {
+ const { geometries, ...recipe } = still();
+ geometries[0] = { id: 'vine', type: 'curve', ...spline, radius: 0.02, radialSegments: 4, grow: { name: 'vine', steps: 4 } };
+ const geometry = createAgentScene({ ...recipe, geometries }).parts[0].geometry;
+ const vertexCount = geometry.positions.length;
+ assert.equal(geometry.morphTargets.length, 4, name);
+ assert.equal(geometry.morphTargets[0].positions.length, vertexCount, `${name} first state`);
+ assert.equal(geometry.morphTargets[3].positions.length, vertexCount, `${name} last state`);
+ }
+});