textureDisplayManager.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { IDisplayManager } from './displayManager';
  2. import { NodeMaterialBlock } from 'babylonjs/Materials/Node/nodeMaterialBlock';
  3. import { TextureBlock } from 'babylonjs/Materials/Node/Blocks/Dual/textureBlock';
  4. import { RefractionBlock } from 'babylonjs/Materials/Node/Blocks/PBR/refractionBlock';
  5. import { ReflectionTextureBlock } from 'babylonjs/Materials/Node/Blocks/Dual/reflectionTextureBlock';
  6. import { TextureLineComponent } from '../../sharedComponents/textureLineComponent';
  7. import { CurrentScreenBlock } from 'babylonjs/Materials/Node/Blocks/Dual/currentScreenBlock';
  8. import { ParticleTextureBlock } from 'babylonjs/Materials/Node/Blocks/Particle/particleTextureBlock';
  9. export class TextureDisplayManager implements IDisplayManager {
  10. private _previewCanvas: HTMLCanvasElement;
  11. private _previewImage: HTMLImageElement;
  12. public getHeaderClass(block: NodeMaterialBlock) {
  13. return "";
  14. }
  15. public shouldDisplayPortLabels(block: NodeMaterialBlock): boolean {
  16. return true;
  17. }
  18. public getHeaderText(block: NodeMaterialBlock): string {
  19. return block.name;
  20. }
  21. public getBackgroundColor(block: NodeMaterialBlock): string {
  22. return "#323232";
  23. }
  24. public updatePreviewContent(block: NodeMaterialBlock, contentArea: HTMLDivElement): void {
  25. const textureBlock = block as TextureBlock | ReflectionTextureBlock | RefractionBlock | CurrentScreenBlock;
  26. if (!this._previewCanvas) {
  27. contentArea.classList.add("texture-block");
  28. if (block instanceof TextureBlock || block instanceof CurrentScreenBlock || block instanceof ParticleTextureBlock) {
  29. contentArea.classList.add("regular-texture-block");
  30. }
  31. this._previewCanvas = contentArea.ownerDocument!.createElement("canvas");
  32. this._previewImage = contentArea.ownerDocument!.createElement("img");
  33. contentArea.appendChild(this._previewImage);
  34. this._previewImage.classList.add("empty");
  35. }
  36. if (textureBlock.texture) {
  37. TextureLineComponent.UpdatePreview(this._previewCanvas, textureBlock.texture, 140, {
  38. face: 0,
  39. displayRed: true,
  40. displayAlpha: true,
  41. displayBlue: true,
  42. displayGreen: true
  43. }, () => {
  44. this._previewImage.src = this._previewCanvas.toDataURL("image/png");
  45. this._previewImage.classList.remove("empty");
  46. });
  47. } else {
  48. this._previewImage.classList.add("empty");
  49. }
  50. }
  51. }