babylon.sixDofDragBehavior.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. module BABYLON {
  2. /**
  3. * 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
  4. */
  5. export class SixDofDragBehavior implements Behavior<Mesh> {
  6. private static _virtualScene:Scene;
  7. private _ownerNode:Mesh;
  8. private _sceneRenderObserver:Nullable<Observer<Scene>> = null;
  9. private _scene:Scene;
  10. private _targetPosition = new Vector3(0,0,0);
  11. private _virtualOriginMesh:AbstractMesh;
  12. private _virtualDragMesh:AbstractMesh;
  13. private _pointerObserver:Nullable<Observer<PointerInfo>>;
  14. private _moving = false;
  15. private _startingOrientation = new Quaternion();
  16. /**
  17. * 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: 5)
  18. */
  19. private zDragFactor = 5;
  20. /**
  21. * If the behavior is currently in a dragging state
  22. */
  23. public dragging = false;
  24. /**
  25. * 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)
  26. */
  27. public dragDeltaRatio = 0.2;
  28. /**
  29. * The id of the pointer that is currently interacting with the behavior (-1 when no pointer is active)
  30. */
  31. public currentDraggingPointerID = -1;
  32. constructor(){
  33. }
  34. /**
  35. * The name of the behavior
  36. */
  37. public get name(): string {
  38. return "SixDofDrag";
  39. }
  40. /**
  41. * Initializes the behavior
  42. */
  43. public init() {}
  44. /**
  45. * Attaches the scale behavior the passed in mesh
  46. * @param ownerNode The mesh that will be scaled around once attached
  47. */
  48. public attach(ownerNode: Mesh): void {
  49. this._ownerNode = ownerNode;
  50. this._scene = this._ownerNode.getScene();
  51. if(!SixDofDragBehavior._virtualScene){
  52. SixDofDragBehavior._virtualScene = new BABYLON.Scene(this._scene.getEngine());
  53. this._scene.getEngine().scenes.pop();
  54. }
  55. var pickedMesh:Nullable<AbstractMesh> = null;
  56. var lastSixDofOriginPosition = new BABYLON.Vector3(0,0,0);
  57. // Setup virtual meshes to be used for dragging without dirtying the existing scene
  58. this._virtualOriginMesh = new BABYLON.AbstractMesh("", SixDofDragBehavior._virtualScene);
  59. this._virtualOriginMesh.rotationQuaternion = new Quaternion();
  60. this._virtualDragMesh = new BABYLON.AbstractMesh("", SixDofDragBehavior._virtualScene);
  61. this._virtualDragMesh.rotationQuaternion = new Quaternion();
  62. var pickPredicate = (m:AbstractMesh)=>{
  63. return this._ownerNode == m || m.isDescendantOf(this._ownerNode);
  64. }
  65. this._pointerObserver = this._scene.onPointerObservable.add((pointerInfo, eventState)=>{
  66. if (pointerInfo.type == BABYLON.PointerEventTypes.POINTERDOWN) {
  67. if(!this.dragging && pointerInfo.pickInfo && pointerInfo.pickInfo.hit && pointerInfo.pickInfo.pickedMesh && pointerInfo.pickInfo.ray && pickPredicate(pointerInfo.pickInfo.pickedMesh)){
  68. pickedMesh = pointerInfo.pickInfo.pickedMesh;
  69. lastSixDofOriginPosition.copyFrom(pointerInfo.pickInfo.ray.origin);
  70. // Set position and orientation of the controller
  71. this._virtualOriginMesh.position.copyFrom(pointerInfo.pickInfo.ray.origin);
  72. this._virtualOriginMesh.lookAt(pointerInfo.pickInfo.ray.origin.subtract(pointerInfo.pickInfo.ray.direction));
  73. // Attach the virtual drag mesh to the virtual origin mesh so it can be dragged
  74. this._virtualOriginMesh.removeChild(this._virtualDragMesh);
  75. this._virtualDragMesh.position.copyFrom(pickedMesh.absolutePosition);
  76. if(!pickedMesh.rotationQuaternion){
  77. pickedMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(pickedMesh.rotation.y,pickedMesh.rotation.x,pickedMesh.rotation.z);
  78. }
  79. var oldParent=pickedMesh.parent;
  80. pickedMesh.setParent(null);
  81. this._virtualDragMesh.rotationQuaternion!.copyFrom(pickedMesh.rotationQuaternion);
  82. pickedMesh.setParent(oldParent);
  83. this._virtualOriginMesh.addChild(this._virtualDragMesh);
  84. // Update state
  85. this._targetPosition.copyFrom(this._virtualDragMesh.absolutePosition);
  86. this.dragging = true;
  87. this.currentDraggingPointerID = (<PointerEvent>pointerInfo.event).pointerId;
  88. }
  89. }else if(pointerInfo.type == BABYLON.PointerEventTypes.POINTERUP){
  90. if(this.currentDraggingPointerID == (<PointerEvent>pointerInfo.event).pointerId){
  91. this.dragging = false;
  92. this._moving = false;
  93. this.currentDraggingPointerID = -1;
  94. pickedMesh = null;
  95. this._virtualOriginMesh.removeChild(this._virtualDragMesh);
  96. }
  97. }else if(pointerInfo.type == BABYLON.PointerEventTypes.POINTERMOVE){
  98. if(this.currentDraggingPointerID == (<PointerEvent>pointerInfo.event).pointerId && this.dragging && pointerInfo.pickInfo && pointerInfo.pickInfo.ray && pickedMesh){
  99. // Calculate controller drag distance in controller space
  100. var originDragDifference = pointerInfo.pickInfo.ray.origin.subtract(lastSixDofOriginPosition);
  101. lastSixDofOriginPosition.copyFrom(pointerInfo.pickInfo.ray.origin);
  102. var localOriginDragDifference = Vector3.TransformCoordinates(originDragDifference, Matrix.Invert(this._virtualOriginMesh.getWorldMatrix().getRotationMatrix()));
  103. this._virtualOriginMesh.addChild(this._virtualDragMesh);
  104. // Determine how much the controller moved to/away towards the dragged object and use this to move the object further when its further away
  105. var zDragDistance = Vector3.Dot(localOriginDragDifference, this._virtualOriginMesh.position.normalizeToNew());
  106. this._virtualDragMesh.position.z -= this._virtualDragMesh.position.z < 1 ? zDragDistance*this.zDragFactor : zDragDistance*this.zDragFactor*this._virtualDragMesh.position.z;
  107. if(this._virtualDragMesh.position.z < 0){
  108. this._virtualDragMesh.position.z = 0;
  109. }
  110. // Update the controller position
  111. this._virtualOriginMesh.position.copyFrom(pointerInfo.pickInfo.ray.origin);
  112. this._virtualOriginMesh.lookAt(pointerInfo.pickInfo.ray.origin.subtract(pointerInfo.pickInfo.ray.direction));
  113. this._virtualOriginMesh.removeChild(this._virtualDragMesh)
  114. // Move the virtualObjectsPosition into the picked mesh's space if needed
  115. this._targetPosition.copyFrom(this._virtualDragMesh.absolutePosition);
  116. if(pickedMesh.parent){
  117. Vector3.TransformCoordinatesToRef(this._targetPosition, Matrix.Invert(pickedMesh.parent.getWorldMatrix()), this._targetPosition);
  118. }
  119. if(!this._moving){
  120. this._startingOrientation.copyFrom(this._virtualDragMesh.rotationQuaternion!)
  121. }
  122. this._moving = true;
  123. }
  124. }
  125. });
  126. var tmpQuaternion = new Quaternion();
  127. // On every frame move towards target scaling to avoid jitter caused by vr controllers
  128. this._sceneRenderObserver = ownerNode.getScene().onBeforeRenderObservable.add(()=>{
  129. if(this.dragging && this._moving && pickedMesh){
  130. // Slowly move mesh to avoid jitter
  131. pickedMesh.position.addInPlace(this._targetPosition.subtract(pickedMesh.position).scale(this.dragDeltaRatio));
  132. // Get change in rotation
  133. tmpQuaternion.copyFrom(this._startingOrientation);
  134. tmpQuaternion.x = -tmpQuaternion.x;
  135. tmpQuaternion.y = -tmpQuaternion.y;
  136. tmpQuaternion.z = -tmpQuaternion.z;
  137. this._virtualDragMesh.rotationQuaternion!.multiplyToRef(tmpQuaternion, tmpQuaternion);
  138. // Convert change in rotation to only y axis rotation
  139. Quaternion.RotationYawPitchRollToRef(tmpQuaternion.toEulerAngles("xyz").y,0,0, tmpQuaternion);
  140. tmpQuaternion.multiplyToRef(this._startingOrientation, tmpQuaternion);
  141. // Slowly move mesh to avoid jitter
  142. var oldParent=pickedMesh.parent;
  143. pickedMesh.setParent(null);
  144. Quaternion.SlerpToRef(pickedMesh.rotationQuaternion!, tmpQuaternion, this.dragDeltaRatio, pickedMesh.rotationQuaternion!);
  145. pickedMesh.setParent(oldParent);
  146. }
  147. });
  148. }
  149. /**
  150. * Detaches the behavior from the mesh
  151. */
  152. public detach(): void {
  153. this._scene.onPointerObservable.remove(this._pointerObserver);
  154. this._ownerNode.getScene().onBeforeRenderObservable.remove(this._sceneRenderObserver);
  155. this._virtualOriginMesh.dispose()
  156. this._virtualDragMesh.dispose();
  157. }
  158. }
  159. }