@babylonjs/viewer 5.0.0 → 5.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/configuration/configuration.d.ts +107 -107
  2. package/configuration/configuration.js +15 -15
  3. package/configuration/configurationCompatibility.d.ts +8 -8
  4. package/configuration/configurationCompatibility.js +65 -65
  5. package/configuration/configurationContainer.d.ts +10 -10
  6. package/configuration/configurationContainer.js +9 -9
  7. package/configuration/globals.d.ts +6 -6
  8. package/configuration/globals.js +17 -17
  9. package/configuration/index.d.ts +2 -2
  10. package/configuration/index.js +3 -3
  11. package/configuration/interfaces/cameraConfiguration.d.ts +31 -31
  12. package/configuration/interfaces/cameraConfiguration.js +1 -1
  13. package/configuration/interfaces/colorGradingConfiguration.d.ts +81 -81
  14. package/configuration/interfaces/colorGradingConfiguration.js +1 -1
  15. package/configuration/interfaces/defaultRenderingPipelineConfiguration.d.ts +20 -20
  16. package/configuration/interfaces/defaultRenderingPipelineConfiguration.js +1 -1
  17. package/configuration/interfaces/environmentMapConfiguration.d.ts +22 -22
  18. package/configuration/interfaces/environmentMapConfiguration.js +1 -1
  19. package/configuration/interfaces/groundConfiguration.d.ts +24 -24
  20. package/configuration/interfaces/groundConfiguration.js +1 -1
  21. package/configuration/interfaces/imageProcessingConfiguration.d.ts +43 -43
  22. package/configuration/interfaces/imageProcessingConfiguration.js +1 -1
  23. package/configuration/interfaces/index.d.ts +15 -15
  24. package/configuration/interfaces/index.js +15 -15
  25. package/configuration/interfaces/lightConfiguration.d.ts +60 -60
  26. package/configuration/interfaces/lightConfiguration.js +1 -1
  27. package/configuration/interfaces/modelAnimationConfiguration.d.ts +26 -26
  28. package/configuration/interfaces/modelAnimationConfiguration.js +1 -1
  29. package/configuration/interfaces/modelConfiguration.d.ts +65 -65
  30. package/configuration/interfaces/modelConfiguration.js +1 -1
  31. package/configuration/interfaces/observersConfiguration.d.ts +5 -5
  32. package/configuration/interfaces/observersConfiguration.js +1 -1
  33. package/configuration/interfaces/sceneConfiguration.d.ts +48 -48
  34. package/configuration/interfaces/sceneConfiguration.js +1 -1
  35. package/configuration/interfaces/sceneOptimizerConfiguration.d.ts +23 -23
  36. package/configuration/interfaces/sceneOptimizerConfiguration.js +1 -1
  37. package/configuration/interfaces/skyboxConfiguration.d.ts +21 -21
  38. package/configuration/interfaces/skyboxConfiguration.js +1 -1
  39. package/configuration/interfaces/templateConfiguration.d.ts +67 -67
  40. package/configuration/interfaces/templateConfiguration.js +1 -1
  41. package/configuration/interfaces/vrConfiguration.d.ts +16 -16
  42. package/configuration/interfaces/vrConfiguration.js +1 -1
  43. package/configuration/loader.d.ts +4 -4
  44. package/configuration/loader.js +16 -16
  45. package/configuration/mappers.d.ts +42 -42
  46. package/configuration/mappers.js +190 -190
  47. package/configuration/renderOnlyLoader.d.ts +33 -33
  48. package/configuration/renderOnlyLoader.js +161 -161
  49. package/configuration/types/default.d.ts +6 -6
  50. package/configuration/types/default.js +120 -120
  51. package/configuration/types/environmentMap.d.ts +5 -5
  52. package/configuration/types/environmentMap.js +13 -13
  53. package/configuration/types/extended.d.ts +6 -6
  54. package/configuration/types/extended.js +316 -316
  55. package/configuration/types/index.d.ts +13 -13
  56. package/configuration/types/index.js +49 -49
  57. package/configuration/types/minimal.d.ts +6 -6
  58. package/configuration/types/minimal.js +42 -42
  59. package/configuration/types/renderOnlyDefault.d.ts +30 -30
  60. package/configuration/types/renderOnlyDefault.js +30 -30
  61. package/configuration/types/shadowLight.d.ts +9 -9
  62. package/configuration/types/shadowLight.js +63 -63
  63. package/helper/index.d.ts +26 -26
  64. package/helper/index.js +62 -62
  65. package/index.d.ts +30 -30
  66. package/index.js +45 -45
  67. package/initializer.d.ts +11 -11
  68. package/initializer.js +34 -34
  69. package/interfaces.d.ts +5 -5
  70. package/interfaces.js +1 -1
  71. package/labs/environmentSerializer.d.ts +126 -126
  72. package/labs/environmentSerializer.js +190 -190
  73. package/labs/texture.d.ts +183 -183
  74. package/labs/texture.js +300 -300
  75. package/labs/viewerLabs.d.ts +51 -51
  76. package/labs/viewerLabs.js +133 -133
  77. package/loader/modelLoader.d.ts +47 -47
  78. package/loader/modelLoader.js +189 -189
  79. package/loader/plugins/applyMaterialConfig.d.ts +12 -12
  80. package/loader/plugins/applyMaterialConfig.js +15 -15
  81. package/loader/plugins/extendedMaterialLoaderPlugin.d.ts +9 -9
  82. package/loader/plugins/extendedMaterialLoaderPlugin.js +15 -15
  83. package/loader/plugins/index.d.ts +18 -18
  84. package/loader/plugins/index.js +42 -42
  85. package/loader/plugins/loaderPlugin.d.ts +24 -24
  86. package/loader/plugins/loaderPlugin.js +1 -1
  87. package/loader/plugins/msftLodLoaderPlugin.d.ts +12 -12
  88. package/loader/plugins/msftLodLoaderPlugin.js +20 -20
  89. package/loader/plugins/telemetryLoaderPlugin.d.ts +12 -12
  90. package/loader/plugins/telemetryLoaderPlugin.js +35 -35
  91. package/managers/observablesManager.d.ts +66 -66
  92. package/managers/observablesManager.js +34 -34
  93. package/managers/sceneManager.d.ts +243 -243
  94. package/managers/sceneManager.js +1388 -1388
  95. package/managers/telemetryManager.d.ts +57 -57
  96. package/managers/telemetryManager.js +113 -113
  97. package/model/modelAnimation.d.ts +215 -215
  98. package/model/modelAnimation.js +232 -232
  99. package/model/viewerModel.d.ts +228 -228
  100. package/model/viewerModel.js +669 -669
  101. package/optimizer/custom/extended.d.ts +11 -11
  102. package/optimizer/custom/extended.js +98 -98
  103. package/optimizer/custom/index.d.ts +8 -8
  104. package/optimizer/custom/index.js +24 -24
  105. package/package.json +5 -5
  106. package/renderOnlyIndex.d.ts +11 -11
  107. package/renderOnlyIndex.js +17 -17
  108. package/templating/eventManager.d.ts +35 -35
  109. package/templating/eventManager.js +65 -65
  110. package/templating/plugins/hdButtonPlugin.d.ts +9 -9
  111. package/templating/plugins/hdButtonPlugin.js +21 -21
  112. package/templating/plugins/printButton.d.ts +9 -9
  113. package/templating/plugins/printButton.js +40 -40
  114. package/templating/templateManager.d.ts +190 -190
  115. package/templating/templateManager.js +553 -553
  116. package/templating/viewerTemplatePlugin.d.ts +21 -21
  117. package/templating/viewerTemplatePlugin.js +68 -68
  118. package/viewer/defaultViewer.d.ts +122 -122
  119. package/viewer/defaultViewer.js +665 -665
  120. package/viewer/renderOnlyViewer.d.ts +8 -8
  121. package/viewer/renderOnlyViewer.js +43 -43
  122. package/viewer/viewer.d.ts +254 -254
  123. package/viewer/viewer.js +777 -777
  124. package/viewer/viewerManager.d.ts +55 -55
  125. package/viewer/viewerManager.js +87 -87
  126. package/viewer/viewerWithTemplate.d.ts +9 -9
  127. package/viewer/viewerWithTemplate.js +19 -19
