touchMeshButton3D.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { TransformNode } from "babylonjs/Meshes/transformNode";
  2. import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
  3. import { Mesh } from "babylonjs/Meshes/mesh";
  4. import { Scene } from "babylonjs/scene";
  5. import { TouchButton3D } from "./touchButton3D";
  6. /**
  7. * Class used to create an interactable object. It's a touchable 3D button using a mesh coming from the current scene
  8. */
  9. export class TouchMeshButton3D extends TouchButton3D {
  10. /** @hidden */
  11. protected _currentMesh: Mesh;
  12. /**
  13. * Creates a new 3D button based on a mesh
  14. * @param mesh mesh to become a 3D button
  15. * @param collisionMesh mesh to track collisions with
  16. * @param name defines the control name
  17. */
  18. constructor(mesh: Mesh, options: {collisionMesh: Mesh, useDynamicMesh?: boolean}, name?: string) {
  19. if (options.useDynamicMesh) {
  20. super(name, options.collisionMesh);
  21. }
  22. else {
  23. var newCollisionMesh = options.collisionMesh.clone();
  24. newCollisionMesh.isVisible = false;
  25. super(name, newCollisionMesh);
  26. }
  27. this._currentMesh = mesh;
  28. /**
  29. * Provides a default behavior on hover/out & up/down
  30. * Override those function to create your own desired behavior specific to your mesh
  31. */
  32. this.pointerEnterAnimation = () => {
  33. if (!this.mesh) {
  34. return;
  35. }
  36. this.mesh.scaling.scaleInPlace(1.1);
  37. };
  38. this.pointerOutAnimation = () => {
  39. if (!this.mesh) {
  40. return;
  41. }
  42. this.mesh.scaling.scaleInPlace(1.0 / 1.1);
  43. };
  44. this.pointerDownAnimation = () => {
  45. if (!this.mesh) {
  46. return;
  47. }
  48. this.mesh.scaling.scaleInPlace(0.95);
  49. };
  50. this.pointerUpAnimation = () => {
  51. if (!this.mesh) {
  52. return;
  53. }
  54. this.mesh.scaling.scaleInPlace(1.0 / 0.95);
  55. };
  56. }
  57. protected _getTypeName(): string {
  58. return "TouchMeshButton3D";
  59. }
  60. // Mesh association
  61. protected _createNode(scene: Scene): TransformNode {
  62. this._enableCollisions(scene);
  63. this._currentMesh.getChildMeshes().forEach((mesh) => {
  64. mesh.metadata = this;
  65. });
  66. return this._currentMesh;
  67. }
  68. protected _affectMaterial(mesh: AbstractMesh) {
  69. }
  70. }