Skip to content

3D engine that AI agents drive

Add transform_selection: rotate and scale a selection about a pivot with falloff (#2141)

Nguyen Xuan An7b61bfa3d52e
diff --git a/changes/2141.md b/changes/2141.md
new file mode 100644
index 00000000..998e7ff4
--- /dev/null
+++ b/changes/2141.md
@@ -0,0 +1 @@
+- `mesh_edit` and recipe edits gain `transform_selection`: rotate (axis and angle in radians) and scale (one factor or per axis) a selection about a pivot, default the selection centre, with the same falloff as `translate` (#2141)
diff --git a/changes/2141.size.md b/changes/2141.size.md
new file mode 100644
index 00000000..f5233288
--- /dev/null
+++ b/changes/2141.size.md
@@ -0,0 +1 @@
+- scene:agent-scene+lazy: transform_selection adds rotate and scale about a pivot with falloff to the agent scene bundle (about 700 bytes gzip against main, part of it the five ops of #2126 that this PR stacks on); without it agents hand-write per-vertex set_position expressions.
diff --git a/docs/mesh.md b/docs/mesh.md
index 134a3e89..861ab86e 100644
--- a/docs/mesh.md
+++ b/docs/mesh.md
@@ -138,6 +138,7 @@ Edits (names follow Blender's `bmesh.ops` where they match):
| `delete` | `select` (required) | |
| `flip_normals` | `select` | |
| `translate` | `select`, `offset` [x, y, z], optional `falloff` `{ radius, curve, connected }` | |
+| `transform_selection` | `select`, optional `pivot` [x, y, z] (default: mean of the selected vertices), `rotation` `{ axis, angle }` (angle in radians), `scale` (above 0, up to 1000; one number or [x, y, z]), the same `falloff` as `translate` | Needs `rotation` or `scale`. Scales, then rotates about the pivot. With `falloff`, a vertex follows by its curve weight: it turns by weight times the angle and scales by 1 + weight times (scale - 1). |
| `merge_by_distance` | `distance` (default 1e-4) | |
| `tag` | `select`, `name` (required) | `<name>` |
| `boolean` | `operation` (`union`, `difference`, `intersect`), `cutter`, optional `translation`, `rotation` (XYZW), `scale`, `name` | `<name>.cut` |
diff --git a/docs/parity.md b/docs/parity.md
index e395914e..c3986fb2 100644
--- a/docs/parity.md
+++ b/docs/parity.md
@@ -122,7 +122,7 @@ To move to a new Blender release, run `node scripts/parity-manual-snapshot.mjs`.
| Modeling > Modifiers > Physics > Soft Body Modifier | missing | Not built: a closed mesh needs volume or shape-matching constraints, closed-mesh input checks and a recipe field with schema and capability text, which was not measured to fit the 1.5 KB gzip limit. | | #62 |
| Modeling > Modifiers > Geometry Nodes Modifier | partial | Bounded JSON field expressions drive `set_position`, `displace`, a `field` selection rule and `instances` (including curve points by count or length, fillet and `realize`); they are one language, `src/expression.js`, with motion drivers. There is no node graph, no loops, no custom node groups, and no volume nodes. | [src/mesh-fields.js](../src/mesh-fields.js), [tests/mesh-fields.test.js](../tests/mesh-fields.test.js), [tests/blender-golden.test.js](../tests/blender-golden.test.js), [pillar-court task](../bench/agent-modeling/tasks.mjs) | #43, #698 |
| Modeling > Geometry Nodes | partial | Fields over attributes, noise and math (position, normal, index, tags), Set Position, Instance on Points (grid, scatter, surface, curve, with Poisson spacing, density, variants, rotation and scale fields) and Displace. Noise is not Blender's noise function, and there is no Blender comparison for instancing. Issue #43 chose bounded JSON expressions and not a visual editor. | [src/mesh-fields.js](../src/mesh-fields.js), [tests/mesh-fields.test.js](../tests/mesh-fields.test.js), [tests/blender-golden.test.js](../tests/blender-golden.test.js) | #43 |
-| Modeling > Transform | partial | Node transforms, and `translate` on a selection with falloff. There is no rotate or scale on a selection. | | |
+| Modeling > Transform | partial | Node transforms, and `translate` and `transform_selection` (rotate and scale about a pivot) on a selection, both with falloff. | | |
| Sculpting & Painting > Brushes | out of scope | Brush settings for a hand. Strokes have explicit parameters. | | |
| Sculpting & Painting > Selection & Visibility | out of scope | Interface for a person in a paint mode. | | |
| Sculpting & Painting > Navigating in Paint Modes | out of scope | Interface for a person in a paint mode. | | |
diff --git a/scripts/mcp-server.mjs b/scripts/mcp-server.mjs
index 1a1f0230..bc1d2b34 100644
--- a/scripts/mcp-server.mjs
+++ b/scripts/mcp-server.mjs
@@ -394,6 +394,7 @@ const meshEdit = z.discriminatedUnion('op', [
z.strictObject({ op: z.literal('delete'), select: selectRule }),
z.strictObject({ op: z.literal('flip_normals'), select: selectRule.optional() }),
z.strictObject({ op: z.literal('translate'), select: selectRule.optional(), offset: vector3, falloff: z.strictObject({ radius: finite, curve: z.enum(['smooth', 'sphere', 'root', 'sharp', 'linear', 'constant', 'inverseSquare']).optional(), connected: z.boolean().optional().describe('Measure distance along edges instead of straight lines.') }).optional() }),
+ z.strictObject({ op: z.literal('transform_selection'), select: selectRule.optional(), pivot: vector3.optional().describe('Point to turn and scale about. Defaults to the mean of the selected vertices.'), rotation: z.strictObject({ axis: vector3, angle: bounded(-6.283185307179586, 6.283185307179586).describe('Radians, not degrees: 90 degrees is 1.5708.') }).optional(), scale: z.union([bounded(0.000001, 1000), z.array(bounded(0.000001, 1000)).length(3)]).optional().describe('Factor above 0, or one per axis; applied before the rotation.'), falloff: z.strictObject({ radius: finite, curve: z.enum(['smooth', 'sphere', 'root', 'sharp', 'linear', 'constant', 'inverseSquare']).optional(), connected: z.boolean().optional().describe('Measure distance along edges instead of straight lines.') }).optional() }).describe('Rotate and/or scale the selected vertices about a pivot; falloff fades the move to nothing at its radius.'),
z.strictObject({ op: z.literal('merge_by_distance'), distance: finite.optional() }),
z.strictObject({
op: z.literal('loop_cut'), name: editName,
diff --git a/src/mesh-operations.js b/src/mesh-operations.js
index 6a86c588..51ecfb93 100644
--- a/src/mesh-operations.js
+++ b/src/mesh-operations.js
@@ -35,6 +35,7 @@ export const MESH_EDIT_OPERATIONS = /* @__PURE__ */ Object.freeze({
delete: { fields: ['select'], tags: [] },
flip_normals: { fields: ['select'], tags: [] },
translate: { fields: ['select', 'offset', 'falloff'], tags: [] },
+ transform_selection: { fields: ['select', 'pivot', 'rotation', 'scale', 'falloff'], tags: [] },
merge_by_distance: { fields: ['distance'], tags: [] },
tag: { fields: ['select', 'name'], tags: [] },
boolean: { fields: ['name', 'operation', 'cutter', 'translation', 'rotation', 'scale'], tags: ['cut'] },
diff --git a/src/mesh-ops.js b/src/mesh-ops.js
index 2f5a0bca..0400d027 100644
--- a/src/mesh-ops.js
+++ b/src/mesh-ops.js
@@ -572,23 +572,71 @@ function spatialDistances(edit, seeds, radius, maxVisits) {
* vertices follow with a weight from Blender's proportional-editing curves (distance along edges when connected).
*/
export function translateFaces(edit, mask, { offset, falloff }, { maxFalloffVisits = 50_000_000 } = {}) {
- const seeds = faceVertexSet(edit, maskedFaces(edit, mask));
- if (!falloff) {
- moveVertices(edit, seeds, offset);
- return { movedVertices: seeds.size };
+ const weights = selectionWeights(edit, mask, falloff, maxFalloffVisits);
+ const position = edit.vertices.arrays.position;
+ for (const [vertex, weight] of weights) {
+ position[vertex * 3] += offset[0] * weight;
+ position[vertex * 3 + 1] += offset[1] * weight;
+ position[vertex * 3 + 2] += offset[2] * weight;
}
+ return { movedVertices: weights.size };
+}
+
+/** Weight of every vertex a selection moves: 1 on the selected vertices, then the falloff curve out to its radius. */
+function selectionWeights(edit, mask, falloff, maxFalloffVisits) {
+ const seeds = faceVertexSet(edit, maskedFaces(edit, mask));
+ if (!falloff) return new Map([...seeds].map(vertex => [vertex, 1]));
const curve = FALLOFF_CURVES[falloff.curve ?? 'smooth'];
const distances = falloff.connected
? connectedDistances(edit, seeds, falloff.radius)
: spatialDistances(edit, seeds, falloff.radius, maxFalloffVisits);
+ const weights = new Map();
+ for (const [vertex, distance] of distances) weights.set(vertex, seeds.has(vertex) ? 1 : curve(distance / falloff.radius));
+ return weights;
+}
+
+/** Mean position of the vertices of the selected faces: the default pivot of transformFaces. */
+function selectionCentre(edit, mask) {
+ const vertices = faceVertexSet(edit, maskedFaces(edit, mask));
const position = edit.vertices.arrays.position;
- for (const [vertex, distance] of distances) {
- const weight = seeds.has(vertex) ? 1 : curve(distance / falloff.radius);
- position[vertex * 3] += offset[0] * weight;
- position[vertex * 3 + 1] += offset[1] * weight;
- position[vertex * 3 + 2] += offset[2] * weight;
+ const centre = [0, 0, 0];
+ for (const vertex of vertices) {
+ for (let axis = 0; axis < 3; axis++) centre[axis] += position[vertex * 3 + axis] / vertices.size;
+ }
+ return centre;
+}
+
+/** Rotate `point` about the unit `axis` by `angle` radians (Rodrigues' formula). */
+function rotateAboutAxis(point, axis, angle) {
+ const cosine = Math.cos(angle);
+ const sine = Math.sin(angle);
+ const along = axis[0] * point[0] + axis[1] * point[1] + axis[2] * point[2];
+ const cross = [
+ axis[1] * point[2] - axis[2] * point[1],
+ axis[2] * point[0] - axis[0] * point[2],
+ axis[0] * point[1] - axis[1] * point[0],
+ ];
+ return point.map((component, index) => component * cosine + cross[index] * sine + axis[index] * along * (1 - cosine));
+}
+
+/**
+ * Rotate and scale the vertices of the selected faces about `pivot` (default: their mean position). The rotation
+ * `{ axis, angle }` turns by `angle` radians about `axis`; `scale` is one factor or one per axis, applied before the
+ * rotation. With `falloff`, nearby vertices follow by the curve weight: they turn by weight * angle and scale by
+ * 1 + weight * (scale - 1), so the edit fades to nothing at the radius.
+ */
+export function transformFaces(edit, mask, { pivot, rotation, scale, falloff }, { maxFalloffVisits = 50_000_000 } = {}) {
+ const weights = selectionWeights(edit, mask, falloff, maxFalloffVisits);
+ const centre = pivot ?? selectionCentre(edit, mask);
+ const axis = rotation ? normalize(rotation.axis) : null;
+ const factors = scale === undefined ? [1, 1, 1] : (Array.isArray(scale) ? scale : [scale, scale, scale]);
+ const position = edit.vertices.arrays.position;
+ for (const [vertex, weight] of weights) {
+ const offsetFromPivot = [0, 1, 2].map(component => (position[vertex * 3 + component] - centre[component]) * (1 + weight * (factors[component] - 1)));
+ const turned = axis ? rotateAboutAxis(offsetFromPivot, axis, rotation.angle * weight) : offsetFromPivot;
+ for (let component = 0; component < 3; component++) position[vertex * 3 + component] = centre[component] + turned[component];
}
- return { movedVertices: distances.size };
+ return { movedVertices: weights.size };
}
/**
diff --git a/src/mesh-recipe.js b/src/mesh-recipe.js
index 19682097..94f51f62 100644
--- a/src/mesh-recipe.js
+++ b/src/mesh-recipe.js
@@ -3,7 +3,7 @@ import { invalidField } from './field-error.js';
import { editField, editBoolean } from './mesh-edit-field.js';
import { countSelected, selectFaces } from './mesh-select.js';
import {
- extrudeFaceRegion, extrudeIndividual, insetRegion, insetIndividual, deleteFaces, flipFaces, translateFaces, mergeByDistance, tagFaces,
+ extrudeFaceRegion, extrudeIndividual, insetRegion, insetIndividual, deleteFaces, flipFaces, translateFaces, transformFaces, mergeByDistance, tagFaces,
FALLOFF_CURVES,
} from './mesh-ops.js';
import { meshBoolean, BOOLEAN_OPERATIONS } from './mesh-boolean.js';
@@ -115,6 +115,56 @@ function normalizeStroke(stroke, index, strokeIndex) {
return normalized;
}
+function normalizeFalloff(falloff, index) {
+ if (!falloff || typeof falloff !== 'object') throw editField(index, 'falloff', falloff, 'an object such as { "radius": 1, "curve": "smooth", "connected": false }');
+ for (const key of Object.keys(falloff)) {
+ if (!['radius', 'curve', 'connected'].includes(key)) throw editField(index, `falloff.${key}`, falloff[key], `one of the falloff fields radius, curve, connected; remove "${key}"`);
+ }
+ const curve = falloff.curve ?? 'smooth';
+ if (!Object.hasOwn(FALLOFF_CURVES, curve)) throw editField(index, 'falloff.curve', curve, `one of ${Object.keys(FALLOFF_CURVES).join(', ')}`);
+ if (falloff.connected !== undefined && typeof falloff.connected !== 'boolean') throw editField(index, 'falloff.connected', falloff.connected, 'true or false');
+ return {
+ radius: finiteNumber(falloff.radius, index, 'falloff.radius', { minimum: 0, exclusiveMinimum: true }),
+ curve, connected: falloff.connected ?? false,
+ };
+}
+
+const MAX_TRANSFORM_SCALE = 1000;
+
+function normalizeTransformRotation(rotation, index) {
+ if (!rotation || typeof rotation !== 'object' || Array.isArray(rotation)) throw editField(index, 'rotation', rotation, 'an object such as { "axis": [0, 1, 0], "angle": 1.5708 } (angle in radians)');
+ for (const key of Object.keys(rotation)) {
+ if (!['axis', 'angle'].includes(key)) throw editField(index, `rotation.${key}`, rotation[key], `one of the rotation fields axis, angle; remove "${key}"`);
+ }
+ const axis = vector(rotation.axis, index, 'rotation.axis');
+ if (axis.every(component => component === 0)) throw editField(index, 'rotation.axis', axis, 'a direction that is not all zeros, such as [0, 1, 0]');
+ const angle = finiteNumber(rotation.angle, index, 'rotation.angle', { minimum: -2 * Math.PI, maximum: 2 * Math.PI });
+ return { axis, angle };
+}
+
+function normalizeTransformScale(scale, index) {
+ const bounds = { minimum: 0, maximum: MAX_TRANSFORM_SCALE, exclusiveMinimum: true };
+ if (!Array.isArray(scale)) return finiteNumber(scale, index, 'scale', bounds);
+ if (scale.length !== 3) throw editField(index, 'scale', scale, 'one number above 0, or an array of three, such as 0.5 or [1, 2, 1]');
+ return scale.map(component => finiteNumber(component, index, 'scale', bounds));
+}
+
+// rotation.angle is radians; a value in degrees is the usual slip, so the bound error says so.
+function normalizeTransformSelection(edit, normalized, index) {
+ if (edit.rotation === undefined && edit.scale === undefined) throw editField(index, 'rotation', undefined, 'a rotation such as { "axis": [0, 1, 0], "angle": 0.5 } (radians; 90 degrees is 1.5708), a scale such as 0.5, or both');
+ if (edit.pivot !== undefined) normalized.pivot = vector(edit.pivot, index, 'pivot');
+ if (edit.rotation !== undefined) {
+ try {
+ normalized.rotation = normalizeTransformRotation(edit.rotation, index);
+ } catch (error) {
+ if (error.fieldPath?.endsWith('rotation.angle')) error.message += ' The angle is in radians (90 degrees is 1.5708), not degrees.';
+ throw error;
+ }
+ }
+ if (edit.scale !== undefined) normalized.scale = normalizeTransformScale(edit.scale, index);
+ if (edit.falloff !== undefined) normalized.falloff = normalizeFalloff(edit.falloff, index);
+}
+
/** Validate an edit list without running it. Returns normalized copies with default names filled in. */
export function normalizeMeshEdits(edits) {
if (!Array.isArray(edits)) throw invalidField('edits', edits, 'an array of edits, such as [{ "op": "subdivide", "levels": 1 }]', TypeError);
@@ -154,20 +204,10 @@ export function normalizeMeshEdits(edits) {
break;
case 'translate':
normalized.offset = vector(edit.offset, index, 'offset');
- if (edit.falloff !== undefined) {
- const falloff = edit.falloff;
- if (!falloff || typeof falloff !== 'object') throw editField(index, 'falloff', falloff, 'an object such as { "radius": 1, "curve": "smooth", "connected": false }');
- for (const key of Object.keys(falloff)) {
- if (!['radius', 'curve', 'connected'].includes(key)) throw editField(index, `falloff.${key}`, falloff[key], `one of the falloff fields radius, curve, connected; remove "${key}"`);
- }
- const curve = falloff.curve ?? 'smooth';
- if (!Object.hasOwn(FALLOFF_CURVES, curve)) throw editField(index, 'falloff.curve', curve, `one of ${Object.keys(FALLOFF_CURVES).join(', ')}`);
- if (falloff.connected !== undefined && typeof falloff.connected !== 'boolean') throw editField(index, 'falloff.connected', falloff.connected, 'true or false');
- normalized.falloff = {
- radius: finiteNumber(falloff.radius, index, 'falloff.radius', { minimum: 0, exclusiveMinimum: true }),
- curve, connected: falloff.connected ?? false,
- };
- }
+ if (edit.falloff !== undefined) normalized.falloff = normalizeFalloff(edit.falloff, index);
+ break;
+ case 'transform_selection':
+ normalizeTransformSelection(edit, normalized, index);
break;
case 'boolean': {
if (!BOOLEAN_OPERATIONS.includes(edit.operation)) throw editField(index, 'operation', edit.operation, `one of ${BOOLEAN_OPERATIONS.join(', ')}`);
@@ -631,6 +671,9 @@ function runEdit(current, edit, index, { resolveCutter, limits }) {
case 'translate':
result.movedVertices = translateFaces(editable, mask, edit).movedVertices;
break;
+ case 'transform_selection':
+ result.movedVertices = transformFaces(editable, mask, edit).movedVertices;
+ break;
case 'tag':
tagFaces(editable, Array.from(mask.keys()).filter(face => mask[face]), edit.name);
result.tags = { [edit.name]: selected };
diff --git a/tests/mcp-mesh-edit-ops.test.js b/tests/mcp-mesh-edit-ops.test.js
index b6d9bf41..7155d899 100644
--- a/tests/mcp-mesh-edit-ops.test.js
+++ b/tests/mcp-mesh-edit-ops.test.js
@@ -60,6 +60,7 @@ const OP_CASES = {
circularize: { prelude: [{ op: 'loop_cut', name: 'ring', edges: { along: [0, 1, 0] } }], edit: { op: 'circularize', verts: { inside: { min: [-1, -0.01, -1], max: [1, 0.01, 1] } } } },
unsubdivide: { prelude: [{ op: 'subdivide', levels: 1 }], edit: { op: 'unsubdivide' } },
delete_loose: { prelude: [], edit: { op: 'delete_loose' }, engineError: /no loose vertex/ },
+ transform_selection: { prelude: [], edit: { op: 'transform_selection', select: { tag: 'top' }, rotation: { axis: [0, 0, 1], angle: 0.5 }, scale: [1, 2, 1], falloff: { radius: 2, curve: 'linear' } } },
};
function recipeMeshReport(edits) {
diff --git a/tests/mesh-transform-selection.test.js b/tests/mesh-transform-selection.test.js
new file mode 100644
index 00000000..8f38aec8
--- /dev/null
+++ b/tests/mesh-transform-selection.test.js
@@ -0,0 +1,111 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { box } from '../src/geometry.js';
+import { meshFromGeometry, applyMeshEdits } from '../src/mesh.js';
+import { normalizeMeshEdits, MESH_EDIT_OPERATIONS } from '../src/mesh-recipe.js';
+
+const cube = () => meshFromGeometry(box(0, 1));
+
+function positionsOf(mesh) {
+ return Array.from({ length: mesh.vertexCount }, (_, vertex) => [...mesh.positions.subarray(vertex * 3, vertex * 3 + 3)]);
+}
+
+function assertClose(actual, expected, message) {
+ actual.forEach((component, axis) => assert.ok(Math.abs(component - expected[axis]) < 1e-5, `${message}: got [${actual}] expected [${expected}]`));
+}
+
+// The cube spans -0.5 to 0.5; its top face is the four vertices at y = 0.5.
+function transformTop(extra) {
+ const before = positionsOf(cube());
+ const { mesh, results } = applyMeshEdits(cube(), [{ op: 'transform_selection', select: { tag: 'top' }, ...extra }]);
+ return { before, after: positionsOf(mesh), results };
+}
+
+test('transform_selection is listed with its fields', () => {
+ assert.deepEqual(MESH_EDIT_OPERATIONS.transform_selection.fields, ['select', 'pivot', 'rotation', 'scale', 'falloff']);
+});
+
+test('rotating the top 90 degrees about the x axis through the origin moves known vertices to known positions', () => {
+ const { before, after } = transformTop({ pivot: [0, 0, 0], rotation: { axis: [1, 0, 0], angle: Math.PI / 2 } });
+ before.forEach((point, vertex) => {
+ // A top vertex (x, 0.5, z) turns to (x, -z, 0.5); the others stay put.
+ const expected = point[1] > 0 ? [point[0], -point[2], 0.5] : point;
+ assertClose(after[vertex], expected, `vertex ${vertex}`);
+ });
+});
+
+test('the default pivot is the centre of the selection, so rotating the top 90 degrees about y keeps its centre and unselected vertices still', () => {
+ const { before, after } = transformTop({ rotation: { axis: [0, 1, 0], angle: Math.PI / 2 } });
+ before.forEach((point, vertex) => {
+ // A top vertex (x, 0.5, z) about the axis through (0, 0.5, 0) turns to (z, 0.5, -x).
+ assertClose(after[vertex], point[1] > 0 ? [point[2], 0.5, -point[0]] : point, `vertex ${vertex}`);
+ });
+});
+
+test('scale about a given pivot and about the default centre leaves unselected vertices alone', () => {
+ const aboutPivot = transformTop({ pivot: [1, 0.5, 0], scale: 2 });
+ aboutPivot.before.forEach((point, vertex) => {
+ assertClose(aboutPivot.after[vertex], point[1] > 0 ? [1 + (point[0] - 1) * 2, 0.5, point[2] * 2] : point, `pivot vertex ${vertex}`);
+ });
+ const aboutCentre = transformTop({ scale: [2, 1, 0.5] });
+ aboutCentre.before.forEach((point, vertex) => {
+ assertClose(aboutCentre.after[vertex], point[1] > 0 ? [point[0] * 2, 0.5, point[2] * 0.5] : point, `centre vertex ${vertex}`);
+ });
+ assert.equal(aboutCentre.results[0].movedVertices, 4);
+});
+
+test('rotation and scale together scale first, then rotate', () => {
+ const { before, after } = transformTop({ scale: 2, rotation: { axis: [0, 1, 0], angle: Math.PI / 2 } });
+ before.forEach((point, vertex) => {
+ assertClose(after[vertex], point[1] > 0 ? [point[2] * 2, 0.5, -point[0] * 2] : point, `vertex ${vertex}`);
+ });
+});
+
+test('falloff gives vertices inside the radius a smooth fraction and leaves the radius edge and beyond untouched', () => {
+ // The bottom vertices sit 1 below the top. A linear falloff of radius 2 gives them weight 0.5, so scale 3 becomes 2.
+ const inside = transformTop({ pivot: [0, 0.5, 0], scale: 3, falloff: { radius: 2, curve: 'linear' } });
+ inside.before.forEach((point, vertex) => {
+ const expected = point[1] > 0 ? [point[0] * 3, 0.5, point[2] * 3] : [point[0] * 2, 0.5 + (point[1] - 0.5) * 2, point[2] * 2];
+ assertClose(inside.after[vertex], expected, `inside vertex ${vertex}`);
+ });
+ assert.equal(inside.results[0].movedVertices, 8);
+ // At exactly the radius the weight is 0, and a vertex past it is not reached.
+ const edge = transformTop({ rotation: { axis: [0, 0, 1], angle: 1 }, falloff: { radius: 1, curve: 'linear' } });
+ edge.before.forEach((point, vertex) => {
+ if (point[1] < 0) assertClose(edge.after[vertex], point, `edge vertex ${vertex}`);
+ });
+ const outside = transformTop({ rotation: { axis: [0, 0, 1], angle: 1 }, falloff: { radius: 0.5, curve: 'linear' } });
+ assert.equal(outside.results[0].movedVertices, 4);
+ outside.before.forEach((point, vertex) => {
+ if (point[1] < 0) assertClose(outside.after[vertex], point, `outside vertex ${vertex}`);
+ });
+});
+
+test('falloff rotates by the weighted angle', () => {
+ const { before, after } = transformTop({ pivot: [0, 0.5, 0], rotation: { axis: [0, 1, 0], angle: Math.PI }, falloff: { radius: 2, curve: 'linear' } });
+ before.forEach((point, vertex) => {
+ // Bottom vertices have weight 0.5, so they turn a quarter turn about the vertical axis through the pivot: (x, z) to (z, -x).
+ assertClose(after[vertex], point[1] > 0 ? [-point[0], 0.5, -point[2]] : [point[2], point[1], -point[0]], `vertex ${vertex}`);
+ });
+});
+
+test('bad fields throw errors that name the field and the fix', () => {
+ const edit = extra => () => normalizeMeshEdits([{ op: 'transform_selection', ...extra }]);
+ assert.throws(edit({}), /edits\[0\]\.rotation.*radians/s);
+ assert.throws(edit({ scale: 0 }), /edits\[0\]\.scale.*above 0/s);
+ assert.throws(edit({ scale: -1 }), /edits\[0\]\.scale/);
+ assert.throws(edit({ scale: [1, 0, 1] }), /edits\[0\]\.scale/);
+ assert.throws(edit({ scale: 5000 }), /edits\[0\]\.scale/);
+ assert.throws(edit({ scale: Number.NaN }), /edits\[0\]\.scale/);
+ assert.throws(edit({ rotation: { axis: [0, 0, 0], angle: 1 } }), /edits\[0\]\.rotation\.axis/);
+ assert.throws(edit({ rotation: { axis: [0, 1, 0], angle: 90 } }), /edits\[0\]\.rotation\.angle.*radians.*1\.5708/s);
+ assert.throws(edit({ rotation: { axis: [0, 1, 0] } }), /edits\[0\]\.rotation\.angle/);
+ assert.throws(edit({ rotation: { axis: [0, 1, 0], angle: 1, degrees: 5 } }), /edits\[0\]\.rotation\.degrees/);
+ assert.throws(edit({ rotation: 1 }), /edits\[0\]\.rotation/);
+ assert.throws(edit({ scale: 2, pivot: [0, 0] }), /edits\[0\]\.pivot/);
+ assert.throws(edit({ scale: 2, falloff: { radius: 0 } }), /edits\[0\]\.falloff\.radius/);
+});
+
+test('an empty selection throws and names the select field', () => {
+ assert.throws(() => applyMeshEdits(cube(), [{ op: 'delete', select: { tag: 'top' } }, { op: 'transform_selection', select: { tag: 'top' }, scale: 2 }]), /edits\[1\]\.select/);
+});