@@ -1,1389 +1,1389 @@
1
- import { getConfigurationKey } from "../configuration/configuration.js";
2
- import { ModelState } from "../model/viewerModel.js";
3
- import { extendClassWithConfig, deepmerge } from "../helper/index.js";
4
- import { ViewerLabs } from "../labs/viewerLabs.js";
5
- import { getCustomOptimizerByName } from "../optimizer/custom/index.js";
6
- import { Observable } from "@babylonjs/core/Misc/observable.js";
7
- import { SceneOptimizer, SceneOptimizerOptions } from "@babylonjs/core/Misc/sceneOptimizer.js";
8
- import { ArcRotateCamera } from "@babylonjs/core/Cameras/arcRotateCamera.js";
9
- import { Light } from "@babylonjs/core/Lights/light.js";
10
- import { EnvironmentHelper } from "@babylonjs/core/Helpers/environmentHelper.js";
11
- import { Color3, Quaternion, Vector3, Axis, Matrix } from "@babylonjs/core/Maths/math.js";
12
- import { DefaultRenderingPipeline } from "@babylonjs/core/PostProcesses/RenderPipeline/Pipelines/defaultRenderingPipeline.js";
13
- import { Animation } from "@babylonjs/core/Animations/index.js";
14
- import { AnimationPropertiesOverride } from "@babylonjs/core/Animations/animationPropertiesOverride.js";
15
- import { RenderTargetTexture } from "@babylonjs/core/Materials/Textures/renderTargetTexture.js";
16
- import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
17
- import { ShadowLight } from "@babylonjs/core/Lights/shadowLight.js";
18
- import { CubeTexture } from "@babylonjs/core/Materials/Textures/cubeTexture.js";
19
- import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight.js";
20
- import { Scalar } from "@babylonjs/core/Maths/math.scalar.js";
21
- import { CreatePlane } from "@babylonjs/core/Meshes/Builders/planeBuilder.js";
22
- import { Tags } from "@babylonjs/core/Misc/tags.js";
23
- import { Scene } from "@babylonjs/core/scene.js";
24
- import { ShadowGenerator } from "@babylonjs/core/Lights/Shadows/shadowGenerator.js";
25
- import { Constants } from "@babylonjs/core/Engines/constants.js";
26
- import "@babylonjs/core/Audio/audioSceneComponent.js";
27
- import "@babylonjs/core/Helpers/sceneHelpers.js";
28
- var SceneManager = /** @class */ (function () {
29
- function SceneManager(_engine, _configurationContainer, _observablesManager) {
30
- var _this = this;
31
- this._engine = _engine;
32
- this._configurationContainer = _configurationContainer;
33
- this._observablesManager = _observablesManager;
34
- this._white = Color3.White();
35
- this._forceShadowUpdate = false;
36
- this._processShadows = true;
37
- this._groundEnabled = true;
38
- this._groundMirrorEnabled = true;
39
- this._defaultRenderingPipelineEnabled = false;
40
- this._globalConfiguration = {};
41
- this._defaultRenderingPipelineShouldBuild = true;
42
- // default from rendering pipeline
43
- this._bloomEnabled = false;
44
- // default from rendering pipeline
45
- this._fxaaEnabled = false;
46
- this._focusOnModel = function (model) {
47
- var boundingInfo = model.rootMesh.getHierarchyBoundingVectors(true);
48
- var sizeVec = boundingInfo.max.subtract(boundingInfo.min);
49
- var halfSizeVec = sizeVec.scale(0.5);
50
- var center = boundingInfo.min.add(halfSizeVec);
51
- _this.camera.setTarget(center);
52
- _this.camera.alpha = (_this._globalConfiguration.camera && _this._globalConfiguration.camera.alpha) || _this.camera.alpha;
53
- _this.camera.beta = (_this._globalConfiguration.camera && _this._globalConfiguration.camera.beta) || _this.camera.beta;
54
- _this.camera.radius = (_this._globalConfiguration.camera && _this._globalConfiguration.camera.radius) || _this.camera.radius;
55
- var sceneDiagonalLenght = sizeVec.length();
56
- if (isFinite(sceneDiagonalLenght)) {
57
- _this.camera.upperRadiusLimit = sceneDiagonalLenght * 4;
58
- }
59
- if (_this._configurationContainer.configuration) {
60
- _this._configureEnvironment(_this._configurationContainer.configuration.skybox, _this._configurationContainer.configuration.ground);
61
- }
62
- /*this.scene.lights.filter(light => light instanceof ShadowLight).forEach(light => {
63
- // casting ais safe, due to the constraints tested before
64
- (<ShadowLight>light).setDirectionToTarget(center);
65
- });*/
66
- };
67
- this._cameraBehaviorMapping = {};
68
- this.models = [];
69
- this.onCameraConfiguredObservable = new Observable();
70
- this.onLightsConfiguredObservable = new Observable();
71
- this.onModelsConfiguredObservable = new Observable();
72
- this.onSceneConfiguredObservable = new Observable();
73
- this.onSceneInitObservable = new Observable();
74
- this.onSceneOptimizerConfiguredObservable = new Observable();
75
- this.onEnvironmentConfiguredObservable = new Observable();
76
- this.onVRConfiguredObservable = new Observable();
77
- //this._viewer.onEngineInitObservable.add(() => {
78
- this._handleHardwareLimitations();
79
- //});
80
- this.onSceneInitObservable.add(function (scene) {
81
- _this.scene.animationPropertiesOverride = _this.scene.animationPropertiesOverride || new AnimationPropertiesOverride();
82
- _this.labs = new ViewerLabs(scene);
83
- var updateShadows = function () {
84
- for (var _i = 0, _a = _this.scene.lights; _i < _a.length; _i++) {
85
- var light = _a[_i];
86
- var generator = light.getShadowGenerator();
87
- if (generator) {
88
- // Processing shadows if animates
89
- var shadowMap = generator.getShadowMap();
90
- if (shadowMap) {
91
- shadowMap.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
92
- }
93
- }
94
- }
95
- };
96
- scene.registerBeforeRender(function () {
97
- if (_this._forceShadowUpdate || (scene.animatables && scene.animatables.length > 0)) {
98
- // make sure all models are loaded
99
- updateShadows();
100
- _this._forceShadowUpdate = false;
101
- }
102
- else if (!_this.models.every(function (model) {
103
- if (!model.shadowsRenderedAfterLoad) {
104
- model.shadowsRenderedAfterLoad = true;
105
- return false;
106
- }
107
- return model.state === ModelState.COMPLETE && !model.currentAnimation;
108
- })) {
109
- updateShadows();
110
- }
111
- });
112
- return _this._observablesManager && _this._observablesManager.onSceneInitObservable.notifyObserversWithPromise(_this.scene);
113
- });
114
- if (this._observablesManager) {
115
- this._observablesManager.onModelLoadedObservable.add(function (model) {
116
- for (var _i = 0, _a = _this.scene.lights; _i < _a.length; _i++) {
117
- var light = _a[_i];
118
- var generator = light.getShadowGenerator();
119
- if (generator) {
120
- // Processing shadows if animates
121
- var shadowMap = generator.getShadowMap();
122
- if (shadowMap) {
123
- shadowMap.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
124
- }
125
- }
126
- }
127
- _this._focusOnModel(model);
128
- });
129
- this._observablesManager.onModelAddedObservable.add(function (model) {
130
- _this.models.push(model);
131
- });
132
- this._observablesManager.onModelRemovedObservable.add(function (model) {
133
- _this.models.splice(_this.models.indexOf(model), 1);
134
- });
135
- }
136
- }
137
- Object.defineProperty(SceneManager.prototype, "defaultRenderingPipeline", {
138
- get: function () {
139
- return this._defaultRenderingPipeline;
140
- },
141
- enumerable: false,
142
- configurable: true
143
- });
144
- Object.defineProperty(SceneManager.prototype, "vrHelper", {
145
- get: function () {
146
- return this._vrHelper;
147
- },
148
- enumerable: false,
149
- configurable: true
150
- });
151
- Object.defineProperty(SceneManager.prototype, "isHdrSupported", {
152
- /**
153
- * Returns a boolean representing HDR support
154
- */
155
- get: function () {
156
- return this._hdrSupport;
157
- },
158
- enumerable: false,
159
- configurable: true
160
- });
161
- Object.defineProperty(SceneManager.prototype, "mainColor", {
162
- /**
163
- * Return the main color defined in the configuration.
164
- */
165
- get: function () {
166
- return this._configurationContainer.mainColor;
167
- },
168
- enumerable: false,
169
- configurable: true
170
- });
171
- Object.defineProperty(SceneManager.prototype, "reflectionColor", {
172
- get: function () {
173
- return this._configurationContainer.reflectionColor;
174
- },
175
- enumerable: false,
176
- configurable: true
177
- });
178
- Object.defineProperty(SceneManager.prototype, "animationBlendingEnabled", {
179
- get: function () {
180
- return this.scene && this.scene.animationPropertiesOverride.enableBlending;
181
- },
182
- set: function (value) {
183
- this.scene.animationPropertiesOverride.enableBlending = value;
184
- },
185
- enumerable: false,
186
- configurable: true
187
- });
188
- Object.defineProperty(SceneManager.prototype, "observablesManager", {
189
- get: function () {
190
- return this._observablesManager;
191
- },
192
- enumerable: false,
193
- configurable: true
194
- });
195
- Object.defineProperty(SceneManager.prototype, "processShadows", {
196
- /**
197
- * The flag defining whether shadows are rendered constantly or once.
198
- */
199
- get: function () {
200
- return this._processShadows;
201
- },
202
- /**
203
- * Should shadows be rendered every frame, or only once and stop.
204
- * This can be used to optimize a scene.
205
- *
206
- * Not that the shadows will NOT disapear but will remain in place.
207
- * @param process if true shadows will be updated once every frame. if false they will stop being updated.
208
- */
209
- set: function (process) {
210
- var refreshType = process ? RenderTargetTexture.REFRESHRATE_RENDER_ONEVERYFRAME : RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
211
- for (var _i = 0, _a = this.scene.lights; _i < _a.length; _i++) {
212
- var light = _a[_i];
213
- var generator = light.getShadowGenerator();
214
- if (generator) {
215
- var shadowMap = generator.getShadowMap();
216
- if (shadowMap) {
217
- shadowMap.refreshRate = refreshType;
218
- }
219
- }
220
- }
221
- this._processShadows = process;
222
- },
223
- enumerable: false,
224
- configurable: true
225
- });
226
- Object.defineProperty(SceneManager.prototype, "groundEnabled", {
227
- get: function () {
228
- return this._groundEnabled;
229
- },
230
- set: function (newValue) {
231
- if (newValue === this._groundEnabled) {
232
- return;
233
- }
234
- this._groundEnabled = newValue;
235
- if (this.environmentHelper && this.environmentHelper.ground) {
236
- this.environmentHelper.ground.setEnabled(this._groundEnabled);
237
- }
238
- },
239
- enumerable: false,
240
- configurable: true
241
- });
242
- Object.defineProperty(SceneManager.prototype, "groundMirrorEnabled", {
243
- /**
244
- * gets whether the reflection is disabled.
245
- */
246
- get: function () {
247
- return this._groundMirrorEnabled;
248
- },
249
- /**
250
- * sets whether the reflection is disabled.
251
- */
252
- set: function (value) {
253
- if (this._groundMirrorEnabled === value) {
254
- return;
255
- }
256
- this._groundMirrorEnabled = value;
257
- if (this.environmentHelper && this.environmentHelper.groundMaterial && this.environmentHelper.groundMirror) {
258
- if (!value) {
259
- this.environmentHelper.groundMaterial.reflectionTexture = null;
260
- }
261
- else {
262
- this.environmentHelper.groundMaterial.reflectionTexture = this.environmentHelper.groundMirror;
263
- }
264
- }
265
- },
266
- enumerable: false,
267
- configurable: true
268
- });
269
- Object.defineProperty(SceneManager.prototype, "defaultRenderingPipelineEnabled", {
270
- get: function () {
271
- return this._defaultRenderingPipelineEnabled;
272
- },
273
- set: function (value) {
274
- if (value === this._defaultRenderingPipelineEnabled) {
275
- return;
276
- }
277
- this._defaultRenderingPipelineEnabled = value;
278
- this._rebuildPostprocesses();
279
- if (this._defaultRenderingPipeline) {
280
- this._defaultRenderingPipelineShouldBuild = false;
281
- this._defaultRenderingPipeline.prepare();
282
- this.scene.imageProcessingConfiguration.applyByPostProcess = true;
283
- }
284
- },
285
- enumerable: false,
286
- configurable: true
287
- });
288
- /**
289
- * Sets the engine flags to unlock all babylon features.
290
- * Can also be configured using the scene.flags configuration object
291
- */
292
- SceneManager.prototype.unlockBabylonFeatures = function () {
293
- this.scene.shadowsEnabled = true;
294
- this.scene.particlesEnabled = true;
295
- this.scene.postProcessesEnabled = true;
296
- this.scene.collisionsEnabled = true;
297
- this.scene.lightsEnabled = true;
298
- this.scene.texturesEnabled = true;
299
- this.scene.lensFlaresEnabled = true;
300
- this.scene.proceduralTexturesEnabled = true;
301
- this.scene.renderTargetsEnabled = true;
302
- this.scene.spritesEnabled = true;
303
- this.scene.skeletonsEnabled = true;
304
- this.scene.audioEnabled = true;
305
- };
306
- /**
307
- * initialize the scene. Calling this function again will dispose the old scene, if exists.
308
- * @param sceneConfiguration
309
- */
310
- SceneManager.prototype.initScene = function (sceneConfiguration) {
311
- if (sceneConfiguration === void 0) { sceneConfiguration = {}; }
312
- // if the scene exists, dispose it.
313
- if (this.scene) {
314
- this.scene.dispose();
315
- }
316
- // create a new scene
317
- this.scene = new Scene(this._engine);
318
- this._configurationContainer.scene = this.scene;
319
- // set a default PBR material
320
- if (!sceneConfiguration.defaultMaterial) {
321
- var defaultMaterial = new PBRMaterial("defaultMaterial", this.scene);
322
- defaultMaterial.reflectivityColor = new Color3(0.1, 0.1, 0.1);
323
- defaultMaterial.microSurface = 0.6;
324
- if (this.scene.defaultMaterial) {
325
- this.scene.defaultMaterial.dispose();
326
- }
327
- this.scene.defaultMaterial = defaultMaterial;
328
- }
329
- this.scene.animationPropertiesOverride = new AnimationPropertiesOverride();
330
- Animation.AllowMatricesInterpolation = true;
331
- /*if (sceneConfiguration.glow) {
332
- let options: Partial<IGlowLayerOptions> = {
333
- mainTextureFixedSize: 512
334
- };
335
- if (typeof sceneConfiguration.glow === 'object') {
336
- options = sceneConfiguration.glow
337
- }
338
- var gl = new GlowLayer("glow", this.scene, options);
339
- }*/
340
- return this.onSceneInitObservable.notifyObserversWithPromise(this.scene);
341
- };
342
- SceneManager.prototype.clearScene = function (clearModels, clearLights) {
343
- if (clearModels === void 0) { clearModels = true; }
344
- if (clearLights === void 0) { clearLights = false; }
345
- if (clearModels) {
346
- this.models.forEach(function (m) { return m.dispose(); });
347
- this.models.length = 0;
348
- }
349
- if (clearLights) {
350
- this.scene.lights.forEach(function (l) { return l.dispose(); });
351
- }
352
- };
353
- /**
354
- * This will update the scene's configuration, including camera, lights, environment.
355
- * @param newConfiguration the delta that should be configured. This includes only the changes
356
- */
357
- SceneManager.prototype.updateConfiguration = function (newConfiguration) {
358
- var _this = this;
359
- if (this._configurationContainer) {
360
- this._globalConfiguration = this._configurationContainer.configuration;
361
- }
362
- else {
363
- this._globalConfiguration = newConfiguration;
364
- }
365
- // update scene configuration
366
- if (newConfiguration.scene) {
367
- this._configureScene(newConfiguration.scene);
368
- }
369
- // optimizer
370
- if (newConfiguration.optimizer !== undefined) {
371
- this._configureOptimizer(newConfiguration.optimizer);
372
- }
373
- // configure model
374
- /*if (newConfiguration.model && typeof newConfiguration.model === 'object') {
375
- this._configureModel(newConfiguration.model);
376
- }*/
377
- // lights
378
- this._configureLights(newConfiguration.lights);
379
- // environment
380
- if (newConfiguration.skybox !== undefined || newConfiguration.ground !== undefined) {
381
- this._configureEnvironment(newConfiguration.skybox, newConfiguration.ground);
382
- }
383
- // camera
384
- this._configureCamera(newConfiguration.camera);
385
- if (newConfiguration.environmentMap !== undefined) {
386
- this._configureEnvironmentMap(newConfiguration.environmentMap);
387
- }
388
- if (newConfiguration.vr !== undefined) {
389
- this._configureVR(newConfiguration.vr);
390
- }
391
- if (newConfiguration.lab) {
392
- // rendering pipelines
393
- if (newConfiguration.lab.defaultRenderingPipelines) {
394
- var pipelineConfig = newConfiguration.lab.defaultRenderingPipelines;
395
- if (typeof pipelineConfig === "boolean") {
396
- this.defaultRenderingPipelineEnabled = pipelineConfig;
397
- }
398
- else {
399
- this.defaultRenderingPipelineEnabled = true;
400
- }
401
- }
402
- if (this.environmentHelper && newConfiguration.lab.environmentMainColor) {
403
- var mainColor = new Color3(newConfiguration.lab.environmentMainColor.r, newConfiguration.lab.environmentMainColor.g, newConfiguration.lab.environmentMainColor.b);
404
- this.environmentHelper.setMainColor(mainColor);
405
- }
406
- if (newConfiguration.lab.globalLightRotation !== undefined) {
407
- // rotate all lights that are shadow lights
408
- this.scene.lights
409
- .filter(function (light) { return light instanceof ShadowLight; })
410
- .forEach(function (light) {
411
- // casting and '!' are safe, due to the constraints tested before
412
- _this.labs.rotateShadowLight(light, newConfiguration.lab.globalLightRotation);
413
- });
414
- this._forceShadowUpdate = true;
415
- }
416
- }
417
- if (this._defaultRenderingPipeline && this._defaultRenderingPipeline.imageProcessing) {
418
- this._defaultRenderingPipeline.imageProcessing.fromLinearSpace = true;
419
- }
420
- if (this._defaultRenderingPipelineShouldBuild && this._defaultRenderingPipeline) {
421
- this._defaultRenderingPipelineShouldBuild = false;
422
- this._defaultRenderingPipeline.prepare();
423
- }
424
- };
425
- SceneManager.prototype._rebuildPostprocesses = function (configuration) {
426
- if (!this._defaultRenderingPipelineEnabled || !getConfigurationKey("scene.imageProcessingConfiguration.isEnabled", this._globalConfiguration)) {
427
- if (this._defaultRenderingPipeline) {
428
- this._defaultRenderingPipeline.dispose();
429
- this._defaultRenderingPipeline = null;
430
- this.scene.autoClearDepthAndStencil = true;
431
- this.scene.autoClear = true;
432
- this.scene.imageProcessingConfiguration.applyByPostProcess = false;
433
- }
434
- return;
435
- }
436
- var pipelineConfig = configuration || (this._globalConfiguration.lab && this._globalConfiguration.lab.defaultRenderingPipelines);
437
- if (pipelineConfig) {
438
- if (!this._defaultRenderingPipeline) {
439
- // Create pipeline in manual mode to avoid triggering multiple shader compilations
440
- this._defaultRenderingPipeline = new DefaultRenderingPipeline("default rendering pipeline", this._hdrSupport, this.scene, [this.camera], false);
441
- }
442
- this.scene.autoClear = false;
443
- this.scene.autoClearDepthAndStencil = false;
444
- this._defaultRenderingPipelineShouldBuild = true;
445
- var bloomEnabled = this._bloomEnabled;
446
- if (typeof pipelineConfig !== "boolean") {
447
- extendClassWithConfig(this._defaultRenderingPipeline, pipelineConfig);
448
- this._bloomEnabled = !!pipelineConfig.bloomEnabled;
449
- this._fxaaEnabled = !!pipelineConfig.fxaaEnabled;
450
- bloomEnabled = this._bloomEnabled && pipelineConfig.bloomWeight !== undefined && pipelineConfig.bloomWeight > 0;
451
- this._defaultRenderingPipeline.bloomWeight = (pipelineConfig.bloomWeight !== undefined && pipelineConfig.bloomWeight) || this._defaultRenderingPipeline.bloomWeight;
452
- }
453
- this._defaultRenderingPipeline.bloomEnabled = bloomEnabled;
454
- this._defaultRenderingPipeline.fxaaEnabled = this.fxaaEnabled;
455
- }
456
- };
457
- Object.defineProperty(SceneManager.prototype, "bloomEnabled", {
458
- get: function () {
459
- return this._bloomEnabled;
460
- },
461
- set: function (value) {
462
- if (this._bloomEnabled === value) {
463
- return;
464
- }
465
- this._bloomEnabled = value;
466
- this._rebuildPostprocesses();
467
- if (this._defaultRenderingPipeline) {
468
- this._defaultRenderingPipelineShouldBuild = false;
469
- this._defaultRenderingPipeline.prepare();
470
- this.scene.imageProcessingConfiguration.applyByPostProcess = true;
471
- }
472
- },
473
- enumerable: false,
474
- configurable: true
475
- });
476
- Object.defineProperty(SceneManager.prototype, "fxaaEnabled", {
477
- get: function () {
478
- return this._fxaaEnabled;
479
- },
480
- set: function (value) {
481
- if (this._fxaaEnabled === value) {
482
- return;
483
- }
484
- this._fxaaEnabled = value;
485
- this._rebuildPostprocesses();
486
- if (this._defaultRenderingPipeline) {
487
- this._defaultRenderingPipelineShouldBuild = false;
488
- this._defaultRenderingPipeline.prepare();
489
- this.scene.imageProcessingConfiguration.applyByPostProcess = true;
490
- }
491
- },
492
- enumerable: false,
493
- configurable: true
494
- });
495
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
496
- SceneManager.prototype.setDefaultMaterial = function (sceneConfig) { };
497
- /**
498
- * internally configure the scene using the provided configuration.
499
- * The scene will not be recreated, but just updated.
500
- * @param sceneConfig the (new) scene configuration
501
- */
502
- SceneManager.prototype._configureScene = function (sceneConfig) {
503
- // sanity check!
504
- if (!this.scene) {
505
- return;
506
- }
507
- var cc = sceneConfig.clearColor;
508
- var oldcc = this.scene.clearColor;
509
- if (cc) {
510
- if (cc.r !== undefined) {
511
- oldcc.r = cc.r;
512
- }
513
- if (cc.g !== undefined) {
514
- oldcc.g = cc.g;
515
- }
516
- if (cc.b !== undefined) {
517
- oldcc.b = cc.b;
518
- }
519
- if (cc.a !== undefined) {
520
- oldcc.a = cc.a;
521
- }
522
- }
523
- if (sceneConfig.assetsRootURL) {
524
- this._assetsRootURL = sceneConfig.assetsRootURL;
525
- }
526
- // image processing configuration - optional.
527
- if (sceneConfig.imageProcessingConfiguration) {
528
- extendClassWithConfig(this.scene.imageProcessingConfiguration, sceneConfig.imageProcessingConfiguration);
529
- }
530
- //animation properties override
531
- if (sceneConfig.animationPropertiesOverride) {
532
- extendClassWithConfig(this.scene.animationPropertiesOverride, sceneConfig.animationPropertiesOverride);
533
- }
534
- if (sceneConfig.environmentTexture) {
535
- if (!(this.scene.environmentTexture && this.scene.environmentTexture.url === sceneConfig.environmentTexture)) {
536
- if (this.scene.environmentTexture && this.scene.environmentTexture.dispose) {
537
- this.scene.environmentTexture.dispose();
538
- }
539
- var environmentTexture = CubeTexture.CreateFromPrefilteredData(sceneConfig.environmentTexture, this.scene);
540
- this.scene.environmentTexture = environmentTexture;
541
- }
542
- }
543
- if (sceneConfig.debug === true) {
544
- this.scene.debugLayer.show();
545
- }
546
- else if (sceneConfig.debug === false) {
547
- if (this.scene.debugLayer.isVisible()) {
548
- this.scene.debugLayer.hide();
549
- }
550
- }
551
- if (sceneConfig.disableHdr) {
552
- this._handleHardwareLimitations(false);
553
- }
554
- else {
555
- this._handleHardwareLimitations(true);
556
- }
557
- if (sceneConfig.renderInBackground !== undefined) {
558
- this._engine.renderEvenInBackground = !!sceneConfig.renderInBackground;
559
- }
560
- var canvas = this._engine.getInputElement();
561
- if (canvas) {
562
- if (this.camera && sceneConfig.disableCameraControl) {
563
- this.camera.detachControl();
564
- }
565
- else if (this.camera && sceneConfig.disableCameraControl === false) {
566
- this.camera.attachControl();
567
- }
568
- }
569
- this.setDefaultMaterial(sceneConfig);
570
- if (sceneConfig.flags) {
571
- extendClassWithConfig(this.scene, sceneConfig.flags);
572
- }
573
- this.onSceneConfiguredObservable.notifyObservers({
574
- sceneManager: this,
575
- object: this.scene,
576
- newConfiguration: sceneConfig,
577
- });
578
- };
579
- /**
580
- * Configure the scene optimizer.
581
- * The existing scene optimizer will be disposed and a new one will be created.
582
- * @param optimizerConfig the (new) optimizer configuration
583
- */
584
- SceneManager.prototype._configureOptimizer = function (optimizerConfig) {
585
- var _this = this;
586
- if (typeof optimizerConfig === "boolean") {
587
- if (this.sceneOptimizer) {
588
- this.sceneOptimizer.stop();
589
- this.sceneOptimizer.dispose();
590
- delete this.sceneOptimizer;
591
- }
592
- if (optimizerConfig) {
593
- this.sceneOptimizer = new SceneOptimizer(this.scene);
594
- this.sceneOptimizer.start();
595
- }
596
- }
597
- else {
598
- var optimizerOptions = new SceneOptimizerOptions(optimizerConfig.targetFrameRate, optimizerConfig.trackerDuration);
599
- // check for degradation
600
- if (optimizerConfig.degradation) {
601
- switch (optimizerConfig.degradation) {
602
- case "low":
603
- optimizerOptions = SceneOptimizerOptions.LowDegradationAllowed(optimizerConfig.targetFrameRate);
604
- break;
605
- case "moderate":
606
- optimizerOptions = SceneOptimizerOptions.ModerateDegradationAllowed(optimizerConfig.targetFrameRate);
607
- break;
608
- case "hight":
609
- optimizerOptions = SceneOptimizerOptions.HighDegradationAllowed(optimizerConfig.targetFrameRate);
610
- break;
611
- }
612
- }
613
- if (this.sceneOptimizer) {
614
- this.sceneOptimizer.stop();
615
- this.sceneOptimizer.dispose();
616
- }
617
- if (optimizerConfig.custom) {
618
- var customOptimizer_1 = getCustomOptimizerByName(optimizerConfig.custom, optimizerConfig.improvementMode);
619
- if (customOptimizer_1) {
620
- optimizerOptions.addCustomOptimization(function () {
621
- return customOptimizer_1(_this);
622
- }, function () {
623
- return "Babylon Viewer ".concat(optimizerConfig.custom, " custom optimization");
624
- });
625
- }
626
- }
627
- this.sceneOptimizer = new SceneOptimizer(this.scene, optimizerOptions, optimizerConfig.autoGeneratePriorities, optimizerConfig.improvementMode);
628
- this.sceneOptimizer.start();
629
- }
630
- this.onSceneOptimizerConfiguredObservable.notifyObservers({
631
- sceneManager: this,
632
- object: this.sceneOptimizer,
633
- newConfiguration: optimizerConfig,
634
- });
635
- };
636
- /**
637
- * configure all models using the configuration.
638
- * @param modelConfiguration the configuration to use to reconfigure the models
639
- */
640
- /*protected _configureModel(modelConfiguration: Partial<IModelConfiguration>) {
641
- this.models.forEach(model => {
642
- model.updateConfiguration(modelConfiguration);
643
- });
644
-
645
- this.onModelsConfiguredObservable.notifyObservers({
646
- sceneManager: this,
647
- object: this.models,
648
- newConfiguration: modelConfiguration
649
- });
650
- }*/
651
- SceneManager.prototype._configureVR = function (vrConfig) {
652
- var _this = this;
653
- if (vrConfig.disabled) {
654
- if (this._vrHelper) {
655
- if (this._vrHelper.isInVRMode) {
656
- this._vrHelper.exitVR();
657
- }
658
- this._vrHelper.dispose();
659
- this._vrHelper = undefined;
660
- }
661
- return;
662
- }
663
- var vrOptions = deepmerge({
664
- useCustomVRButton: true,
665
- createDeviceOrientationCamera: false,
666
- trackPosition: true,
667
- }, vrConfig.vrOptions || {});
668
- this._vrHelper = this.scene.createDefaultVRExperience(vrOptions);
669
- if (!vrConfig.disableInteractions) {
670
- this._vrHelper.enableInteractions();
671
- }
672
- if (!vrConfig.disableTeleportation) {
673
- var floorMeshName = vrConfig.overrideFloorMeshName || "BackgroundPlane";
674
- this._vrHelper.enableTeleportation({
675
- floorMeshName: floorMeshName,
676
- });
677
- }
678
- if (vrConfig.rotateUsingControllers) {
679
- var rotationOffset_1;
680
- this._vrHelper.onControllerMeshLoadedObservable.add(function (controller) {
681
- controller.onTriggerStateChangedObservable.add(function (data) {
682
- if (controller.mesh && controller.mesh.rotationQuaternion) {
683
- if (data.pressed) {
684
- if (!rotationOffset_1) {
685
- _this.models[0].rootMesh.rotationQuaternion = _this.models[0].rootMesh.rotationQuaternion || new Quaternion();
686
- rotationOffset_1 = controller.mesh.rotationQuaternion.conjugate().multiply(_this.models[0].rootMesh.rotationQuaternion);
687
- }
688
- }
689
- else {
690
- rotationOffset_1 = null;
691
- }
692
- }
693
- });
694
- _this.scene.registerBeforeRender(function () {
695
- if (_this.models[0]) {
696
- if (rotationOffset_1 && controller.mesh && controller.mesh.rotationQuaternion) {
697
- _this.models[0].rootMesh.rotationQuaternion.copyFrom(controller.mesh.rotationQuaternion).multiplyInPlace(rotationOffset_1);
698
- }
699
- else {
700
- _this.models[0].rootMesh.rotationQuaternion = null;
701
- }
702
- }
703
- });
704
- });
705
- }
706
- this._vrHelper.onEnteringVRObservable.add(function () {
707
- if (_this._observablesManager) {
708
- _this._observablesManager.onEnteringVRObservable.notifyObservers(_this);
709
- }
710
- });
711
- this._vrHelper.onExitingVRObservable.add(function () {
712
- if (_this._observablesManager) {
713
- _this._observablesManager.onExitingVRObservable.notifyObservers(_this);
714
- }
715
- });
716
- this.onVRConfiguredObservable.notifyObservers({
717
- sceneManager: this,
718
- object: this._vrHelper,
719
- newConfiguration: vrConfig,
720
- });
721
- };
722
- SceneManager.prototype._configureEnvironmentMap = function (environmentMapConfiguration) {
723
- if (environmentMapConfiguration.texture) {
724
- this.scene.environmentTexture = new CubeTexture(this._getAssetUrl(environmentMapConfiguration.texture), this.scene);
725
- }
726
- //sanity check
727
- if (this.scene.environmentTexture) {
728
- var rotatquatRotationionY = Quaternion.RotationAxis(Axis.Y, environmentMapConfiguration.rotationY || 0);
729
- Matrix.FromQuaternionToRef(rotatquatRotationionY, this.scene.environmentTexture.getReflectionTextureMatrix());
730
- }
731
- // process mainColor changes:
732
- if (environmentMapConfiguration.mainColor) {
733
- this._configurationContainer.mainColor = this.mainColor || Color3.White();
734
- var mc = environmentMapConfiguration.mainColor;
735
- if (mc.r !== undefined) {
736
- this.mainColor.r = mc.r;
737
- }
738
- if (mc.g !== undefined) {
739
- this.mainColor.g = mc.g;
740
- }
741
- if (mc.b !== undefined) {
742
- this.mainColor.b = mc.b;
743
- }
744
- this.reflectionColor.copyFrom(this.mainColor);
745
- var environmentTint = getConfigurationKey("environmentMap.tintLevel", this._globalConfiguration) || 0;
746
- // reflection color
747
- this.reflectionColor.toLinearSpaceToRef(this.reflectionColor);
748
- this.reflectionColor.scaleToRef(1 / this.scene.imageProcessingConfiguration.exposure, this.reflectionColor);
749
- var tmpColor3 = Color3.Lerp(this._white, this.reflectionColor, environmentTint);
750
- this.reflectionColor.copyFrom(tmpColor3);
751
- //update the environment, if exists
752
- if (this.environmentHelper) {
753
- if (this.environmentHelper.groundMaterial) {
754
- this.environmentHelper.groundMaterial._perceptualColor = this.mainColor;
755
- }
756
- if (this.environmentHelper.skyboxMaterial) {
757
- this.environmentHelper.skyboxMaterial._perceptualColor = this.mainColor;
758
- }
759
- }
760
- }
761
- };
762
- /**
763
- * (Re) configure the camera. The camera will only be created once and from this point will only be reconfigured.
764
- * @param cameraConfig the new camera configuration
765
- */
766
- SceneManager.prototype._configureCamera = function (cameraConfig) {
767
- var _this = this;
768
- if (cameraConfig === void 0) { cameraConfig = {}; }
769
- if (!this.scene.activeCamera) {
770
- // Inline scene.createDefaultCamera to reduce file size
771
- // Dispose existing camera in replace mode.
772
- if (this.scene.activeCamera) {
773
- this.scene.activeCamera.dispose();
774
- this.scene.activeCamera = null;
775
- }
776
- // Camera
777
- if (!this.scene.activeCamera) {
778
- var worldExtends = this.scene.getWorldExtends();
779
- var worldSize = worldExtends.max.subtract(worldExtends.min);
780
- var worldCenter = worldExtends.min.add(worldSize.scale(0.5));
781
- var radius = worldSize.length() * 1.5;
782
- // empty scene scenario!
783
- if (!isFinite(radius)) {
784
- radius = 1;
785
- worldCenter.copyFromFloats(0, 0, 0);
786
- }
787
- var arcRotateCamera = new ArcRotateCamera("default camera", -(Math.PI / 2), Math.PI / 2, radius, worldCenter, this.scene);
788
- arcRotateCamera.lowerRadiusLimit = radius * 0.01;
789
- arcRotateCamera.wheelPrecision = 100 / radius;
790
- var camera = arcRotateCamera;
791
- camera.minZ = radius * 0.01;
792
- camera.maxZ = radius * 1000;
793
- camera.speed = radius * 0.2;
794
- this.scene.activeCamera = camera;
795
- }
796
- var canvas = this.scene.getEngine().getInputElement();
797
- if (canvas) {
798
- this.scene.activeCamera.attachControl();
799
- }
800
- this.camera = this.scene.activeCamera;
801
- this.camera.setTarget(Vector3.Zero());
802
- }
803
- if (!this.camera) {
804
- this.camera = this.scene.activeCamera;
805
- }
806
- if (cameraConfig.position) {
807
- var newPosition = this.camera.position.clone();
808
- extendClassWithConfig(newPosition, cameraConfig.position);
809
- this.camera.setPosition(newPosition);
810
- }
811
- if (cameraConfig.target) {
812
- var newTarget = this.camera.target.clone();
813
- extendClassWithConfig(newTarget, cameraConfig.target);
814
- this.camera.setTarget(newTarget);
815
- } /*else if (this.models.length && !cameraConfig.disableAutoFocus) {
816
- this._focusOnModel(this.models[0]);
817
- }*/
818
- if (cameraConfig.rotation) {
819
- this.camera.rotationQuaternion = new Quaternion(cameraConfig.rotation.x || 0, cameraConfig.rotation.y || 0, cameraConfig.rotation.z || 0, cameraConfig.rotation.w || 0);
820
- }
821
- if (cameraConfig.behaviors) {
822
- for (var name_1 in cameraConfig.behaviors) {
823
- if (cameraConfig.behaviors[name_1] !== undefined) {
824
- this._setCameraBehavior(name_1, cameraConfig.behaviors[name_1]);
825
- }
826
- }
827
- }
828
- var sceneExtends = this.scene.getWorldExtends(function (mesh) {
829
- return !_this.environmentHelper || (mesh !== _this.environmentHelper.ground && mesh !== _this.environmentHelper.rootMesh && mesh !== _this.environmentHelper.skybox);
830
- });
831
- var sceneDiagonal = sceneExtends.max.subtract(sceneExtends.min);
832
- var sceneDiagonalLength = sceneDiagonal.length();
833
- if (isFinite(sceneDiagonalLength)) {
834
- this.camera.upperRadiusLimit = sceneDiagonalLength * 4;
835
- }
836
- // sanity check!
837
- if (this.scene.imageProcessingConfiguration) {
838
- this.scene.imageProcessingConfiguration.colorCurvesEnabled = true;
839
- this.scene.imageProcessingConfiguration.vignetteEnabled = true;
840
- this.scene.imageProcessingConfiguration.toneMappingEnabled = !!getConfigurationKey("camera.toneMappingEnabled", this._globalConfiguration);
841
- }
842
- extendClassWithConfig(this.camera, cameraConfig);
843
- this.onCameraConfiguredObservable.notifyObservers({
844
- sceneManager: this,
845
- object: this.camera,
846
- newConfiguration: cameraConfig,
847
- });
848
- };
849
- SceneManager.prototype._configureEnvironment = function (skyboxConifguration, groundConfiguration) {
850
- var _this = this;
851
- if (!skyboxConifguration && !groundConfiguration) {
852
- if (this.environmentHelper) {
853
- this.environmentHelper.dispose();
854
- this.environmentHelper = undefined;
855
- }
856
- }
857
- else {
858
- var options = {
859
- createGround: !!groundConfiguration && this._groundEnabled,
860
- createSkybox: !!skyboxConifguration,
861
- setupImageProcessing: false, // will be done at the scene level!,
862
- };
863
- // will that cause problems with model ground configuration?
864
- /*if (model) {
865
- const boundingInfo = model.rootMesh.getHierarchyBoundingVectors(true);
866
- const sizeVec = boundingInfo.max.subtract(boundingInfo.min);
867
- const halfSizeVec = sizeVec.scale(0.5);
868
- const center = boundingInfo.min.add(halfSizeVec);
869
- options.groundYBias = -center.y;
870
- }*/
871
- if (groundConfiguration) {
872
- var groundConfig_1 = typeof groundConfiguration === "boolean" ? {} : groundConfiguration;
873
- var groundSize = groundConfig_1.size || (typeof skyboxConifguration === "object" && skyboxConifguration.scale);
874
- if (groundSize) {
875
- options.groundSize = groundSize;
876
- }
877
- options.enableGroundShadow = groundConfig_1 === true || groundConfig_1.receiveShadows;
878
- if (groundConfig_1.shadowLevel !== undefined) {
879
- options.groundShadowLevel = groundConfig_1.shadowLevel;
880
- }
881
- options.enableGroundMirror = !!groundConfig_1.mirror && this.groundMirrorEnabled;
882
- if (groundConfig_1.texture) {
883
- options.groundTexture = this._getAssetUrl(groundConfig_1.texture);
884
- }
885
- if (groundConfig_1.color) {
886
- options.groundColor = new Color3(groundConfig_1.color.r, groundConfig_1.color.g, groundConfig_1.color.b);
887
- }
888
- if (groundConfig_1.opacity !== undefined) {
889
- options.groundOpacity = groundConfig_1.opacity;
890
- }
891
- if (groundConfig_1.mirror) {
892
- options.enableGroundMirror = true;
893
- // to prevent undefines
894
- if (typeof groundConfig_1.mirror === "object") {
895
- if (groundConfig_1.mirror.amount !== undefined) {
896
- options.groundMirrorAmount = groundConfig_1.mirror.amount;
897
- }
898
- if (groundConfig_1.mirror.sizeRatio !== undefined) {
899
- options.groundMirrorSizeRatio = groundConfig_1.mirror.sizeRatio;
900
- }
901
- if (groundConfig_1.mirror.blurKernel !== undefined) {
902
- options.groundMirrorBlurKernel = groundConfig_1.mirror.blurKernel;
903
- }
904
- if (groundConfig_1.mirror.fresnelWeight !== undefined) {
905
- options.groundMirrorFresnelWeight = groundConfig_1.mirror.fresnelWeight;
906
- }
907
- if (groundConfig_1.mirror.fallOffDistance !== undefined) {
908
- options.groundMirrorFallOffDistance = groundConfig_1.mirror.fallOffDistance;
909
- }
910
- if (this._defaultPipelineTextureType !== undefined) {
911
- options.groundMirrorTextureType = this._defaultPipelineTextureType;
912
- }
913
- }
914
- }
915
- }
916
- var postInitSkyboxMaterial = false;
917
- if (skyboxConifguration) {
918
- var conf = skyboxConifguration === true ? {} : skyboxConifguration;
919
- if (conf.material && conf.material.imageProcessingConfiguration) {
920
- options.setupImageProcessing = false; // will be configured later manually.
921
- }
922
- var skyboxSize = conf.scale;
923
- if (skyboxSize) {
924
- options.skyboxSize = skyboxSize;
925
- }
926
- options.sizeAuto = !options.skyboxSize;
927
- if (conf.color) {
928
- options.skyboxColor = new Color3(conf.color.r, conf.color.g, conf.color.b);
929
- }
930
- if (conf.cubeTexture && conf.cubeTexture.url) {
931
- if (typeof conf.cubeTexture.url === "string") {
932
- options.skyboxTexture = this._getAssetUrl(conf.cubeTexture.url);
933
- }
934
- else {
935
- // init later!
936
- postInitSkyboxMaterial = true;
937
- }
938
- }
939
- if (conf.material) {
940
- postInitSkyboxMaterial = true;
941
- }
942
- }
943
- options.setupImageProcessing = false; // TMP
944
- if (!this.environmentHelper) {
945
- this.environmentHelper = new EnvironmentHelper(options, this.scene);
946
- }
947
- else {
948
- // unlikely, but there might be a new scene! we need to dispose.
949
- // get the scene used by the envHelper
950
- var scene = this.environmentHelper.rootMesh.getScene();
951
- // is it a different scene? Oh no!
952
- if (scene !== this.scene) {
953
- this.environmentHelper.dispose();
954
- this.environmentHelper = new EnvironmentHelper(options, this.scene);
955
- }
956
- else {
957
- // recreate the ground
958
- if (this.environmentHelper.ground) {
959
- this.environmentHelper.ground.dispose();
960
- }
961
- // recreate the skybox
962
- if (this.environmentHelper.skybox) {
963
- this.environmentHelper.skybox.dispose();
964
- }
965
- this.environmentHelper.updateOptions(options);
966
- // update doesn't change the size of the skybox and ground, so we have to recreate!
967
- //this.environmentHelper.dispose();
968
- //this.environmentHelper = new EnvironmentHelper(options, this.scene);
969
- }
970
- }
971
- if (this.environmentHelper.rootMesh && this._globalConfiguration.scene && this._globalConfiguration.scene.environmentRotationY !== undefined) {
972
- this.environmentHelper.rootMesh.rotation.y = this._globalConfiguration.scene.environmentRotationY;
973
- }
974
- var groundConfig = typeof groundConfiguration === "boolean" ? {} : groundConfiguration;
975
- if (this.environmentHelper.groundMaterial && groundConfig) {
976
- this.environmentHelper.groundMaterial._perceptualColor = this.mainColor;
977
- if (groundConfig.material) {
978
- extendClassWithConfig(this.environmentHelper.groundMaterial, groundConfig.material);
979
- }
980
- if (this.environmentHelper.groundMirror) {
981
- var mirrorClearColor = this.environmentHelper.groundMaterial._perceptualColor.toLinearSpace();
982
- // TODO user camera exposure value to set the mirror clear color
983
- var exposure = Math.pow(2.0, -this.scene.imageProcessingConfiguration.exposure) * Math.PI;
984
- mirrorClearColor.scaleToRef(1 / exposure, mirrorClearColor);
985
- this.environmentHelper.groundMirror.clearColor.r = Scalar.Clamp(mirrorClearColor.r);
986
- this.environmentHelper.groundMirror.clearColor.g = Scalar.Clamp(mirrorClearColor.g);
987
- this.environmentHelper.groundMirror.clearColor.b = Scalar.Clamp(mirrorClearColor.b);
988
- this.environmentHelper.groundMirror.clearColor.a = 1;
989
- if (!this.groundMirrorEnabled) {
990
- this.environmentHelper.groundMaterial.reflectionTexture = null;
991
- }
992
- }
993
- }
994
- var skyboxMaterial = this.environmentHelper.skyboxMaterial;
995
- if (skyboxMaterial) {
996
- skyboxMaterial._perceptualColor = this.mainColor;
997
- if (postInitSkyboxMaterial) {
998
- if (typeof skyboxConifguration === "object" && skyboxConifguration.material) {
999
- extendClassWithConfig(skyboxMaterial, skyboxConifguration.material);
1000
- }
1001
- }
1002
- }
1003
- }
1004
- this._observablesManager &&
1005
- this._observablesManager.onModelLoadedObservable.add(function (model) {
1006
- _this._updateGroundMirrorRenderList(model);
1007
- });
1008
- this.onEnvironmentConfiguredObservable.notifyObservers({
1009
- sceneManager: this,
1010
- object: this.environmentHelper,
1011
- newConfiguration: {
1012
- skybox: skyboxConifguration,
1013
- ground: groundConfiguration,
1014
- },
1015
- });
1016
- };
1017
- /**
1018
- * configure the lights.
1019
- * @param lightsConfiguration the (new) light(s) configuration
1020
- */
1021
- SceneManager.prototype._configureLights = function (lightsConfiguration) {
1022
- var _this = this;
1023
- if (lightsConfiguration === void 0) { lightsConfiguration = {}; }
1024
- // sanity check!
1025
- var lightKeys = Object.keys(lightsConfiguration).filter(function (name) { return name !== "globalRotation"; });
1026
- if (!lightKeys.length) {
1027
- if (!this.scene.lights.length) {
1028
- new HemisphericLight("default light", Vector3.Up(), this.scene);
1029
- }
1030
- }
1031
- else {
1032
- var lightsAvailable_1 = this.scene.lights.map(function (light) { return light.name; });
1033
- // compare to the global (!) configuration object and dispose unneeded:
1034
- var lightsToConfigure_1 = Object.keys(this._globalConfiguration.lights || []);
1035
- if (Object.keys(lightsToConfigure_1).length !== lightsAvailable_1.length) {
1036
- lightsAvailable_1.forEach(function (lName) {
1037
- if (lightsToConfigure_1.indexOf(lName) === -1) {
1038
- _this.scene.getLightByName(lName).dispose();
1039
- }
1040
- });
1041
- }
1042
- lightKeys.forEach(function (name) {
1043
- var lightConfig = { type: 0 };
1044
- if (typeof lightsConfiguration[name] === "object") {
1045
- lightConfig = lightsConfiguration[name];
1046
- }
1047
- if (typeof lightsConfiguration[name] === "number") {
1048
- lightConfig.type = lightsConfiguration[name];
1049
- }
1050
- lightConfig.name = name;
1051
- var light;
1052
- // light is not already available
1053
- if (lightsAvailable_1.indexOf(name) === -1) {
1054
- var constructor = Light.GetConstructorFromName(lightConfig.type, lightConfig.name, _this.scene);
1055
- if (!constructor) {
1056
- return;
1057
- }
1058
- light = constructor();
1059
- }
1060
- else {
1061
- // available? get it from the scene
1062
- light = _this.scene.getLightByName(name);
1063
- if (typeof lightsConfiguration[name] === "boolean") {
1064
- lightConfig.type = light.getTypeID();
1065
- }
1066
- lightsAvailable_1 = lightsAvailable_1.filter(function (ln) { return ln !== name; });
1067
- if (lightConfig.type !== undefined && light.getTypeID() !== lightConfig.type) {
1068
- light.dispose();
1069
- var constructor = Light.GetConstructorFromName(lightConfig.type, lightConfig.name, _this.scene);
1070
- if (!constructor) {
1071
- return;
1072
- }
1073
- light = constructor();
1074
- }
1075
- }
1076
- // if config set the light to false, dispose it.
1077
- if (lightsConfiguration[name] === false) {
1078
- light.dispose();
1079
- return;
1080
- }
1081
- //enabled
1082
- var enabled = lightConfig.enabled !== undefined ? lightConfig.enabled : !lightConfig.disabled;
1083
- light.setEnabled(enabled);
1084
- extendClassWithConfig(light, lightConfig);
1085
- //position. Some lights don't support shadows
1086
- if (light instanceof ShadowLight) {
1087
- // set default values
1088
- light.shadowMinZ = light.shadowMinZ || 0.2;
1089
- light.shadowMaxZ = Math.min(10, light.shadowMaxZ || 10); //large far clips reduce shadow depth precision
1090
- if (lightConfig.target) {
1091
- if (light.setDirectionToTarget) {
1092
- var target = Vector3.Zero().copyFromFloats(lightConfig.target.x, lightConfig.target.y, lightConfig.target.z);
1093
- light.setDirectionToTarget(target);
1094
- }
1095
- }
1096
- else if (lightConfig.direction) {
1097
- var direction = Vector3.Zero().copyFromFloats(lightConfig.direction.x, lightConfig.direction.y, lightConfig.direction.z);
1098
- light.direction = direction;
1099
- }
1100
- var isShadowEnabled = false;
1101
- if (light.getTypeID() === Light.LIGHTTYPEID_DIRECTIONALLIGHT) {
1102
- light.shadowFrustumSize = lightConfig.shadowFrustumSize || 2;
1103
- isShadowEnabled = true;
1104
- }
1105
- else if (light.getTypeID() === Light.LIGHTTYPEID_SPOTLIGHT) {
1106
- var spotLight = light;
1107
- if (lightConfig.spotAngle !== undefined) {
1108
- spotLight.angle = (lightConfig.spotAngle * Math.PI) / 180;
1109
- }
1110
- if (spotLight.angle && lightConfig.shadowFieldOfView) {
1111
- spotLight.shadowAngleScale = lightConfig.shadowFieldOfView / spotLight.angle;
1112
- }
1113
- isShadowEnabled = true;
1114
- }
1115
- else if (light.getTypeID() === Light.LIGHTTYPEID_POINTLIGHT) {
1116
- if (lightConfig.shadowFieldOfView) {
1117
- light.shadowAngle = (lightConfig.shadowFieldOfView * Math.PI) / 180;
1118
- }
1119
- isShadowEnabled = true;
1120
- }
1121
- var shadowGenerator_1 = light.getShadowGenerator();
1122
- if (isShadowEnabled && lightConfig.shadowEnabled && _this._maxShadows) {
1123
- var bufferSize = lightConfig.shadowBufferSize || 256;
1124
- if (!shadowGenerator_1) {
1125
- shadowGenerator_1 = new ShadowGenerator(bufferSize, light);
1126
- }
1127
- var blurKernel = _this.getBlurKernel(light, bufferSize);
1128
- shadowGenerator_1.bias = _this._shadowGeneratorBias;
1129
- shadowGenerator_1.blurKernel = blurKernel;
1130
- //override defaults
1131
- extendClassWithConfig(shadowGenerator_1, lightConfig.shadowConfig || {});
1132
- // add the focus meshes to the shadow list
1133
- _this._observablesManager &&
1134
- _this._observablesManager.onModelLoadedObservable.add(function (model) {
1135
- _this._updateShadowRenderList(shadowGenerator_1, model);
1136
- });
1137
- //if (model) {
1138
- _this._updateShadowRenderList(shadowGenerator_1);
1139
- //}
1140
- }
1141
- else if (shadowGenerator_1) {
1142
- shadowGenerator_1.dispose();
1143
- }
1144
- }
1145
- });
1146
- // render priority
1147
- var globalLightsConfiguration = this._globalConfiguration.lights || {};
1148
- Object.keys(globalLightsConfiguration)
1149
- .sort()
1150
- .forEach(function (name, idx) {
1151
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1152
- // const configuration = globalLightsConfiguration[name];
1153
- var light = _this.scene.getLightByName(name);
1154
- // sanity check
1155
- if (!light) {
1156
- return;
1157
- }
1158
- light.renderPriority = -idx;
1159
- });
1160
- }
1161
- this.onLightsConfiguredObservable.notifyObservers({
1162
- sceneManager: this,
1163
- object: this.scene.lights,
1164
- newConfiguration: lightsConfiguration,
1165
- });
1166
- };
1167
- SceneManager.prototype._updateShadowRenderList = function (shadowGenerator, model, resetList) {
1168
- var focusMeshes = model ? model.meshes : this.scene.meshes;
1169
- // add the focus meshes to the shadow list
1170
- var shadownMap = shadowGenerator.getShadowMap();
1171
- if (!shadownMap) {
1172
- return;
1173
- }
1174
- if (resetList && shadownMap.renderList) {
1175
- shadownMap.renderList.length = 0;
1176
- }
1177
- else {
1178
- shadownMap.renderList = shadownMap.renderList || [];
1179
- }
1180
- for (var index = 0; index < focusMeshes.length; index++) {
1181
- var mesh = focusMeshes[index];
1182
- if (Tags.MatchesQuery(mesh, "castShadow") && shadownMap.renderList.indexOf(mesh) === -1) {
1183
- shadownMap.renderList.push(mesh);
1184
- }
1185
- }
1186
- if (!this._shadowGroundPlane) {
1187
- if (shadowGenerator.useBlurCloseExponentialShadowMap) {
1188
- var shadowGroundPlane = CreatePlane("shadowGroundPlane", { size: 100 }, this.scene);
1189
- shadowGroundPlane.useVertexColors = false;
1190
- //material isn't ever used in rendering, just used to set back face culling
1191
- shadowGroundPlane.material = new PBRMaterial("shadowGroundPlaneMaterial", this.scene);
1192
- shadowGroundPlane.material.backFaceCulling = false;
1193
- shadowGroundPlane.rotation.x = Math.PI * 0.5;
1194
- shadowGroundPlane.freezeWorldMatrix();
1195
- this._shadowGroundPlane = shadowGroundPlane;
1196
- this.scene.removeMesh(shadowGroundPlane);
1197
- }
1198
- }
1199
- else {
1200
- if (!shadowGenerator.useBlurCloseExponentialShadowMap) {
1201
- this._shadowGroundPlane.dispose();
1202
- this._shadowGroundPlane = null;
1203
- }
1204
- }
1205
- if (this._shadowGroundPlane && shadownMap.renderList.indexOf(this._shadowGroundPlane) === -1) {
1206
- shadownMap.renderList.push(this._shadowGroundPlane);
1207
- }
1208
- };
1209
- SceneManager.prototype._updateGroundMirrorRenderList = function (model, resetList) {
1210
- if (this.environmentHelper && this.environmentHelper.groundMirror && this.environmentHelper.groundMirror.renderList) {
1211
- var focusMeshes = model ? model.meshes : this.scene.meshes;
1212
- var renderList = this.environmentHelper.groundMirror.renderList;
1213
- if (resetList) {
1214
- renderList.length = 0;
1215
- }
1216
- for (var index = 0; index < focusMeshes.length; index++) {
1217
- var mesh = focusMeshes[index];
1218
- if (renderList.indexOf(mesh) === -1) {
1219
- renderList.push(mesh);
1220
- }
1221
- }
1222
- }
1223
- };
1224
- /**
1225
- * Gets the shadow map blur kernel according to the light configuration.
1226
- * @param light The light used to generate the shadows
1227
- * @param bufferSize The size of the shadow map
1228
- * @return the kernel blur size
1229
- */
1230
- SceneManager.prototype.getBlurKernel = function (light, bufferSize) {
1231
- var normalizedBlurKernel = 0.05; // TODO Should come from the config.
1232
- if (light.getTypeID() === Light.LIGHTTYPEID_DIRECTIONALLIGHT) {
1233
- normalizedBlurKernel = normalizedBlurKernel / light.shadowFrustumSize;
1234
- }
1235
- else if (light.getTypeID() === Light.LIGHTTYPEID_POINTLIGHT) {
1236
- normalizedBlurKernel = normalizedBlurKernel / light.shadowAngle;
1237
- }
1238
- else if (light.getTypeID() === Light.LIGHTTYPEID_SPOTLIGHT) {
1239
- normalizedBlurKernel = normalizedBlurKernel / (light.angle * light.shadowAngleScale);
1240
- }
1241
- var minimumBlurKernel = 5 / (bufferSize / 256); //magic number that aims to keep away sawtooth shadows
1242
- var blurKernel = Math.max(bufferSize * normalizedBlurKernel, minimumBlurKernel);
1243
- return blurKernel;
1244
- };
1245
- /**
1246
- * Alters render settings to reduce features based on hardware feature limitations
1247
- * @param enableHDR Allows the viewer to run in HDR mode.
1248
- */
1249
- SceneManager.prototype._handleHardwareLimitations = function (enableHDR) {
1250
- if (enableHDR === void 0) { enableHDR = true; }
1251
- //flip rendering settings switches based on hardware support
1252
- var maxVaryingRows = this._engine.getCaps().maxVaryingVectors;
1253
- var maxFragmentSamplers = this._engine.getCaps().maxTexturesImageUnits;
1254
- //shadows are disabled if there's not enough varyings for a single shadow
1255
- if (maxVaryingRows < 8 || maxFragmentSamplers < 8) {
1256
- this._maxShadows = 0;
1257
- }
1258
- else {
1259
- this._maxShadows = 3;
1260
- }
1261
- //can we render to any >= 16-bit targets (required for HDR)
1262
- var caps = this._engine.getCaps();
1263
- var linearHalfFloatTargets = caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering;
1264
- var linearFloatTargets = caps.textureFloatRender && caps.textureFloatLinearFiltering;
1265
- this._hdrSupport = enableHDR && !!(linearFloatTargets || linearHalfFloatTargets);
1266
- if (linearHalfFloatTargets) {
1267
- this._defaultHighpTextureType = Constants.TEXTURETYPE_HALF_FLOAT;
1268
- this._shadowGeneratorBias = 0.002;
1269
- }
1270
- else if (linearFloatTargets) {
1271
- this._defaultHighpTextureType = Constants.TEXTURETYPE_FLOAT;
1272
- this._shadowGeneratorBias = 0.001;
1273
- }
1274
- else {
1275
- this._defaultHighpTextureType = Constants.TEXTURETYPE_UNSIGNED_INT;
1276
- this._shadowGeneratorBias = 0.001;
1277
- }
1278
- this._defaultPipelineTextureType = this._hdrSupport ? this._defaultHighpTextureType : Constants.TEXTURETYPE_UNSIGNED_INT;
1279
- };
1280
- /**
1281
- * Dispose the entire viewer including the scene and the engine
1282
- */
1283
- SceneManager.prototype.dispose = function () {
1284
- // this.onCameraConfiguredObservable.clear();
1285
- this.onEnvironmentConfiguredObservable.clear();
1286
- this.onLightsConfiguredObservable.clear();
1287
- this.onModelsConfiguredObservable.clear();
1288
- this.onSceneConfiguredObservable.clear();
1289
- this.onSceneInitObservable.clear();
1290
- this.onSceneOptimizerConfiguredObservable.clear();
1291
- this.onVRConfiguredObservable.clear();
1292
- if (this.sceneOptimizer) {
1293
- this.sceneOptimizer.stop();
1294
- this.sceneOptimizer.dispose();
1295
- }
1296
- if (this.environmentHelper) {
1297
- this.environmentHelper.dispose();
1298
- }
1299
- this.models.forEach(function (model) {
1300
- model.dispose();
1301
- });
1302
- if (this._defaultRenderingPipeline) {
1303
- this._defaultRenderingPipeline.dispose();
1304
- }
1305
- this.models.length = 0;
1306
- if (this.scene) {
1307
- this.scene.dispose();
1308
- }
1309
- };
1310
- /**
1311
- * Get an environment asset url by using the configuration if the path is not absolute.
1312
- * @param url Asset url
1313
- * @returns The Asset url using the `environmentAssetsRootURL` if the url is not an absolute path.
1314
- */
1315
- SceneManager.prototype._getAssetUrl = function (url) {
1316
- var returnUrl = url;
1317
- if (url && url.toLowerCase().indexOf("//") === -1) {
1318
- if (!this._assetsRootURL) {
1319
- // Tools.Warn("Please, specify the root url of your assets before loading the configuration (labs.environmentAssetsRootURL) or disable the background through the viewer options.");
1320
- return url;
1321
- }
1322
- returnUrl = this._assetsRootURL + returnUrl;
1323
- }
1324
- return returnUrl;
1325
- };
1326
- SceneManager.prototype._setCameraBehavior = function (name, behaviorConfig) {
1327
- var behavior;
1328
- var type;
1329
- if (typeof behaviorConfig === "object") {
1330
- type = behaviorConfig.type;
1331
- }
1332
- else if (typeof behaviorConfig === "number") {
1333
- type = behaviorConfig;
1334
- }
1335
- else {
1336
- type = this._cameraBehaviorMapping[name];
1337
- }
1338
- if (type === undefined) {
1339
- return;
1340
- }
1341
- var config = typeof behaviorConfig === "object" ? behaviorConfig : {};
1342
- var enabled = true;
1343
- if (typeof behaviorConfig === "boolean") {
1344
- enabled = behaviorConfig;
1345
- }
1346
- // constructing behavior
1347
- switch (type) {
1348
- case 0 /* AUTOROTATION */:
1349
- this.camera.useAutoRotationBehavior = enabled;
1350
- behavior = this.camera.autoRotationBehavior;
1351
- break;
1352
- case 1 /* BOUNCING */:
1353
- this.camera.useBouncingBehavior = enabled;
1354
- behavior = this.camera.bouncingBehavior;
1355
- break;
1356
- case 2 /* FRAMING */:
1357
- this.camera.useFramingBehavior = enabled;
1358
- behavior = this.camera.framingBehavior;
1359
- break;
1360
- default:
1361
- behavior = null;
1362
- break;
1363
- }
1364
- if (behavior) {
1365
- this._cameraBehaviorMapping[name] = type;
1366
- if (typeof behaviorConfig === "object") {
1367
- extendClassWithConfig(behavior, behaviorConfig);
1368
- }
1369
- }
1370
- // post attach configuration. Some functionalities require the attached camera.
1371
- switch (type) {
1372
- case 0 /* AUTOROTATION */:
1373
- break;
1374
- case 1 /* BOUNCING */:
1375
- break;
1376
- case 2 /* FRAMING */:
1377
- this._observablesManager &&
1378
- this._observablesManager.onModelLoadedObservable.add(function (model) {
1379
- if (config.zoomOnBoundingInfo) {
1380
- behavior.zoomOnMeshHierarchy(model.rootMesh);
1381
- }
1382
- });
1383
- break;
1384
- }
1385
- };
1386
- return SceneManager;
1387
- }());
1388
- export { SceneManager };
1
+ import { getConfigurationKey } from "../configuration/configuration.js";
2
+ import { ModelState } from "../model/viewerModel.js";
3
+ import { extendClassWithConfig, deepmerge } from "../helper/index.js";
4
+ import { ViewerLabs } from "../labs/viewerLabs.js";
5
+ import { getCustomOptimizerByName } from "../optimizer/custom/index.js";
6
+ import { Observable } from "@babylonjs/core/Misc/observable.js";
7
+ import { SceneOptimizer, SceneOptimizerOptions } from "@babylonjs/core/Misc/sceneOptimizer.js";
8
+ import { ArcRotateCamera } from "@babylonjs/core/Cameras/arcRotateCamera.js";
9
+ import { Light } from "@babylonjs/core/Lights/light.js";
10
+ import { EnvironmentHelper } from "@babylonjs/core/Helpers/environmentHelper.js";
11
+ import { Color3, Quaternion, Vector3, Axis, Matrix } from "@babylonjs/core/Maths/math.js";
12
+ import { DefaultRenderingPipeline } from "@babylonjs/core/PostProcesses/RenderPipeline/Pipelines/defaultRenderingPipeline.js";
13
+ import { Animation } from "@babylonjs/core/Animations/index.js";
14
+ import { AnimationPropertiesOverride } from "@babylonjs/core/Animations/animationPropertiesOverride.js";
15
+ import { RenderTargetTexture } from "@babylonjs/core/Materials/Textures/renderTargetTexture.js";
16
+ import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
17
+ import { ShadowLight } from "@babylonjs/core/Lights/shadowLight.js";
18
+ import { CubeTexture } from "@babylonjs/core/Materials/Textures/cubeTexture.js";
19
+ import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight.js";
20
+ import { Scalar } from "@babylonjs/core/Maths/math.scalar.js";
21
+ import { CreatePlane } from "@babylonjs/core/Meshes/Builders/planeBuilder.js";
22
+ import { Tags } from "@babylonjs/core/Misc/tags.js";
23
+ import { Scene } from "@babylonjs/core/scene.js";
24
+ import { ShadowGenerator } from "@babylonjs/core/Lights/Shadows/shadowGenerator.js";
25
+ import { Constants } from "@babylonjs/core/Engines/constants.js";
26
+ import "@babylonjs/core/Audio/audioSceneComponent.js";
27
+ import "@babylonjs/core/Helpers/sceneHelpers.js";
28
+ var SceneManager = /** @class */ (function () {
29
+ function SceneManager(_engine, _configurationContainer, _observablesManager) {
30
+ var _this = this;
31
+ this._engine = _engine;
32
+ this._configurationContainer = _configurationContainer;
33
+ this._observablesManager = _observablesManager;
34
+ this._white = Color3.White();
35
+ this._forceShadowUpdate = false;
36
+ this._processShadows = true;
37
+ this._groundEnabled = true;
38
+ this._groundMirrorEnabled = true;
39
+ this._defaultRenderingPipelineEnabled = false;
40
+ this._globalConfiguration = {};
41
+ this._defaultRenderingPipelineShouldBuild = true;
42
+ // default from rendering pipeline
43
+ this._bloomEnabled = false;
44
+ // default from rendering pipeline
45
+ this._fxaaEnabled = false;
46
+ this._focusOnModel = function (model) {
47
+ var boundingInfo = model.rootMesh.getHierarchyBoundingVectors(true);
48
+ var sizeVec = boundingInfo.max.subtract(boundingInfo.min);
49
+ var halfSizeVec = sizeVec.scale(0.5);
50
+ var center = boundingInfo.min.add(halfSizeVec);
51
+ _this.camera.setTarget(center);
52
+ _this.camera.alpha = (_this._globalConfiguration.camera && _this._globalConfiguration.camera.alpha) || _this.camera.alpha;
53
+ _this.camera.beta = (_this._globalConfiguration.camera && _this._globalConfiguration.camera.beta) || _this.camera.beta;
54
+ _this.camera.radius = (_this._globalConfiguration.camera && _this._globalConfiguration.camera.radius) || _this.camera.radius;
55
+ var sceneDiagonalLenght = sizeVec.length();
56
+ if (isFinite(sceneDiagonalLenght)) {
57
+ _this.camera.upperRadiusLimit = sceneDiagonalLenght * 4;
58
+ }
59
+ if (_this._configurationContainer.configuration) {
60
+ _this._configureEnvironment(_this._configurationContainer.configuration.skybox, _this._configurationContainer.configuration.ground);
61
+ }
62
+ /*this.scene.lights.filter(light => light instanceof ShadowLight).forEach(light => {
63
+ // casting ais safe, due to the constraints tested before
64
+ (<ShadowLight>light).setDirectionToTarget(center);
65
+ });*/
66
+ };
67
+ this._cameraBehaviorMapping = {};
68
+ this.models = [];
69
+ this.onCameraConfiguredObservable = new Observable();
70
+ this.onLightsConfiguredObservable = new Observable();
71
+ this.onModelsConfiguredObservable = new Observable();
72
+ this.onSceneConfiguredObservable = new Observable();
73
+ this.onSceneInitObservable = new Observable();
74
+ this.onSceneOptimizerConfiguredObservable = new Observable();
75
+ this.onEnvironmentConfiguredObservable = new Observable();
76
+ this.onVRConfiguredObservable = new Observable();
77
+ //this._viewer.onEngineInitObservable.add(() => {
78
+ this._handleHardwareLimitations();
79
+ //});
80
+ this.onSceneInitObservable.add(function (scene) {
81
+ _this.scene.animationPropertiesOverride = _this.scene.animationPropertiesOverride || new AnimationPropertiesOverride();
82
+ _this.labs = new ViewerLabs(scene);
83
+ var updateShadows = function () {
84
+ for (var _i = 0, _a = _this.scene.lights; _i < _a.length; _i++) {
85
+ var light = _a[_i];
86
+ var generator = light.getShadowGenerator();
87
+ if (generator) {
88
+ // Processing shadows if animates
89
+ var shadowMap = generator.getShadowMap();
90
+ if (shadowMap) {
91
+ shadowMap.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
92
+ }
93
+ }
94
+ }
95
+ };
96
+ scene.registerBeforeRender(function () {
97
+ if (_this._forceShadowUpdate || (scene.animatables && scene.animatables.length > 0)) {
98
+ // make sure all models are loaded
99
+ updateShadows();
100
+ _this._forceShadowUpdate = false;
101
+ }
102
+ else if (!_this.models.every(function (model) {
103
+ if (!model.shadowsRenderedAfterLoad) {
104
+ model.shadowsRenderedAfterLoad = true;
105
+ return false;
106
+ }
107
+ return model.state === ModelState.COMPLETE && !model.currentAnimation;
108
+ })) {
109
+ updateShadows();
110
+ }
111
+ });
112
+ return _this._observablesManager && _this._observablesManager.onSceneInitObservable.notifyObserversWithPromise(_this.scene);
113
+ });
114
+ if (this._observablesManager) {
115
+ this._observablesManager.onModelLoadedObservable.add(function (model) {
116
+ for (var _i = 0, _a = _this.scene.lights; _i < _a.length; _i++) {
117
+ var light = _a[_i];
118
+ var generator = light.getShadowGenerator();
119
+ if (generator) {
120
+ // Processing shadows if animates
121
+ var shadowMap = generator.getShadowMap();
122
+ if (shadowMap) {
123
+ shadowMap.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
124
+ }
125
+ }
126
+ }
127
+ _this._focusOnModel(model);
128
+ });
129
+ this._observablesManager.onModelAddedObservable.add(function (model) {
130
+ _this.models.push(model);
131
+ });
132
+ this._observablesManager.onModelRemovedObservable.add(function (model) {
133
+ _this.models.splice(_this.models.indexOf(model), 1);
134
+ });
135
+ }
136
+ }
137
+ Object.defineProperty(SceneManager.prototype, "defaultRenderingPipeline", {
138
+ get: function () {
139
+ return this._defaultRenderingPipeline;
140
+ },
141
+ enumerable: false,
142
+ configurable: true
143
+ });
144
+ Object.defineProperty(SceneManager.prototype, "vrHelper", {
145
+ get: function () {
146
+ return this._vrHelper;
147
+ },
148
+ enumerable: false,
149
+ configurable: true
150
+ });
151
+ Object.defineProperty(SceneManager.prototype, "isHdrSupported", {
152
+ /**
153
+ * Returns a boolean representing HDR support
154
+ */
155
+ get: function () {
156
+ return this._hdrSupport;
157
+ },
158
+ enumerable: false,
159
+ configurable: true
160
+ });
161
+ Object.defineProperty(SceneManager.prototype, "mainColor", {
162
+ /**
163
+ * Return the main color defined in the configuration.
164
+ */
165
+ get: function () {
166
+ return this._configurationContainer.mainColor;
167
+ },
168
+ enumerable: false,
169
+ configurable: true
170
+ });
171
+ Object.defineProperty(SceneManager.prototype, "reflectionColor", {
172
+ get: function () {
173
+ return this._configurationContainer.reflectionColor;
174
+ },
175
+ enumerable: false,
176
+ configurable: true
177
+ });
178
+ Object.defineProperty(SceneManager.prototype, "animationBlendingEnabled", {
179
+ get: function () {
180
+ return this.scene && this.scene.animationPropertiesOverride.enableBlending;
181
+ },
182
+ set: function (value) {
183
+ this.scene.animationPropertiesOverride.enableBlending = value;
184
+ },
185
+ enumerable: false,
186
+ configurable: true
187
+ });
188
+ Object.defineProperty(SceneManager.prototype, "observablesManager", {
189
+ get: function () {
190
+ return this._observablesManager;
191
+ },
192
+ enumerable: false,
193
+ configurable: true
194
+ });
195
+ Object.defineProperty(SceneManager.prototype, "processShadows", {
196
+ /**
197
+ * The flag defining whether shadows are rendered constantly or once.
198
+ */
199
+ get: function () {
200
+ return this._processShadows;
201
+ },
202
+ /**
203
+ * Should shadows be rendered every frame, or only once and stop.
204
+ * This can be used to optimize a scene.
205
+ *
206
+ * Not that the shadows will NOT disapear but will remain in place.
207
+ * @param process if true shadows will be updated once every frame. if false they will stop being updated.
208
+ */
209
+ set: function (process) {
210
+ var refreshType = process ? RenderTargetTexture.REFRESHRATE_RENDER_ONEVERYFRAME : RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
211
+ for (var _i = 0, _a = this.scene.lights; _i < _a.length; _i++) {
212
+ var light = _a[_i];
213
+ var generator = light.getShadowGenerator();
214
+ if (generator) {
215
+ var shadowMap = generator.getShadowMap();
216
+ if (shadowMap) {
217
+ shadowMap.refreshRate = refreshType;
218
+ }
219
+ }
220
+ }
221
+ this._processShadows = process;
222
+ },
223
+ enumerable: false,
224
+ configurable: true
225
+ });
226
+ Object.defineProperty(SceneManager.prototype, "groundEnabled", {
227
+ get: function () {
228
+ return this._groundEnabled;
229
+ },
230
+ set: function (newValue) {
231
+ if (newValue === this._groundEnabled) {
232
+ return;
233
+ }
234
+ this._groundEnabled = newValue;
235
+ if (this.environmentHelper && this.environmentHelper.ground) {
236
+ this.environmentHelper.ground.setEnabled(this._groundEnabled);
237
+ }
238
+ },
239
+ enumerable: false,
240
+ configurable: true
241
+ });
242
+ Object.defineProperty(SceneManager.prototype, "groundMirrorEnabled", {
243
+ /**
244
+ * gets whether the reflection is disabled.
245
+ */
246
+ get: function () {
247
+ return this._groundMirrorEnabled;
248
+ },
249
+ /**
250
+ * sets whether the reflection is disabled.
251
+ */
252
+ set: function (value) {
253
+ if (this._groundMirrorEnabled === value) {
254
+ return;
255
+ }
256
+ this._groundMirrorEnabled = value;
257
+ if (this.environmentHelper && this.environmentHelper.groundMaterial && this.environmentHelper.groundMirror) {
258
+ if (!value) {
259
+ this.environmentHelper.groundMaterial.reflectionTexture = null;
260
+ }
261
+ else {
262
+ this.environmentHelper.groundMaterial.reflectionTexture = this.environmentHelper.groundMirror;
263
+ }
264
+ }
265
+ },
266
+ enumerable: false,
267
+ configurable: true
268
+ });
269
+ Object.defineProperty(SceneManager.prototype, "defaultRenderingPipelineEnabled", {
270
+ get: function () {
271
+ return this._defaultRenderingPipelineEnabled;
272
+ },
273
+ set: function (value) {
274
+ if (value === this._defaultRenderingPipelineEnabled) {
275
+ return;
276
+ }
277
+ this._defaultRenderingPipelineEnabled = value;
278
+ this._rebuildPostprocesses();
279
+ if (this._defaultRenderingPipeline) {
280
+ this._defaultRenderingPipelineShouldBuild = false;
281
+ this._defaultRenderingPipeline.prepare();
282
+ this.scene.imageProcessingConfiguration.applyByPostProcess = true;
283
+ }
284
+ },
285
+ enumerable: false,
286
+ configurable: true
287
+ });
288
+ /**
289
+ * Sets the engine flags to unlock all babylon features.
290
+ * Can also be configured using the scene.flags configuration object
291
+ */
292
+ SceneManager.prototype.unlockBabylonFeatures = function () {
293
+ this.scene.shadowsEnabled = true;
294
+ this.scene.particlesEnabled = true;
295
+ this.scene.postProcessesEnabled = true;
296
+ this.scene.collisionsEnabled = true;
297
+ this.scene.lightsEnabled = true;
298
+ this.scene.texturesEnabled = true;
299
+ this.scene.lensFlaresEnabled = true;
300
+ this.scene.proceduralTexturesEnabled = true;
301
+ this.scene.renderTargetsEnabled = true;
302
+ this.scene.spritesEnabled = true;
303
+ this.scene.skeletonsEnabled = true;
304
+ this.scene.audioEnabled = true;
305
+ };
306
+ /**
307
+ * initialize the scene. Calling this function again will dispose the old scene, if exists.
308
+ * @param sceneConfiguration
309
+ */
310
+ SceneManager.prototype.initScene = function (sceneConfiguration) {
311
+ if (sceneConfiguration === void 0) { sceneConfiguration = {}; }
312
+ // if the scene exists, dispose it.
313
+ if (this.scene) {
314
+ this.scene.dispose();
315
+ }
316
+ // create a new scene
317
+ this.scene = new Scene(this._engine);
318
+ this._configurationContainer.scene = this.scene;
319
+ // set a default PBR material
320
+ if (!sceneConfiguration.defaultMaterial) {
321
+ var defaultMaterial = new PBRMaterial("defaultMaterial", this.scene);
322
+ defaultMaterial.reflectivityColor = new Color3(0.1, 0.1, 0.1);
323
+ defaultMaterial.microSurface = 0.6;
324
+ if (this.scene.defaultMaterial) {
325
+ this.scene.defaultMaterial.dispose();
326
+ }
327
+ this.scene.defaultMaterial = defaultMaterial;
328
+ }
329
+ this.scene.animationPropertiesOverride = new AnimationPropertiesOverride();
330
+ Animation.AllowMatricesInterpolation = true;
331
+ /*if (sceneConfiguration.glow) {
332
+ let options: Partial<IGlowLayerOptions> = {
333
+ mainTextureFixedSize: 512
334
+ };
335
+ if (typeof sceneConfiguration.glow === 'object') {
336
+ options = sceneConfiguration.glow
337
+ }
338
+ var gl = new GlowLayer("glow", this.scene, options);
339
+ }*/
340
+ return this.onSceneInitObservable.notifyObserversWithPromise(this.scene);
341
+ };
342
+ SceneManager.prototype.clearScene = function (clearModels, clearLights) {
343
+ if (clearModels === void 0) { clearModels = true; }
344
+ if (clearLights === void 0) { clearLights = false; }
345
+ if (clearModels) {
346
+ this.models.forEach(function (m) { return m.dispose(); });
347
+ this.models.length = 0;
348
+ }
349
+ if (clearLights) {
350
+ this.scene.lights.forEach(function (l) { return l.dispose(); });
351
+ }
352
+ };
353
+ /**
354
+ * This will update the scene's configuration, including camera, lights, environment.
355
+ * @param newConfiguration the delta that should be configured. This includes only the changes
356
+ */
357
+ SceneManager.prototype.updateConfiguration = function (newConfiguration) {
358
+ var _this = this;
359
+ if (this._configurationContainer) {
360
+ this._globalConfiguration = this._configurationContainer.configuration;
361
+ }
362
+ else {
363
+ this._globalConfiguration = newConfiguration;
364
+ }
365
+ // update scene configuration
366
+ if (newConfiguration.scene) {
367
+ this._configureScene(newConfiguration.scene);
368
+ }
369
+ // optimizer
370
+ if (newConfiguration.optimizer !== undefined) {
371
+ this._configureOptimizer(newConfiguration.optimizer);
372
+ }
373
+ // configure model
374
+ /*if (newConfiguration.model && typeof newConfiguration.model === 'object') {
375
+ this._configureModel(newConfiguration.model);
376
+ }*/
377
+ // lights
378
+ this._configureLights(newConfiguration.lights);
379
+ // environment
380
+ if (newConfiguration.skybox !== undefined || newConfiguration.ground !== undefined) {
381
+ this._configureEnvironment(newConfiguration.skybox, newConfiguration.ground);
382
+ }
383
+ // camera
384
+ this._configureCamera(newConfiguration.camera);
385
+ if (newConfiguration.environmentMap !== undefined) {
386
+ this._configureEnvironmentMap(newConfiguration.environmentMap);
387
+ }
388
+ if (newConfiguration.vr !== undefined) {
389
+ this._configureVR(newConfiguration.vr);
390
+ }
391
+ if (newConfiguration.lab) {
392
+ // rendering pipelines
393
+ if (newConfiguration.lab.defaultRenderingPipelines) {
394
+ var pipelineConfig = newConfiguration.lab.defaultRenderingPipelines;
395
+ if (typeof pipelineConfig === "boolean") {
396
+ this.defaultRenderingPipelineEnabled = pipelineConfig;
397
+ }
398
+ else {
399
+ this.defaultRenderingPipelineEnabled = true;
400
+ }
401
+ }
402
+ if (this.environmentHelper && newConfiguration.lab.environmentMainColor) {
403
+ var mainColor = new Color3(newConfiguration.lab.environmentMainColor.r, newConfiguration.lab.environmentMainColor.g, newConfiguration.lab.environmentMainColor.b);
404
+ this.environmentHelper.setMainColor(mainColor);
405
+ }
406
+ if (newConfiguration.lab.globalLightRotation !== undefined) {
407
+ // rotate all lights that are shadow lights
408
+ this.scene.lights
409
+ .filter(function (light) { return light instanceof ShadowLight; })
410
+ .forEach(function (light) {
411
+ // casting and '!' are safe, due to the constraints tested before
412
+ _this.labs.rotateShadowLight(light, newConfiguration.lab.globalLightRotation);
413
+ });
414
+ this._forceShadowUpdate = true;
415
+ }
416
+ }
417
+ if (this._defaultRenderingPipeline && this._defaultRenderingPipeline.imageProcessing) {
418
+ this._defaultRenderingPipeline.imageProcessing.fromLinearSpace = true;
419
+ }
420
+ if (this._defaultRenderingPipelineShouldBuild && this._defaultRenderingPipeline) {
421
+ this._defaultRenderingPipelineShouldBuild = false;
422
+ this._defaultRenderingPipeline.prepare();
423
+ }
424
+ };
425
+ SceneManager.prototype._rebuildPostprocesses = function (configuration) {
426
+ if (!this._defaultRenderingPipelineEnabled || !getConfigurationKey("scene.imageProcessingConfiguration.isEnabled", this._globalConfiguration)) {
427
+ if (this._defaultRenderingPipeline) {
428
+ this._defaultRenderingPipeline.dispose();
429
+ this._defaultRenderingPipeline = null;
430
+ this.scene.autoClearDepthAndStencil = true;
431
+ this.scene.autoClear = true;
432
+ this.scene.imageProcessingConfiguration.applyByPostProcess = false;
433
+ }
434
+ return;
435
+ }
436
+ var pipelineConfig = configuration || (this._globalConfiguration.lab && this._globalConfiguration.lab.defaultRenderingPipelines);
437
+ if (pipelineConfig) {
438
+ if (!this._defaultRenderingPipeline) {
439
+ // Create pipeline in manual mode to avoid triggering multiple shader compilations
440
+ this._defaultRenderingPipeline = new DefaultRenderingPipeline("default rendering pipeline", this._hdrSupport, this.scene, [this.camera], false);
441
+ }
442
+ this.scene.autoClear = false;
443
+ this.scene.autoClearDepthAndStencil = false;
444
+ this._defaultRenderingPipelineShouldBuild = true;
445
+ var bloomEnabled = this._bloomEnabled;
446
+ if (typeof pipelineConfig !== "boolean") {
447
+ extendClassWithConfig(this._defaultRenderingPipeline, pipelineConfig);
448
+ this._bloomEnabled = !!pipelineConfig.bloomEnabled;
449
+ this._fxaaEnabled = !!pipelineConfig.fxaaEnabled;
450
+ bloomEnabled = this._bloomEnabled && pipelineConfig.bloomWeight !== undefined && pipelineConfig.bloomWeight > 0;
451
+ this._defaultRenderingPipeline.bloomWeight = (pipelineConfig.bloomWeight !== undefined && pipelineConfig.bloomWeight) || this._defaultRenderingPipeline.bloomWeight;
452
+ }
453
+ this._defaultRenderingPipeline.bloomEnabled = bloomEnabled;
454
+ this._defaultRenderingPipeline.fxaaEnabled = this.fxaaEnabled;
455
+ }
456
+ };
457
+ Object.defineProperty(SceneManager.prototype, "bloomEnabled", {
458
+ get: function () {
459
+ return this._bloomEnabled;
460
+ },
461
+ set: function (value) {
462
+ if (this._bloomEnabled === value) {
463
+ return;
464
+ }
465
+ this._bloomEnabled = value;
466
+ this._rebuildPostprocesses();
467
+ if (this._defaultRenderingPipeline) {
468
+ this._defaultRenderingPipelineShouldBuild = false;
469
+ this._defaultRenderingPipeline.prepare();
470
+ this.scene.imageProcessingConfiguration.applyByPostProcess = true;
471
+ }
472
+ },
473
+ enumerable: false,
474
+ configurable: true
475
+ });
476
+ Object.defineProperty(SceneManager.prototype, "fxaaEnabled", {
477
+ get: function () {
478
+ return this._fxaaEnabled;
479
+ },
480
+ set: function (value) {
481
+ if (this._fxaaEnabled === value) {
482
+ return;
483
+ }
484
+ this._fxaaEnabled = value;
485
+ this._rebuildPostprocesses();
486
+ if (this._defaultRenderingPipeline) {
487
+ this._defaultRenderingPipelineShouldBuild = false;
488
+ this._defaultRenderingPipeline.prepare();
489
+ this.scene.imageProcessingConfiguration.applyByPostProcess = true;
490
+ }
491
+ },
492
+ enumerable: false,
493
+ configurable: true
494
+ });
495
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
496
+ SceneManager.prototype.setDefaultMaterial = function (sceneConfig) { };
497
+ /**
498
+ * internally configure the scene using the provided configuration.
499
+ * The scene will not be recreated, but just updated.
500
+ * @param sceneConfig the (new) scene configuration
501
+ */
502
+ SceneManager.prototype._configureScene = function (sceneConfig) {
503
+ // sanity check!
504
+ if (!this.scene) {
505
+ return;
506
+ }
507
+ var cc = sceneConfig.clearColor;
508
+ var oldcc = this.scene.clearColor;
509
+ if (cc) {
510
+ if (cc.r !== undefined) {
511
+ oldcc.r = cc.r;
512
+ }
513
+ if (cc.g !== undefined) {
514
+ oldcc.g = cc.g;
515
+ }
516
+ if (cc.b !== undefined) {
517
+ oldcc.b = cc.b;
518
+ }
519
+ if (cc.a !== undefined) {
520
+ oldcc.a = cc.a;
521
+ }
522
+ }
523
+ if (sceneConfig.assetsRootURL) {
524
+ this._assetsRootURL = sceneConfig.assetsRootURL;
525
+ }
526
+ // image processing configuration - optional.
527
+ if (sceneConfig.imageProcessingConfiguration) {
528
+ extendClassWithConfig(this.scene.imageProcessingConfiguration, sceneConfig.imageProcessingConfiguration);
529
+ }
530
+ //animation properties override
531
+ if (sceneConfig.animationPropertiesOverride) {
532
+ extendClassWithConfig(this.scene.animationPropertiesOverride, sceneConfig.animationPropertiesOverride);
533
+ }
534
+ if (sceneConfig.environmentTexture) {
535
+ if (!(this.scene.environmentTexture && this.scene.environmentTexture.url === sceneConfig.environmentTexture)) {
536
+ if (this.scene.environmentTexture && this.scene.environmentTexture.dispose) {
537
+ this.scene.environmentTexture.dispose();
538
+ }
539
+ var environmentTexture = CubeTexture.CreateFromPrefilteredData(sceneConfig.environmentTexture, this.scene);
540
+ this.scene.environmentTexture = environmentTexture;
541
+ }
542
+ }
543
+ if (sceneConfig.debug === true) {
544
+ this.scene.debugLayer.show();
545
+ }
546
+ else if (sceneConfig.debug === false) {
547
+ if (this.scene.debugLayer.isVisible()) {
548
+ this.scene.debugLayer.hide();
549
+ }
550
+ }
551
+ if (sceneConfig.disableHdr) {
552
+ this._handleHardwareLimitations(false);
553
+ }
554
+ else {
555
+ this._handleHardwareLimitations(true);
556
+ }
557
+ if (sceneConfig.renderInBackground !== undefined) {
558
+ this._engine.renderEvenInBackground = !!sceneConfig.renderInBackground;
559
+ }
560
+ var canvas = this._engine.getInputElement();
561
+ if (canvas) {
562
+ if (this.camera && sceneConfig.disableCameraControl) {
563
+ this.camera.detachControl();
564
+ }
565
+ else if (this.camera && sceneConfig.disableCameraControl === false) {
566
+ this.camera.attachControl();
567
+ }
568
+ }
569
+ this.setDefaultMaterial(sceneConfig);
570
+ if (sceneConfig.flags) {
571
+ extendClassWithConfig(this.scene, sceneConfig.flags);
572
+ }
573
+ this.onSceneConfiguredObservable.notifyObservers({
574
+ sceneManager: this,
575
+ object: this.scene,
576
+ newConfiguration: sceneConfig,
577
+ });
578
+ };
579
+ /**
580
+ * Configure the scene optimizer.
581
+ * The existing scene optimizer will be disposed and a new one will be created.
582
+ * @param optimizerConfig the (new) optimizer configuration
583
+ */
584
+ SceneManager.prototype._configureOptimizer = function (optimizerConfig) {
585
+ var _this = this;
586
+ if (typeof optimizerConfig === "boolean") {
587
+ if (this.sceneOptimizer) {
588
+ this.sceneOptimizer.stop();
589
+ this.sceneOptimizer.dispose();
590
+ delete this.sceneOptimizer;
591
+ }
592
+ if (optimizerConfig) {
593
+ this.sceneOptimizer = new SceneOptimizer(this.scene);
594
+ this.sceneOptimizer.start();
595
+ }
596
+ }
597
+ else {
598
+ var optimizerOptions = new SceneOptimizerOptions(optimizerConfig.targetFrameRate, optimizerConfig.trackerDuration);
599
+ // check for degradation
600
+ if (optimizerConfig.degradation) {
601
+ switch (optimizerConfig.degradation) {
602
+ case "low":
603
+ optimizerOptions = SceneOptimizerOptions.LowDegradationAllowed(optimizerConfig.targetFrameRate);
604
+ break;
605
+ case "moderate":
606
+ optimizerOptions = SceneOptimizerOptions.ModerateDegradationAllowed(optimizerConfig.targetFrameRate);
607
+ break;
608
+ case "hight":
609
+ optimizerOptions = SceneOptimizerOptions.HighDegradationAllowed(optimizerConfig.targetFrameRate);
610
+ break;
611
+ }
612
+ }
613
+ if (this.sceneOptimizer) {
614
+ this.sceneOptimizer.stop();
615
+ this.sceneOptimizer.dispose();
616
+ }
617
+ if (optimizerConfig.custom) {
618
+ var customOptimizer_1 = getCustomOptimizerByName(optimizerConfig.custom, optimizerConfig.improvementMode);
619
+ if (customOptimizer_1) {
620
+ optimizerOptions.addCustomOptimization(function () {
621
+ return customOptimizer_1(_this);
622
+ }, function () {
623
+ return "Babylon Viewer ".concat(optimizerConfig.custom, " custom optimization");
624
+ });
625
+ }
626
+ }
627
+ this.sceneOptimizer = new SceneOptimizer(this.scene, optimizerOptions, optimizerConfig.autoGeneratePriorities, optimizerConfig.improvementMode);
628
+ this.sceneOptimizer.start();
629
+ }
630
+ this.onSceneOptimizerConfiguredObservable.notifyObservers({
631
+ sceneManager: this,
632
+ object: this.sceneOptimizer,
633
+ newConfiguration: optimizerConfig,
634
+ });
635
+ };
636
+ /**
637
+ * configure all models using the configuration.
638
+ * @param modelConfiguration the configuration to use to reconfigure the models
639
+ */
640
+ /*protected _configureModel(modelConfiguration: Partial<IModelConfiguration>) {
641
+ this.models.forEach(model => {
642
+ model.updateConfiguration(modelConfiguration);
643
+ });
644
+
645
+ this.onModelsConfiguredObservable.notifyObservers({
646
+ sceneManager: this,
647
+ object: this.models,
648
+ newConfiguration: modelConfiguration
649
+ });
650
+ }*/
651
+ SceneManager.prototype._configureVR = function (vrConfig) {
652
+ var _this = this;
653
+ if (vrConfig.disabled) {
654
+ if (this._vrHelper) {
655
+ if (this._vrHelper.isInVRMode) {
656
+ this._vrHelper.exitVR();
657
+ }
658
+ this._vrHelper.dispose();
659
+ this._vrHelper = undefined;
660
+ }
661
+ return;
662
+ }
663
+ var vrOptions = deepmerge({
664
+ useCustomVRButton: true,
665
+ createDeviceOrientationCamera: false,
666
+ trackPosition: true,
667
+ }, vrConfig.vrOptions || {});
668
+ this._vrHelper = this.scene.createDefaultVRExperience(vrOptions);
669
+ if (!vrConfig.disableInteractions) {
670
+ this._vrHelper.enableInteractions();
671
+ }
672
+ if (!vrConfig.disableTeleportation) {
673
+ var floorMeshName = vrConfig.overrideFloorMeshName || "BackgroundPlane";
674
+ this._vrHelper.enableTeleportation({
675
+ floorMeshName: floorMeshName,
676
+ });
677
+ }
678
+ if (vrConfig.rotateUsingControllers) {
679
+ var rotationOffset_1;
680
+ this._vrHelper.onControllerMeshLoadedObservable.add(function (controller) {
681
+ controller.onTriggerStateChangedObservable.add(function (data) {
682
+ if (controller.mesh && controller.mesh.rotationQuaternion) {
683
+ if (data.pressed) {
684
+ if (!rotationOffset_1) {
685
+ _this.models[0].rootMesh.rotationQuaternion = _this.models[0].rootMesh.rotationQuaternion || new Quaternion();
686
+ rotationOffset_1 = controller.mesh.rotationQuaternion.conjugate().multiply(_this.models[0].rootMesh.rotationQuaternion);
687
+ }
688
+ }
689
+ else {
690
+ rotationOffset_1 = null;
691
+ }
692
+ }
693
+ });
694
+ _this.scene.registerBeforeRender(function () {
695
+ if (_this.models[0]) {
696
+ if (rotationOffset_1 && controller.mesh && controller.mesh.rotationQuaternion) {
697
+ _this.models[0].rootMesh.rotationQuaternion.copyFrom(controller.mesh.rotationQuaternion).multiplyInPlace(rotationOffset_1);
698
+ }
699
+ else {
700
+ _this.models[0].rootMesh.rotationQuaternion = null;
701
+ }
702
+ }
703
+ });
704
+ });
705
+ }
706
+ this._vrHelper.onEnteringVRObservable.add(function () {
707
+ if (_this._observablesManager) {
708
+ _this._observablesManager.onEnteringVRObservable.notifyObservers(_this);
709
+ }
710
+ });
711
+ this._vrHelper.onExitingVRObservable.add(function () {
712
+ if (_this._observablesManager) {
713
+ _this._observablesManager.onExitingVRObservable.notifyObservers(_this);
714
+ }
715
+ });
716
+ this.onVRConfiguredObservable.notifyObservers({
717
+ sceneManager: this,
718
+ object: this._vrHelper,
719
+ newConfiguration: vrConfig,
720
+ });
721
+ };
722
+ SceneManager.prototype._configureEnvironmentMap = function (environmentMapConfiguration) {
723
+ if (environmentMapConfiguration.texture) {
724
+ this.scene.environmentTexture = new CubeTexture(this._getAssetUrl(environmentMapConfiguration.texture), this.scene);
725
+ }
726
+ //sanity check
727
+ if (this.scene.environmentTexture) {
728
+ var rotatquatRotationionY = Quaternion.RotationAxis(Axis.Y, environmentMapConfiguration.rotationY || 0);
729
+ Matrix.FromQuaternionToRef(rotatquatRotationionY, this.scene.environmentTexture.getReflectionTextureMatrix());
730
+ }
731
+ // process mainColor changes:
732
+ if (environmentMapConfiguration.mainColor) {
733
+ this._configurationContainer.mainColor = this.mainColor || Color3.White();
734
+ var mc = environmentMapConfiguration.mainColor;
735
+ if (mc.r !== undefined) {
736
+ this.mainColor.r = mc.r;
737
+ }
738
+ if (mc.g !== undefined) {
739
+ this.mainColor.g = mc.g;
740
+ }
741
+ if (mc.b !== undefined) {
742
+ this.mainColor.b = mc.b;
743
+ }
744
+ this.reflectionColor.copyFrom(this.mainColor);
745
+ var environmentTint = getConfigurationKey("environmentMap.tintLevel", this._globalConfiguration) || 0;
746
+ // reflection color
747
+ this.reflectionColor.toLinearSpaceToRef(this.reflectionColor);
748
+ this.reflectionColor.scaleToRef(1 / this.scene.imageProcessingConfiguration.exposure, this.reflectionColor);
749
+ var tmpColor3 = Color3.Lerp(this._white, this.reflectionColor, environmentTint);
750
+ this.reflectionColor.copyFrom(tmpColor3);
751
+ //update the environment, if exists
752
+ if (this.environmentHelper) {
753
+ if (this.environmentHelper.groundMaterial) {
754
+ this.environmentHelper.groundMaterial._perceptualColor = this.mainColor;
755
+ }
756
+ if (this.environmentHelper.skyboxMaterial) {
757
+ this.environmentHelper.skyboxMaterial._perceptualColor = this.mainColor;
758
+ }
759
+ }
760
+ }
761
+ };
762
+ /**
763
+ * (Re) configure the camera. The camera will only be created once and from this point will only be reconfigured.
764
+ * @param cameraConfig the new camera configuration
765
+ */
766
+ SceneManager.prototype._configureCamera = function (cameraConfig) {
767
+ var _this = this;
768
+ if (cameraConfig === void 0) { cameraConfig = {}; }
769
+ if (!this.scene.activeCamera) {
770
+ // Inline scene.createDefaultCamera to reduce file size
771
+ // Dispose existing camera in replace mode.
772
+ if (this.scene.activeCamera) {
773
+ this.scene.activeCamera.dispose();
774
+ this.scene.activeCamera = null;
775
+ }
776
+ // Camera
777
+ if (!this.scene.activeCamera) {
778
+ var worldExtends = this.scene.getWorldExtends();
779
+ var worldSize = worldExtends.max.subtract(worldExtends.min);
780
+ var worldCenter = worldExtends.min.add(worldSize.scale(0.5));
781
+ var radius = worldSize.length() * 1.5;
782
+ // empty scene scenario!
783
+ if (!isFinite(radius)) {
784
+ radius = 1;
785
+ worldCenter.copyFromFloats(0, 0, 0);
786
+ }
787
+ var arcRotateCamera = new ArcRotateCamera("default camera", -(Math.PI / 2), Math.PI / 2, radius, worldCenter, this.scene);
788
+ arcRotateCamera.lowerRadiusLimit = radius * 0.01;
789
+ arcRotateCamera.wheelPrecision = 100 / radius;
790
+ var camera = arcRotateCamera;
791
+ camera.minZ = radius * 0.01;
792
+ camera.maxZ = radius * 1000;
793
+ camera.speed = radius * 0.2;
794
+ this.scene.activeCamera = camera;
795
+ }
796
+ var canvas = this.scene.getEngine().getInputElement();
797
+ if (canvas) {
798
+ this.scene.activeCamera.attachControl();
799
+ }
800
+ this.camera = this.scene.activeCamera;
801
+ this.camera.setTarget(Vector3.Zero());
802
+ }
803
+ if (!this.camera) {
804
+ this.camera = this.scene.activeCamera;
805
+ }
806
+ if (cameraConfig.position) {
807
+ var newPosition = this.camera.position.clone();
808
+ extendClassWithConfig(newPosition, cameraConfig.position);
809
+ this.camera.setPosition(newPosition);
810
+ }
811
+ if (cameraConfig.target) {
812
+ var newTarget = this.camera.target.clone();
813
+ extendClassWithConfig(newTarget, cameraConfig.target);
814
+ this.camera.setTarget(newTarget);
815
+ } /*else if (this.models.length && !cameraConfig.disableAutoFocus) {
816
+ this._focusOnModel(this.models[0]);
817
+ }*/
818
+ if (cameraConfig.rotation) {
819
+ this.camera.rotationQuaternion = new Quaternion(cameraConfig.rotation.x || 0, cameraConfig.rotation.y || 0, cameraConfig.rotation.z || 0, cameraConfig.rotation.w || 0);
820
+ }
821
+ if (cameraConfig.behaviors) {
822
+ for (var name_1 in cameraConfig.behaviors) {
823
+ if (cameraConfig.behaviors[name_1] !== undefined) {
824
+ this._setCameraBehavior(name_1, cameraConfig.behaviors[name_1]);
825
+ }
826
+ }
827
+ }
828
+ var sceneExtends = this.scene.getWorldExtends(function (mesh) {
829
+ return !_this.environmentHelper || (mesh !== _this.environmentHelper.ground && mesh !== _this.environmentHelper.rootMesh && mesh !== _this.environmentHelper.skybox);
830
+ });
831
+ var sceneDiagonal = sceneExtends.max.subtract(sceneExtends.min);
832
+ var sceneDiagonalLength = sceneDiagonal.length();
833
+ if (isFinite(sceneDiagonalLength)) {
834
+ this.camera.upperRadiusLimit = sceneDiagonalLength * 4;
835
+ }
836
+ // sanity check!
837
+ if (this.scene.imageProcessingConfiguration) {
838
+ this.scene.imageProcessingConfiguration.colorCurvesEnabled = true;
839
+ this.scene.imageProcessingConfiguration.vignetteEnabled = true;
840
+ this.scene.imageProcessingConfiguration.toneMappingEnabled = !!getConfigurationKey("camera.toneMappingEnabled", this._globalConfiguration);
841
+ }
842
+ extendClassWithConfig(this.camera, cameraConfig);
843
+ this.onCameraConfiguredObservable.notifyObservers({
844
+ sceneManager: this,
845
+ object: this.camera,
846
+ newConfiguration: cameraConfig,
847
+ });
848
+ };
849
+ SceneManager.prototype._configureEnvironment = function (skyboxConifguration, groundConfiguration) {
850
+ var _this = this;
851
+ if (!skyboxConifguration && !groundConfiguration) {
852
+ if (this.environmentHelper) {
853
+ this.environmentHelper.dispose();
854
+ this.environmentHelper = undefined;
855
+ }
856
+ }
857
+ else {
858
+ var options = {
859
+ createGround: !!groundConfiguration && this._groundEnabled,
860
+ createSkybox: !!skyboxConifguration,
861
+ setupImageProcessing: false, // will be done at the scene level!,
862
+ };
863
+ // will that cause problems with model ground configuration?
864
+ /*if (model) {
865
+ const boundingInfo = model.rootMesh.getHierarchyBoundingVectors(true);
866
+ const sizeVec = boundingInfo.max.subtract(boundingInfo.min);
867
+ const halfSizeVec = sizeVec.scale(0.5);
868
+ const center = boundingInfo.min.add(halfSizeVec);
869
+ options.groundYBias = -center.y;
870
+ }*/
871
+ if (groundConfiguration) {
872
+ var groundConfig_1 = typeof groundConfiguration === "boolean" ? {} : groundConfiguration;
873
+ var groundSize = groundConfig_1.size || (typeof skyboxConifguration === "object" && skyboxConifguration.scale);
874
+ if (groundSize) {
875
+ options.groundSize = groundSize;
876
+ }
877
+ options.enableGroundShadow = groundConfig_1 === true || groundConfig_1.receiveShadows;
878
+ if (groundConfig_1.shadowLevel !== undefined) {
879
+ options.groundShadowLevel = groundConfig_1.shadowLevel;
880
+ }
881
+ options.enableGroundMirror = !!groundConfig_1.mirror && this.groundMirrorEnabled;
882
+ if (groundConfig_1.texture) {
883
+ options.groundTexture = this._getAssetUrl(groundConfig_1.texture);
884
+ }
885
+ if (groundConfig_1.color) {
886
+ options.groundColor = new Color3(groundConfig_1.color.r, groundConfig_1.color.g, groundConfig_1.color.b);
887
+ }
888
+ if (groundConfig_1.opacity !== undefined) {
889
+ options.groundOpacity = groundConfig_1.opacity;
890
+ }
891
+ if (groundConfig_1.mirror) {
892
+ options.enableGroundMirror = true;
893
+ // to prevent undefines
894
+ if (typeof groundConfig_1.mirror === "object") {
895
+ if (groundConfig_1.mirror.amount !== undefined) {
896
+ options.groundMirrorAmount = groundConfig_1.mirror.amount;
897
+ }
898
+ if (groundConfig_1.mirror.sizeRatio !== undefined) {
899
+ options.groundMirrorSizeRatio = groundConfig_1.mirror.sizeRatio;
900
+ }
901
+ if (groundConfig_1.mirror.blurKernel !== undefined) {
902
+ options.groundMirrorBlurKernel = groundConfig_1.mirror.blurKernel;
903
+ }
904
+ if (groundConfig_1.mirror.fresnelWeight !== undefined) {
905
+ options.groundMirrorFresnelWeight = groundConfig_1.mirror.fresnelWeight;
906
+ }
907
+ if (groundConfig_1.mirror.fallOffDistance !== undefined) {
908
+ options.groundMirrorFallOffDistance = groundConfig_1.mirror.fallOffDistance;
909
+ }
910
+ if (this._defaultPipelineTextureType !== undefined) {
911
+ options.groundMirrorTextureType = this._defaultPipelineTextureType;
912
+ }
913
+ }
914
+ }
915
+ }
916
+ var postInitSkyboxMaterial = false;
917
+ if (skyboxConifguration) {
918
+ var conf = skyboxConifguration === true ? {} : skyboxConifguration;
919
+ if (conf.material && conf.material.imageProcessingConfiguration) {
920
+ options.setupImageProcessing = false; // will be configured later manually.
921
+ }
922
+ var skyboxSize = conf.scale;
923
+ if (skyboxSize) {
924
+ options.skyboxSize = skyboxSize;
925
+ }
926
+ options.sizeAuto = !options.skyboxSize;
927
+ if (conf.color) {
928
+ options.skyboxColor = new Color3(conf.color.r, conf.color.g, conf.color.b);
929
+ }
930
+ if (conf.cubeTexture && conf.cubeTexture.url) {
931
+ if (typeof conf.cubeTexture.url === "string") {
932
+ options.skyboxTexture = this._getAssetUrl(conf.cubeTexture.url);
933
+ }
934
+ else {
935
+ // init later!
936
+ postInitSkyboxMaterial = true;
937
+ }
938
+ }
939
+ if (conf.material) {
940
+ postInitSkyboxMaterial = true;
941
+ }
942
+ }
943
+ options.setupImageProcessing = false; // TMP
944
+ if (!this.environmentHelper) {
945
+ this.environmentHelper = new EnvironmentHelper(options, this.scene);
946
+ }
947
+ else {
948
+ // unlikely, but there might be a new scene! we need to dispose.
949
+ // get the scene used by the envHelper
950
+ var scene = this.environmentHelper.rootMesh.getScene();
951
+ // is it a different scene? Oh no!
952
+ if (scene !== this.scene) {
953
+ this.environmentHelper.dispose();
954
+ this.environmentHelper = new EnvironmentHelper(options, this.scene);
955
+ }
956
+ else {
957
+ // recreate the ground
958
+ if (this.environmentHelper.ground) {
959
+ this.environmentHelper.ground.dispose();
960
+ }
961
+ // recreate the skybox
962
+ if (this.environmentHelper.skybox) {
963
+ this.environmentHelper.skybox.dispose();
964
+ }
965
+ this.environmentHelper.updateOptions(options);
966
+ // update doesn't change the size of the skybox and ground, so we have to recreate!
967
+ //this.environmentHelper.dispose();
968
+ //this.environmentHelper = new EnvironmentHelper(options, this.scene);
969
+ }
970
+ }
971
+ if (this.environmentHelper.rootMesh && this._globalConfiguration.scene && this._globalConfiguration.scene.environmentRotationY !== undefined) {
972
+ this.environmentHelper.rootMesh.rotation.y = this._globalConfiguration.scene.environmentRotationY;
973
+ }
974
+ var groundConfig = typeof groundConfiguration === "boolean" ? {} : groundConfiguration;
975
+ if (this.environmentHelper.groundMaterial && groundConfig) {
976
+ this.environmentHelper.groundMaterial._perceptualColor = this.mainColor;
977
+ if (groundConfig.material) {
978
+ extendClassWithConfig(this.environmentHelper.groundMaterial, groundConfig.material);
979
+ }
980
+ if (this.environmentHelper.groundMirror) {
981
+ var mirrorClearColor = this.environmentHelper.groundMaterial._perceptualColor.toLinearSpace();
982
+ // TODO user camera exposure value to set the mirror clear color
983
+ var exposure = Math.pow(2.0, -this.scene.imageProcessingConfiguration.exposure) * Math.PI;
984
+ mirrorClearColor.scaleToRef(1 / exposure, mirrorClearColor);
985
+ this.environmentHelper.groundMirror.clearColor.r = Scalar.Clamp(mirrorClearColor.r);
986
+ this.environmentHelper.groundMirror.clearColor.g = Scalar.Clamp(mirrorClearColor.g);
987
+ this.environmentHelper.groundMirror.clearColor.b = Scalar.Clamp(mirrorClearColor.b);
988
+ this.environmentHelper.groundMirror.clearColor.a = 1;
989
+ if (!this.groundMirrorEnabled) {
990
+ this.environmentHelper.groundMaterial.reflectionTexture = null;
991
+ }
992
+ }
993
+ }
994
+ var skyboxMaterial = this.environmentHelper.skyboxMaterial;
995
+ if (skyboxMaterial) {
996
+ skyboxMaterial._perceptualColor = this.mainColor;
997
+ if (postInitSkyboxMaterial) {
998
+ if (typeof skyboxConifguration === "object" && skyboxConifguration.material) {
999
+ extendClassWithConfig(skyboxMaterial, skyboxConifguration.material);
1000
+ }
1001
+ }
1002
+ }
1003
+ }
1004
+ this._observablesManager &&
1005
+ this._observablesManager.onModelLoadedObservable.add(function (model) {
1006
+ _this._updateGroundMirrorRenderList(model);
1007
+ });
1008
+ this.onEnvironmentConfiguredObservable.notifyObservers({
1009
+ sceneManager: this,
1010
+ object: this.environmentHelper,
1011
+ newConfiguration: {
1012
+ skybox: skyboxConifguration,
1013
+ ground: groundConfiguration,
1014
+ },
1015
+ });
1016
+ };
1017
+ /**
1018
+ * configure the lights.
1019
+ * @param lightsConfiguration the (new) light(s) configuration
1020
+ */
1021
+ SceneManager.prototype._configureLights = function (lightsConfiguration) {
1022
+ var _this = this;
1023
+ if (lightsConfiguration === void 0) { lightsConfiguration = {}; }
1024
+ // sanity check!
1025
+ var lightKeys = Object.keys(lightsConfiguration).filter(function (name) { return name !== "globalRotation"; });
1026
+ if (!lightKeys.length) {
1027
+ if (!this.scene.lights.length) {
1028
+ new HemisphericLight("default light", Vector3.Up(), this.scene);
1029
+ }
1030
+ }
1031
+ else {
1032
+ var lightsAvailable_1 = this.scene.lights.map(function (light) { return light.name; });
1033
+ // compare to the global (!) configuration object and dispose unneeded:
1034
+ var lightsToConfigure_1 = Object.keys(this._globalConfiguration.lights || []);
1035
+ if (Object.keys(lightsToConfigure_1).length !== lightsAvailable_1.length) {
1036
+ lightsAvailable_1.forEach(function (lName) {
1037
+ if (lightsToConfigure_1.indexOf(lName) === -1) {
1038
+ _this.scene.getLightByName(lName).dispose();
1039
+ }
1040
+ });
1041
+ }
1042
+ lightKeys.forEach(function (name) {
1043
+ var lightConfig = { type: 0 };
1044
+ if (typeof lightsConfiguration[name] === "object") {
1045
+ lightConfig = lightsConfiguration[name];
1046
+ }
1047
+ if (typeof lightsConfiguration[name] === "number") {
1048
+ lightConfig.type = lightsConfiguration[name];
1049
+ }
1050
+ lightConfig.name = name;
1051
+ var light;
1052
+ // light is not already available
1053
+ if (lightsAvailable_1.indexOf(name) === -1) {
1054
+ var constructor = Light.GetConstructorFromName(lightConfig.type, lightConfig.name, _this.scene);
1055
+ if (!constructor) {
1056
+ return;
1057
+ }
1058
+ light = constructor();
1059
+ }
1060
+ else {
1061
+ // available? get it from the scene
1062
+ light = _this.scene.getLightByName(name);
1063
+ if (typeof lightsConfiguration[name] === "boolean") {
1064
+ lightConfig.type = light.getTypeID();
1065
+ }
1066
+ lightsAvailable_1 = lightsAvailable_1.filter(function (ln) { return ln !== name; });
1067
+ if (lightConfig.type !== undefined && light.getTypeID() !== lightConfig.type) {
1068
+ light.dispose();
1069
+ var constructor = Light.GetConstructorFromName(lightConfig.type, lightConfig.name, _this.scene);
1070
+ if (!constructor) {
1071
+ return;
1072
+ }
1073
+ light = constructor();
1074
+ }
1075
+ }
1076
+ // if config set the light to false, dispose it.
1077
+ if (lightsConfiguration[name] === false) {
1078
+ light.dispose();
1079
+ return;
1080
+ }
1081
+ //enabled
1082
+ var enabled = lightConfig.enabled !== undefined ? lightConfig.enabled : !lightConfig.disabled;
1083
+ light.setEnabled(enabled);
1084
+ extendClassWithConfig(light, lightConfig);
1085
+ //position. Some lights don't support shadows
1086
+ if (light instanceof ShadowLight) {
1087
+ // set default values
1088
+ light.shadowMinZ = light.shadowMinZ || 0.2;
1089
+ light.shadowMaxZ = Math.min(10, light.shadowMaxZ || 10); //large far clips reduce shadow depth precision
1090
+ if (lightConfig.target) {
1091
+ if (light.setDirectionToTarget) {
1092
+ var target = Vector3.Zero().copyFromFloats(lightConfig.target.x, lightConfig.target.y, lightConfig.target.z);
1093
+ light.setDirectionToTarget(target);
1094
+ }
1095
+ }
1096
+ else if (lightConfig.direction) {
1097
+ var direction = Vector3.Zero().copyFromFloats(lightConfig.direction.x, lightConfig.direction.y, lightConfig.direction.z);
1098
+ light.direction = direction;
1099
+ }
1100
+ var isShadowEnabled = false;
1101
+ if (light.getTypeID() === Light.LIGHTTYPEID_DIRECTIONALLIGHT) {
1102
+ light.shadowFrustumSize = lightConfig.shadowFrustumSize || 2;
1103
+ isShadowEnabled = true;
1104
+ }
1105
+ else if (light.getTypeID() === Light.LIGHTTYPEID_SPOTLIGHT) {
1106
+ var spotLight = light;
1107
+ if (lightConfig.spotAngle !== undefined) {
1108
+ spotLight.angle = (lightConfig.spotAngle * Math.PI) / 180;
1109
+ }
1110
+ if (spotLight.angle && lightConfig.shadowFieldOfView) {
1111
+ spotLight.shadowAngleScale = lightConfig.shadowFieldOfView / spotLight.angle;
1112
+ }
1113
+ isShadowEnabled = true;
1114
+ }
1115
+ else if (light.getTypeID() === Light.LIGHTTYPEID_POINTLIGHT) {
1116
+ if (lightConfig.shadowFieldOfView) {
1117
+ light.shadowAngle = (lightConfig.shadowFieldOfView * Math.PI) / 180;
1118
+ }
1119
+ isShadowEnabled = true;
1120
+ }
1121
+ var shadowGenerator_1 = light.getShadowGenerator();
1122
+ if (isShadowEnabled && lightConfig.shadowEnabled && _this._maxShadows) {
1123
+ var bufferSize = lightConfig.shadowBufferSize || 256;
1124
+ if (!shadowGenerator_1) {
1125
+ shadowGenerator_1 = new ShadowGenerator(bufferSize, light);
1126
+ }
1127
+ var blurKernel = _this.getBlurKernel(light, bufferSize);
1128
+ shadowGenerator_1.bias = _this._shadowGeneratorBias;
1129
+ shadowGenerator_1.blurKernel = blurKernel;
1130
+ //override defaults
1131
+ extendClassWithConfig(shadowGenerator_1, lightConfig.shadowConfig || {});
1132
+ // add the focus meshes to the shadow list
1133
+ _this._observablesManager &&
1134
+ _this._observablesManager.onModelLoadedObservable.add(function (model) {
1135
+ _this._updateShadowRenderList(shadowGenerator_1, model);
1136
+ });
1137
+ //if (model) {
1138
+ _this._updateShadowRenderList(shadowGenerator_1);
1139
+ //}
1140
+ }
1141
+ else if (shadowGenerator_1) {
1142
+ shadowGenerator_1.dispose();
1143
+ }
1144
+ }
1145
+ });
1146
+ // render priority
1147
+ var globalLightsConfiguration = this._globalConfiguration.lights || {};
1148
+ Object.keys(globalLightsConfiguration)
1149
+ .sort()
1150
+ .forEach(function (name, idx) {
1151
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1152
+ // const configuration = globalLightsConfiguration[name];
1153
+ var light = _this.scene.getLightByName(name);
1154
+ // sanity check
1155
+ if (!light) {
1156
+ return;
1157
+ }
1158
+ light.renderPriority = -idx;
1159
+ });
1160
+ }
1161
+ this.onLightsConfiguredObservable.notifyObservers({
1162
+ sceneManager: this,
1163
+ object: this.scene.lights,
1164
+ newConfiguration: lightsConfiguration,
1165
+ });
1166
+ };
1167
+ SceneManager.prototype._updateShadowRenderList = function (shadowGenerator, model, resetList) {
1168
+ var focusMeshes = model ? model.meshes : this.scene.meshes;
1169
+ // add the focus meshes to the shadow list
1170
+ var shadownMap = shadowGenerator.getShadowMap();
1171
+ if (!shadownMap) {
1172
+ return;
1173
+ }
1174
+ if (resetList && shadownMap.renderList) {
1175
+ shadownMap.renderList.length = 0;
1176
+ }
1177
+ else {
1178
+ shadownMap.renderList = shadownMap.renderList || [];
1179
+ }
1180
+ for (var index = 0; index < focusMeshes.length; index++) {
1181
+ var mesh = focusMeshes[index];
1182
+ if (Tags.MatchesQuery(mesh, "castShadow") && shadownMap.renderList.indexOf(mesh) === -1) {
1183
+ shadownMap.renderList.push(mesh);
1184
+ }
1185
+ }
1186
+ if (!this._shadowGroundPlane) {
1187
+ if (shadowGenerator.useBlurCloseExponentialShadowMap) {
1188
+ var shadowGroundPlane = CreatePlane("shadowGroundPlane", { size: 100 }, this.scene);
1189
+ shadowGroundPlane.useVertexColors = false;
1190
+ //material isn't ever used in rendering, just used to set back face culling
1191
+ shadowGroundPlane.material = new PBRMaterial("shadowGroundPlaneMaterial", this.scene);
1192
+ shadowGroundPlane.material.backFaceCulling = false;
1193
+ shadowGroundPlane.rotation.x = Math.PI * 0.5;
1194
+ shadowGroundPlane.freezeWorldMatrix();
1195
+ this._shadowGroundPlane = shadowGroundPlane;
1196
+ this.scene.removeMesh(shadowGroundPlane);
1197
+ }
1198
+ }
1199
+ else {
1200
+ if (!shadowGenerator.useBlurCloseExponentialShadowMap) {
1201
+ this._shadowGroundPlane.dispose();
1202
+ this._shadowGroundPlane = null;
1203
+ }
1204
+ }
1205
+ if (this._shadowGroundPlane && shadownMap.renderList.indexOf(this._shadowGroundPlane) === -1) {
1206
+ shadownMap.renderList.push(this._shadowGroundPlane);
1207
+ }
1208
+ };
1209
+ SceneManager.prototype._updateGroundMirrorRenderList = function (model, resetList) {
1210
+ if (this.environmentHelper && this.environmentHelper.groundMirror && this.environmentHelper.groundMirror.renderList) {
1211
+ var focusMeshes = model ? model.meshes : this.scene.meshes;
1212
+ var renderList = this.environmentHelper.groundMirror.renderList;
1213
+ if (resetList) {
1214
+ renderList.length = 0;
1215
+ }
1216
+ for (var index = 0; index < focusMeshes.length; index++) {
1217
+ var mesh = focusMeshes[index];
1218
+ if (renderList.indexOf(mesh) === -1) {
1219
+ renderList.push(mesh);
1220
+ }
1221
+ }
1222
+ }
1223
+ };
1224
+ /**
1225
+ * Gets the shadow map blur kernel according to the light configuration.
1226
+ * @param light The light used to generate the shadows
1227
+ * @param bufferSize The size of the shadow map
1228
+ * @return the kernel blur size
1229
+ */
1230
+ SceneManager.prototype.getBlurKernel = function (light, bufferSize) {
1231
+ var normalizedBlurKernel = 0.05; // TODO Should come from the config.
1232
+ if (light.getTypeID() === Light.LIGHTTYPEID_DIRECTIONALLIGHT) {
1233
+ normalizedBlurKernel = normalizedBlurKernel / light.shadowFrustumSize;
1234
+ }
1235
+ else if (light.getTypeID() === Light.LIGHTTYPEID_POINTLIGHT) {
1236
+ normalizedBlurKernel = normalizedBlurKernel / light.shadowAngle;
1237
+ }
1238
+ else if (light.getTypeID() === Light.LIGHTTYPEID_SPOTLIGHT) {
1239
+ normalizedBlurKernel = normalizedBlurKernel / (light.angle * light.shadowAngleScale);
1240
+ }
1241
+ var minimumBlurKernel = 5 / (bufferSize / 256); //magic number that aims to keep away sawtooth shadows
1242
+ var blurKernel = Math.max(bufferSize * normalizedBlurKernel, minimumBlurKernel);
1243
+ return blurKernel;
1244
+ };
1245
+ /**
1246
+ * Alters render settings to reduce features based on hardware feature limitations
1247
+ * @param enableHDR Allows the viewer to run in HDR mode.
1248
+ */
1249
+ SceneManager.prototype._handleHardwareLimitations = function (enableHDR) {
1250
+ if (enableHDR === void 0) { enableHDR = true; }
1251
+ //flip rendering settings switches based on hardware support
1252
+ var maxVaryingRows = this._engine.getCaps().maxVaryingVectors;
1253
+ var maxFragmentSamplers = this._engine.getCaps().maxTexturesImageUnits;
1254
+ //shadows are disabled if there's not enough varyings for a single shadow
1255
+ if (maxVaryingRows < 8 || maxFragmentSamplers < 8) {
1256
+ this._maxShadows = 0;
1257
+ }
1258
+ else {
1259
+ this._maxShadows = 3;
1260
+ }
1261
+ //can we render to any >= 16-bit targets (required for HDR)
1262
+ var caps = this._engine.getCaps();
1263
+ var linearHalfFloatTargets = caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering;
1264
+ var linearFloatTargets = caps.textureFloatRender && caps.textureFloatLinearFiltering;
1265
+ this._hdrSupport = enableHDR && !!(linearFloatTargets || linearHalfFloatTargets);
1266
+ if (linearHalfFloatTargets) {
1267
+ this._defaultHighpTextureType = Constants.TEXTURETYPE_HALF_FLOAT;
1268
+ this._shadowGeneratorBias = 0.002;
1269
+ }
1270
+ else if (linearFloatTargets) {
1271
+ this._defaultHighpTextureType = Constants.TEXTURETYPE_FLOAT;
1272
+ this._shadowGeneratorBias = 0.001;
1273
+ }
1274
+ else {
1275
+ this._defaultHighpTextureType = Constants.TEXTURETYPE_UNSIGNED_INT;
1276
+ this._shadowGeneratorBias = 0.001;
1277
+ }
1278
+ this._defaultPipelineTextureType = this._hdrSupport ? this._defaultHighpTextureType : Constants.TEXTURETYPE_UNSIGNED_INT;
1279
+ };
1280
+ /**
1281
+ * Dispose the entire viewer including the scene and the engine
1282
+ */
1283
+ SceneManager.prototype.dispose = function () {
1284
+ // this.onCameraConfiguredObservable.clear();
1285
+ this.onEnvironmentConfiguredObservable.clear();
1286
+ this.onLightsConfiguredObservable.clear();
1287
+ this.onModelsConfiguredObservable.clear();
1288
+ this.onSceneConfiguredObservable.clear();
1289
+ this.onSceneInitObservable.clear();
1290
+ this.onSceneOptimizerConfiguredObservable.clear();
1291
+ this.onVRConfiguredObservable.clear();
1292
+ if (this.sceneOptimizer) {
1293
+ this.sceneOptimizer.stop();
1294
+ this.sceneOptimizer.dispose();
1295
+ }
1296
+ if (this.environmentHelper) {
1297
+ this.environmentHelper.dispose();
1298
+ }
1299
+ this.models.forEach(function (model) {
1300
+ model.dispose();
1301
+ });
1302
+ if (this._defaultRenderingPipeline) {
1303
+ this._defaultRenderingPipeline.dispose();
1304
+ }
1305
+ this.models.length = 0;
1306
+ if (this.scene) {
1307
+ this.scene.dispose();
1308
+ }
1309
+ };
1310
+ /**
1311
+ * Get an environment asset url by using the configuration if the path is not absolute.
1312
+ * @param url Asset url
1313
+ * @returns The Asset url using the `environmentAssetsRootURL` if the url is not an absolute path.
1314
+ */
1315
+ SceneManager.prototype._getAssetUrl = function (url) {
1316
+ var returnUrl = url;
1317
+ if (url && url.toLowerCase().indexOf("//") === -1) {
1318
+ if (!this._assetsRootURL) {
1319
+ // Tools.Warn("Please, specify the root url of your assets before loading the configuration (labs.environmentAssetsRootURL) or disable the background through the viewer options.");
1320
+ return url;
1321
+ }
1322
+ returnUrl = this._assetsRootURL + returnUrl;
1323
+ }
1324
+ return returnUrl;
1325
+ };
1326
+ SceneManager.prototype._setCameraBehavior = function (name, behaviorConfig) {
1327
+ var behavior;
1328
+ var type;
1329
+ if (typeof behaviorConfig === "object") {
1330
+ type = behaviorConfig.type;
1331
+ }
1332
+ else if (typeof behaviorConfig === "number") {
1333
+ type = behaviorConfig;
1334
+ }
1335
+ else {
1336
+ type = this._cameraBehaviorMapping[name];
1337
+ }
1338
+ if (type === undefined) {
1339
+ return;
1340
+ }
1341
+ var config = typeof behaviorConfig === "object" ? behaviorConfig : {};
1342
+ var enabled = true;
1343
+ if (typeof behaviorConfig === "boolean") {
1344
+ enabled = behaviorConfig;
1345
+ }
1346
+ // constructing behavior
1347
+ switch (type) {
1348
+ case 0 /* AUTOROTATION */:
1349
+ this.camera.useAutoRotationBehavior = enabled;
1350
+ behavior = this.camera.autoRotationBehavior;
1351
+ break;
1352
+ case 1 /* BOUNCING */:
1353
+ this.camera.useBouncingBehavior = enabled;
1354
+ behavior = this.camera.bouncingBehavior;
1355
+ break;
1356
+ case 2 /* FRAMING */:
1357
+ this.camera.useFramingBehavior = enabled;
1358
+ behavior = this.camera.framingBehavior;
1359
+ break;
1360
+ default:
1361
+ behavior = null;
1362
+ break;
1363
+ }
1364
+ if (behavior) {
1365
+ this._cameraBehaviorMapping[name] = type;
1366
+ if (typeof behaviorConfig === "object") {
1367
+ extendClassWithConfig(behavior, behaviorConfig);
1368
+ }
1369
+ }
1370
+ // post attach configuration. Some functionalities require the attached camera.
1371
+ switch (type) {
1372
+ case 0 /* AUTOROTATION */:
1373
+ break;
1374
+ case 1 /* BOUNCING */:
1375
+ break;
1376
+ case 2 /* FRAMING */:
1377
+ this._observablesManager &&
1378
+ this._observablesManager.onModelLoadedObservable.add(function (model) {
1379
+ if (config.zoomOnBoundingInfo) {
1380
+ behavior.zoomOnMeshHierarchy(model.rootMesh);
1381
+ }
1382
+ });
1383
+ break;
1384
+ }
1385
+ };
1386
+ return SceneManager;
1387
+ }());
1388
+ export { SceneManager };
1389
1389
  //# sourceMappingURL=sceneManager.js.map