attachToBoxBehavior.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @hidden
  3. */
  4. class FaceDirectionInfo {
  5. constructor(public direction: Vector3, public rotatedDirection = new BABYLON.Vector3(), public diff = 0, public ignore = false) {}
  6. }
  7. /**
  8. * A behavior that when attached to a mesh will will place a specified node on the meshes face pointing towards the camera
  9. */
  10. export class AttachToBoxBehavior implements BABYLON.Behavior<BABYLON.Mesh> {
  11. /**
  12. * The name of the behavior
  13. */
  14. public name = "AttachToBoxBehavior";
  15. /**
  16. * The distance away from the face of the mesh that the UI should be attached to (default: 0.15)
  17. */
  18. public distanceAwayFromFace = 0.15;
  19. /**
  20. * The distance from the bottom of the face that the UI should be attached to (default: 0.15)
  21. */
  22. public distanceAwayFromBottomOfFace = 0.15;
  23. private _faceVectors = [new FaceDirectionInfo(BABYLON.Vector3.Up()), new FaceDirectionInfo(BABYLON.Vector3.Down()), new FaceDirectionInfo(BABYLON.Vector3.Left()), new FaceDirectionInfo(BABYLON.Vector3.Right()), new FaceDirectionInfo(BABYLON.Vector3.Forward()), new FaceDirectionInfo(BABYLON.Vector3.Forward().scaleInPlace(-1))];
  24. private _target: Mesh;
  25. private _scene: Scene;
  26. private _onRenderObserver: Nullable<Observer<Scene>>;
  27. private _tmpMatrix = new Matrix();
  28. private _tmpVector = new Vector3();
  29. /**
  30. * Creates the AttachToBoxBehavior, used to attach UI to the closest face of the box to a camera
  31. * @param ui The transform node that should be attched to the mesh
  32. */
  33. constructor(private ui: BABYLON.TransformNode) {
  34. /* Does nothing */
  35. }
  36. /**
  37. * Initializes the behavior
  38. */
  39. public init() {
  40. /* Does nothing */
  41. }
  42. private _closestFace(targetDirection: Vector3) {
  43. // Go over each face and calculate the angle between the face's normal and targetDirection
  44. this._faceVectors.forEach((v) => {
  45. if (!this._target.rotationQuaternion) {
  46. this._target.rotationQuaternion = Quaternion.RotationYawPitchRoll(this._target.rotation.y, this._target.rotation.x, this._target.rotation.z);
  47. }
  48. this._target.rotationQuaternion.toRotationMatrix(this._tmpMatrix);
  49. BABYLON.Vector3.TransformCoordinatesToRef(v.direction, this._tmpMatrix, v.rotatedDirection);
  50. v.diff = BABYLON.Vector3.GetAngleBetweenVectors(v.rotatedDirection, targetDirection, BABYLON.Vector3.Cross(v.rotatedDirection, targetDirection));
  51. });
  52. // Return the face information of the one with the normal closeset to target direction
  53. return this._faceVectors.reduce((min, p) => {
  54. if (min.ignore) {
  55. return p;
  56. }else if (p.ignore) {
  57. return min;
  58. }else {
  59. return min.diff < p.diff ? min : p;
  60. }
  61. }, this._faceVectors[0]);
  62. }
  63. private _zeroVector = Vector3.Zero();
  64. private _lookAtTmpMatrix = new Matrix();
  65. private _lookAtToRef(pos: Vector3, up = new BABYLON.Vector3(0, 1, 0), ref: Quaternion) {
  66. BABYLON.Matrix.LookAtLHToRef(this._zeroVector, pos, up, this._lookAtTmpMatrix);
  67. this._lookAtTmpMatrix.invert();
  68. BABYLON.Quaternion.FromRotationMatrixToRef(this._lookAtTmpMatrix, ref);
  69. }
  70. /**
  71. * Attaches the AttachToBoxBehavior to the passed in mesh
  72. * @param target The mesh that the specified node will be attached to
  73. */
  74. attach(target: BABYLON.Mesh) {
  75. this._target = target;
  76. this._scene = this._target.getScene();
  77. // Every frame, update the app bars position
  78. this._onRenderObserver = this._scene.onBeforeRenderObservable.add(() => {
  79. if (!this._scene.activeCamera) {
  80. return;
  81. }
  82. // Find the face closest to the cameras position
  83. var cameraPos = this._scene.activeCamera.position;
  84. if ((<WebVRFreeCamera>this._scene.activeCamera).devicePosition) {
  85. cameraPos = (<WebVRFreeCamera>this._scene.activeCamera).devicePosition;
  86. }
  87. var facing = this._closestFace(cameraPos.subtract(target.position));
  88. if (this._scene.activeCamera.leftCamera) {
  89. this._scene.activeCamera.leftCamera.computeWorldMatrix().getRotationMatrixToRef(this._tmpMatrix);
  90. }else {
  91. this._scene.activeCamera.computeWorldMatrix().getRotationMatrixToRef(this._tmpMatrix);
  92. }
  93. // Get camera up direction
  94. BABYLON.Vector3.TransformCoordinatesToRef(BABYLON.Vector3.Up(), this._tmpMatrix, this._tmpVector);
  95. // Ignore faces to not select a parrelel face for the up vector of the UI
  96. this._faceVectors.forEach((v) => {
  97. if (facing.direction.x && v.direction.x) {
  98. v.ignore = true;
  99. }
  100. if (facing.direction.y && v.direction.y) {
  101. v.ignore = true;
  102. }
  103. if (facing.direction.z && v.direction.z) {
  104. v.ignore = true;
  105. }
  106. });
  107. var facingUp = this._closestFace(this._tmpVector);
  108. // Unignore faces
  109. this._faceVectors.forEach((v) => {
  110. v.ignore = false;
  111. });
  112. // Position the app bar on that face
  113. this.ui.position.copyFrom(target.position);
  114. if (facing.direction.x) {
  115. facing.rotatedDirection.scaleToRef((target.scaling.x / 2) + this.distanceAwayFromFace, this._tmpVector);
  116. this.ui.position.addInPlace(this._tmpVector);
  117. }
  118. if (facing.direction.y) {
  119. facing.rotatedDirection.scaleToRef((target.scaling.y / 2) + this.distanceAwayFromFace, this._tmpVector);
  120. this.ui.position.addInPlace(this._tmpVector);
  121. }
  122. if (facing.direction.z) {
  123. facing.rotatedDirection.scaleToRef((target.scaling.z / 2) + this.distanceAwayFromFace, this._tmpVector);
  124. this.ui.position.addInPlace(this._tmpVector);
  125. }
  126. // Rotate to be oriented properly to the camera
  127. if (!this.ui.rotationQuaternion) {
  128. this.ui.rotationQuaternion = Quaternion.RotationYawPitchRoll(this.ui.rotation.y, this.ui.rotation.x, this.ui.rotation.z);
  129. }
  130. facing.rotatedDirection.scaleToRef(-1, this._tmpVector);
  131. this._lookAtToRef(this._tmpVector, facingUp.rotatedDirection, this.ui.rotationQuaternion);
  132. // Place ui the correct distance from the bottom of the mesh
  133. if (facingUp.direction.x) {
  134. this.ui.up.scaleToRef(this.distanceAwayFromBottomOfFace - target.scaling.x / 2, this._tmpVector);
  135. }
  136. if (facingUp.direction.y) {
  137. this.ui.up.scaleToRef(this.distanceAwayFromBottomOfFace - target.scaling.y / 2, this._tmpVector);
  138. }
  139. if (facingUp.direction.z) {
  140. this.ui.up.scaleToRef(this.distanceAwayFromBottomOfFace - target.scaling.z / 2, this._tmpVector);
  141. }
  142. this.ui.position.addInPlace(this._tmpVector);
  143. });
  144. }
  145. /**
  146. * Detaches the behavior from the mesh
  147. */
  148. detach() {
  149. this._scene.onBeforeRenderObservable.remove(this._onRenderObserver);
  150. }
  151. }