meshButton3D.ts 1.9 KB

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