sceneHelpers.ts 10.0 KB

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