123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import { Observer, Observable } from "../Misc/observable";
- import { Nullable } from "../types";
- import { PointerInfo } from "../Events/pointerEvents";
- import { Vector3, Matrix } from "../Maths/math";
- import { TransformNode } from "../Meshes/transformNode";
- import { AbstractMesh } from "../Meshes/abstractMesh";
- import { Mesh } from "../Meshes/mesh";
- import { LinesMesh } from "../Meshes/linesMesh";
- import { CylinderBuilder } from "../Meshes/Builders/cylinderBuilder";
- import { PointerDragBehavior } from "../Behaviors/Meshes/pointerDragBehavior";
- import { _TimeToken } from "../Instrumentation/timeToken";
- import { _DepthCullingState, _StencilState, _AlphaState } from "../States/index";
- import { Gizmo } from "./gizmo";
- import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
- import { StandardMaterial } from "../Materials/standardMaterial";
- import { Scene } from "../scene";
- import { PositionGizmo } from "./positionGizmo";
- import { Color3 } from '../Maths/math.color';
- /**
- * Single axis drag gizmo
- */
- export class AxisDragGizmo extends Gizmo {
- /**
- * Drag behavior responsible for the gizmos dragging interactions
- */
- public dragBehavior: PointerDragBehavior;
- private _pointerObserver: Nullable<Observer<PointerInfo>> = null;
- /**
- * Drag distance in babylon units that the gizmo will snap to when dragged (Default: 0)
- */
- public snapDistance = 0;
- /**
- * Event that fires each time the gizmo snaps to a new location.
- * * snapDistance is the the change in distance
- */
- public onSnapObservable = new Observable<{ snapDistance: number }>();
- private _isEnabled: boolean = true;
- private _parent: Nullable<PositionGizmo> = null;
- private _arrow: TransformNode;
- private _coloredMaterial: StandardMaterial;
- private _hoverMaterial: StandardMaterial;
- /** @hidden */
- public static _CreateArrow(scene: Scene, material: StandardMaterial): TransformNode {
- var arrow = new TransformNode("arrow", scene);
- var cylinder = CylinderBuilder.CreateCylinder("cylinder", { diameterTop: 0, height: 0.075, diameterBottom: 0.0375, tessellation: 96 }, scene);
- var line = CylinderBuilder.CreateCylinder("cylinder", { diameterTop: 0.005, height: 0.275, diameterBottom: 0.005, tessellation: 96 }, scene);
- line.material = material;
- cylinder.parent = arrow;
- line.parent = arrow;
- // Position arrow pointing in its drag axis
- cylinder.material = material;
- cylinder.rotation.x = Math.PI / 2;
- cylinder.position.z += 0.3;
- line.position.z += 0.275 / 2;
- line.rotation.x = Math.PI / 2;
- return arrow;
- }
- /** @hidden */
- public static _CreateArrowInstance(scene: Scene, arrow: TransformNode): TransformNode {
- const instance = new TransformNode("arrow", scene);
- for (const mesh of arrow.getChildMeshes()) {
- const childInstance = (mesh as Mesh).createInstance(mesh.name);
- childInstance.parent = instance;
- }
- return instance;
- }
- /**
- * Creates an AxisDragGizmo
- * @param gizmoLayer The utility layer the gizmo will be added to
- * @param dragAxis The axis which the gizmo will be able to drag on
- * @param color The color of the gizmo
- */
- constructor(dragAxis: Vector3, color: Color3 = Color3.Gray(), gizmoLayer: UtilityLayerRenderer = UtilityLayerRenderer.DefaultUtilityLayer, parent: Nullable<PositionGizmo> = null) {
- super(gizmoLayer);
- this._parent = parent;
- // Create Material
- this._coloredMaterial = new StandardMaterial("", gizmoLayer.utilityLayerScene);
- this._coloredMaterial.diffuseColor = color;
- this._coloredMaterial.specularColor = color.subtract(new Color3(0.1, 0.1, 0.1));
- this._hoverMaterial = new StandardMaterial("", gizmoLayer.utilityLayerScene);
- this._hoverMaterial.diffuseColor = color.add(new Color3(0.3, 0.3, 0.3));
- // Build mesh on root node
- this._arrow = AxisDragGizmo._CreateArrow(gizmoLayer.utilityLayerScene, this._coloredMaterial);
- this._arrow.lookAt(this._rootMesh.position.add(dragAxis));
- this._arrow.scaling.scaleInPlace(1 / 3);
- this._arrow.parent = this._rootMesh;
- var currentSnapDragDistance = 0;
- var tmpVector = new Vector3();
- var tmpSnapEvent = { snapDistance: 0 };
- // Add drag behavior to handle events when the gizmo is dragged
- this.dragBehavior = new PointerDragBehavior({ dragAxis: dragAxis });
- this.dragBehavior.moveAttached = false;
- this._rootMesh.addBehavior(this.dragBehavior);
- var localDelta = new Vector3();
- var tmpMatrix = new Matrix();
- this.dragBehavior.onDragObservable.add((event) => {
- if (this.attachedMesh) {
- // Convert delta to local translation if it has a parent
- if (this.attachedMesh.parent) {
- this.attachedMesh.parent.computeWorldMatrix().invertToRef(tmpMatrix);
- tmpMatrix.setTranslationFromFloats(0, 0, 0);
- Vector3.TransformCoordinatesToRef(event.delta, tmpMatrix, localDelta);
- } else {
- localDelta.copyFrom(event.delta);
- }
- // Snapping logic
- if (this.snapDistance == 0) {
- this.attachedMesh.position.addInPlace(localDelta);
- } else {
- currentSnapDragDistance += event.dragDistance;
- if (Math.abs(currentSnapDragDistance) > this.snapDistance) {
- var dragSteps = Math.floor(Math.abs(currentSnapDragDistance) / this.snapDistance);
- currentSnapDragDistance = currentSnapDragDistance % this.snapDistance;
- localDelta.normalizeToRef(tmpVector);
- tmpVector.scaleInPlace(this.snapDistance * dragSteps);
- this.attachedMesh.position.addInPlace(tmpVector);
- tmpSnapEvent.snapDistance = this.snapDistance * dragSteps;
- this.onSnapObservable.notifyObservers(tmpSnapEvent);
- }
- }
- }
- });
- this._pointerObserver = gizmoLayer.utilityLayerScene.onPointerObservable.add((pointerInfo) => {
- if (this._customMeshSet) {
- return;
- }
- var isHovered = pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1);
- var material = isHovered ? this._hoverMaterial : this._coloredMaterial;
- this._rootMesh.getChildMeshes().forEach((m) => {
- m.material = material;
- if ((<LinesMesh>m).color) {
- (<LinesMesh>m).color = material.diffuseColor;
- }
- });
- });
- var light = gizmoLayer._getSharedGizmoLight();
- light.includedOnlyMeshes = light.includedOnlyMeshes.concat(this._rootMesh.getChildMeshes(false));
- }
- protected _attachedMeshChanged(value: Nullable<AbstractMesh>) {
- if (this.dragBehavior) {
- this.dragBehavior.enabled = value ? true : false;
- }
- }
- /**
- * If the gizmo is enabled
- */
- public set isEnabled(value: boolean) {
- this._isEnabled = value;
- if (!value) {
- this.attachedMesh = null;
- }
- else {
- if (this._parent) {
- this.attachedMesh = this._parent.attachedMesh;
- }
- }
- }
- public get isEnabled(): boolean {
- return this._isEnabled;
- }
- /**
- * Disposes of the gizmo
- */
- public dispose() {
- this.onSnapObservable.clear();
- this.gizmoLayer.utilityLayerScene.onPointerObservable.remove(this._pointerObserver);
- this.dragBehavior.detach();
- if (this._arrow) {
- this._arrow.dispose();
- }
- [this._coloredMaterial, this._hoverMaterial].forEach((matl) => {
- if (matl) {
- matl.dispose();
- }
- });
- super.dispose();
- }
- }
|