texturePropertyTabComponent.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. updateAftertextureLoad() {
  19. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  20. this.props.globalState.onRebuildRequiredObservable.notifyObservers();
  21. }
  22. /**
  23. * Replaces the texture of the node
  24. * @param file the file of the texture to use
  25. */
  26. replaceTexture(file: File) {
  27. if (!this.props.node) {
  28. return;
  29. }
  30. let texture = this.props.node.texture as BaseTexture;
  31. if (!texture) {
  32. this.props.node.texture = new Texture(null, Engine.LastCreatedScene)
  33. texture = this.props.node.texture;
  34. }
  35. Tools.ReadFile(file, (data) => {
  36. var blob = new Blob([data], { type: "octet/stream" });
  37. var reader = new FileReader();
  38. reader.readAsDataURL(blob);
  39. reader.onloadend = () => {
  40. let base64data = reader.result as string;
  41. if (texture.isCube) {
  42. let extension: string | undefined = undefined;
  43. if (file.name.toLowerCase().indexOf(".dds") > 0) {
  44. extension = ".dds";
  45. } else if (file.name.toLowerCase().indexOf(".env") > 0) {
  46. extension = ".env";
  47. }
  48. (texture as Texture).updateURL(base64data, extension, () => this.updateAftertextureLoad());
  49. } else {
  50. (texture as Texture).updateURL(base64data, null, () => this.updateAftertextureLoad());
  51. }
  52. }
  53. }, undefined, true);
  54. }
  55. render() {
  56. return (
  57. <div>
  58. <LineContainerComponent title="GENERAL">
  59. <TextLineComponent label="Type" value="Texture" />
  60. <TextInputLineComponent label="Name" propertyName="name" target={this.props.node.block!} onChange={() => this.props.globalState.onUpdateRequiredObservable.notifyObservers()} />
  61. </LineContainerComponent>
  62. <LineContainerComponent title="PROPERTIES">
  63. <CheckBoxLineComponent label="Auto select UV" propertyName="autoSelectUV" target={this.props.node.block!} />
  64. <FileButtonLineComponent label="Replace texture" onClick={(file) => this.replaceTexture(file)} accept=".jpg, .png, .tga, .dds, .env" />
  65. </LineContainerComponent>
  66. </div>
  67. );
  68. }
  69. }