CubeTextureElement.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. module INSPECTOR {
  2. /**
  3. * Display a very small div. A new canvas is created, with a new Babylon.js scene, containing only the
  4. * cube texture in a cube
  5. */
  6. export class CubeTextureElement extends BasicElement{
  7. /** The big div displaying the full image */
  8. private _textureDiv: HTMLElement;
  9. private _engine : BABYLON.Engine;
  10. protected _scene : BABYLON.Scene;
  11. protected _cube : BABYLON.Mesh;
  12. private _canvas : HTMLCanvasElement;
  13. protected _textureUrl: string;
  14. // On pause the engine will not render anything
  15. private _pause : boolean = false;
  16. /** The texture given as a parameter should be cube. */
  17. constructor(tex : BABYLON.Texture) {
  18. super();
  19. this._div.className = 'fa fa-search texture-element';
  20. // Create the texture viewer
  21. this._textureDiv = Helpers.CreateDiv('texture-viewer', this._div);
  22. // canvas
  23. this._canvas = Helpers.CreateElement('canvas', 'texture-viewer-img', this._textureDiv) as HTMLCanvasElement;
  24. if (tex) {
  25. this._textureUrl = tex.name;
  26. }
  27. this._div.addEventListener('mouseover', this._showViewer.bind(this, 'flex'));
  28. this._div.addEventListener('mouseout', this._showViewer.bind(this, 'none'));
  29. }
  30. public update(tex?:BABYLON.Texture) {
  31. if (tex && tex.url === this._textureUrl) {
  32. // Nothing to do, as the old texture is the same as the old one
  33. } else {
  34. if (tex) {
  35. this._textureUrl = tex.name;
  36. }
  37. if (this._engine) {
  38. // Dispose old material and cube
  39. this._cube.material.dispose(true, true);
  40. this._cube.dispose();
  41. } else {
  42. this._initEngine();
  43. }
  44. // and create it again
  45. this._populateScene();
  46. }
  47. }
  48. /** Creates the box */
  49. protected _populateScene() {
  50. // Create the hdr texture
  51. let hdrTexture = new BABYLON.CubeTexture(this._textureUrl, this._scene);
  52. hdrTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  53. this._cube = BABYLON.Mesh.CreateBox("hdrSkyBox", 10.0, this._scene);
  54. let hdrSkyboxMaterial = new BABYLON.StandardMaterial("skyBox", this._scene);
  55. hdrSkyboxMaterial.backFaceCulling = false;
  56. hdrSkyboxMaterial.reflectionTexture = hdrTexture;
  57. hdrSkyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  58. hdrSkyboxMaterial.disableLighting = true;
  59. this._cube.material = hdrSkyboxMaterial;
  60. this._cube.registerBeforeRender(() => {
  61. this._cube.rotation.y += 0.01;
  62. })
  63. }
  64. /** Init the babylon engine */
  65. private _initEngine() {
  66. this._engine = new BABYLON.Engine(this._canvas);
  67. this._scene = new BABYLON.Scene(this._engine);
  68. this._scene.clearColor = new BABYLON.Color4(0,0,0, 0);
  69. let cam = new BABYLON.FreeCamera('cam', new BABYLON.Vector3(0,0,-20), this._scene);
  70. let light = new BABYLON.HemisphericLight('', new BABYLON.Vector3(0,1,0), this._scene);
  71. this._engine.runRenderLoop(() => {
  72. if (!this._pause) {
  73. this._scene.render();
  74. }
  75. });
  76. this._canvas.setAttribute('width', '110');
  77. this._canvas.setAttribute('height', '110');
  78. }
  79. private _showViewer(mode:string) {
  80. // If displaying...
  81. if (mode != 'none') {
  82. // ... and the canvas is not initialized
  83. if (!this._engine) {
  84. this._initEngine();
  85. this._populateScene();
  86. }
  87. // In every cases, unpause the engine
  88. this._pause = false;
  89. } else {
  90. // hide : pause the engine
  91. this._pause = true;
  92. }
  93. this._textureDiv.style.display = mode;
  94. }
  95. /** Removes properly the babylon engine */
  96. public dispose () {
  97. if (this._engine) {
  98. this._engine.dispose();
  99. this._engine = null;
  100. }
  101. }
  102. }
  103. }