UNPKG

@pmndrs/handle

Version:

framework agnostic expandable handle implementation for threejs

68 lines (67 loc) 2.43 kB
import { Group, TorusGeometry, Vector3 } from 'three'; import { AxisRotateHandle } from './axis.js'; import { FreeRotateHandle } from './free.js'; import { ScreenSpaceRotateHandle } from './screen.js'; import { computeHandlesScale } from '../utils.js'; export function createCircleGeometry(radius, arc) { const geometry = new TorusGeometry(radius, 0.0075, 3, 64, arc * Math.PI * 2); geometry.rotateY(Math.PI / 2); geometry.rotateX(Math.PI / 2); return geometry; } const vectorHelper = new Vector3(); export class RotateHandles extends Group { context; fixed; size; rotationX; rotationY; rotationZ; free; screen; constructor(context, fixed, size) { super(); this.context = context; this.fixed = fixed; this.size = size; this.rotationX = new AxisRotateHandle(this.context, 'x'); this.add(this.rotationX); this.rotationY = new AxisRotateHandle(this.context, 'y'); this.add(this.rotationY); this.rotationZ = new AxisRotateHandle(this.context, 'z'); this.add(this.rotationZ); this.free = new FreeRotateHandle(this.context); this.add(this.free); this.screen = new ScreenSpaceRotateHandle(this.context); this.add(this.screen); } update(camera) { this.updateWorldMatrix(true, false); this.rotationX.update(camera); this.rotationY.update(camera); this.rotationZ.update(camera); this.free.update(camera); this.screen.update(camera); this.scale.setScalar(1); const target = this.context.getTarget(); if (target != null) { target.getWorldScale(vectorHelper); this.scale.divide(vectorHelper); } this.scale.multiplyScalar(computeHandlesScale(this, camera, this.fixed ?? true, this.size ?? 1)); } bind(options) { const unbindTranslationX = this.rotationX.bind(0xff0000, options); const unbindTranslationY = this.rotationY.bind(0x00ff00, options); const unbindTranslationZ = this.rotationZ.bind(0x0000ff, options); const unbindScreen = this.screen.bind(options); const unbindFree = this.free.bind(options); return () => { unbindTranslationX?.(); unbindTranslationY?.(); unbindTranslationZ?.(); unbindScreen?.(); unbindFree?.(); }; } }