@babylonjs/viewer 5.0.0-rc.4 → 5.0.2

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