environment.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Environments
  2. var dropdownContentEnv = document.getElementById("dropdownContent-env");
  3. var btnEnvironment = document.getElementById("btnEnvironment");
  4. var skyboxes = [
  5. "https://assets.babylonjs.com/environments/environmentSpecular.env",
  6. "https://assets.babylonjs.com/environments/studio.env",
  7. ];
  8. var skyboxesNames = [
  9. "Default",
  10. "Studio",
  11. ];
  12. var readLocaStorageValue = function(key, defautlValue) {
  13. if (typeof (Storage) !== "undefined" && localStorage.getItem(key) !== null) {
  14. return parseInt(localStorage.getItem(key));
  15. }
  16. return defautlValue;
  17. }
  18. var defaultSkyboxIndex = readLocaStorageValue("defaultSkyboxId", 0);
  19. function loadSkyboxPathTexture(path, scene) {
  20. if (path.indexOf(".hdr") === (path.length - 4)) {
  21. return new BABYLON.HDRCubeTexture(path, scene, 256, false, true, false, true);
  22. }
  23. return BABYLON.CubeTexture.CreateFromPrefilteredData(path, scene);
  24. }
  25. function displayDropdownContentEnv(display) {
  26. if (display) {
  27. dropdownContentEnv.classList.remove("hidden");
  28. btnEnvironment.classList.add("open");
  29. clickInterceptor.classList.remove("hidden");
  30. }
  31. else {
  32. dropdownContentEnv.classList.add("hidden");
  33. btnEnvironment.classList.remove("open");
  34. clickInterceptor.classList.add("hidden");
  35. }
  36. }
  37. btnEnvironment.addEventListener('click', function() {
  38. displayDropdownContentEnv(dropdownContentEnv.classList.contains("hidden"));
  39. }, false);
  40. addEnvironmentLoader = function(index) {
  41. var env = document.createElement("div");
  42. env.innerHTML = skyboxesNames[index];
  43. env.title = skyboxesNames[index];
  44. env.addEventListener("click", function() {
  45. // hide the content of the dropdown
  46. displayDropdownContentEnv(false);
  47. if (typeof (Storage) !== "undefined") {
  48. localStorage.setItem("defaultSkyboxId", index);
  49. }
  50. defaultSkyboxIndex = index;
  51. skyboxPath = skyboxes[defaultSkyboxIndex];
  52. if (filesInput) {
  53. filesInput.reload();
  54. }
  55. else {
  56. var currentScene = BABYLON.Engine.LastCreatedScene;
  57. currentScene.environmentTexture = loadSkyboxPathTexture(skyboxPath, currentScene);
  58. for (var i = 0; i < currentScene.materials.length; i++) {
  59. var material = currentScene.materials[i];
  60. if (material.name === "skyBox") {
  61. var reflectionTexture = material.reflectionTexture;
  62. if (reflectionTexture && reflectionTexture.coordinatesMode === BABYLON.Texture.SKYBOX_MODE) {
  63. material.reflectionTexture = currentScene.environmentTexture.clone();
  64. if (material.reflectionTexture) {
  65. material.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. });
  72. return env;
  73. }
  74. for (var index = 0; index < skyboxes.length; index++) {
  75. var env = addEnvironmentLoader(index);
  76. dropdownContentEnv.appendChild(env);
  77. }