previewManager.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { GlobalState } from '../../globalState';
  2. import { NodeMaterial } from 'babylonjs/Materials/Node/nodeMaterial';
  3. import { Nullable } from 'babylonjs/types';
  4. import { Observer } from 'babylonjs/Misc/observable';
  5. import { Engine } from 'babylonjs/Engines/engine';
  6. import { Scene } from 'babylonjs/scene';
  7. import { Mesh } from 'babylonjs/Meshes/mesh';
  8. import { Vector3 } from 'babylonjs/Maths/math.vector';
  9. import { HemisphericLight } from 'babylonjs/Lights/hemisphericLight';
  10. import { ArcRotateCamera } from 'babylonjs/Cameras/arcRotateCamera';
  11. import { PreviewMeshType } from './previewMeshType';
  12. export class PreviewManager {
  13. private _nodeMaterial: NodeMaterial;
  14. private _onBuildObserver: Nullable<Observer<NodeMaterial>>;
  15. private _onPreviewMeshTypeChangedObserver: Nullable<Observer<void>>;
  16. private _onUpdateRequiredObserver: Nullable<Observer<void>>;
  17. private _engine: Engine;
  18. private _scene: Scene;
  19. private _light: HemisphericLight;
  20. private _dummy: Mesh;
  21. private _camera: ArcRotateCamera;
  22. private _material: NodeMaterial;
  23. private _globalState: GlobalState;
  24. public constructor(targetCanvas: HTMLCanvasElement, globalState: GlobalState) {
  25. this._nodeMaterial = globalState.nodeMaterial;
  26. this._globalState = globalState;
  27. this._onBuildObserver = this._nodeMaterial.onBuildObservable.add((nodeMaterial) => {
  28. let serializationObject = nodeMaterial.serialize();
  29. this._updatePreview(serializationObject);
  30. });
  31. this._onPreviewMeshTypeChangedObserver = globalState.onPreviewMeshTypeChanged.add(() => {
  32. this._refreshPreviewMesh();
  33. });
  34. this._onUpdateRequiredObserver = globalState.onUpdateRequiredObservable.add(() => {
  35. let serializationObject = this._nodeMaterial.serialize();
  36. this._updatePreview(serializationObject);
  37. });
  38. this._engine = new Engine(targetCanvas, true);
  39. this._scene = new Scene(this._engine);
  40. this._camera = new ArcRotateCamera("Camera", 0, 0.8, 4, Vector3.Zero(), this._scene);
  41. this._light = new HemisphericLight("light", new Vector3(0, 1, 0), this._scene);
  42. this._camera.lowerRadiusLimit = 3;
  43. this._camera.upperRadiusLimit = 10;
  44. this._camera.wheelPrecision = 20;
  45. this._camera.attachControl(targetCanvas, false);
  46. this._refreshPreviewMesh();
  47. this._engine.runRenderLoop(() => {
  48. this._engine.resize();
  49. this._scene.render();
  50. });
  51. let serializationObject = this._nodeMaterial.serialize();
  52. this._updatePreview(serializationObject);
  53. }
  54. private _refreshPreviewMesh() {
  55. if (this._dummy) {
  56. this._dummy.dispose();
  57. }
  58. switch (this._globalState.previewMeshType) {
  59. case PreviewMeshType.Box:
  60. this._dummy = Mesh.CreateBox("dummy-box", 2, this._scene);
  61. break;
  62. case PreviewMeshType.Sphere:
  63. this._dummy = Mesh.CreateSphere("dummy-sphere", 32, 2, this._scene);
  64. break;
  65. case PreviewMeshType.Torus:
  66. this._dummy = Mesh.CreateTorus("dummy-torus", 2, 0.5, 32, this._scene);
  67. break;
  68. case PreviewMeshType.Cylinder:
  69. this._dummy = Mesh.CreateCylinder("dummy-cylinder", 2, 1, 1.2, 32, 1, this._scene);
  70. break;
  71. case PreviewMeshType.Plane:
  72. this._dummy = Mesh.CreateGround("dummy-plane", 2, 2, 128, this._scene);
  73. break;
  74. }
  75. this._dummy.material = this._material;
  76. }
  77. private _updatePreview(serializationObject: any) {
  78. if (this._material) {
  79. this._material.dispose();
  80. }
  81. this._material = NodeMaterial.Parse(serializationObject, this._scene);
  82. this._material.build();
  83. this._dummy.material = this._material;
  84. }
  85. public dispose() {
  86. this._nodeMaterial.onBuildObservable.remove(this._onBuildObserver);
  87. this._globalState.onPreviewMeshTypeChanged.remove(this._onPreviewMeshTypeChangedObserver);
  88. this._globalState.onUpdateRequiredObservable.remove(this._onUpdateRequiredObserver);
  89. if (this._material) {
  90. this._material.dispose();
  91. }
  92. this._camera.dispose();
  93. this._dummy.dispose();
  94. this._light.dispose();
  95. this._engine.dispose();
  96. }
  97. }