diff --git a/build/studio/bundle/index.d.ts b/build/studio/bundle/index.d.ts index a0bb8ee..e177fa2 100644 --- a/build/studio/bundle/index.d.ts +++ b/build/studio/bundle/index.d.ts @@ -845,24 +845,13 @@ declare class SceneController { declare module "three" { interface Object3D { - vstack(buffer?: number): THREE.Object3D; vspace(distanceBetween?: number): THREE.Object3D; - setScale(factor: number): THREE.Object3D; moveNextTo(target: THREE.Object3D, direction: THREE.Vector3, distance?: number): void; moveToRightOf(target: THREE.Object3D, distance?: number): void; moveToLeftOf(target: THREE.Object3D, distance?: number): void; moveAbove(target: THREE.Object3D, distance?: number): void; moveBelow(target: THREE.Object3D, distance?: number): void; - setOpacity(opacity: number, config?: any): THREE.Object3D; - setInvisible(config?: any): THREE.Object3D; - setVisible(config?: any): THREE.Object3D; - setUpright(): THREE.Object3D; - recenter(center: THREE.Vector3): THREE.Object3D; - reorient(zRotation: number): void; pointAlongCurve(t: number): THREE.Vector3; - addComponent(name: K, child: T): this & { - [P in K]: T; - }; updateComponent(name: string, child: THREE.Object3D): void; removeComponent(name: string): THREE.Object3D; hideComponents(): THREE.Object3D; diff --git a/build/studio/bundle/index.js b/build/studio/bundle/index.js index 7a35aab..8c9eabd 100644 --- a/build/studio/bundle/index.js +++ b/build/studio/bundle/index.js @@ -57362,17 +57362,9 @@ class SceneController { } } -THREE.Object3D.prototype.vstack = function (buffer = 0.2) { - return vstack(this, buffer); -}; THREE.Object3D.prototype.vspace = function (distanceBetween) { return vspace(this, distanceBetween); }; -THREE.Object3D.prototype.setScale = function (factor) { - this.scale.x = factor; - this.scale.y = factor; - return this; -}; THREE.Object3D.prototype.pointAlongCurve = function (t) { return pointAlongCurve(this, t); }; @@ -57391,23 +57383,6 @@ THREE.Object3D.prototype.moveAbove = function (target, distance) { THREE.Object3D.prototype.moveBelow = function (target, distance) { return moveBelow(target, this, distance); }; -THREE.Object3D.prototype.addComponent = function (name, child) { - if (this.components?.has(name)) { - throw new Error(`Failed to add component ${name}: Component or attribute already exists`); - } - if (!this.components) { - this.components = new Map(); - } - this.components.set(name, child); - child.parentComponent = this; - this.add(child); - Object.defineProperty(this, name, { - get: () => this.components.get(name), - set: (value) => this.setComponent(name, value), - configurable: true, - }); - return this; -}; THREE.Object3D.prototype.updateComponent = (name, child) => { throw new Error("Not implemented"); }; @@ -57521,76 +57496,5 @@ function component(_, context) { }, }; } -THREE.Object3D.prototype.setOpacity = function (opacity, config) { - let family = true; - if (config && config.family === false) { - family = false; - } - if (family) { - this.traverse((child) => { - if (child instanceof THREE.Mesh) { - child.material.opacity = opacity; - } - }); - } - else { - [this.stroke, this.fill].forEach((mesh) => { - if (!mesh) - return; - mesh.material.opacity = opacity; - }); - } - return this; -}; -THREE.Object3D.prototype.setInvisible = function (config) { - let family = true; - if (config && config.family === false) { - family = false; - } - return this.setOpacity(0, { family }); -}; -THREE.Object3D.prototype.setVisible = function (config) { - let family = true; - if (config && config.family === false) { - family = false; - } - return this.setOpacity(1, { family }); -}; -THREE.Object3D.prototype.setUpright = function () { - const worldQuaternion = new THREE.Quaternion(); - this.getWorldQuaternion(worldQuaternion); - const inverseQuaternion = worldQuaternion.clone().invert(); - this.quaternion.copy(inverseQuaternion); - return this; -}; -THREE.Object3D.prototype.recenter = function (globalPosition) { - const localPosition = globalPosition.clone(); - this.worldToLocal(globalPosition.clone()); - const offset = new THREE.Vector3().subVectors(localPosition, this.position); - this.position.add(offset); - if (this.points) { - // Update stroke and fill geometries. - const newPoints = this.points.map((point) => point.clone().sub(offset)); - if (this.stroke) { - this.stroke.geometry.setPoints(newPoints); - } - if (this.fill) { - for (let i = 0; i < this.stroke.geometry.points.length - 1; i++) { - const { x, y, z } = newPoints[i]; - this.fill.geometry.attributes.position.array[i * 3] = x; - this.fill.geometry.attributes.position.array[i * 3 + 1] = y; - this.fill.geometry.attributes.position.array[i * 3 + 2] = z; - } - } - } - // Update children. - this.children.forEach((child) => { - if (child === this.stroke || child === this.fill) - return; - child.position.sub(offset); - }); - return this; -}; -THREE.Object3D.prototype.reorient = () => { }; export { index as Animation, constants as Constants, diagram as Diagram, frame as Frame, index$1 as Geometry, graphing as Graphing, MeshLineMaterial, SceneController, text as Text, utils as Utils, component, setCameraDimensions, setCanvasViewport, setupCanvas }; diff --git a/build/three/index.d.ts b/build/three/index.d.ts index 8ba49bf..2a41f56 100644 --- a/build/three/index.d.ts +++ b/build/three/index.d.ts @@ -19193,7 +19193,6 @@ declare module "three" { rotateZ(angle: number): Vector3; transformBetweenSpaces(from, to): Vector3; } - class UpdaterRegistry { private namedUpdaters: Map void>; private updaters: (() => void)[]; @@ -19201,7 +19200,7 @@ declare module "three" { constructor(owner: any); register( nameOrFunc: ((...args: any[]) => any) | string, - func?: (...args: any[]) => any, + func?: (...args: any[]) => any ): void; unregister(nameOrFunc: ((...args: any[]) => any) | string): boolean; has(name: string): boolean; @@ -19212,9 +19211,23 @@ declare module "three" { update(dt: number, t: number): void; addUpdater( nameOrFunc: ((...args: any[]) => any) | string, - func?: (...args: any[]) => any, + func?: (...args: any[]) => any ): void; removeUpdater(nameOrFunc: ((...args: any[]) => any) | string): boolean; + + vstack(buffer?: number): Object3D; + setScale(factor: number): Object3D; + setOpacity(opacity: number, config?: any): Object3D; + setInvisible(config?: any): Object3D; + setVisible(config?: any): Object3D; + setUpright(): Object3D; + recenter(center: Vector3): Object3D; + reorient(zRotation: number): void; + pointAlongCurve(t: number): Vector3; + addComponent( + name: K, + child: T + ): this & { [P in K]: T }; } } diff --git a/src/index.ts b/src/index.ts index 5c41e32..f365129 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,29 +3,18 @@ import * as Utils from "./utils.js"; declare module "three" { export interface Object3D { - vstack(buffer?: number): THREE.Object3D; vspace(distanceBetween?: number): THREE.Object3D; - setScale(factor: number): THREE.Object3D; moveNextTo( target: THREE.Object3D, direction: THREE.Vector3, - distance?: number, + distance?: number ): void; moveToRightOf(target: THREE.Object3D, distance?: number): void; moveToLeftOf(target: THREE.Object3D, distance?: number): void; moveAbove(target: THREE.Object3D, distance?: number): void; moveBelow(target: THREE.Object3D, distance?: number): void; - setOpacity(opacity: number, config?: any): THREE.Object3D; - setInvisible(config?: any): THREE.Object3D; - setVisible(config?: any): THREE.Object3D; - setUpright(): THREE.Object3D; - recenter(center: THREE.Vector3): THREE.Object3D; - reorient(zRotation: number): void; pointAlongCurve(t: number): THREE.Vector3; - addComponent( - name: K, - child: T, - ): this & { [P in K]: T }; + updateComponent(name: string, child: THREE.Object3D): void; removeComponent(name: string): THREE.Object3D; hideComponents(): THREE.Object3D; @@ -44,27 +33,17 @@ declare module "three" { traverseComponents(f: () => void, config?: { includeSelf: boolean }): void; traverseAncestorComponents( f: () => void, - config?: { includeSelf: boolean }, + config?: { includeSelf: boolean } ): void; } } -THREE.Object3D.prototype.vstack = function (buffer = 0.2): THREE.Object3D { - return Utils.vstack(this, buffer); -}; - THREE.Object3D.prototype.vspace = function ( - distanceBetween?: number, + distanceBetween?: number ): THREE.Object3D { return Utils.vspace(this, distanceBetween); }; -THREE.Object3D.prototype.setScale = function (factor): THREE.Object3D { - this.scale.x = factor; - this.scale.y = factor; - return this; -}; - THREE.Object3D.prototype.pointAlongCurve = function (t: number) { return Utils.pointAlongCurve(this, t); }; @@ -72,65 +51,42 @@ THREE.Object3D.prototype.pointAlongCurve = function (t: number) { THREE.Object3D.prototype.moveNextTo = function ( target: THREE.Object3D, direction: THREE.Vector3, - distance?: number, + distance?: number ) { return Utils.moveNextTo(target, this, direction, distance); }; THREE.Object3D.prototype.moveToRightOf = function ( target: THREE.Object3D, - distance?: number, + distance?: number ) { return Utils.moveToRightOf(target, this, distance); }; THREE.Object3D.prototype.moveToLeftOf = function ( target: THREE.Object3D, - distance?: number, + distance?: number ) { return Utils.moveToLeftOf(target, this, distance); }; THREE.Object3D.prototype.moveAbove = function ( target: THREE.Object3D, - distance?: number, + distance?: number ) { return Utils.moveAbove(target, this, distance); }; THREE.Object3D.prototype.moveBelow = function ( target: THREE.Object3D, - distance?: number, + distance?: number ) { return Utils.moveBelow(target, this, distance); }; -THREE.Object3D.prototype.addComponent = function ( - name: string, - child: T & { parentComponent: THREE.Object3D | undefined }, -) { - if (this.components?.has(name)) { - throw new Error( - `Failed to add component ${name}: Component or attribute already exists`, - ); - } - if (!this.components) { - this.components = new Map(); - } - this.components.set(name, child); - child.parentComponent = this; - this.add(child); - Object.defineProperty(this, name, { - get: () => this.components.get(name), - set: (value) => this.setComponent(name, value), - configurable: true, - }); - return this; -}; - THREE.Object3D.prototype.updateComponent = ( name: string, - child: THREE.Object3D, + child: THREE.Object3D ) => { throw new Error("Not implemented"); }; @@ -203,7 +159,7 @@ THREE.Object3D.prototype.revealAncestors = function (config?: { }) { this.traverseAncestorComponents( (obj) => obj.parentComponent && obj.reveal(), - config, + config ); return this; }; @@ -213,7 +169,7 @@ THREE.Object3D.prototype.hideAncestors = function (config?: { }) { this.traverseAncestorComponents( (obj) => obj.parentComponent && obj.hide(), - config, + config ); return this; }; @@ -230,7 +186,7 @@ THREE.Object3D.prototype.hideComponents = function () { THREE.Object3D.prototype.traverseComponents = function ( f: (o: THREE.Object3D) => void, - config?: { includeSelf: boolean }, + config?: { includeSelf: boolean } ) { if (config?.includeSelf) f(this); if (!this.components) return; @@ -242,7 +198,7 @@ THREE.Object3D.prototype.traverseComponents = function ( THREE.Object3D.prototype.traverseAncestorComponents = function ( f: (o: THREE.Object3D) => void, - config?: { includeSelf: boolean }, + config?: { includeSelf: boolean } ) { if (config?.includeSelf) f(this); if (!this.parentComponent) return; @@ -267,7 +223,7 @@ type ComponentParent = THREE.Object3D & { }; function component( _: ClassAccessorDecoratorTarget, - context: ClassAccessorDecoratorContext, + context: ClassAccessorDecoratorContext ): ClassAccessorDecoratorResult { const propertyName = String(context.name); return { @@ -282,92 +238,6 @@ function component( }; } -THREE.Object3D.prototype.setOpacity = function ( - opacity: number, - config?: any, -): THREE.Object3D { - let family = true; - if (config && config.family === false) { - family = false; - } - - if (family) { - this.traverse((child) => { - if (child instanceof THREE.Mesh) { - child.material.opacity = opacity; - } - }); - } else { - [this.stroke, this.fill].forEach((mesh) => { - if (!mesh) return; - mesh.material.opacity = opacity; - }); - } - return this; -}; - -THREE.Object3D.prototype.setInvisible = function ( - config?: any, -): THREE.Object3D { - let family = true; - if (config && config.family === false) { - family = false; - } - - return this.setOpacity(0, { family }); -}; - -THREE.Object3D.prototype.setVisible = function (config?: any): THREE.Object3D { - let family = true; - if (config && config.family === false) { - family = false; - } - return this.setOpacity(1, { family }); -}; - -THREE.Object3D.prototype.setUpright = function (): THREE.Object3D { - const worldQuaternion = new THREE.Quaternion(); - this.getWorldQuaternion(worldQuaternion); - - const inverseQuaternion = worldQuaternion.clone().invert(); - this.quaternion.copy(inverseQuaternion); - return this; -}; - -THREE.Object3D.prototype.recenter = function ( - globalPosition: THREE.Vector3, -): THREE.Object3D { - const localPosition = globalPosition.clone(); - this.worldToLocal(globalPosition.clone()); - const offset = new THREE.Vector3().subVectors(localPosition, this.position); - this.position.add(offset); - - if (this.points) { - // Update stroke and fill geometries. - const newPoints = this.points.map((point) => point.clone().sub(offset)); - if (this.stroke) { - this.stroke.geometry.setPoints(newPoints); - } - if (this.fill) { - for (let i = 0; i < this.stroke.geometry.points.length - 1; i++) { - const { x, y, z } = newPoints[i]; - this.fill.geometry.attributes.position.array[i * 3] = x; - this.fill.geometry.attributes.position.array[i * 3 + 1] = y; - this.fill.geometry.attributes.position.array[i * 3 + 2] = z; - } - } - } - - // Update children. - this.children.forEach((child) => { - if (child === this.stroke || child === this.fill) return; - child.position.sub(offset); - }); - - return this; -}; - -THREE.Object3D.prototype.reorient = (): void => {}; import MeshLineMaterial, { setCameraDimensions, setCanvasViewport, diff --git a/three-types/index.d.ts b/three-types/index.d.ts index 16fa197..5989460 100644 --- a/three-types/index.d.ts +++ b/three-types/index.d.ts @@ -6,7 +6,6 @@ declare module "three" { rotateZ(angle: number): Vector3; transformBetweenSpaces(from, to): Vector3; } - class UpdaterRegistry { private namedUpdaters: Map void>; private updaters: (() => void)[]; @@ -14,7 +13,7 @@ declare module "three" { constructor(owner: any); register( nameOrFunc: ((...args: any[]) => any) | string, - func?: (...args: any[]) => any, + func?: (...args: any[]) => any ): void; unregister(nameOrFunc: ((...args: any[]) => any) | string): boolean; has(name: string): boolean; @@ -25,9 +24,23 @@ declare module "three" { update(dt: number, t: number): void; addUpdater( nameOrFunc: ((...args: any[]) => any) | string, - func?: (...args: any[]) => any, + func?: (...args: any[]) => any ): void; removeUpdater(nameOrFunc: ((...args: any[]) => any) | string): boolean; + + vstack(buffer?: number): Object3D; + setScale(factor: number): Object3D; + setOpacity(opacity: number, config?: any): Object3D; + setInvisible(config?: any): Object3D; + setVisible(config?: any): Object3D; + setUpright(): Object3D; + recenter(center: Vector3): Object3D; + reorient(zRotation: number): void; + pointAlongCurve(t: number): Vector3; + addComponent( + name: K, + child: T + ): this & { [P in K]: T }; } }