button3D.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Class used to create a button in 3D
  5. */
  6. export class Button3D extends Control3D {
  7. /** @hidden */
  8. protected _currentMaterial: Material;
  9. private _facadeTexture: AdvancedDynamicTexture;
  10. private _content: Control;
  11. /**
  12. * Creates a new button
  13. * @param name defines the control name
  14. */
  15. constructor(name?: string) {
  16. super(name);
  17. // Default animations
  18. this.pointerEnterAnimation = () => {
  19. if (!this.mesh) {
  20. return;
  21. }
  22. (<StandardMaterial>this._currentMaterial).emissiveColor = Color3.Red();
  23. }
  24. this.pointerOutAnimation = () => {
  25. (<StandardMaterial>this._currentMaterial).emissiveColor = Color3.Black();
  26. }
  27. this.pointerDownAnimation = () => {
  28. if (!this.mesh) {
  29. return;
  30. }
  31. this.mesh.scaling.scaleInPlace(0.95);
  32. }
  33. this.pointerUpAnimation = () => {
  34. if (!this.mesh) {
  35. return;
  36. }
  37. this.mesh.scaling.scaleInPlace(1.0 / 0.95);
  38. }
  39. }
  40. /**
  41. * Gets or sets the GUI 2D content used to display the button's facade
  42. */
  43. public get content(): Control {
  44. return this._content;
  45. }
  46. public set content(value: Control) {
  47. if (!this._host || !this._host.utilityLayer) {
  48. return;
  49. }
  50. if (!this._facadeTexture) {
  51. this._facadeTexture = new BABYLON.GUI.AdvancedDynamicTexture("Facade", 512, 512, this._host.utilityLayer.utilityLayerScene, true, BABYLON.Texture.TRILINEAR_SAMPLINGMODE);
  52. this._facadeTexture.rootContainer.scaleX = 2;
  53. this._facadeTexture.rootContainer.scaleY = 2;
  54. this._facadeTexture.premulAlpha = true;
  55. }
  56. this._facadeTexture.addControl(value);
  57. this._applyFacade(this._facadeTexture);
  58. }
  59. /**
  60. * Apply the facade texture (created from the content property).
  61. * This function can be overloaded by child classes
  62. * @param facadeTexture defines the AdvancedDynamicTexture to use
  63. */
  64. protected _applyFacade(facadeTexture: AdvancedDynamicTexture) {
  65. (<any>this._currentMaterial).emissiveTexture = facadeTexture;
  66. }
  67. protected _getTypeName(): string {
  68. return "Button3D";
  69. }
  70. // Mesh association
  71. protected _createNode(scene: Scene): TransformNode {
  72. var faceUV = new Array(6);
  73. for (var i = 0; i < 6; i++) {
  74. faceUV[i] = new BABYLON.Vector4(0, 0, 0, 0);
  75. }
  76. faceUV[1] = new BABYLON.Vector4(0, 0, 1, 1);
  77. let mesh = MeshBuilder.CreateBox(this.name + "_rootMesh", {
  78. width: 1.0,
  79. height: 1.0,
  80. depth: 0.08,
  81. faceUV: faceUV
  82. }, scene);
  83. return mesh;
  84. }
  85. protected _affectMaterial(mesh: AbstractMesh) {
  86. let material = new StandardMaterial(this.name + "Material", mesh.getScene());
  87. material.specularColor = Color3.Black();
  88. mesh.material = material;
  89. this._currentMaterial = material;
  90. }
  91. /**
  92. * Releases all associated resources
  93. */
  94. public dispose() {
  95. super.dispose();
  96. if (this._currentMaterial) {
  97. this._currentMaterial.dispose();
  98. }
  99. }
  100. }
  101. }