holographicButton.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import { Button3D } from "./button3D";
  2. import { Nullable } from "babylonjs/types";
  3. import { Observer } from "babylonjs/Misc/observable";
  4. import { Color3, Vector3 } from "babylonjs/Maths/math";
  5. import { StandardMaterial } from "babylonjs/Materials/standardMaterial";
  6. import { TransformNode } from "babylonjs/Meshes/transformNode";
  7. import { Mesh } from "babylonjs/Meshes/mesh";
  8. import { PlaneBuilder } from "babylonjs/Meshes/Builders/planeBuilder";
  9. import { BoxBuilder } from "babylonjs/Meshes/Builders/boxBuilder";
  10. import { FadeInOutBehavior } from "babylonjs/Behaviors/Meshes/fadeInOutBehavior";
  11. import { Scene } from "babylonjs/scene";
  12. import { FluentMaterial } from "../materials/fluentMaterial";
  13. import { StackPanel } from "../../2D/controls/stackPanel";
  14. import { Image } from "../../2D/controls/image";
  15. import { TextBlock } from "../../2D/controls/textBlock";
  16. import { AdvancedDynamicTexture } from "../../2D/advancedDynamicTexture";
  17. import { Control3D } from "./control3D";
  18. /**
  19. * Class used to create a holographic button in 3D
  20. */
  21. export class HolographicButton extends Button3D {
  22. private _backPlate: Mesh;
  23. private _textPlate: Mesh;
  24. private _frontPlate: Mesh;
  25. private _text: string;
  26. private _imageUrl: string;
  27. private _shareMaterials = true;
  28. private _frontMaterial: FluentMaterial;
  29. private _backMaterial: FluentMaterial;
  30. private _plateMaterial: StandardMaterial;
  31. private _pickedPointObserver: Nullable<Observer<Nullable<Vector3>>>;
  32. // Tooltip
  33. private _tooltipFade: Nullable<FadeInOutBehavior>;
  34. private _tooltipTextBlock: Nullable<TextBlock>;
  35. private _tooltipTexture: Nullable<AdvancedDynamicTexture>;
  36. private _tooltipMesh: Nullable<Mesh>;
  37. private _tooltipHoverObserver: Nullable<Observer<Control3D>>;
  38. private _tooltipOutObserver: Nullable<Observer<Control3D>>;
  39. private _disposeTooltip() {
  40. this._tooltipFade = null;
  41. if (this._tooltipTextBlock) {
  42. this._tooltipTextBlock.dispose();
  43. }
  44. if (this._tooltipTexture) {
  45. this._tooltipTexture.dispose();
  46. }
  47. if (this._tooltipMesh) {
  48. this._tooltipMesh.dispose();
  49. }
  50. this.onPointerEnterObservable.remove(this._tooltipHoverObserver);
  51. this.onPointerOutObservable.remove(this._tooltipOutObserver);
  52. }
  53. /**
  54. * Text to be displayed on the tooltip shown when hovering on the button. When set to null tooltip is disabled. (Default: null)
  55. */
  56. public set tooltipText(text: Nullable<string>) {
  57. if (!text) {
  58. this._disposeTooltip();
  59. return;
  60. }
  61. if (!this._tooltipFade) {
  62. // Create tooltip with mesh and text
  63. this._tooltipMesh = PlaneBuilder.CreatePlane("", { size: 1 }, this._backPlate._scene);
  64. var tooltipBackground = PlaneBuilder.CreatePlane("", { size: 1, sideOrientation: Mesh.DOUBLESIDE }, this._backPlate._scene);
  65. var mat = new StandardMaterial("", this._backPlate._scene);
  66. mat.diffuseColor = Color3.FromHexString("#212121");
  67. tooltipBackground.material = mat;
  68. tooltipBackground.isPickable = false;
  69. this._tooltipMesh.addChild(tooltipBackground);
  70. tooltipBackground.position.z = 0.05;
  71. this._tooltipMesh.scaling.y = 1 / 3;
  72. this._tooltipMesh.position.y = 0.7;
  73. this._tooltipMesh.position.z = -0.15;
  74. this._tooltipMesh.isPickable = false;
  75. this._tooltipMesh.parent = this._backPlate;
  76. // Create text texture for the tooltip
  77. this._tooltipTexture = AdvancedDynamicTexture.CreateForMesh(this._tooltipMesh);
  78. this._tooltipTextBlock = new TextBlock();
  79. this._tooltipTextBlock.scaleY = 3;
  80. this._tooltipTextBlock.color = "white";
  81. this._tooltipTextBlock.fontSize = 130;
  82. this._tooltipTexture.addControl(this._tooltipTextBlock);
  83. // Add hover action to tooltip
  84. this._tooltipFade = new FadeInOutBehavior();
  85. this._tooltipFade.delay = 500;
  86. this._tooltipMesh.addBehavior(this._tooltipFade);
  87. this._tooltipHoverObserver = this.onPointerEnterObservable.add(() => {
  88. if (this._tooltipFade) {
  89. this._tooltipFade.fadeIn(true);
  90. }
  91. });
  92. this._tooltipOutObserver = this.onPointerOutObservable.add(() => {
  93. if (this._tooltipFade) {
  94. this._tooltipFade.fadeIn(false);
  95. }
  96. });
  97. }
  98. if (this._tooltipTextBlock) {
  99. this._tooltipTextBlock.text = text;
  100. }
  101. }
  102. public get tooltipText() {
  103. if (this._tooltipTextBlock) {
  104. return this._tooltipTextBlock.text;
  105. }
  106. return null;
  107. }
  108. /**
  109. * Gets or sets text for the button
  110. */
  111. public get text(): string {
  112. return this._text;
  113. }
  114. public set text(value: string) {
  115. if (this._text === value) {
  116. return;
  117. }
  118. this._text = value;
  119. this._rebuildContent();
  120. }
  121. /**
  122. * Gets or sets the image url for the button
  123. */
  124. public get imageUrl(): string {
  125. return this._imageUrl;
  126. }
  127. public set imageUrl(value: string) {
  128. if (this._imageUrl === value) {
  129. return;
  130. }
  131. this._imageUrl = value;
  132. this._rebuildContent();
  133. }
  134. /**
  135. * Gets the back material used by this button
  136. */
  137. public get backMaterial(): FluentMaterial {
  138. return this._backMaterial;
  139. }
  140. /**
  141. * Gets the front material used by this button
  142. */
  143. public get frontMaterial(): FluentMaterial {
  144. return this._frontMaterial;
  145. }
  146. /**
  147. * Gets the plate material used by this button
  148. */
  149. public get plateMaterial(): StandardMaterial {
  150. return this._plateMaterial;
  151. }
  152. /**
  153. * Gets a boolean indicating if this button shares its material with other HolographicButtons
  154. */
  155. public get shareMaterials(): boolean {
  156. return this._shareMaterials;
  157. }
  158. /**
  159. * Creates a new button
  160. * @param name defines the control name
  161. */
  162. constructor(name?: string, shareMaterials = true) {
  163. super(name);
  164. this._shareMaterials = shareMaterials;
  165. // Default animations
  166. this.pointerEnterAnimation = () => {
  167. if (!this.mesh) {
  168. return;
  169. }
  170. this._frontPlate.setEnabled(true);
  171. };
  172. this.pointerOutAnimation = () => {
  173. if (!this.mesh) {
  174. return;
  175. }
  176. this._frontPlate.setEnabled(false);
  177. };
  178. }
  179. protected _getTypeName(): string {
  180. return "HolographicButton";
  181. }
  182. private _rebuildContent(): void {
  183. this._disposeFacadeTexture();
  184. let panel = new StackPanel();
  185. panel.isVertical = true;
  186. if (this._imageUrl) {
  187. let image = new Image();
  188. image.source = this._imageUrl;
  189. image.paddingTop = "40px";
  190. image.height = "180px";
  191. image.width = "100px";
  192. image.paddingBottom = "40px";
  193. panel.addControl(image);
  194. }
  195. if (this._text) {
  196. let text = new TextBlock();
  197. text.text = this._text;
  198. text.color = "white";
  199. text.height = "30px";
  200. text.fontSize = 24;
  201. panel.addControl(text);
  202. }
  203. if (this._frontPlate) {
  204. this.content = panel;
  205. }
  206. }
  207. // Mesh association
  208. protected _createNode(scene: Scene): TransformNode {
  209. this._backPlate = BoxBuilder.CreateBox(this.name + "BackMesh", {
  210. width: 1.0,
  211. height: 1.0,
  212. depth: 0.08
  213. }, scene);
  214. this._frontPlate = BoxBuilder.CreateBox(this.name + "FrontMesh", {
  215. width: 1.0,
  216. height: 1.0,
  217. depth: 0.08
  218. }, scene);
  219. this._frontPlate.parent = this._backPlate;
  220. this._frontPlate.position.z = -0.08;
  221. this._frontPlate.isPickable = false;
  222. this._frontPlate.setEnabled(false);
  223. this._textPlate = <Mesh>super._createNode(scene);
  224. this._textPlate.parent = this._backPlate;
  225. this._textPlate.position.z = -0.08;
  226. this._textPlate.isPickable = false;
  227. return this._backPlate;
  228. }
  229. protected _applyFacade(facadeTexture: AdvancedDynamicTexture) {
  230. this._plateMaterial.emissiveTexture = facadeTexture;
  231. this._plateMaterial.opacityTexture = facadeTexture;
  232. }
  233. private _createBackMaterial(mesh: Mesh) {
  234. this._backMaterial = new FluentMaterial(this.name + "Back Material", mesh.getScene());
  235. this._backMaterial.renderHoverLight = true;
  236. this._pickedPointObserver = this._host.onPickedPointChangedObservable.add((pickedPoint) => {
  237. if (pickedPoint) {
  238. this._backMaterial.hoverPosition = pickedPoint;
  239. this._backMaterial.hoverColor.a = 1.0;
  240. } else {
  241. this._backMaterial.hoverColor.a = 0;
  242. }
  243. });
  244. }
  245. private _createFrontMaterial(mesh: Mesh) {
  246. this._frontMaterial = new FluentMaterial(this.name + "Front Material", mesh.getScene());
  247. this._frontMaterial.innerGlowColorIntensity = 0; // No inner glow
  248. this._frontMaterial.alpha = 0.5; // Additive
  249. this._frontMaterial.renderBorders = true;
  250. }
  251. private _createPlateMaterial(mesh: Mesh) {
  252. this._plateMaterial = new StandardMaterial(this.name + "Plate Material", mesh.getScene());
  253. this._plateMaterial.specularColor = Color3.Black();
  254. }
  255. protected _affectMaterial(mesh: Mesh) {
  256. // Back
  257. if (this._shareMaterials) {
  258. if (!this._host._sharedMaterials["backFluentMaterial"]) {
  259. this._createBackMaterial(mesh);
  260. this._host._sharedMaterials["backFluentMaterial"] = this._backMaterial;
  261. } else {
  262. this._backMaterial = this._host._sharedMaterials["backFluentMaterial"] as FluentMaterial;
  263. }
  264. // Front
  265. if (!this._host._sharedMaterials["frontFluentMaterial"]) {
  266. this._createFrontMaterial(mesh);
  267. this._host._sharedMaterials["frontFluentMaterial"] = this._frontMaterial;
  268. } else {
  269. this._frontMaterial = this._host._sharedMaterials["frontFluentMaterial"] as FluentMaterial;
  270. }
  271. } else {
  272. this._createBackMaterial(mesh);
  273. this._createFrontMaterial(mesh);
  274. }
  275. this._createPlateMaterial(mesh);
  276. this._backPlate.material = this._backMaterial;
  277. this._frontPlate.material = this._frontMaterial;
  278. this._textPlate.material = this._plateMaterial;
  279. this._rebuildContent();
  280. }
  281. /**
  282. * Releases all associated resources
  283. */
  284. public dispose() {
  285. super.dispose(); // will dispose main mesh ie. back plate
  286. this._disposeTooltip();
  287. if (!this.shareMaterials) {
  288. this._backMaterial.dispose();
  289. this._frontMaterial.dispose();
  290. this._plateMaterial.dispose();
  291. if (this._pickedPointObserver) {
  292. this._host.onPickedPointChangedObservable.remove(this._pickedPointObserver);
  293. this._pickedPointObserver = null;
  294. }
  295. }
  296. }
  297. }