environmentHelper.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. import { Observable } from "../Misc/observable";
  2. import { Nullable } from "../types";
  3. import { ArcRotateCamera } from "../Cameras/arcRotateCamera";
  4. import { Scene } from "../scene";
  5. import { Vector3 } from "../Maths/math.vector";
  6. import { Color3, Color4 } from '../Maths/math.color';
  7. import { AbstractMesh } from "../Meshes/abstractMesh";
  8. import { Mesh } from "../Meshes/mesh";
  9. import { BaseTexture } from "../Materials/Textures/baseTexture";
  10. import { Texture } from "../Materials/Textures/texture";
  11. import { MirrorTexture } from "../Materials/Textures/mirrorTexture";
  12. import { CubeTexture } from "../Materials/Textures/cubeTexture";
  13. import { BackgroundMaterial } from "../Materials/Background/backgroundMaterial";
  14. import { _TimeToken } from "../Instrumentation/timeToken";
  15. import { _DepthCullingState, _StencilState, _AlphaState } from "../States/index";
  16. import { Constants } from "../Engines/constants";
  17. import "../Meshes/Builders/planeBuilder";
  18. import "../Meshes/Builders/boxBuilder";
  19. import { Plane } from '../Maths/math.plane';
  20. /**
  21. * Represents the different options available during the creation of
  22. * a Environment helper.
  23. *
  24. * This can control the default ground, skybox and image processing setup of your scene.
  25. */
  26. export interface IEnvironmentHelperOptions {
  27. /**
  28. * Specifies wether or not to create a ground.
  29. * True by default.
  30. */
  31. createGround: boolean;
  32. /**
  33. * Specifies the ground size.
  34. * 15 by default.
  35. */
  36. groundSize: number;
  37. /**
  38. * The texture used on the ground for the main color.
  39. * Comes from the BabylonJS CDN by default.
  40. *
  41. * Remarks: Can be either a texture or a url.
  42. */
  43. groundTexture: string | BaseTexture;
  44. /**
  45. * The color mixed in the ground texture by default.
  46. * BabylonJS clearColor by default.
  47. */
  48. groundColor: Color3;
  49. /**
  50. * Specifies the ground opacity.
  51. * 1 by default.
  52. */
  53. groundOpacity: number;
  54. /**
  55. * Enables the ground to receive shadows.
  56. * True by default.
  57. */
  58. enableGroundShadow: boolean;
  59. /**
  60. * Helps preventing the shadow to be fully black on the ground.
  61. * 0.5 by default.
  62. */
  63. groundShadowLevel: number;
  64. /**
  65. * Creates a mirror texture attach to the ground.
  66. * false by default.
  67. */
  68. enableGroundMirror: boolean;
  69. /**
  70. * Specifies the ground mirror size ratio.
  71. * 0.3 by default as the default kernel is 64.
  72. */
  73. groundMirrorSizeRatio: number;
  74. /**
  75. * Specifies the ground mirror blur kernel size.
  76. * 64 by default.
  77. */
  78. groundMirrorBlurKernel: number;
  79. /**
  80. * Specifies the ground mirror visibility amount.
  81. * 1 by default
  82. */
  83. groundMirrorAmount: number;
  84. /**
  85. * Specifies the ground mirror reflectance weight.
  86. * This uses the standard weight of the background material to setup the fresnel effect
  87. * of the mirror.
  88. * 1 by default.
  89. */
  90. groundMirrorFresnelWeight: number;
  91. /**
  92. * Specifies the ground mirror Falloff distance.
  93. * This can helps reducing the size of the reflection.
  94. * 0 by Default.
  95. */
  96. groundMirrorFallOffDistance: number;
  97. /**
  98. * Specifies the ground mirror texture type.
  99. * Unsigned Int by Default.
  100. */
  101. groundMirrorTextureType: number;
  102. /**
  103. * Specifies a bias applied to the ground vertical position to prevent z-fighting with
  104. * the shown objects.
  105. */
  106. groundYBias: number;
  107. /**
  108. * Specifies wether or not to create a skybox.
  109. * True by default.
  110. */
  111. createSkybox: boolean;
  112. /**
  113. * Specifies the skybox size.
  114. * 20 by default.
  115. */
  116. skyboxSize: number;
  117. /**
  118. * The texture used on the skybox for the main color.
  119. * Comes from the BabylonJS CDN by default.
  120. *
  121. * Remarks: Can be either a texture or a url.
  122. */
  123. skyboxTexture: string | BaseTexture;
  124. /**
  125. * The color mixed in the skybox texture by default.
  126. * BabylonJS clearColor by default.
  127. */
  128. skyboxColor: Color3;
  129. /**
  130. * The background rotation around the Y axis of the scene.
  131. * This helps aligning the key lights of your scene with the background.
  132. * 0 by default.
  133. */
  134. backgroundYRotation: number;
  135. /**
  136. * Compute automatically the size of the elements to best fit with the scene.
  137. */
  138. sizeAuto: boolean;
  139. /**
  140. * Default position of the rootMesh if autoSize is not true.
  141. */
  142. rootPosition: Vector3;
  143. /**
  144. * Sets up the image processing in the scene.
  145. * true by default.
  146. */
  147. setupImageProcessing: boolean;
  148. /**
  149. * The texture used as your environment texture in the scene.
  150. * Comes from the BabylonJS CDN by default and in use if setupImageProcessing is true.
  151. *
  152. * Remarks: Can be either a texture or a url.
  153. */
  154. environmentTexture: string | BaseTexture;
  155. /**
  156. * The value of the exposure to apply to the scene.
  157. * 0.6 by default if setupImageProcessing is true.
  158. */
  159. cameraExposure: number;
  160. /**
  161. * The value of the contrast to apply to the scene.
  162. * 1.6 by default if setupImageProcessing is true.
  163. */
  164. cameraContrast: number;
  165. /**
  166. * Specifies wether or not tonemapping should be enabled in the scene.
  167. * true by default if setupImageProcessing is true.
  168. */
  169. toneMappingEnabled: boolean;
  170. }
  171. interface ISceneSize {
  172. groundSize: number;
  173. skyboxSize: number;
  174. rootPosition: Vector3;
  175. }
  176. /**
  177. * The Environment helper class can be used to add a fully featuread none expensive background to your scene.
  178. * It includes by default a skybox and a ground relying on the BackgroundMaterial.
  179. * It also helps with the default setup of your imageProcessing configuration.
  180. */
  181. export class EnvironmentHelper {
  182. /**
  183. * Default ground texture URL.
  184. */
  185. private static _groundTextureCDNUrl = "https://assets.babylonjs.com/environments/backgroundGround.png";
  186. /**
  187. * Default skybox texture URL.
  188. */
  189. private static _skyboxTextureCDNUrl = "https://assets.babylonjs.com/environments/backgroundSkybox.dds";
  190. /**
  191. * Default environment texture URL.
  192. */
  193. private static _environmentTextureCDNUrl = "https://assets.babylonjs.com/environments/environmentSpecular.env";
  194. /**
  195. * Creates the default options for the helper.
  196. */
  197. private static _getDefaultOptions(): IEnvironmentHelperOptions {
  198. return {
  199. createGround: true,
  200. groundSize: 15,
  201. groundTexture: this._groundTextureCDNUrl,
  202. groundColor: new Color3(0.2, 0.2, 0.3).toLinearSpace().scale(3),
  203. groundOpacity: 0.9,
  204. enableGroundShadow: true,
  205. groundShadowLevel: 0.5,
  206. enableGroundMirror: false,
  207. groundMirrorSizeRatio: 0.3,
  208. groundMirrorBlurKernel: 64,
  209. groundMirrorAmount: 1,
  210. groundMirrorFresnelWeight: 1,
  211. groundMirrorFallOffDistance: 0,
  212. groundMirrorTextureType: Constants.TEXTURETYPE_UNSIGNED_INT,
  213. groundYBias: 0.00001,
  214. createSkybox: true,
  215. skyboxSize: 20,
  216. skyboxTexture: this._skyboxTextureCDNUrl,
  217. skyboxColor: new Color3(0.2, 0.2, 0.3).toLinearSpace().scale(3),
  218. backgroundYRotation: 0,
  219. sizeAuto: true,
  220. rootPosition: Vector3.Zero(),
  221. setupImageProcessing: true,
  222. environmentTexture: this._environmentTextureCDNUrl,
  223. cameraExposure: 0.8,
  224. cameraContrast: 1.2,
  225. toneMappingEnabled: true,
  226. };
  227. }
  228. private _rootMesh: Mesh;
  229. /**
  230. * Gets the root mesh created by the helper.
  231. */
  232. public get rootMesh(): Mesh {
  233. return this._rootMesh;
  234. }
  235. private _skybox: Nullable<Mesh>;
  236. /**
  237. * Gets the skybox created by the helper.
  238. */
  239. public get skybox(): Nullable<Mesh> {
  240. return this._skybox;
  241. }
  242. private _skyboxTexture: Nullable<BaseTexture>;
  243. /**
  244. * Gets the skybox texture created by the helper.
  245. */
  246. public get skyboxTexture(): Nullable<BaseTexture> {
  247. return this._skyboxTexture;
  248. }
  249. private _skyboxMaterial: Nullable<BackgroundMaterial>;
  250. /**
  251. * Gets the skybox material created by the helper.
  252. */
  253. public get skyboxMaterial(): Nullable<BackgroundMaterial> {
  254. return this._skyboxMaterial;
  255. }
  256. private _ground: Nullable<Mesh>;
  257. /**
  258. * Gets the ground mesh created by the helper.
  259. */
  260. public get ground(): Nullable<Mesh> {
  261. return this._ground;
  262. }
  263. private _groundTexture: Nullable<BaseTexture>;
  264. /**
  265. * Gets the ground texture created by the helper.
  266. */
  267. public get groundTexture(): Nullable<BaseTexture> {
  268. return this._groundTexture;
  269. }
  270. private _groundMirror: Nullable<MirrorTexture>;
  271. /**
  272. * Gets the ground mirror created by the helper.
  273. */
  274. public get groundMirror(): Nullable<MirrorTexture> {
  275. return this._groundMirror;
  276. }
  277. /**
  278. * Gets the ground mirror render list to helps pushing the meshes
  279. * you wish in the ground reflection.
  280. */
  281. public get groundMirrorRenderList(): Nullable<AbstractMesh[]> {
  282. if (this._groundMirror) {
  283. return this._groundMirror.renderList;
  284. }
  285. return null;
  286. }
  287. private _groundMaterial: Nullable<BackgroundMaterial>;
  288. /**
  289. * Gets the ground material created by the helper.
  290. */
  291. public get groundMaterial(): Nullable<BackgroundMaterial> {
  292. return this._groundMaterial;
  293. }
  294. /**
  295. * Stores the creation options.
  296. */
  297. private readonly _scene: Scene;
  298. private _options: IEnvironmentHelperOptions;
  299. /**
  300. * This observable will be notified with any error during the creation of the environment,
  301. * mainly texture creation errors.
  302. */
  303. public onErrorObservable: Observable<{ message?: string, exception?: any }>;
  304. /**
  305. * constructor
  306. * @param options Defines the options we want to customize the helper
  307. * @param scene The scene to add the material to
  308. */
  309. constructor(options: Partial<IEnvironmentHelperOptions>, scene: Scene) {
  310. this._options = {
  311. ...EnvironmentHelper._getDefaultOptions(),
  312. ...options
  313. };
  314. this._scene = scene;
  315. this.onErrorObservable = new Observable();
  316. this._setupBackground();
  317. this._setupImageProcessing();
  318. }
  319. /**
  320. * Updates the background according to the new options
  321. * @param options
  322. */
  323. public updateOptions(options: Partial<IEnvironmentHelperOptions>) {
  324. const newOptions = {
  325. ...this._options,
  326. ...options
  327. };
  328. if (this._ground && !newOptions.createGround) {
  329. this._ground.dispose();
  330. this._ground = null;
  331. }
  332. if (this._groundMaterial && !newOptions.createGround) {
  333. this._groundMaterial.dispose();
  334. this._groundMaterial = null;
  335. }
  336. if (this._groundTexture) {
  337. if (this._options.groundTexture != newOptions.groundTexture) {
  338. this._groundTexture.dispose();
  339. this._groundTexture = null;
  340. }
  341. }
  342. if (this._skybox && !newOptions.createSkybox) {
  343. this._skybox.dispose();
  344. this._skybox = null;
  345. }
  346. if (this._skyboxMaterial && !newOptions.createSkybox) {
  347. this._skyboxMaterial.dispose();
  348. this._skyboxMaterial = null;
  349. }
  350. if (this._skyboxTexture) {
  351. if (this._options.skyboxTexture != newOptions.skyboxTexture) {
  352. this._skyboxTexture.dispose();
  353. this._skyboxTexture = null;
  354. }
  355. }
  356. if (this._groundMirror && !newOptions.enableGroundMirror) {
  357. this._groundMirror.dispose();
  358. this._groundMirror = null;
  359. }
  360. if (this._scene.environmentTexture) {
  361. if (this._options.environmentTexture != newOptions.environmentTexture) {
  362. this._scene.environmentTexture.dispose();
  363. }
  364. }
  365. this._options = newOptions;
  366. this._setupBackground();
  367. this._setupImageProcessing();
  368. }
  369. /**
  370. * Sets the primary color of all the available elements.
  371. * @param color the main color to affect to the ground and the background
  372. */
  373. public setMainColor(color: Color3): void {
  374. if (this.groundMaterial) {
  375. this.groundMaterial.primaryColor = color;
  376. }
  377. if (this.skyboxMaterial) {
  378. this.skyboxMaterial.primaryColor = color;
  379. }
  380. if (this.groundMirror) {
  381. this.groundMirror.clearColor = new Color4(color.r, color.g, color.b, 1.0);
  382. }
  383. }
  384. /**
  385. * Setup the image processing according to the specified options.
  386. */
  387. private _setupImageProcessing(): void {
  388. if (this._options.setupImageProcessing) {
  389. this._scene.imageProcessingConfiguration.contrast = this._options.cameraContrast;
  390. this._scene.imageProcessingConfiguration.exposure = this._options.cameraExposure;
  391. this._scene.imageProcessingConfiguration.toneMappingEnabled = this._options.toneMappingEnabled;
  392. this._setupEnvironmentTexture();
  393. }
  394. }
  395. /**
  396. * Setup the environment texture according to the specified options.
  397. */
  398. private _setupEnvironmentTexture(): void {
  399. if (this._scene.environmentTexture) {
  400. return;
  401. }
  402. if (this._options.environmentTexture instanceof BaseTexture) {
  403. this._scene.environmentTexture = this._options.environmentTexture;
  404. return;
  405. }
  406. const environmentTexture = CubeTexture.CreateFromPrefilteredData(this._options.environmentTexture, this._scene);
  407. this._scene.environmentTexture = environmentTexture;
  408. }
  409. /**
  410. * Setup the background according to the specified options.
  411. */
  412. private _setupBackground(): void {
  413. if (!this._rootMesh) {
  414. this._rootMesh = new Mesh("BackgroundHelper", this._scene);
  415. }
  416. this._rootMesh.rotation.y = this._options.backgroundYRotation;
  417. const sceneSize = this._getSceneSize();
  418. if (this._options.createGround) {
  419. this._setupGround(sceneSize);
  420. this._setupGroundMaterial();
  421. this._setupGroundDiffuseTexture();
  422. if (this._options.enableGroundMirror) {
  423. this._setupGroundMirrorTexture(sceneSize);
  424. }
  425. this._setupMirrorInGroundMaterial();
  426. }
  427. if (this._options.createSkybox) {
  428. this._setupSkybox(sceneSize);
  429. this._setupSkyboxMaterial();
  430. this._setupSkyboxReflectionTexture();
  431. }
  432. this._rootMesh.position.x = sceneSize.rootPosition.x;
  433. this._rootMesh.position.z = sceneSize.rootPosition.z;
  434. this._rootMesh.position.y = sceneSize.rootPosition.y;
  435. }
  436. /**
  437. * Get the scene sizes according to the setup.
  438. */
  439. private _getSceneSize(): ISceneSize {
  440. let groundSize = this._options.groundSize;
  441. let skyboxSize = this._options.skyboxSize;
  442. let rootPosition = this._options.rootPosition;
  443. if (!this._scene.meshes || this._scene.meshes.length === 1) { // 1 only means the root of the helper.
  444. return { groundSize, skyboxSize, rootPosition };
  445. }
  446. const sceneExtends = this._scene.getWorldExtends((mesh) => {
  447. return (mesh !== this._ground && mesh !== this._rootMesh && mesh !== this._skybox);
  448. });
  449. const sceneDiagonal = sceneExtends.max.subtract(sceneExtends.min);
  450. if (this._options.sizeAuto) {
  451. if (this._scene.activeCamera instanceof ArcRotateCamera &&
  452. this._scene.activeCamera.upperRadiusLimit) {
  453. groundSize = this._scene.activeCamera.upperRadiusLimit * 2;
  454. skyboxSize = groundSize;
  455. }
  456. const sceneDiagonalLenght = sceneDiagonal.length();
  457. if (sceneDiagonalLenght > groundSize) {
  458. groundSize = sceneDiagonalLenght * 2;
  459. skyboxSize = groundSize;
  460. }
  461. // 10 % bigger.
  462. groundSize *= 1.1;
  463. skyboxSize *= 1.5;
  464. rootPosition = sceneExtends.min.add(sceneDiagonal.scale(0.5));
  465. rootPosition.y = sceneExtends.min.y - this._options.groundYBias;
  466. }
  467. return { groundSize, skyboxSize, rootPosition };
  468. }
  469. /**
  470. * Setup the ground according to the specified options.
  471. */
  472. private _setupGround(sceneSize: ISceneSize): void {
  473. if (!this._ground || this._ground.isDisposed()) {
  474. this._ground = Mesh.CreatePlane("BackgroundPlane", sceneSize.groundSize, this._scene);
  475. this._ground.rotation.x = Math.PI / 2; // Face up by default.
  476. this._ground.parent = this._rootMesh;
  477. this._ground.onDisposeObservable.add(() => { this._ground = null; });
  478. }
  479. this._ground.receiveShadows = this._options.enableGroundShadow;
  480. }
  481. /**
  482. * Setup the ground material according to the specified options.
  483. */
  484. private _setupGroundMaterial(): void {
  485. if (!this._groundMaterial) {
  486. this._groundMaterial = new BackgroundMaterial("BackgroundPlaneMaterial", this._scene);
  487. }
  488. this._groundMaterial.alpha = this._options.groundOpacity;
  489. this._groundMaterial.alphaMode = Constants.ALPHA_PREMULTIPLIED_PORTERDUFF;
  490. this._groundMaterial.shadowLevel = this._options.groundShadowLevel;
  491. this._groundMaterial.primaryColor = this._options.groundColor;
  492. this._groundMaterial.useRGBColor = false;
  493. this._groundMaterial.enableNoise = true;
  494. if (this._ground) {
  495. this._ground.material = this._groundMaterial;
  496. }
  497. }
  498. /**
  499. * Setup the ground diffuse texture according to the specified options.
  500. */
  501. private _setupGroundDiffuseTexture(): void {
  502. if (!this._groundMaterial) {
  503. return;
  504. }
  505. if (this._groundTexture) {
  506. return;
  507. }
  508. if (this._options.groundTexture instanceof BaseTexture) {
  509. this._groundMaterial.diffuseTexture = this._options.groundTexture;
  510. return;
  511. }
  512. const diffuseTexture = new Texture(this._options.groundTexture, this._scene, undefined, undefined, undefined, undefined, this._errorHandler);
  513. diffuseTexture.gammaSpace = false;
  514. diffuseTexture.hasAlpha = true;
  515. this._groundMaterial.diffuseTexture = diffuseTexture;
  516. }
  517. /**
  518. * Setup the ground mirror texture according to the specified options.
  519. */
  520. private _setupGroundMirrorTexture(sceneSize: ISceneSize): void {
  521. let wrapping = Texture.CLAMP_ADDRESSMODE;
  522. if (!this._groundMirror) {
  523. this._groundMirror = new MirrorTexture("BackgroundPlaneMirrorTexture",
  524. { ratio: this._options.groundMirrorSizeRatio },
  525. this._scene,
  526. false,
  527. this._options.groundMirrorTextureType,
  528. Texture.BILINEAR_SAMPLINGMODE,
  529. true);
  530. this._groundMirror.mirrorPlane = new Plane(0, -1, 0, sceneSize.rootPosition.y);
  531. this._groundMirror.anisotropicFilteringLevel = 1;
  532. this._groundMirror.wrapU = wrapping;
  533. this._groundMirror.wrapV = wrapping;
  534. this._groundMirror.gammaSpace = false;
  535. if (this._groundMirror.renderList) {
  536. for (let i = 0; i < this._scene.meshes.length; i++) {
  537. const mesh = this._scene.meshes[i];
  538. if (mesh !== this._ground &&
  539. mesh !== this._skybox &&
  540. mesh !== this._rootMesh) {
  541. this._groundMirror.renderList.push(mesh);
  542. }
  543. }
  544. }
  545. }
  546. this._groundMirror.clearColor = new Color4(
  547. this._options.groundColor.r,
  548. this._options.groundColor.g,
  549. this._options.groundColor.b,
  550. 1);
  551. this._groundMirror.adaptiveBlurKernel = this._options.groundMirrorBlurKernel;
  552. }
  553. /**
  554. * Setup the ground to receive the mirror texture.
  555. */
  556. private _setupMirrorInGroundMaterial(): void {
  557. if (this._groundMaterial) {
  558. this._groundMaterial.reflectionTexture = this._groundMirror;
  559. this._groundMaterial.reflectionFresnel = true;
  560. this._groundMaterial.reflectionAmount = this._options.groundMirrorAmount;
  561. this._groundMaterial.reflectionStandardFresnelWeight = this._options.groundMirrorFresnelWeight;
  562. this._groundMaterial.reflectionFalloffDistance = this._options.groundMirrorFallOffDistance;
  563. }
  564. }
  565. /**
  566. * Setup the skybox according to the specified options.
  567. */
  568. private _setupSkybox(sceneSize: ISceneSize): void {
  569. if (!this._skybox || this._skybox.isDisposed()) {
  570. this._skybox = Mesh.CreateBox("BackgroundSkybox", sceneSize.skyboxSize, this._scene, undefined, Mesh.BACKSIDE);
  571. this._skybox.onDisposeObservable.add(() => { this._skybox = null; });
  572. }
  573. this._skybox.parent = this._rootMesh;
  574. }
  575. /**
  576. * Setup the skybox material according to the specified options.
  577. */
  578. private _setupSkyboxMaterial(): void {
  579. if (!this._skybox) {
  580. return;
  581. }
  582. if (!this._skyboxMaterial) {
  583. this._skyboxMaterial = new BackgroundMaterial("BackgroundSkyboxMaterial", this._scene);
  584. }
  585. this._skyboxMaterial.useRGBColor = false;
  586. this._skyboxMaterial.primaryColor = this._options.skyboxColor;
  587. this._skyboxMaterial.enableNoise = true;
  588. this._skybox.material = this._skyboxMaterial;
  589. }
  590. /**
  591. * Setup the skybox reflection texture according to the specified options.
  592. */
  593. private _setupSkyboxReflectionTexture(): void {
  594. if (!this._skyboxMaterial) {
  595. return;
  596. }
  597. if (this._skyboxTexture) {
  598. return;
  599. }
  600. if (this._options.skyboxTexture instanceof BaseTexture) {
  601. this._skyboxMaterial.reflectionTexture = this._options.skyboxTexture;
  602. return;
  603. }
  604. this._skyboxTexture = new CubeTexture(this._options.skyboxTexture, this._scene, undefined, undefined, undefined, undefined, this._errorHandler);
  605. this._skyboxTexture.coordinatesMode = Texture.SKYBOX_MODE;
  606. this._skyboxTexture.gammaSpace = false;
  607. this._skyboxMaterial.reflectionTexture = this._skyboxTexture;
  608. }
  609. private _errorHandler = (message?: string, exception?: any) => {
  610. this.onErrorObservable.notifyObservers({ message: message, exception: exception });
  611. }
  612. /**
  613. * Dispose all the elements created by the Helper.
  614. */
  615. public dispose(): void {
  616. if (this._groundMaterial) {
  617. this._groundMaterial.dispose(true, true);
  618. }
  619. if (this._skyboxMaterial) {
  620. this._skyboxMaterial.dispose(true, true);
  621. }
  622. this._rootMesh.dispose(false);
  623. }
  624. }