sceneHelpers.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { Logger } from "../Misc/logger";
  2. import { Nullable } from "../types";
  3. import { Scene } from "../scene";
  4. import { Vector3 } from "../Maths/math";
  5. import { Mesh } from "../Meshes/mesh";
  6. import { BaseTexture } from "../Materials/Textures/baseTexture";
  7. import { Texture } from "../Materials/Textures/texture";
  8. import { StandardMaterial } from "../Materials/standardMaterial";
  9. import { PBRMaterial } from "../Materials/PBR/pbrMaterial";
  10. import { HemisphericLight } from "../Lights/hemisphericLight";
  11. import { IEnvironmentHelperOptions, EnvironmentHelper } from "./environmentHelper";
  12. import { FreeCamera } from "../Cameras/freeCamera";
  13. import { ArcRotateCamera } from "../Cameras/arcRotateCamera";
  14. import { TargetCamera } from "../Cameras/targetCamera";
  15. import { VRExperienceHelperOptions, VRExperienceHelper } from "../Cameras/VR/vrExperienceHelper";
  16. import "../Materials/Textures/Loaders/ddsTextureLoader";
  17. import "../Materials/Textures/Loaders/envTextureLoader";
  18. import "../Materials/Textures/Loaders/ktxTextureLoader";
  19. import "../Meshes/Builders/boxBuilder";
  20. import { WebXRDefaultExperience, WebXRDefaultExperienceOptions } from '../Cameras/XR/webXRDefaultExperience';
  21. /** @hidden */
  22. export var _forceSceneHelpersToBundle = true;
  23. declare module "../scene" {
  24. export interface Scene {
  25. /**
  26. * Creates a default light for the scene.
  27. * @see http://doc.babylonjs.com/How_To/Fast_Build#create-default-light
  28. * @param replace has the default false, when true replaces the existing lights in the scene with a hemispheric light
  29. */
  30. createDefaultLight(replace?: boolean): void;
  31. /**
  32. * Creates a default camera for the scene.
  33. * @see http://doc.babylonjs.com/How_To/Fast_Build#create-default-camera
  34. * @param createArcRotateCamera has the default false which creates a free camera, when true creates an arc rotate camera
  35. * @param replace has default false, when true replaces the active camera in the scene
  36. * @param attachCameraControls has default false, when true attaches camera controls to the canvas.
  37. */
  38. createDefaultCamera(createArcRotateCamera?: boolean, replace?: boolean, attachCameraControls?: boolean): void;
  39. /**
  40. * Creates a default camera and a default light.
  41. * @see http://doc.babylonjs.com/how_to/Fast_Build#create-default-camera-or-light
  42. * @param createArcRotateCamera has the default false which creates a free camera, when true creates an arc rotate camera
  43. * @param replace has the default false, when true replaces the active camera/light in the scene
  44. * @param attachCameraControls has the default false, when true attaches camera controls to the canvas.
  45. */
  46. createDefaultCameraOrLight(createArcRotateCamera?: boolean, replace?: boolean, attachCameraControls?: boolean): void;
  47. /**
  48. * Creates a new sky box
  49. * @see http://doc.babylonjs.com/how_to/Fast_Build#create-default-skybox
  50. * @param environmentTexture defines the texture to use as environment texture
  51. * @param pbr has default false which requires the StandardMaterial to be used, when true PBRMaterial must be used
  52. * @param scale defines the overall scale of the skybox
  53. * @param blur is only available when pbr is true, default is 0, no blur, maximum value is 1
  54. * @param setGlobalEnvTexture has default true indicating that scene.environmentTexture must match the current skybox texture
  55. * @returns a new mesh holding the sky box
  56. */
  57. createDefaultSkybox(environmentTexture?: BaseTexture, pbr?: boolean, scale?: number, blur?: number, setGlobalEnvTexture?: boolean): Nullable<Mesh>;
  58. /**
  59. * Creates a new environment
  60. * @see http://doc.babylonjs.com/How_To/Fast_Build#create-default-environment
  61. * @param options defines the options you can use to configure the environment
  62. * @returns the new EnvironmentHelper
  63. */
  64. createDefaultEnvironment(options?: Partial<IEnvironmentHelperOptions>): Nullable<EnvironmentHelper>;
  65. /**
  66. * Creates a new VREXperienceHelper
  67. * @see http://doc.babylonjs.com/how_to/webvr_helper
  68. * @param webVROptions defines the options used to create the new VREXperienceHelper
  69. * @returns a new VREXperienceHelper
  70. */
  71. createDefaultVRExperience(webVROptions?: VRExperienceHelperOptions): VRExperienceHelper;
  72. /**
  73. * Creates a new WebXRDefaultExperience
  74. * @see http://doc.babylonjs.com/how_to/webxr
  75. * @param options experience options
  76. * @returns a promise for a new WebXRDefaultExperience
  77. */
  78. createDefaultXRExperienceAsync(options: WebXRDefaultExperienceOptions): Promise<WebXRDefaultExperience>;
  79. }
  80. }
  81. Scene.prototype.createDefaultLight = function(replace = false): void {
  82. // Dispose existing light in replace mode.
  83. if (replace) {
  84. if (this.lights) {
  85. for (var i = 0; i < this.lights.length; i++) {
  86. this.lights[i].dispose();
  87. }
  88. }
  89. }
  90. // Light
  91. if (this.lights.length === 0) {
  92. new HemisphericLight("default light", Vector3.Up(), this);
  93. }
  94. };
  95. Scene.prototype.createDefaultCamera = function(createArcRotateCamera = false, replace = false, attachCameraControls = false): void {
  96. // Dispose existing camera in replace mode.
  97. if (replace) {
  98. if (this.activeCamera) {
  99. this.activeCamera.dispose();
  100. this.activeCamera = null;
  101. }
  102. }
  103. // Camera
  104. if (!this.activeCamera) {
  105. var worldExtends = this.getWorldExtends();
  106. var worldSize = worldExtends.max.subtract(worldExtends.min);
  107. var worldCenter = worldExtends.min.add(worldSize.scale(0.5));
  108. var camera: TargetCamera;
  109. var radius = worldSize.length() * 1.5;
  110. // empty scene scenario!
  111. if (!isFinite(radius)) {
  112. radius = 1;
  113. worldCenter.copyFromFloats(0, 0, 0);
  114. }
  115. if (createArcRotateCamera) {
  116. var arcRotateCamera = new ArcRotateCamera("default camera", -(Math.PI / 2), Math.PI / 2, radius, worldCenter, this);
  117. arcRotateCamera.lowerRadiusLimit = radius * 0.01;
  118. arcRotateCamera.wheelPrecision = 100 / radius;
  119. camera = arcRotateCamera;
  120. }
  121. else {
  122. var freeCamera = new FreeCamera("default camera", new Vector3(worldCenter.x, worldCenter.y, -radius), this);
  123. freeCamera.setTarget(worldCenter);
  124. camera = freeCamera;
  125. }
  126. camera.minZ = radius * 0.01;
  127. camera.maxZ = radius * 1000;
  128. camera.speed = radius * 0.2;
  129. this.activeCamera = camera;
  130. let canvas = this.getEngine().getRenderingCanvas();
  131. if (attachCameraControls && canvas) {
  132. camera.attachControl(canvas);
  133. }
  134. }
  135. };
  136. Scene.prototype.createDefaultCameraOrLight = function(createArcRotateCamera = false, replace = false, attachCameraControls = false): void {
  137. this.createDefaultLight(replace);
  138. this.createDefaultCamera(createArcRotateCamera, replace, attachCameraControls);
  139. };
  140. Scene.prototype.createDefaultSkybox = function(environmentTexture?: BaseTexture, pbr = false, scale = 1000, blur = 0, setGlobalEnvTexture = true): Nullable<Mesh> {
  141. if (!environmentTexture) {
  142. Logger.Warn("Can not create default skybox without environment texture.");
  143. return null;
  144. }
  145. if (setGlobalEnvTexture) {
  146. if (environmentTexture) {
  147. this.environmentTexture = environmentTexture;
  148. }
  149. }
  150. // Skybox
  151. var hdrSkybox = Mesh.CreateBox("hdrSkyBox", scale, this);
  152. if (pbr) {
  153. let hdrSkyboxMaterial = new PBRMaterial("skyBox", this);
  154. hdrSkyboxMaterial.backFaceCulling = false;
  155. hdrSkyboxMaterial.reflectionTexture = environmentTexture.clone();
  156. if (hdrSkyboxMaterial.reflectionTexture) {
  157. hdrSkyboxMaterial.reflectionTexture.coordinatesMode = Texture.SKYBOX_MODE;
  158. }
  159. hdrSkyboxMaterial.microSurface = 1.0 - blur;
  160. hdrSkyboxMaterial.disableLighting = true;
  161. hdrSkyboxMaterial.twoSidedLighting = true;
  162. hdrSkybox.infiniteDistance = true;
  163. hdrSkybox.material = hdrSkyboxMaterial;
  164. }
  165. else {
  166. let skyboxMaterial = new StandardMaterial("skyBox", this);
  167. skyboxMaterial.backFaceCulling = false;
  168. skyboxMaterial.reflectionTexture = environmentTexture.clone();
  169. if (skyboxMaterial.reflectionTexture) {
  170. skyboxMaterial.reflectionTexture.coordinatesMode = Texture.SKYBOX_MODE;
  171. }
  172. skyboxMaterial.disableLighting = true;
  173. hdrSkybox.infiniteDistance = true;
  174. hdrSkybox.material = skyboxMaterial;
  175. }
  176. hdrSkybox.isPickable = false;
  177. return hdrSkybox;
  178. };
  179. Scene.prototype.createDefaultEnvironment = function(options: Partial<IEnvironmentHelperOptions>): Nullable<EnvironmentHelper> {
  180. if (EnvironmentHelper) {
  181. return new EnvironmentHelper(options, this);
  182. }
  183. return null;
  184. };
  185. Scene.prototype.createDefaultVRExperience = function(webVROptions: VRExperienceHelperOptions = {}): VRExperienceHelper {
  186. return new VRExperienceHelper(this, webVROptions);
  187. };
  188. Scene.prototype.createDefaultXRExperienceAsync = function(options: WebXRDefaultExperienceOptions): Promise<WebXRDefaultExperience> {
  189. return WebXRDefaultExperience.CreateAsync(this, options).then((helper) => {
  190. return helper;
  191. });
  192. };