previewManager.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 _engine: Engine;
  17. private _scene: Scene;
  18. private _light: HemisphericLight;
  19. private _dummy: Mesh;
  20. private _camera: ArcRotateCamera;
  21. private _material: NodeMaterial;
  22. private _globalState: GlobalState;
  23. public constructor(targetCanvas: HTMLCanvasElement, globalState: GlobalState) {
  24. this._nodeMaterial = globalState.nodeMaterial;
  25. this._globalState = globalState;
  26. this._onBuildObserver = this._nodeMaterial.onBuildObservable.add((nodeMaterial) => {
  27. let serializationObject = nodeMaterial.serialize();
  28. this._updatePreview(serializationObject);
  29. });
  30. this._onPreviewMeshTypeChangedObserver = globalState.onPreviewMeshTypeChanged.add(() => {
  31. this._refreshPreviewMesh();
  32. });
  33. this._engine = new Engine(targetCanvas, true);
  34. this._scene = new Scene(this._engine);
  35. this._camera = new ArcRotateCamera("Camera", 0, 0.8, 4, Vector3.Zero(), this._scene);
  36. this._light = new HemisphericLight("light", new Vector3(0, 1, 0), this._scene);
  37. this._camera.lowerRadiusLimit = 3;
  38. this._camera.upperRadiusLimit = 10;
  39. this._camera.wheelPrecision = 20;
  40. this._camera.attachControl(targetCanvas, false);
  41. this._refreshPreviewMesh();
  42. this._engine.runRenderLoop(() => {
  43. this._engine.resize();
  44. this._scene.render();
  45. });
  46. let serializationObject = this._nodeMaterial.serialize();
  47. this._updatePreview(serializationObject);
  48. }
  49. private _refreshPreviewMesh() {
  50. if (this._dummy) {
  51. this._dummy.dispose();
  52. }
  53. switch (this._globalState.previewMeshType) {
  54. case PreviewMeshType.Box:
  55. this._dummy = Mesh.CreateBox("dummy", 2, this._scene);
  56. break;
  57. case PreviewMeshType.Sphere:
  58. this._dummy = Mesh.CreateSphere("dummy", 32, 2, this._scene);
  59. break;
  60. case PreviewMeshType.Torus:
  61. this._dummy = Mesh.CreateTorus("dummy", 2, 0.5, 32, this._scene);
  62. break;
  63. case PreviewMeshType.Cylinder:
  64. this._dummy = Mesh.CreateCylinder("dummy", 2, 1, 1.2, 32, 1, this._scene);
  65. break;
  66. }
  67. this._dummy.material = this._material;
  68. }
  69. private _updatePreview(serializationObject: any) {
  70. if (this._material) {
  71. this._material.dispose();
  72. }
  73. this._material = NodeMaterial.Parse(serializationObject, this._scene);
  74. this._material.build(true);
  75. this._dummy.material = this._material;
  76. }
  77. public dispose() {
  78. this._nodeMaterial.onBuildObservable.remove(this._onBuildObserver);
  79. this._globalState.onPreviewMeshTypeChanged.remove(this._onPreviewMeshTypeChangedObserver);
  80. if (this._material) {
  81. this._material.dispose();
  82. }
  83. this._camera.dispose();
  84. this._dummy.dispose();
  85. this._light.dispose();
  86. this._engine.dispose();
  87. }
  88. }