texturePropertyTabComponent.tsx 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import * as React from "react";
  2. import { GlobalState } from '../../../globalState';
  3. import { BaseTexture } from 'babylonjs/Materials/Textures/baseTexture';
  4. import { FileButtonLineComponent } from '../../../sharedComponents/fileButtonLineComponent';
  5. import { Tools } from 'babylonjs/Misc/tools';
  6. import { Engine } from 'babylonjs/Engines/engine';
  7. import { TextureNodeModel } from './textureNodeModel';
  8. import { TextLineComponent } from '../../../sharedComponents/textLineComponent';
  9. import { LineContainerComponent } from '../../../sharedComponents/lineContainerComponent';
  10. import { TextInputLineComponent } from '../../../sharedComponents/textInputLineComponent';
  11. import { CheckBoxLineComponent } from '../../../sharedComponents/checkBoxLineComponent';
  12. import { Texture } from 'babylonjs/Materials/Textures/texture';
  13. interface ITexturePropertyTabComponentProps {
  14. globalState: GlobalState;
  15. node: TextureNodeModel;
  16. }
  17. export class TexturePropertyTabComponent extends React.Component<ITexturePropertyTabComponentProps> {
  18. /**
  19. * Replaces the texture of the node
  20. * @param file the file of the texture to use
  21. */
  22. replaceTexture(file: File) {
  23. if (!this.props.node) {
  24. return;
  25. }
  26. let texture = this.props.node.texture as BaseTexture;
  27. if (!texture) {
  28. this.props.node.texture = new Texture(null, Engine.LastCreatedScene)
  29. texture = this.props.node.texture;
  30. }
  31. Tools.ReadFile(file, (data) => {
  32. var blob = new Blob([data], { type: "octet/stream" });
  33. var url = URL.createObjectURL(blob);
  34. if (texture.isCube) {
  35. let extension: string | undefined = undefined;
  36. if (file.name.toLowerCase().indexOf(".dds") > 0) {
  37. extension = ".dds";
  38. } else if (file.name.toLowerCase().indexOf(".env") > 0) {
  39. extension = ".env";
  40. }
  41. (texture as Texture).updateURL(url, extension, () => this.props.globalState.onUpdateRequiredObservable.notifyObservers());
  42. } else {
  43. (texture as Texture).updateURL(url, null, () => this.props.globalState.onUpdateRequiredObservable.notifyObservers());
  44. }
  45. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  46. this.props.globalState.onRebuildRequiredObservable.notifyObservers();
  47. }, undefined, true);
  48. }
  49. render() {
  50. return (
  51. <div>
  52. <LineContainerComponent title="GENERAL">
  53. <TextLineComponent label="Type" value="Texture" />
  54. <TextInputLineComponent label="Name" propertyName="name" target={this.props.node.block!} onChange={() => this.props.globalState.onUpdateRequiredObservable.notifyObservers()} />
  55. </LineContainerComponent>
  56. <LineContainerComponent title="PROPERTIES">
  57. <CheckBoxLineComponent label="Auto select UV" propertyName="autoSelectUV" target={this.props.node.block!} />
  58. <FileButtonLineComponent label="Replace texture" onClick={(file) => this.replaceTexture(file)} accept=".jpg, .png, .tga, .dds, .env" />
  59. </LineContainerComponent>
  60. </div>
  61. );
  62. }
  63. }