button3D.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. (<any>this._currentMaterial).emissiveTexture = this._facadeTexture;
  58. }
  59. protected _getTypeName(): string {
  60. return "Button3D";
  61. }
  62. // Mesh association
  63. protected _createNode(scene: Scene): TransformNode {
  64. var faceUV = new Array(6);
  65. for (var i = 0; i < 6; i++) {
  66. faceUV[i] = new BABYLON.Vector4(0, 0, 0, 0);
  67. }
  68. faceUV[1] = new BABYLON.Vector4(0, 0, 1, 1);
  69. let mesh = MeshBuilder.CreateBox(this.name + "Mesh", {
  70. width: 1.0,
  71. height: 1.0,
  72. depth: 0.05,
  73. faceUV: faceUV
  74. }, scene);
  75. return mesh;
  76. }
  77. protected _affectMaterial(mesh: Mesh) {
  78. let material = new StandardMaterial(this.name + "Material", mesh.getScene());
  79. material.specularColor = Color3.Black();
  80. mesh.material = material;
  81. this._currentMaterial = material;
  82. }
  83. }
  84. }