touchMeshButton3D.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. let newCollisionMesh = options.collisionMesh.clone("", options.collisionMesh.parent);
  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._currentMesh.getChildMeshes().forEach((mesh) => {
  63. mesh.metadata = this;
  64. });
  65. return this._currentMesh;
  66. }
  67. protected _affectMaterial(mesh: AbstractMesh) {
  68. }
  69. }