texturePropertyTabComponent.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as React from "react";
  2. import { GlobalState } from '../../../globalState';
  3. import { Texture } from 'babylonjs/Materials/Textures/texture';
  4. import { FileButtonLineComponent } from '../../../sharedComponents/fileButtonLineComponent';
  5. import { Tools } from 'babylonjs/Misc/tools';
  6. import { Engine } from 'babylonjs/Engines/engine';
  7. import { TextureNodeModel } from '../../../components/diagram/texture/textureNodeModel';
  8. interface ITexturePropertyTabComponentProps {
  9. globalState: GlobalState;
  10. node: TextureNodeModel;
  11. }
  12. export class TexturePropertyTabComponent extends React.Component<ITexturePropertyTabComponentProps> {
  13. /**
  14. * Replaces the texture of the node
  15. * @param file the file of the texture to use
  16. */
  17. replaceTexture(file: File) {
  18. if (!this.props.node) {
  19. return;
  20. }
  21. let texture = this.props.node.texture as Texture;
  22. if (!texture) {
  23. this.props.node.texture = new Texture(null, Engine.LastCreatedScene)
  24. texture = this.props.node.texture;
  25. }
  26. Tools.ReadFile(file, (data) => {
  27. var blob = new Blob([data], { type: "octet/stream" });
  28. var url = URL.createObjectURL(blob);
  29. if (texture.isCube) {
  30. let extension: string | undefined = undefined;
  31. if (file.name.toLowerCase().indexOf(".dds") > 0) {
  32. extension = ".dds";
  33. } else if (file.name.toLowerCase().indexOf(".env") > 0) {
  34. extension = ".env";
  35. }
  36. (texture as Texture).updateURL(url, extension, () => this.forceUpdate());
  37. } else {
  38. (texture as Texture).updateURL(url, null, () => this.forceUpdate());
  39. }
  40. }, undefined, true);
  41. }
  42. render() {
  43. return (
  44. <div>
  45. <h1>Texture</h1>
  46. <FileButtonLineComponent label="Replace texture" onClick={(file) => this.replaceTexture(file)} accept=".jpg, .png, .tga, .dds, .env" />
  47. </div>
  48. );
  49. }
  50. }