babylon.environmentHelper.ts 22 KB

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