sixDofDragBehavior.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { Behavior } from "Behaviors";
  2. import { Mesh, AbstractMesh } from "Mesh";
  3. import { Scene } from "scene";
  4. import { Nullable, PointerEventTypes } from "index";
  5. import { Vector3, Quaternion } from "Math";
  6. import { PointerInfo } from "Events";
  7. import { Observer, Observable } from "Tools";
  8. import { BoundingBoxGizmo } from "Gizmos";
  9. import { Camera } from "Cameras";
  10. import { Matrix } from "Math";
  11. /**
  12. * A behavior that when attached to a mesh will allow the mesh to be dragged around based on directions and origin of the pointer's ray
  13. */
  14. export class SixDofDragBehavior implements Behavior<Mesh> {
  15. private static _virtualScene: Scene;
  16. private _ownerNode: Mesh;
  17. private _sceneRenderObserver: Nullable<Observer<Scene>> = null;
  18. private _scene: Scene;
  19. private _targetPosition = new Vector3(0, 0, 0);
  20. private _virtualOriginMesh: AbstractMesh;
  21. private _virtualDragMesh: AbstractMesh;
  22. private _pointerObserver: Nullable<Observer<PointerInfo>>;
  23. private _moving = false;
  24. private _startingOrientation = new Quaternion();
  25. /**
  26. * How much faster the object should move when the controller is moving towards it. This is useful to bring objects that are far away from the user to them faster. Set this to 0 to avoid any speed increase. (Default: 3)
  27. */
  28. private zDragFactor = 3;
  29. /**
  30. * If the behavior is currently in a dragging state
  31. */
  32. public dragging = false;
  33. /**
  34. * The distance towards the target drag position to move each frame. This can be useful to avoid jitter. Set this to 1 for no delay. (Default: 0.2)
  35. */
  36. public dragDeltaRatio = 0.2;
  37. /**
  38. * The id of the pointer that is currently interacting with the behavior (-1 when no pointer is active)
  39. */
  40. public currentDraggingPointerID = -1;
  41. /**
  42. * If camera controls should be detached during the drag
  43. */
  44. public detachCameraControls = true;
  45. /**
  46. * Fires each time a drag starts
  47. */
  48. public onDragStartObservable = new Observable<{}>();
  49. /**
  50. * Fires each time a drag ends (eg. mouse release after drag)
  51. */
  52. public onDragEndObservable = new Observable<{}>();
  53. /**
  54. * Instantiates a behavior that when attached to a mesh will allow the mesh to be dragged around based on directions and origin of the pointer's ray
  55. */
  56. constructor() {
  57. }
  58. /**
  59. * The name of the behavior
  60. */
  61. public get name(): string {
  62. return "SixDofDrag";
  63. }
  64. /**
  65. * Initializes the behavior
  66. */
  67. public init() {}
  68. /**
  69. * Attaches the scale behavior the passed in mesh
  70. * @param ownerNode The mesh that will be scaled around once attached
  71. */
  72. public attach(ownerNode: Mesh): void {
  73. this._ownerNode = ownerNode;
  74. this._scene = this._ownerNode.getScene();
  75. if (!SixDofDragBehavior._virtualScene) {
  76. SixDofDragBehavior._virtualScene = new Scene(this._scene.getEngine());
  77. SixDofDragBehavior._virtualScene.detachControl();
  78. this._scene.getEngine().scenes.pop();
  79. }
  80. var pickedMesh: Nullable<AbstractMesh> = null;
  81. var lastSixDofOriginPosition = new Vector3(0, 0, 0);
  82. // Setup virtual meshes to be used for dragging without dirtying the existing scene
  83. this._virtualOriginMesh = new AbstractMesh("", SixDofDragBehavior._virtualScene);
  84. this._virtualOriginMesh.rotationQuaternion = new Quaternion();
  85. this._virtualDragMesh = new AbstractMesh("", SixDofDragBehavior._virtualScene);
  86. this._virtualDragMesh.rotationQuaternion = new Quaternion();
  87. var pickPredicate = (m: AbstractMesh) => {
  88. return this._ownerNode == m || m.isDescendantOf(this._ownerNode);
  89. };
  90. var attachedElement: Nullable<HTMLElement> = null;
  91. this._pointerObserver = this._scene.onPointerObservable.add((pointerInfo, eventState) => {
  92. if (pointerInfo.type == PointerEventTypes.POINTERDOWN) {
  93. if (!this.dragging && pointerInfo.pickInfo && pointerInfo.pickInfo.hit && pointerInfo.pickInfo.pickedMesh && pointerInfo.pickInfo.ray && pickPredicate(pointerInfo.pickInfo.pickedMesh)) {
  94. if (this._scene.activeCamera && this._scene.activeCamera.cameraRigMode == Camera.RIG_MODE_NONE) {
  95. pointerInfo.pickInfo.ray.origin.copyFrom(this._scene.activeCamera!.position);
  96. }
  97. pickedMesh = this._ownerNode;
  98. BoundingBoxGizmo._RemoveAndStorePivotPoint(pickedMesh);
  99. lastSixDofOriginPosition.copyFrom(pointerInfo.pickInfo.ray.origin);
  100. // Set position and orientation of the controller
  101. this._virtualOriginMesh.position.copyFrom(pointerInfo.pickInfo.ray.origin);
  102. this._virtualOriginMesh.lookAt(pointerInfo.pickInfo.ray.origin.subtract(pointerInfo.pickInfo.ray.direction));
  103. // Attach the virtual drag mesh to the virtual origin mesh so it can be dragged
  104. this._virtualOriginMesh.removeChild(this._virtualDragMesh);
  105. pickedMesh.computeWorldMatrix();
  106. this._virtualDragMesh.position.copyFrom(pickedMesh.absolutePosition);
  107. if (!pickedMesh.rotationQuaternion) {
  108. pickedMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(pickedMesh.rotation.y, pickedMesh.rotation.x, pickedMesh.rotation.z);
  109. }
  110. var oldParent = pickedMesh.parent;
  111. pickedMesh.setParent(null);
  112. this._virtualDragMesh.rotationQuaternion!.copyFrom(pickedMesh.rotationQuaternion);
  113. pickedMesh.setParent(oldParent);
  114. this._virtualOriginMesh.addChild(this._virtualDragMesh);
  115. // Update state
  116. this._targetPosition.copyFrom(this._virtualDragMesh.absolutePosition);
  117. this.dragging = true;
  118. this.currentDraggingPointerID = (<PointerEvent>pointerInfo.event).pointerId;
  119. // Detatch camera controls
  120. if (this.detachCameraControls && this._scene.activeCamera && !this._scene.activeCamera.leftCamera) {
  121. if (this._scene.activeCamera.inputs.attachedElement) {
  122. attachedElement = this._scene.activeCamera.inputs.attachedElement;
  123. this._scene.activeCamera.detachControl(this._scene.activeCamera.inputs.attachedElement);
  124. }else {
  125. attachedElement = null;
  126. }
  127. }
  128. BoundingBoxGizmo._RestorePivotPoint(pickedMesh);
  129. this.onDragStartObservable.notifyObservers({});
  130. }
  131. }else if (pointerInfo.type == PointerEventTypes.POINTERUP) {
  132. if (this.currentDraggingPointerID == (<PointerEvent>pointerInfo.event).pointerId) {
  133. this.dragging = false;
  134. this._moving = false;
  135. this.currentDraggingPointerID = -1;
  136. pickedMesh = null;
  137. this._virtualOriginMesh.removeChild(this._virtualDragMesh);
  138. // Reattach camera controls
  139. if (this.detachCameraControls && attachedElement && this._scene.activeCamera && !this._scene.activeCamera.leftCamera) {
  140. this._scene.activeCamera.attachControl(attachedElement, true);
  141. }
  142. this.onDragEndObservable.notifyObservers({});
  143. }
  144. }else if (pointerInfo.type == PointerEventTypes.POINTERMOVE) {
  145. if (this.currentDraggingPointerID == (<PointerEvent>pointerInfo.event).pointerId && this.dragging && pointerInfo.pickInfo && pointerInfo.pickInfo.ray && pickedMesh) {
  146. var zDragFactor = this.zDragFactor;
  147. if (this._scene.activeCamera && this._scene.activeCamera.cameraRigMode == Camera.RIG_MODE_NONE) {
  148. pointerInfo.pickInfo.ray.origin.copyFrom(this._scene.activeCamera!.position);
  149. zDragFactor = 0;
  150. }
  151. // Calculate controller drag distance in controller space
  152. var originDragDifference = pointerInfo.pickInfo.ray.origin.subtract(lastSixDofOriginPosition);
  153. lastSixDofOriginPosition.copyFrom(pointerInfo.pickInfo.ray.origin);
  154. var localOriginDragDifference = -Vector3.Dot(originDragDifference, pointerInfo.pickInfo.ray.direction);
  155. this._virtualOriginMesh.addChild(this._virtualDragMesh);
  156. // Determine how much the controller moved to/away towards the dragged object and use this to move the object further when its further away
  157. this._virtualDragMesh.position.z -= this._virtualDragMesh.position.z < 1 ? localOriginDragDifference * this.zDragFactor : localOriginDragDifference * zDragFactor * this._virtualDragMesh.position.z;
  158. if (this._virtualDragMesh.position.z < 0) {
  159. this._virtualDragMesh.position.z = 0;
  160. }
  161. // Update the controller position
  162. this._virtualOriginMesh.position.copyFrom(pointerInfo.pickInfo.ray.origin);
  163. this._virtualOriginMesh.lookAt(pointerInfo.pickInfo.ray.origin.subtract(pointerInfo.pickInfo.ray.direction));
  164. this._virtualOriginMesh.removeChild(this._virtualDragMesh);
  165. // Move the virtualObjectsPosition into the picked mesh's space if needed
  166. this._targetPosition.copyFrom(this._virtualDragMesh.absolutePosition);
  167. if (pickedMesh.parent) {
  168. Vector3.TransformCoordinatesToRef(this._targetPosition, Matrix.Invert(pickedMesh.parent.getWorldMatrix()), this._targetPosition);
  169. }
  170. if (!this._moving) {
  171. this._startingOrientation.copyFrom(this._virtualDragMesh.rotationQuaternion!);
  172. }
  173. this._moving = true;
  174. }
  175. }
  176. });
  177. var tmpQuaternion = new Quaternion();
  178. // On every frame move towards target scaling to avoid jitter caused by vr controllers
  179. this._sceneRenderObserver = ownerNode.getScene().onBeforeRenderObservable.add(() => {
  180. if (this.dragging && this._moving && pickedMesh) {
  181. BoundingBoxGizmo._RemoveAndStorePivotPoint(pickedMesh);
  182. // Slowly move mesh to avoid jitter
  183. pickedMesh.position.addInPlace(this._targetPosition.subtract(pickedMesh.position).scale(this.dragDeltaRatio));
  184. // Get change in rotation
  185. tmpQuaternion.copyFrom(this._startingOrientation);
  186. tmpQuaternion.x = -tmpQuaternion.x;
  187. tmpQuaternion.y = -tmpQuaternion.y;
  188. tmpQuaternion.z = -tmpQuaternion.z;
  189. this._virtualDragMesh.rotationQuaternion!.multiplyToRef(tmpQuaternion, tmpQuaternion);
  190. // Convert change in rotation to only y axis rotation
  191. Quaternion.RotationYawPitchRollToRef(tmpQuaternion.toEulerAngles("xyz").y, 0, 0, tmpQuaternion);
  192. tmpQuaternion.multiplyToRef(this._startingOrientation, tmpQuaternion);
  193. // Slowly move mesh to avoid jitter
  194. var oldParent = pickedMesh.parent;
  195. pickedMesh.setParent(null);
  196. Quaternion.SlerpToRef(pickedMesh.rotationQuaternion!, tmpQuaternion, this.dragDeltaRatio, pickedMesh.rotationQuaternion!);
  197. pickedMesh.setParent(oldParent);
  198. BoundingBoxGizmo._RestorePivotPoint(pickedMesh);
  199. }
  200. });
  201. }
  202. /**
  203. * Detaches the behavior from the mesh
  204. */
  205. public detach(): void {
  206. if (this._scene) {
  207. this._scene.onPointerObservable.remove(this._pointerObserver);
  208. }
  209. if (this._ownerNode) {
  210. this._ownerNode.getScene().onBeforeRenderObservable.remove(this._sceneRenderObserver);
  211. }
  212. if (this._virtualOriginMesh) {
  213. this._virtualOriginMesh.dispose();
  214. }
  215. if (this._virtualDragMesh) {
  216. this._virtualDragMesh.dispose();
  217. }
  218. this.onDragEndObservable.clear();
  219. this.onDragStartObservable.clear();
  220. }
  221. }