touchMeshButton3D.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, collisionMesh: Mesh, name?: string) {
  19. super(collisionMesh, name);
  20. this._currentMesh = mesh;
  21. /**
  22. * Provides a default behavior on hover/out & up/down
  23. * Override those function to create your own desired behavior specific to your mesh
  24. */
  25. this.pointerEnterAnimation = () => {
  26. if (!this.mesh) {
  27. return;
  28. }
  29. this.mesh.scaling.scaleInPlace(1.1);
  30. };
  31. this.pointerOutAnimation = () => {
  32. if (!this.mesh) {
  33. return;
  34. }
  35. this.mesh.scaling.scaleInPlace(1.0 / 1.1);
  36. };
  37. this.pointerDownAnimation = () => {
  38. if (!this.mesh) {
  39. return;
  40. }
  41. this.mesh.scaling.scaleInPlace(0.95);
  42. };
  43. this.pointerUpAnimation = () => {
  44. if (!this.mesh) {
  45. return;
  46. }
  47. this.mesh.scaling.scaleInPlace(1.0 / 0.95);
  48. };
  49. }
  50. protected _getTypeName(): string {
  51. return "TouchMeshButton3D";
  52. }
  53. // Mesh association
  54. protected _createNode(scene: Scene): TransformNode {
  55. this._enableCollisions(scene);
  56. this._currentMesh.getChildMeshes().forEach((mesh) => {
  57. mesh.metadata = this;
  58. });
  59. return this._currentMesh;
  60. }
  61. protected _affectMaterial(mesh: AbstractMesh) {
  62. }
  63. }