sixDofDragBehavior.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { Behavior } from "../../Behaviors/behavior";
  2. import { Mesh } from "../../Meshes/mesh";
  3. import { AbstractMesh } from "../../Meshes/abstractMesh";
  4. import { Scene } from "../../scene";
  5. import { Nullable } from "../../types";
  6. import { PointerInfo, PointerEventTypes } from "../../Events/pointerEvents";
  7. import { Vector3, Quaternion, Matrix } from "../../Maths/math.vector";
  8. import { Observer, Observable } from "../../Misc/observable";
  9. import { Camera } from "../../Cameras/camera";
  10. import { PivotTools } from "../../Misc/pivotTools";
  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. private _attachedToElement: boolean = false;
  26. /**
  27. * 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)
  28. */
  29. private zDragFactor = 3;
  30. /**
  31. * If the object should rotate to face the drag origin
  32. */
  33. public rotateDraggedObject = true;
  34. /**
  35. * If the behavior is currently in a dragging state
  36. */
  37. public dragging = false;
  38. /**
  39. * 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)
  40. */
  41. public dragDeltaRatio = 0.2;
  42. /**
  43. * The id of the pointer that is currently interacting with the behavior (-1 when no pointer is active)
  44. */
  45. public currentDraggingPointerID = -1;
  46. /**
  47. * If camera controls should be detached during the drag
  48. */
  49. public detachCameraControls = true;
  50. /**
  51. * Fires each time a drag starts
  52. */
  53. public onDragStartObservable = new Observable<{}>();
  54. /**
  55. * Fires each time a drag happens
  56. */
  57. public onDragObservable = new Observable<void>();
  58. /**
  59. * Fires each time a drag ends (eg. mouse release after drag)
  60. */
  61. public onDragEndObservable = new Observable<{}>();
  62. /**
  63. * 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
  64. */
  65. constructor() {
  66. }
  67. /**
  68. * The name of the behavior
  69. */
  70. public get name(): string {
  71. return "SixDofDrag";
  72. }
  73. /**
  74. * Initializes the behavior
  75. */
  76. public init() { }
  77. /**
  78. * In the case of multiple active cameras, the cameraToUseForPointers should be used if set instead of active camera
  79. */
  80. private get _pointerCamera() {
  81. if (this._scene.cameraToUseForPointers) {
  82. return this._scene.cameraToUseForPointers;
  83. } else {
  84. return this._scene.activeCamera;
  85. }
  86. }
  87. /**
  88. * Attaches the scale behavior the passed in mesh
  89. * @param ownerNode The mesh that will be scaled around once attached
  90. */
  91. public attach(ownerNode: Mesh): void {
  92. this._ownerNode = ownerNode;
  93. this._scene = this._ownerNode.getScene();
  94. if (!SixDofDragBehavior._virtualScene) {
  95. SixDofDragBehavior._virtualScene = new Scene(this._scene.getEngine(), {virtual: true});
  96. SixDofDragBehavior._virtualScene.detachControl();
  97. this._scene.getEngine().scenes.pop();
  98. }
  99. var pickedMesh: Nullable<AbstractMesh> = null;
  100. var lastSixDofOriginPosition = new Vector3(0, 0, 0);
  101. // Setup virtual meshes to be used for dragging without dirtying the existing scene
  102. this._virtualOriginMesh = new AbstractMesh("", SixDofDragBehavior._virtualScene);
  103. this._virtualOriginMesh.rotationQuaternion = new Quaternion();
  104. this._virtualDragMesh = new AbstractMesh("", SixDofDragBehavior._virtualScene);
  105. this._virtualDragMesh.rotationQuaternion = new Quaternion();
  106. var pickPredicate = (m: AbstractMesh) => {
  107. return this._ownerNode == m || m.isDescendantOf(this._ownerNode);
  108. };
  109. this._pointerObserver = this._scene.onPointerObservable.add((pointerInfo, eventState) => {
  110. if (pointerInfo.type == PointerEventTypes.POINTERDOWN) {
  111. if (!this.dragging && pointerInfo.pickInfo && pointerInfo.pickInfo.hit && pointerInfo.pickInfo.pickedMesh && pointerInfo.pickInfo.ray && pickPredicate(pointerInfo.pickInfo.pickedMesh)) {
  112. if (this._pointerCamera && this._pointerCamera.cameraRigMode == Camera.RIG_MODE_NONE) {
  113. pointerInfo.pickInfo.ray.origin.copyFrom(this._pointerCamera!.globalPosition);
  114. }
  115. pickedMesh = this._ownerNode;
  116. PivotTools._RemoveAndStorePivotPoint(pickedMesh);
  117. lastSixDofOriginPosition.copyFrom(pointerInfo.pickInfo.ray.origin);
  118. // Set position and orientation of the controller
  119. this._virtualOriginMesh.position.copyFrom(pointerInfo.pickInfo.ray.origin);
  120. this._virtualOriginMesh.lookAt(pointerInfo.pickInfo.ray.origin.add(pointerInfo.pickInfo.ray.direction));
  121. // Attach the virtual drag mesh to the virtual origin mesh so it can be dragged
  122. this._virtualOriginMesh.removeChild(this._virtualDragMesh);
  123. pickedMesh.computeWorldMatrix();
  124. this._virtualDragMesh.position.copyFrom(pickedMesh.absolutePosition);
  125. if (!pickedMesh.rotationQuaternion) {
  126. pickedMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(pickedMesh.rotation.y, pickedMesh.rotation.x, pickedMesh.rotation.z);
  127. }
  128. var oldParent = pickedMesh.parent;
  129. pickedMesh.setParent(null);
  130. this._virtualDragMesh.rotationQuaternion!.copyFrom(pickedMesh.rotationQuaternion);
  131. pickedMesh.setParent(oldParent);
  132. this._virtualOriginMesh.addChild(this._virtualDragMesh);
  133. // Update state
  134. this._targetPosition.copyFrom(this._virtualDragMesh.absolutePosition);
  135. this.dragging = true;
  136. this.currentDraggingPointerID = (<PointerEvent>pointerInfo.event).pointerId;
  137. // Detach camera controls
  138. if (this.detachCameraControls && this._pointerCamera && !this._pointerCamera.leftCamera) {
  139. if (this._pointerCamera.inputs.attachedToElement) {
  140. this._pointerCamera.detachControl();
  141. this._attachedToElement = true;
  142. } else {
  143. this._attachedToElement = false;
  144. }
  145. }
  146. PivotTools._RestorePivotPoint(pickedMesh);
  147. this.onDragStartObservable.notifyObservers({});
  148. }
  149. } else if (pointerInfo.type == PointerEventTypes.POINTERUP || pointerInfo.type == PointerEventTypes.POINTERDOUBLETAP) {
  150. if (this.currentDraggingPointerID == (<PointerEvent>pointerInfo.event).pointerId) {
  151. this.dragging = false;
  152. this._moving = false;
  153. this.currentDraggingPointerID = -1;
  154. pickedMesh = null;
  155. this._virtualOriginMesh.removeChild(this._virtualDragMesh);
  156. // Reattach camera controls
  157. if (this.detachCameraControls && this._attachedToElement && this._pointerCamera && !this._pointerCamera.leftCamera) {
  158. this._pointerCamera.attachControl(true);
  159. this._attachedToElement = false;
  160. }
  161. this.onDragEndObservable.notifyObservers({});
  162. }
  163. } else if (pointerInfo.type == PointerEventTypes.POINTERMOVE) {
  164. if (this.currentDraggingPointerID == (<PointerEvent>pointerInfo.event).pointerId && this.dragging && pointerInfo.pickInfo && pointerInfo.pickInfo.ray && pickedMesh) {
  165. var zDragFactor = this.zDragFactor;
  166. if (this._pointerCamera && this._pointerCamera.cameraRigMode == Camera.RIG_MODE_NONE) {
  167. pointerInfo.pickInfo.ray.origin.copyFrom(this._pointerCamera!.globalPosition);
  168. zDragFactor = 0;
  169. }
  170. // Calculate controller drag distance in controller space
  171. var originDragDifference = pointerInfo.pickInfo.ray.origin.subtract(lastSixDofOriginPosition);
  172. lastSixDofOriginPosition.copyFrom(pointerInfo.pickInfo.ray.origin);
  173. var localOriginDragDifference = -Vector3.Dot(originDragDifference, pointerInfo.pickInfo.ray.direction);
  174. this._virtualOriginMesh.addChild(this._virtualDragMesh);
  175. // Determine how much the controller moved to/away towards the dragged object and use this to move the object further when its further away
  176. this._virtualDragMesh.position.z -= this._virtualDragMesh.position.z < 1 ? localOriginDragDifference * this.zDragFactor : localOriginDragDifference * zDragFactor * this._virtualDragMesh.position.z;
  177. if (this._virtualDragMesh.position.z < 0) {
  178. this._virtualDragMesh.position.z = 0;
  179. }
  180. // Update the controller position
  181. this._virtualOriginMesh.position.copyFrom(pointerInfo.pickInfo.ray.origin);
  182. this._virtualOriginMesh.lookAt(pointerInfo.pickInfo.ray.origin.add(pointerInfo.pickInfo.ray.direction));
  183. this._virtualOriginMesh.removeChild(this._virtualDragMesh);
  184. // Move the virtualObjectsPosition into the picked mesh's space if needed
  185. this._targetPosition.copyFrom(this._virtualDragMesh.absolutePosition);
  186. if (pickedMesh.parent) {
  187. Vector3.TransformCoordinatesToRef(this._targetPosition, Matrix.Invert(pickedMesh.parent.getWorldMatrix()), this._targetPosition);
  188. }
  189. if (!this._moving) {
  190. this._startingOrientation.copyFrom(this._virtualDragMesh.rotationQuaternion!);
  191. }
  192. this._moving = true;
  193. }
  194. }
  195. });
  196. var tmpQuaternion = new Quaternion();
  197. // On every frame move towards target scaling to avoid jitter caused by vr controllers
  198. this._sceneRenderObserver = ownerNode.getScene().onBeforeRenderObservable.add(() => {
  199. if (this.dragging && this._moving && pickedMesh) {
  200. PivotTools._RemoveAndStorePivotPoint(pickedMesh);
  201. // Slowly move mesh to avoid jitter
  202. pickedMesh.position.addInPlace(this._targetPosition.subtract(pickedMesh.position).scale(this.dragDeltaRatio));
  203. if (this.rotateDraggedObject) {
  204. // Get change in rotation
  205. tmpQuaternion.copyFrom(this._startingOrientation);
  206. tmpQuaternion.x = -tmpQuaternion.x;
  207. tmpQuaternion.y = -tmpQuaternion.y;
  208. tmpQuaternion.z = -tmpQuaternion.z;
  209. this._virtualDragMesh.rotationQuaternion!.multiplyToRef(tmpQuaternion, tmpQuaternion);
  210. // Convert change in rotation to only y axis rotation
  211. Quaternion.RotationYawPitchRollToRef(tmpQuaternion.toEulerAngles("xyz").y, 0, 0, tmpQuaternion);
  212. tmpQuaternion.multiplyToRef(this._startingOrientation, tmpQuaternion);
  213. // Slowly move mesh to avoid jitter
  214. var oldParent = pickedMesh.parent;
  215. // Only rotate the mesh if it's parent has uniform scaling
  216. if (!oldParent || ((oldParent as Mesh).scaling && !(oldParent as Mesh).scaling.isNonUniformWithinEpsilon(0.001))) {
  217. pickedMesh.setParent(null);
  218. Quaternion.SlerpToRef(pickedMesh.rotationQuaternion!, tmpQuaternion, this.dragDeltaRatio, pickedMesh.rotationQuaternion!);
  219. pickedMesh.setParent(oldParent);
  220. }
  221. }
  222. PivotTools._RestorePivotPoint(pickedMesh);
  223. this.onDragObservable.notifyObservers();
  224. }
  225. });
  226. }
  227. /**
  228. * Detaches the behavior from the mesh
  229. */
  230. public detach(): void {
  231. if (this._scene) {
  232. if (this.detachCameraControls && this._attachedToElement && this._pointerCamera && !this._pointerCamera.leftCamera) {
  233. this._pointerCamera.attachControl(true);
  234. this._attachedToElement = false;
  235. }
  236. this._scene.onPointerObservable.remove(this._pointerObserver);
  237. }
  238. if (this._ownerNode) {
  239. this._ownerNode.getScene().onBeforeRenderObservable.remove(this._sceneRenderObserver);
  240. }
  241. if (this._virtualOriginMesh) {
  242. this._virtualOriginMesh.dispose();
  243. }
  244. if (this._virtualDragMesh) {
  245. this._virtualDragMesh.dispose();
  246. }
  247. this.onDragEndObservable.clear();
  248. this.onDragObservable.clear();
  249. this.onDragStartObservable.clear();
  250. }
  251. }