meshButton3D.ts 2.0 KB

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