advancedDynamicTexture.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. import { DynamicTexture, Nullable, Observer, Camera, Engine, KeyboardInfoPre, PointerInfoPre, PointerInfo, Layer, Viewport, Scene, Texture, KeyboardEventTypes, Vector3, Matrix, Vector2, Tools, PointerEventTypes, AbstractMesh, StandardMaterial, Color3 } from "babylonjs";
  2. import { Container } from "./controls/container";
  3. import { Control } from "./controls/control";
  4. import { Style } from "./style";
  5. import { Measure } from "./measure";
  6. /**
  7. * Interface used to define a control that can receive focus
  8. */
  9. export interface IFocusableControl {
  10. /**
  11. * Function called when the control receives the focus
  12. */
  13. onFocus(): void;
  14. /**
  15. * Function called when the control loses the focus
  16. */
  17. onBlur(): void;
  18. /**
  19. * Function called to let the control handle keyboard events
  20. * @param evt defines the current keyboard event
  21. */
  22. processKeyboard(evt: KeyboardEvent): void;
  23. }
  24. /**
  25. * Class used to create texture to support 2D GUI elements
  26. * @see http://doc.babylonjs.com/how_to/gui
  27. */
  28. export class AdvancedDynamicTexture extends DynamicTexture {
  29. private _isDirty = false;
  30. private _renderObserver: Nullable<Observer<Camera>>;
  31. private _resizeObserver: Nullable<Observer<Engine>>;
  32. private _preKeyboardObserver: Nullable<Observer<KeyboardInfoPre>>;
  33. private _pointerMoveObserver: Nullable<Observer<PointerInfoPre>>;
  34. private _pointerObserver: Nullable<Observer<PointerInfo>>;
  35. private _canvasPointerOutObserver: Nullable<Observer<PointerEvent>>;
  36. private _background: string;
  37. /** @hidden */
  38. public _rootContainer = new Container("root");
  39. /** @hidden */
  40. public _lastPickedControl: Control;
  41. /** @hidden */
  42. public _lastControlOver: { [pointerId: number]: Control } = {};
  43. /** @hidden */
  44. public _lastControlDown: { [pointerId: number]: Control } = {};
  45. /** @hidden */
  46. public _capturingControl: { [pointerId: number]: Control } = {};
  47. /** @hidden */
  48. public _shouldBlockPointer: boolean;
  49. /** @hidden */
  50. public _layerToDispose: Nullable<Layer>;
  51. /** @hidden */
  52. public _linkedControls = new Array<Control>();
  53. private _isFullscreen = false;
  54. private _fullscreenViewport = new Viewport(0, 0, 1, 1);
  55. private _idealWidth = 0;
  56. private _idealHeight = 0;
  57. private _useSmallestIdeal: boolean = false;
  58. private _renderAtIdealSize = false;
  59. private _focusedControl: Nullable<IFocusableControl>;
  60. private _blockNextFocusCheck = false;
  61. private _renderScale = 1;
  62. /**
  63. * Gets or sets a boolean defining if alpha is stored as premultiplied
  64. */
  65. public premulAlpha = false;
  66. /**
  67. * Gets or sets a number used to scale rendering size (2 means that the texture will be twice bigger).
  68. * Useful when you want more antialiasing
  69. */
  70. public get renderScale(): number {
  71. return this._renderScale;
  72. }
  73. public set renderScale(value: number) {
  74. if (value === this._renderScale) {
  75. return;
  76. }
  77. this._renderScale = value;
  78. this._onResize();
  79. }
  80. /** Gets or sets the background color */
  81. public get background(): string {
  82. return this._background;
  83. }
  84. public set background(value: string) {
  85. if (this._background === value) {
  86. return;
  87. }
  88. this._background = value;
  89. this.markAsDirty();
  90. }
  91. /**
  92. * Gets or sets the ideal width used to design controls.
  93. * The GUI will then rescale everything accordingly
  94. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  95. */
  96. public get idealWidth(): number {
  97. return this._idealWidth;
  98. }
  99. public set idealWidth(value: number) {
  100. if (this._idealWidth === value) {
  101. return;
  102. }
  103. this._idealWidth = value;
  104. this.markAsDirty();
  105. this._rootContainer._markAllAsDirty();
  106. }
  107. /**
  108. * Gets or sets the ideal height used to design controls.
  109. * The GUI will then rescale everything accordingly
  110. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  111. */
  112. public get idealHeight(): number {
  113. return this._idealHeight;
  114. }
  115. public set idealHeight(value: number) {
  116. if (this._idealHeight === value) {
  117. return;
  118. }
  119. this._idealHeight = value;
  120. this.markAsDirty();
  121. this._rootContainer._markAllAsDirty();
  122. }
  123. /**
  124. * Gets or sets a boolean indicating if the smallest ideal value must be used if idealWidth and idealHeight are both set
  125. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  126. */
  127. public get useSmallestIdeal(): boolean {
  128. return this._useSmallestIdeal;
  129. }
  130. public set useSmallestIdeal(value: boolean) {
  131. if (this._useSmallestIdeal === value) {
  132. return;
  133. }
  134. this._useSmallestIdeal = value;
  135. this.markAsDirty();
  136. this._rootContainer._markAllAsDirty();
  137. }
  138. /**
  139. * Gets or sets a boolean indicating if adaptive scaling must be used
  140. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  141. */
  142. public get renderAtIdealSize(): boolean {
  143. return this._renderAtIdealSize;
  144. }
  145. public set renderAtIdealSize(value: boolean) {
  146. if (this._renderAtIdealSize === value) {
  147. return;
  148. }
  149. this._renderAtIdealSize = value;
  150. this._onResize();
  151. }
  152. /**
  153. * Gets the underlying layer used to render the texture when in fullscreen mode
  154. */
  155. public get layer(): Nullable<Layer> {
  156. return this._layerToDispose;
  157. }
  158. /**
  159. * Gets the root container control
  160. */
  161. public get rootContainer(): Container {
  162. return this._rootContainer;
  163. }
  164. /**
  165. * Gets or sets the current focused control
  166. */
  167. public get focusedControl(): Nullable<IFocusableControl> {
  168. return this._focusedControl;
  169. }
  170. public set focusedControl(control: Nullable<IFocusableControl>) {
  171. if (this._focusedControl == control) {
  172. return;
  173. }
  174. if (this._focusedControl) {
  175. this._focusedControl.onBlur();
  176. }
  177. if (control) {
  178. control.onFocus();
  179. }
  180. this._focusedControl = control;
  181. }
  182. /**
  183. * Gets or sets a boolean indicating if the texture must be rendered in background or foreground when in fullscreen mode
  184. */
  185. public get isForeground(): boolean {
  186. if (!this.layer) {
  187. return true;
  188. }
  189. return (!this.layer.isBackground);
  190. }
  191. public set isForeground(value: boolean) {
  192. if (!this.layer) {
  193. return;
  194. }
  195. if (this.layer.isBackground === !value) {
  196. return;
  197. }
  198. this.layer.isBackground = !value;
  199. }
  200. /**
  201. * Creates a new AdvancedDynamicTexture
  202. * @param name defines the name of the texture
  203. * @param width defines the width of the texture
  204. * @param height defines the height of the texture
  205. * @param scene defines the hosting scene
  206. * @param generateMipMaps defines a boolean indicating if mipmaps must be generated (false by default)
  207. * @param samplingMode defines the texture sampling mode (Texture.NEAREST_SAMPLINGMODE by default)
  208. */
  209. constructor(name: string, width = 0, height = 0, scene: Nullable<Scene>, generateMipMaps = false, samplingMode = Texture.NEAREST_SAMPLINGMODE) {
  210. super(name, { width: width, height: height }, scene, generateMipMaps, samplingMode, Engine.TEXTUREFORMAT_RGBA);
  211. scene = this.getScene();
  212. if (!scene || !this._texture) {
  213. return;
  214. }
  215. this._renderObserver = scene.onBeforeCameraRenderObservable.add((camera: Camera) => this._checkUpdate(camera));
  216. this._preKeyboardObserver = scene.onPreKeyboardObservable.add(info => {
  217. if (!this._focusedControl) {
  218. return;
  219. }
  220. if (info.type === KeyboardEventTypes.KEYDOWN) {
  221. this._focusedControl.processKeyboard(info.event);
  222. }
  223. info.skipOnPointerObservable = true;
  224. });
  225. this._rootContainer._link(null, this);
  226. this.hasAlpha = true;
  227. if (!width || !height) {
  228. this._resizeObserver = scene.getEngine().onResizeObservable.add(() => this._onResize());
  229. this._onResize();
  230. }
  231. this._texture.isReady = true;
  232. }
  233. /**
  234. * Function used to execute a function on all controls
  235. * @param func defines the function to execute
  236. * @param container defines the container where controls belong. If null the root container will be used
  237. */
  238. public executeOnAllControls(func: (control: Control) => void, container?: Container) {
  239. if (!container) {
  240. container = this._rootContainer;
  241. }
  242. func(container);
  243. for (var child of container.children) {
  244. if ((<any>child).children) {
  245. this.executeOnAllControls(func, (<Container>child));
  246. continue;
  247. }
  248. func(child);
  249. }
  250. }
  251. /**
  252. * Marks the texture as dirty forcing a complete update
  253. */
  254. public markAsDirty() {
  255. this._isDirty = true;
  256. this.executeOnAllControls((control) => {
  257. if (control._isFontSizeInPercentage) {
  258. control._resetFontCache();
  259. }
  260. });
  261. }
  262. /**
  263. * Helper function used to create a new style
  264. * @returns a new style
  265. * @see http://doc.babylonjs.com/how_to/gui#styles
  266. */
  267. public createStyle(): Style {
  268. return new Style(this);
  269. }
  270. /**
  271. * Adds a new control to the root container
  272. * @param control defines the control to add
  273. * @returns the current texture
  274. */
  275. public addControl(control: Control): AdvancedDynamicTexture {
  276. this._rootContainer.addControl(control);
  277. return this;
  278. }
  279. /**
  280. * Removes a control from the root container
  281. * @param control defines the control to remove
  282. * @returns the current texture
  283. */
  284. public removeControl(control: Control): AdvancedDynamicTexture {
  285. this._rootContainer.removeControl(control);
  286. return this;
  287. }
  288. /**
  289. * Release all resources
  290. */
  291. public dispose(): void {
  292. let scene = this.getScene();
  293. if (!scene) {
  294. return;
  295. }
  296. scene.onBeforeCameraRenderObservable.remove(this._renderObserver);
  297. if (this._resizeObserver) {
  298. scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  299. }
  300. if (this._pointerMoveObserver) {
  301. scene.onPrePointerObservable.remove(this._pointerMoveObserver);
  302. }
  303. if (this._pointerObserver) {
  304. scene.onPointerObservable.remove(this._pointerObserver);
  305. }
  306. if (this._preKeyboardObserver) {
  307. scene.onPreKeyboardObservable.remove(this._preKeyboardObserver);
  308. }
  309. if (this._canvasPointerOutObserver) {
  310. scene.getEngine().onCanvasPointerOutObservable.remove(this._canvasPointerOutObserver);
  311. }
  312. if (this._layerToDispose) {
  313. this._layerToDispose.texture = null;
  314. this._layerToDispose.dispose();
  315. this._layerToDispose = null;
  316. }
  317. this._rootContainer.dispose();
  318. super.dispose();
  319. }
  320. private _onResize(): void {
  321. let scene = this.getScene();
  322. if (!scene) {
  323. return;
  324. }
  325. // Check size
  326. var engine = scene.getEngine();
  327. var textureSize = this.getSize();
  328. var renderWidth = engine.getRenderWidth() * this._renderScale;
  329. var renderHeight = engine.getRenderHeight() * this._renderScale;
  330. if (this._renderAtIdealSize) {
  331. if (this._idealWidth) {
  332. renderHeight = (renderHeight * this._idealWidth) / renderWidth;
  333. renderWidth = this._idealWidth;
  334. } else if (this._idealHeight) {
  335. renderWidth = (renderWidth * this._idealHeight) / renderHeight;
  336. renderHeight = this._idealHeight;
  337. }
  338. }
  339. if (textureSize.width !== renderWidth || textureSize.height !== renderHeight) {
  340. this.scaleTo(renderWidth, renderHeight);
  341. this.markAsDirty();
  342. if (this._idealWidth || this._idealHeight) {
  343. this._rootContainer._markAllAsDirty();
  344. }
  345. }
  346. }
  347. /** @hidden */
  348. public _getGlobalViewport(scene: Scene): Viewport {
  349. var engine = scene.getEngine();
  350. return this._fullscreenViewport.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  351. }
  352. /**
  353. * Get screen coordinates for a vector3
  354. * @param position defines the position to project
  355. * @param worldMatrix defines the world matrix to use
  356. * @returns the projected position
  357. */
  358. public getProjectedPosition(position: Vector3, worldMatrix: Matrix): Vector2 {
  359. var scene = this.getScene();
  360. if (!scene) {
  361. return Vector2.Zero();
  362. }
  363. var globalViewport = this._getGlobalViewport(scene);
  364. var projectedPosition = Vector3.Project(position, worldMatrix, scene.getTransformMatrix(), globalViewport);
  365. projectedPosition.scaleInPlace(this.renderScale);
  366. return new Vector2(projectedPosition.x, projectedPosition.y);
  367. }
  368. private _checkUpdate(camera: Camera): void {
  369. if (this._layerToDispose) {
  370. if ((camera.layerMask & this._layerToDispose.layerMask) === 0) {
  371. return;
  372. }
  373. }
  374. if (this._isFullscreen && this._linkedControls.length) {
  375. var scene = this.getScene();
  376. if (!scene) {
  377. return;
  378. }
  379. var globalViewport = this._getGlobalViewport(scene);
  380. for (var control of this._linkedControls) {
  381. if (!control.isVisible) {
  382. continue;
  383. }
  384. var mesh = control._linkedMesh;
  385. if (!mesh || mesh.isDisposed()) {
  386. Tools.SetImmediate(() => {
  387. control.linkWithMesh(null);
  388. });
  389. continue;
  390. }
  391. var position = mesh.getBoundingInfo().boundingSphere.center;
  392. var projectedPosition = Vector3.Project(position, mesh.getWorldMatrix(), scene.getTransformMatrix(), globalViewport);
  393. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  394. control.notRenderable = true;
  395. continue;
  396. }
  397. control.notRenderable = false;
  398. // Account for RenderScale.
  399. projectedPosition.scaleInPlace(this.renderScale);
  400. control._moveToProjectedPosition(projectedPosition);
  401. }
  402. }
  403. if (!this._isDirty && !this._rootContainer.isDirty) {
  404. return;
  405. }
  406. this._isDirty = false;
  407. this._render();
  408. this.update(true, this.premulAlpha);
  409. }
  410. private _render(): void {
  411. var textureSize = this.getSize();
  412. var renderWidth = textureSize.width;
  413. var renderHeight = textureSize.height;
  414. // Clear
  415. var context = this.getContext();
  416. context.clearRect(0, 0, renderWidth, renderHeight);
  417. if (this._background) {
  418. context.save();
  419. context.fillStyle = this._background;
  420. context.fillRect(0, 0, renderWidth, renderHeight);
  421. context.restore();
  422. }
  423. // Render
  424. context.font = "18px Arial";
  425. context.strokeStyle = "white";
  426. var measure = new Measure(0, 0, renderWidth, renderHeight);
  427. this._rootContainer._draw(measure, context);
  428. }
  429. private _doPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): void {
  430. var scene = this.getScene();
  431. if (!scene) {
  432. return;
  433. }
  434. var engine = scene.getEngine();
  435. var textureSize = this.getSize();
  436. if (this._isFullscreen) {
  437. x = x * (textureSize.width / engine.getRenderWidth());
  438. y = y * (textureSize.height / engine.getRenderHeight());
  439. }
  440. if (this._capturingControl[pointerId]) {
  441. this._capturingControl[pointerId]._processObservables(type, x, y, pointerId, buttonIndex);
  442. return;
  443. }
  444. if (!this._rootContainer._processPicking(x, y, type, pointerId, buttonIndex)) {
  445. if (type === PointerEventTypes.POINTERMOVE) {
  446. if (this._lastControlOver[pointerId]) {
  447. this._lastControlOver[pointerId]._onPointerOut(this._lastControlOver[pointerId]);
  448. }
  449. delete this._lastControlOver[pointerId];
  450. }
  451. }
  452. this._manageFocus();
  453. }
  454. /** @hidden */
  455. public _cleanControlAfterRemovalFromList(list: { [pointerId: number]: Control }, control: Control) {
  456. for (var pointerId in list) {
  457. if (!list.hasOwnProperty(pointerId)) {
  458. continue;
  459. }
  460. var lastControlOver = list[pointerId];
  461. if (lastControlOver === control) {
  462. delete list[pointerId];
  463. }
  464. }
  465. }
  466. /** @hidden */
  467. public _cleanControlAfterRemoval(control: Control) {
  468. this._cleanControlAfterRemovalFromList(this._lastControlDown, control);
  469. this._cleanControlAfterRemovalFromList(this._lastControlOver, control);
  470. }
  471. /** Attach to all scene events required to support pointer events */
  472. public attach(): void {
  473. var scene = this.getScene();
  474. if (!scene) {
  475. return;
  476. }
  477. this._pointerMoveObserver = scene.onPrePointerObservable.add((pi, state) => {
  478. if (scene!.isPointerCaptured((<PointerEvent>(pi.event)).pointerId)) {
  479. return;
  480. }
  481. if (pi.type !== PointerEventTypes.POINTERMOVE
  482. && pi.type !== PointerEventTypes.POINTERUP
  483. && pi.type !== PointerEventTypes.POINTERDOWN) {
  484. return;
  485. }
  486. if (!scene) {
  487. return;
  488. }
  489. let camera = scene.cameraToUseForPointers || scene.activeCamera;
  490. if (!camera) {
  491. return;
  492. }
  493. let engine = scene.getEngine();
  494. let viewport = camera.viewport;
  495. let x = (scene.pointerX / engine.getHardwareScalingLevel() - viewport.x * engine.getRenderWidth()) / viewport.width;
  496. let y = (scene.pointerY / engine.getHardwareScalingLevel() - viewport.y * engine.getRenderHeight()) / viewport.height;
  497. this._shouldBlockPointer = false;
  498. // Do picking modifies _shouldBlockPointer
  499. this._doPicking(x, y, pi.type, (pi.event as PointerEvent).pointerId || 0, pi.event.button);
  500. // Avoid overwriting a true skipOnPointerObservable to false
  501. if(this._shouldBlockPointer){
  502. pi.skipOnPointerObservable = this._shouldBlockPointer;
  503. }
  504. });
  505. this._attachToOnPointerOut(scene);
  506. }
  507. /**
  508. * Connect the texture to a hosting mesh to enable interactions
  509. * @param mesh defines the mesh to attach to
  510. * @param supportPointerMove defines a boolean indicating if pointer move events must be catched as well
  511. */
  512. public attachToMesh(mesh: AbstractMesh, supportPointerMove = true): void {
  513. var scene = this.getScene();
  514. if (!scene) {
  515. return;
  516. }
  517. this._pointerObserver = scene.onPointerObservable.add((pi, state) => {
  518. if (pi.type !== PointerEventTypes.POINTERMOVE
  519. && pi.type !== PointerEventTypes.POINTERUP
  520. && pi.type !== PointerEventTypes.POINTERDOWN) {
  521. return;
  522. }
  523. var pointerId = (pi.event as PointerEvent).pointerId || 0;
  524. if (pi.pickInfo && pi.pickInfo.hit && pi.pickInfo.pickedMesh === mesh) {
  525. var uv = pi.pickInfo.getTextureCoordinates();
  526. if (uv) {
  527. let size = this.getSize();
  528. this._doPicking(uv.x * size.width, (1.0 - uv.y) * size.height, pi.type, pointerId, pi.event.button);
  529. }
  530. } else if (pi.type === PointerEventTypes.POINTERUP) {
  531. if (this._lastControlDown[pointerId]) {
  532. this._lastControlDown[pointerId]._forcePointerUp(pointerId);
  533. }
  534. delete this._lastControlDown[pointerId];
  535. this.focusedControl = null;
  536. } else if (pi.type === PointerEventTypes.POINTERMOVE) {
  537. if (this._lastControlOver[pointerId]) {
  538. this._lastControlOver[pointerId]._onPointerOut(this._lastControlOver[pointerId]);
  539. }
  540. delete this._lastControlOver[pointerId];
  541. }
  542. });
  543. mesh.enablePointerMoveEvents = supportPointerMove;
  544. this._attachToOnPointerOut(scene);
  545. }
  546. /**
  547. * Move the focus to a specific control
  548. * @param control defines the control which will receive the focus
  549. */
  550. public moveFocusToControl(control: IFocusableControl): void {
  551. this.focusedControl = control;
  552. this._lastPickedControl = <any>control;
  553. this._blockNextFocusCheck = true;
  554. }
  555. private _manageFocus(): void {
  556. if (this._blockNextFocusCheck) {
  557. this._blockNextFocusCheck = false;
  558. this._lastPickedControl = <any>this._focusedControl;
  559. return;
  560. }
  561. // Focus management
  562. if (this._focusedControl) {
  563. if (this._focusedControl !== (<any>this._lastPickedControl)) {
  564. if (this._lastPickedControl.isFocusInvisible) {
  565. return;
  566. }
  567. this.focusedControl = null;
  568. }
  569. }
  570. }
  571. private _attachToOnPointerOut(scene: Scene): void {
  572. this._canvasPointerOutObserver = scene.getEngine().onCanvasPointerOutObservable.add((pointerEvent) => {
  573. if (this._lastControlOver[pointerEvent.pointerId]) {
  574. this._lastControlOver[pointerEvent.pointerId]._onPointerOut(this._lastControlOver[pointerEvent.pointerId]);
  575. }
  576. delete this._lastControlOver[pointerEvent.pointerId];
  577. if (this._lastControlDown[pointerEvent.pointerId]) {
  578. this._lastControlDown[pointerEvent.pointerId]._forcePointerUp();
  579. }
  580. delete this._lastControlDown[pointerEvent.pointerId];
  581. });
  582. }
  583. // Statics
  584. /**
  585. * Creates a new AdvancedDynamicTexture in projected mode (ie. attached to a mesh)
  586. * @param mesh defines the mesh which will receive the texture
  587. * @param width defines the texture width (1024 by default)
  588. * @param height defines the texture height (1024 by default)
  589. * @param supportPointerMove defines a boolean indicating if the texture must capture move events (true by default)
  590. * @param onlyAlphaTesting defines a boolean indicating that alpha blending will not be used (only alpha testing) (false by default)
  591. * @returns a new AdvancedDynamicTexture
  592. */
  593. public static CreateForMesh(mesh: AbstractMesh, width = 1024, height = 1024, supportPointerMove = true, onlyAlphaTesting = false): AdvancedDynamicTexture {
  594. var result = new AdvancedDynamicTexture(mesh.name + " AdvancedDynamicTexture", width, height, mesh.getScene(), true, Texture.TRILINEAR_SAMPLINGMODE);
  595. var material = new StandardMaterial("AdvancedDynamicTextureMaterial", mesh.getScene());
  596. material.backFaceCulling = false;
  597. material.diffuseColor = Color3.Black();
  598. material.specularColor = Color3.Black();
  599. if (onlyAlphaTesting) {
  600. material.diffuseTexture = result;
  601. material.emissiveTexture = result;
  602. result.hasAlpha = true;
  603. } else {
  604. material.emissiveTexture = result;
  605. material.opacityTexture = result;
  606. }
  607. mesh.material = material;
  608. result.attachToMesh(mesh, supportPointerMove);
  609. return result;
  610. }
  611. /**
  612. * Creates a new AdvancedDynamicTexture in fullscreen mode.
  613. * In this mode the texture will rely on a layer for its rendering.
  614. * This allows it to be treated like any other layer.
  615. * As such, if you have a multi camera setup, you can set the layerMask on the GUI as well.
  616. * LayerMask is set through advancedTexture.layer.layerMask
  617. * @param name defines name for the texture
  618. * @param foreground defines a boolean indicating if the texture must be rendered in foreground (default is true)
  619. * @param scene defines the hsoting scene
  620. * @param sampling defines the texture sampling mode (Texture.BILINEAR_SAMPLINGMODE by default)
  621. * @returns a new AdvancedDynamicTexture
  622. */
  623. public static CreateFullscreenUI(name: string, foreground: boolean = true, scene: Nullable<Scene> = null, sampling = Texture.BILINEAR_SAMPLINGMODE): AdvancedDynamicTexture {
  624. var result = new AdvancedDynamicTexture(name, 0, 0, scene, false, sampling);
  625. // Display
  626. var layer = new Layer(name + "_layer", null, scene, !foreground);
  627. layer.texture = result;
  628. result._layerToDispose = layer;
  629. result._isFullscreen = true;
  630. // Attach
  631. result.attach();
  632. return result;
  633. }
  634. }