babylon.sceneHelpers.ts 8.6 KB

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