sceneHelpers.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Logger } from "../Misc/logger.js";
  2. import { Scene } from "../scene.js";
  3. import { Vector3 } from "../Maths/math.vector.js";
  4. import { Texture } from "../Materials/Textures/texture.js";
  5. import { StandardMaterial } from "../Materials/standardMaterial.js";
  6. import { PBRMaterial } from "../Materials/PBR/pbrMaterial.js";
  7. import { HemisphericLight } from "../Lights/hemisphericLight.js";
  8. import { EnvironmentHelper } from "./environmentHelper.js";
  9. import { FreeCamera } from "../Cameras/freeCamera.js";
  10. import { ArcRotateCamera } from "../Cameras/arcRotateCamera.js";
  11. import { VRExperienceHelper } from "../Cameras/VR/vrExperienceHelper.js";
  12. import "../Materials/Textures/Loaders/ddsTextureLoader.js";
  13. import "../Materials/Textures/Loaders/envTextureLoader.js";
  14. import "../Materials/Textures/Loaders/ktxTextureLoader.js";
  15. import { CreateBox } from "../Meshes/Builders/boxBuilder.js";
  16. import { WebXRDefaultExperience } from "../XR/webXRDefaultExperience.js";
  17. /** @internal */
  18. // eslint-disable-next-line no-var
  19. export var _forceSceneHelpersToBundle = true;
  20. Scene.prototype.createDefaultLight = function (replace = false) {
  21. // Dispose existing light in replace mode.
  22. if (replace) {
  23. if (this.lights) {
  24. for (let i = 0; i < this.lights.length; i++) {
  25. this.lights[i].dispose();
  26. }
  27. }
  28. }
  29. // Light
  30. if (this.lights.length === 0) {
  31. new HemisphericLight("default light", Vector3.Up(), this);
  32. }
  33. };
  34. Scene.prototype.createDefaultCamera = function (createArcRotateCamera = false, replace = false, attachCameraControls = false) {
  35. // Dispose existing camera in replace mode.
  36. if (replace) {
  37. if (this.activeCamera) {
  38. this.activeCamera.dispose();
  39. this.activeCamera = null;
  40. }
  41. }
  42. // Camera
  43. if (!this.activeCamera) {
  44. const worldExtends = this.getWorldExtends((mesh) => mesh.isVisible && mesh.isEnabled());
  45. const worldSize = worldExtends.max.subtract(worldExtends.min);
  46. const worldCenter = worldExtends.min.add(worldSize.scale(0.5));
  47. let camera;
  48. let radius = worldSize.length() * 1.5;
  49. // empty scene scenario!
  50. if (!isFinite(radius)) {
  51. radius = 1;
  52. worldCenter.copyFromFloats(0, 0, 0);
  53. }
  54. if (createArcRotateCamera) {
  55. const arcRotateCamera = new ArcRotateCamera("default camera", -(Math.PI / 2), Math.PI / 2, radius, worldCenter, this);
  56. arcRotateCamera.lowerRadiusLimit = radius * 0.01;
  57. arcRotateCamera.wheelPrecision = 100 / radius;
  58. camera = arcRotateCamera;
  59. }
  60. else {
  61. const freeCamera = new FreeCamera("default camera", new Vector3(worldCenter.x, worldCenter.y, -radius), this);
  62. freeCamera.setTarget(worldCenter);
  63. camera = freeCamera;
  64. }
  65. camera.minZ = radius * 0.01;
  66. camera.maxZ = radius * 1000;
  67. camera.speed = radius * 0.2;
  68. this.activeCamera = camera;
  69. if (attachCameraControls) {
  70. camera.attachControl();
  71. }
  72. }
  73. };
  74. Scene.prototype.createDefaultCameraOrLight = function (createArcRotateCamera = false, replace = false, attachCameraControls = false) {
  75. this.createDefaultLight(replace);
  76. this.createDefaultCamera(createArcRotateCamera, replace, attachCameraControls);
  77. };
  78. Scene.prototype.createDefaultSkybox = function (environmentTexture, pbr = false, scale = 1000, blur = 0, setGlobalEnvTexture = true) {
  79. if (!environmentTexture) {
  80. Logger.Warn("Can not create default skybox without environment texture.");
  81. return null;
  82. }
  83. if (setGlobalEnvTexture) {
  84. if (environmentTexture) {
  85. this.environmentTexture = environmentTexture;
  86. }
  87. }
  88. // Skybox
  89. const hdrSkybox = CreateBox("hdrSkyBox", { size: scale }, this);
  90. if (pbr) {
  91. const hdrSkyboxMaterial = new PBRMaterial("skyBox", this);
  92. hdrSkyboxMaterial.backFaceCulling = false;
  93. hdrSkyboxMaterial.reflectionTexture = environmentTexture.clone();
  94. if (hdrSkyboxMaterial.reflectionTexture) {
  95. hdrSkyboxMaterial.reflectionTexture.coordinatesMode = Texture.SKYBOX_MODE;
  96. }
  97. hdrSkyboxMaterial.microSurface = 1.0 - blur;
  98. hdrSkyboxMaterial.disableLighting = true;
  99. hdrSkyboxMaterial.twoSidedLighting = true;
  100. hdrSkybox.material = hdrSkyboxMaterial;
  101. }
  102. else {
  103. const skyboxMaterial = new StandardMaterial("skyBox", this);
  104. skyboxMaterial.backFaceCulling = false;
  105. skyboxMaterial.reflectionTexture = environmentTexture.clone();
  106. if (skyboxMaterial.reflectionTexture) {
  107. skyboxMaterial.reflectionTexture.coordinatesMode = Texture.SKYBOX_MODE;
  108. }
  109. skyboxMaterial.disableLighting = true;
  110. hdrSkybox.material = skyboxMaterial;
  111. }
  112. hdrSkybox.isPickable = false;
  113. hdrSkybox.infiniteDistance = true;
  114. hdrSkybox.ignoreCameraMaxZ = true;
  115. return hdrSkybox;
  116. };
  117. Scene.prototype.createDefaultEnvironment = function (options) {
  118. if (EnvironmentHelper) {
  119. return new EnvironmentHelper(options, this);
  120. }
  121. return null;
  122. };
  123. Scene.prototype.createDefaultVRExperience = function (webVROptions = {}) {
  124. return new VRExperienceHelper(this, webVROptions);
  125. };
  126. Scene.prototype.createDefaultXRExperienceAsync = function (options = {}) {
  127. return WebXRDefaultExperience.CreateAsync(this, options).then((helper) => {
  128. return helper;
  129. });
  130. };
  131. //# sourceMappingURL=sceneHelpers.js.map