updateConfiguration.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import { Helper } from "../../../commons/helper";
  2. import { assert, expect, should } from "../viewerReference";
  3. export let name = "configuration update";
  4. describe(name + " scene", () => {
  5. it("should be used as a template for the following tests", (done) => {
  6. let viewer = Helper.getNewViewerInstance(undefined, { extends: "none" });
  7. viewer.onInitDoneObservable.add(() => {
  8. viewer.dispose();
  9. done();
  10. });
  11. });
  12. it("should enable and disable the debug layer", (done) => {
  13. let viewer = Helper.getNewViewerInstance(undefined, { extends: "none" });
  14. let showCalled = 0;
  15. let hideCalled = 0;
  16. let isVisible = false;
  17. viewer.onSceneInitObservable.add((scene) => {
  18. scene.debugLayer.show = () => {
  19. showCalled++;
  20. isVisible = true;
  21. }
  22. scene.debugLayer.hide = () => {
  23. hideCalled++;
  24. isVisible = false;
  25. }
  26. scene.debugLayer.isVisible = () => {
  27. return isVisible;
  28. }
  29. })
  30. viewer.onInitDoneObservable.add(() => {
  31. // assert.isUndefined(viewer.configuration.scene);
  32. assert.equal(showCalled, 0);
  33. assert.equal(hideCalled, 0);
  34. viewer.updateConfiguration({
  35. scene: {
  36. debug: true
  37. }
  38. });
  39. assert.equal(showCalled, 1);
  40. viewer.updateConfiguration({
  41. scene: {
  42. debug: false
  43. }
  44. });
  45. assert.equal(showCalled, 1);
  46. assert.equal(hideCalled, 1);
  47. viewer.dispose();
  48. done();
  49. });
  50. });
  51. it("should update the image processing configuration values", (done) => {
  52. let viewer = Helper.getNewViewerInstance(undefined, { extends: "none" });
  53. viewer.onInitDoneObservable.add(() => {
  54. // check babylon defaults
  55. assert.isFalse(viewer.sceneManager.scene.imageProcessingConfiguration.applyByPostProcess);
  56. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.exposure, 1);
  57. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.contrast, 1);
  58. assert.isFalse(viewer.sceneManager.scene.imageProcessingConfiguration.colorGradingEnabled);
  59. if (viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves) {
  60. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.globalDensity, 0);
  61. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.globalHue, 30);
  62. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.globalSaturation, 0);
  63. } else {
  64. assert.fail(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves, {}, "color curves was not initialized");
  65. }
  66. // update with fixed values
  67. viewer.updateConfiguration({
  68. scene: {
  69. imageProcessingConfiguration: {
  70. applyByPostProcess: true,
  71. exposure: 0,
  72. contrast: 0,
  73. colorGradingEnabled: true,
  74. colorCurves: {
  75. globalDensity: 1,
  76. globalHue: 0.2,
  77. globalSaturation: 0.5
  78. }
  79. }
  80. }
  81. });
  82. assert.isTrue(viewer.sceneManager.scene.imageProcessingConfiguration.applyByPostProcess, "apply by post process should be true");
  83. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.exposure, 0);
  84. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.contrast, 0);
  85. assert.isTrue(viewer.sceneManager.scene.imageProcessingConfiguration.colorGradingEnabled);
  86. if (viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves) {
  87. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.globalDensity, 1);
  88. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.globalHue, 0.2);
  89. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.globalSaturation, 0.5);
  90. } else {
  91. assert.fail(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves, {}, "color curves was not initialized");
  92. }
  93. let randoms = [0, 1, 2, 3, 4].map(n => Math.random());
  94. viewer.updateConfiguration({
  95. scene: {
  96. imageProcessingConfiguration: {
  97. exposure: randoms[0],
  98. contrast: randoms[1],
  99. colorCurves: {
  100. highlightsDensity: randoms[2],
  101. highlightsHue: randoms[3],
  102. highlightsSaturation: randoms[4]
  103. }
  104. }
  105. }
  106. });
  107. assert.isTrue(viewer.sceneManager.scene.imageProcessingConfiguration.applyByPostProcess, "apply by post process should be true");
  108. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.exposure, randoms[0]);
  109. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.contrast, randoms[1]);
  110. assert.isTrue(viewer.sceneManager.scene.imageProcessingConfiguration.colorGradingEnabled);
  111. if (viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves) {
  112. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.highlightsDensity, randoms[2]);
  113. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.highlightsHue, randoms[3]);
  114. assert.equal(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves.highlightsSaturation, randoms[4]);
  115. } else {
  116. assert.fail(viewer.sceneManager.scene.imageProcessingConfiguration.colorCurves, {}, "color curves was not initialized");
  117. }
  118. viewer.dispose();
  119. done();
  120. });
  121. });
  122. it("should update main color and reflection color", (done) => {
  123. let viewer = Helper.getNewViewerInstance(undefined, { extends: "none" });
  124. viewer.onInitDoneObservable.add(() => {
  125. assert.equal(viewer.sceneManager.mainColor.r, 1);
  126. assert.equal(viewer.sceneManager.mainColor.g, 1);
  127. assert.equal(viewer.sceneManager.mainColor.b, 1);
  128. assert.equal(viewer.sceneManager.reflectionColor.r, 1);
  129. assert.equal(viewer.sceneManager.reflectionColor.g, 1);
  130. assert.equal(viewer.sceneManager.reflectionColor.b, 1);
  131. viewer.updateConfiguration({
  132. environmentMap: {
  133. tintLevel: 1,
  134. texture: "",
  135. rotationY: 0,
  136. mainColor: {
  137. r: 0.5,
  138. g: 0.5,
  139. b: 0.5
  140. }
  141. }
  142. });
  143. assert.equal(viewer.sceneManager.mainColor.r, 0.5);
  144. assert.equal(viewer.sceneManager.mainColor.g, 0.5);
  145. assert.equal(viewer.sceneManager.mainColor.b, 0.5);
  146. // to black
  147. viewer.updateConfiguration({
  148. scene: {
  149. mainColor: {
  150. r: 0,
  151. g: 0,
  152. b: 0
  153. }
  154. }
  155. });
  156. assert.equal(viewer.sceneManager.mainColor.r, 0);
  157. assert.equal(viewer.sceneManager.mainColor.g, 0);
  158. assert.equal(viewer.sceneManager.mainColor.b, 0);
  159. assert.equal(viewer.sceneManager.reflectionColor.r, 0);
  160. assert.equal(viewer.sceneManager.reflectionColor.g, 0);
  161. assert.equal(viewer.sceneManager.reflectionColor.b, 0);
  162. let randomColor = Math.random();
  163. //only update red
  164. viewer.updateConfiguration({
  165. scene: {
  166. mainColor: {
  167. r: randomColor
  168. }
  169. }
  170. });
  171. assert.equal(viewer.sceneManager.mainColor.r, randomColor);
  172. assert.equal(viewer.sceneManager.mainColor.g, 0);
  173. assert.equal(viewer.sceneManager.mainColor.b, 0);
  174. viewer.dispose();
  175. done();
  176. });
  177. });
  178. it("should update the flags correctly", (done) => {
  179. let viewer = Helper.getNewViewerInstance(undefined, { extends: "none" });
  180. viewer.onInitDoneObservable.add(() => {
  181. viewer.updateConfiguration({
  182. scene: {
  183. flags: {
  184. audioEnabled: false
  185. }
  186. }
  187. });
  188. assert.isFalse(viewer.sceneManager.scene.audioEnabled);
  189. viewer.updateConfiguration({
  190. scene: {
  191. flags: {
  192. audioEnabled: true,
  193. lightsEnabled: false
  194. }
  195. }
  196. });
  197. assert.isTrue(viewer.sceneManager.scene.audioEnabled);
  198. assert.isFalse(viewer.sceneManager.scene.lightsEnabled);
  199. viewer.updateConfiguration({
  200. scene: {
  201. flags: {
  202. lightsEnabled: true,
  203. shadowsEnabled: false
  204. }
  205. }
  206. });
  207. assert.isTrue(viewer.sceneManager.scene.audioEnabled);
  208. assert.isTrue(viewer.sceneManager.scene.lightsEnabled);
  209. assert.isFalse(viewer.sceneManager.scene.shadowsEnabled);
  210. viewer.dispose();
  211. done();
  212. });
  213. });
  214. });
  215. describe(name + " scene optimizer", () => {
  216. it("should be enabled and disabled with booleans", (done) => {
  217. let viewer = Helper.getNewViewerInstance(undefined, { extends: "none" });
  218. let started = false;
  219. let constructed = false;
  220. let optimizerFunction = BABYLON.SceneOptimizer;
  221. //mock!
  222. (<any>BABYLON.SceneOptimizer) = function () {
  223. constructed = true;
  224. }
  225. BABYLON.SceneOptimizer.prototype.start = function () {
  226. started = true;
  227. }
  228. BABYLON.SceneOptimizer.prototype.stop = function () {
  229. started = false;
  230. }
  231. BABYLON.SceneOptimizer.prototype.dispose = function () {
  232. }
  233. viewer.onInitDoneObservable.add(() => {
  234. assert.isUndefined(viewer.sceneManager.sceneOptimizer);
  235. viewer.updateConfiguration({
  236. optimizer: true
  237. });
  238. assert.isDefined(viewer.sceneManager.sceneOptimizer);
  239. assert.isTrue(started);
  240. assert.isTrue(constructed);
  241. viewer.updateConfiguration({
  242. optimizer: false
  243. });
  244. assert.isUndefined(viewer.sceneManager.sceneOptimizer);
  245. assert.isFalse(started);
  246. BABYLON.SceneOptimizer = optimizerFunction;
  247. viewer.dispose();
  248. done();
  249. });
  250. });
  251. });
  252. describe(name + " camera", () => {
  253. it("should enable and disable camera behaviors", (done) => {
  254. let viewer = Helper.getNewViewerInstance(undefined, { extends: "none" });
  255. viewer.onInitDoneObservable.add(() => {
  256. assert.isFalse(viewer.sceneManager.camera.useAutoRotationBehavior);
  257. viewer.updateConfiguration({
  258. camera: {
  259. behaviors: {
  260. autoRotate: {
  261. type: 0
  262. }
  263. }
  264. }
  265. });
  266. assert.isTrue(viewer.sceneManager.camera.useAutoRotationBehavior);
  267. viewer.updateConfiguration({
  268. camera: {
  269. behaviors: {
  270. autoRotate: false
  271. }
  272. }
  273. });
  274. assert.isFalse(viewer.sceneManager.camera.useAutoRotationBehavior);
  275. viewer.dispose();
  276. done();
  277. });
  278. })
  279. })