texturePropertyTabComponent.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import * as React from "react";
  2. import { BaseTexture } from 'babylonjs/Materials/Textures/baseTexture';
  3. import { FileButtonLineComponent } from '../../sharedComponents/fileButtonLineComponent';
  4. import { Tools } from 'babylonjs/Misc/tools';
  5. import { LineContainerComponent } from '../../sharedComponents/lineContainerComponent';
  6. import { TextInputLineComponent } from '../../sharedComponents/textInputLineComponent';
  7. import { CheckBoxLineComponent } from '../../sharedComponents/checkBoxLineComponent';
  8. import { Texture } from 'babylonjs/Materials/Textures/texture';
  9. import { SliderLineComponent } from '../../sharedComponents/sliderLineComponent';
  10. import { FloatLineComponent } from '../../sharedComponents/floatLineComponent';
  11. import { ButtonLineComponent } from '../../sharedComponents/buttonLineComponent';
  12. import { CubeTexture } from 'babylonjs/Materials/Textures/cubeTexture';
  13. import { OptionsLineComponent } from '../../sharedComponents/optionsLineComponent';
  14. import { IPropertyComponentProps } from './propertyComponentProps';
  15. import { ReflectionTextureBlock } from 'babylonjs/Materials/Node/Blocks/Dual/reflectionTextureBlock';
  16. import { ReflectionBlock } from 'babylonjs/Materials/Node/Blocks/PBR/reflectionBlock';
  17. import { RefractionBlock } from 'babylonjs/Materials/Node/Blocks/PBR/refractionBlock';
  18. import { TextureBlock } from 'babylonjs/Materials/Node/Blocks/Dual/textureBlock';
  19. import { CurrentScreenBlock } from 'babylonjs/Materials/Node/Blocks/Dual/currentScreenBlock';
  20. import { ParticleTextureBlock } from 'babylonjs/Materials/Node/Blocks/Particle/particleTextureBlock';
  21. import { GeneralPropertyTabComponent, GenericPropertyTabComponent } from './genericNodePropertyComponent';
  22. import { NodeMaterialModes } from 'babylonjs/Materials/Node/Enums/nodeMaterialModes';
  23. type ReflectionTexture = ReflectionTextureBlock | ReflectionBlock | RefractionBlock;
  24. type AnyTexture = TextureBlock | ReflectionTexture | CurrentScreenBlock | ParticleTextureBlock;
  25. export class TexturePropertyTabComponent extends React.Component<IPropertyComponentProps, {isEmbedded: boolean, loadAsCubeTexture: boolean}> {
  26. get textureBlock(): AnyTexture {
  27. return this.props.block as AnyTexture;
  28. }
  29. constructor(props: IPropertyComponentProps) {
  30. super(props);
  31. let texture = this.textureBlock.texture as BaseTexture;
  32. this.state = {isEmbedded: !texture || texture.name.substring(0, 4) === "data", loadAsCubeTexture: texture && texture.isCube};
  33. }
  34. UNSAFE_componentWillUpdate(nextProps: IPropertyComponentProps, nextState: {isEmbedded: boolean, loadAsCubeTexture: boolean}) {
  35. if (nextProps.block !== this.props.block) {
  36. let texture = (nextProps.block as AnyTexture).texture as BaseTexture;
  37. nextState.isEmbedded = !texture || texture.name.substring(0, 4) === "data";
  38. nextState.loadAsCubeTexture = texture && texture.isCube;
  39. }
  40. }
  41. private _generateRandomForCache() {
  42. return 'xxxxxxxxxxxxxxxxxxxx'.replace(/[x]/g, (c) => {
  43. var r = Math.random() * 10 | 0;
  44. return r.toString();
  45. });
  46. }
  47. updateAfterTextureLoad() {
  48. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  49. this.props.globalState.onRebuildRequiredObservable.notifyObservers();
  50. this.forceUpdate();
  51. }
  52. removeTexture() {
  53. let texture = this.textureBlock.texture as BaseTexture;
  54. if (texture) {
  55. texture.dispose();
  56. (texture as any) = null;
  57. this.textureBlock.texture = null;
  58. }
  59. this.updateAfterTextureLoad();
  60. }
  61. _prepareTexture() {
  62. let texture = this.textureBlock.texture as BaseTexture;
  63. if (texture && texture.isCube !== this.state.loadAsCubeTexture) {
  64. texture.dispose();
  65. (texture as any) = null;
  66. }
  67. if (!texture) {
  68. if (!this.state.loadAsCubeTexture) {
  69. this.textureBlock.texture = new Texture(null, this.props.globalState.nodeMaterial.getScene(), false,
  70. this.textureBlock instanceof ReflectionTextureBlock || this.textureBlock instanceof ReflectionBlock || this.textureBlock instanceof RefractionBlock || this.props.globalState.mode === NodeMaterialModes.PostProcess);
  71. texture = this.textureBlock.texture;
  72. texture.coordinatesMode = Texture.EQUIRECTANGULAR_MODE;
  73. } else {
  74. this.textureBlock.texture = new CubeTexture("", this.props.globalState.nodeMaterial.getScene());
  75. texture = this.textureBlock.texture;
  76. texture.coordinatesMode = Texture.CUBIC_MODE;
  77. }
  78. }
  79. }
  80. /**
  81. * Replaces the texture of the node
  82. * @param file the file of the texture to use
  83. */
  84. replaceTexture(file: File) {
  85. this._prepareTexture();
  86. let texture = this.textureBlock.texture as BaseTexture;
  87. Tools.ReadFile(file, (data) => {
  88. var blob = new Blob([data], { type: "octet/stream" });
  89. var reader = new FileReader();
  90. reader.readAsDataURL(blob);
  91. reader.onloadend = () => {
  92. let base64data = reader.result as string;
  93. let extension: string | undefined = undefined;
  94. if (file.name.toLowerCase().indexOf(".dds") > 0) {
  95. extension = ".dds";
  96. } else if (file.name.toLowerCase().indexOf(".env") > 0) {
  97. extension = ".env";
  98. }
  99. (texture as Texture).updateURL(base64data, extension, () => this.updateAfterTextureLoad());
  100. }
  101. }, undefined, true);
  102. }
  103. replaceTextureWithUrl(url: string) {
  104. this._prepareTexture();
  105. let texture = this.textureBlock.texture as BaseTexture;
  106. if (texture.isCube || this.textureBlock instanceof ReflectionTextureBlock || this.textureBlock instanceof ReflectionBlock || this.textureBlock instanceof RefractionBlock) {
  107. let extension: string | undefined = undefined;
  108. if (url.toLowerCase().indexOf(".dds") > 0) {
  109. extension = ".dds";
  110. } else if (url.toLowerCase().indexOf(".env") > 0) {
  111. extension = ".env";
  112. }
  113. (texture as Texture).updateURL(url, extension, () => this.updateAfterTextureLoad());
  114. } else {
  115. (texture as Texture).updateURL(url, null, () => this.updateAfterTextureLoad());
  116. }
  117. }
  118. render() {
  119. let url = "";
  120. let texture = this.textureBlock.texture as BaseTexture;
  121. if (texture && texture.name && texture.name.substring(0, 4) !== "data") {
  122. url = texture.name;
  123. }
  124. url = url.replace(/\?nocache=\d+/, "");
  125. let isInReflectionMode = this.textureBlock instanceof ReflectionTextureBlock || this.textureBlock instanceof ReflectionBlock || this.textureBlock instanceof RefractionBlock;
  126. let isFrozenTexture = this.textureBlock instanceof CurrentScreenBlock || this.textureBlock instanceof ParticleTextureBlock;
  127. var reflectionModeOptions: {label: string, value: number}[] = [
  128. {
  129. label: "Cubic", value: Texture.CUBIC_MODE
  130. },
  131. {
  132. label: "Equirectangular", value: Texture.EQUIRECTANGULAR_MODE
  133. },
  134. {
  135. label: "Explicit", value: Texture.EXPLICIT_MODE
  136. },
  137. {
  138. label: "Fixed equirectangular", value: Texture.FIXED_EQUIRECTANGULAR_MODE
  139. },
  140. {
  141. label: "Fixed mirrored equirectangular", value: Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE
  142. },
  143. {
  144. label: "Planar", value: Texture.PLANAR_MODE
  145. },
  146. {
  147. label: "Projection", value: Texture.PROJECTION_MODE
  148. },
  149. {
  150. label: "Skybox", value: Texture.SKYBOX_MODE
  151. },
  152. {
  153. label: "Spherical", value: Texture.SPHERICAL_MODE
  154. },
  155. ];
  156. return (
  157. <div>
  158. <GeneralPropertyTabComponent globalState={this.props.globalState} block={this.props.block}/>
  159. <LineContainerComponent title="PROPERTIES">
  160. <CheckBoxLineComponent label="Auto select UV" propertyName="autoSelectUV" target={this.props.block} onValueChanged={() => {
  161. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  162. }}/>
  163. {
  164. texture && !isInReflectionMode &&
  165. <CheckBoxLineComponent label="Convert to gamma space" propertyName="convertToGammaSpace" target={this.props.block} onValueChanged={() => {
  166. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  167. }}/>
  168. }
  169. {
  170. texture && !isInReflectionMode &&
  171. <CheckBoxLineComponent label="Convert to linear space" propertyName="convertToLinearSpace" target={this.props.block} onValueChanged={() => {
  172. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  173. }}/>
  174. }
  175. {
  176. texture && isInReflectionMode &&
  177. <OptionsLineComponent label="Reflection mode" options={reflectionModeOptions} target={texture} propertyName="coordinatesMode" onSelect={(value: any) => {
  178. texture.coordinatesMode = value;
  179. this.forceUpdate();
  180. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  181. }} />
  182. }
  183. {
  184. texture && !isInReflectionMode && !isFrozenTexture &&
  185. <CheckBoxLineComponent label="Clamp U" isSelected={() => texture.wrapU === Texture.CLAMP_ADDRESSMODE} onSelect={(value) => {
  186. texture.wrapU = value ? Texture.CLAMP_ADDRESSMODE : Texture.WRAP_ADDRESSMODE;
  187. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  188. }} />
  189. }
  190. {
  191. texture && !isInReflectionMode && !isFrozenTexture &&
  192. <CheckBoxLineComponent label="Clamp V" isSelected={() => texture.wrapV === Texture.CLAMP_ADDRESSMODE} onSelect={(value) => {
  193. texture.wrapV = value ? Texture.CLAMP_ADDRESSMODE : Texture.WRAP_ADDRESSMODE;
  194. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  195. }} />
  196. }
  197. {
  198. texture && !isInReflectionMode && !isFrozenTexture &&
  199. <FloatLineComponent globalState={this.props.globalState} label="Offset U" target={texture} propertyName="uOffset"
  200. onChange={() => {
  201. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  202. }}
  203. />
  204. }
  205. {
  206. texture && !isInReflectionMode && !isFrozenTexture &&
  207. <FloatLineComponent globalState={this.props.globalState} label="Offset V" target={texture} propertyName="vOffset"
  208. onChange={() => {
  209. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  210. }}
  211. />
  212. }
  213. {
  214. texture && !isInReflectionMode && !isFrozenTexture &&
  215. <FloatLineComponent globalState={this.props.globalState} label="Scale U" target={texture} propertyName="uScale"
  216. onChange={() => {
  217. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  218. }} />
  219. }
  220. {
  221. texture && !isInReflectionMode && !isFrozenTexture &&
  222. <FloatLineComponent globalState={this.props.globalState} label="Scale V" target={texture} propertyName="vScale"
  223. onChange={() => {
  224. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  225. }} />
  226. }
  227. {
  228. texture && !isInReflectionMode && !isFrozenTexture &&
  229. <SliderLineComponent label="Rotation U" target={texture} globalState={this.props.globalState} propertyName="uAng" minimum={0} maximum={Math.PI * 2} useEuler={true} step={0.1}
  230. onChange={() => {
  231. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  232. }}
  233. />
  234. }
  235. {
  236. texture && !isInReflectionMode && !isFrozenTexture &&
  237. <SliderLineComponent label="Rotation V" target={texture} globalState={this.props.globalState} propertyName="vAng" minimum={0} maximum={Math.PI * 2} useEuler={true} step={0.1}
  238. onChange={() => {
  239. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  240. }}
  241. />
  242. }
  243. {
  244. texture && !isInReflectionMode && !isFrozenTexture &&
  245. <SliderLineComponent label="Rotation W" target={texture} globalState={this.props.globalState} propertyName="wAng" minimum={0} maximum={Math.PI * 2} useEuler={true} step={0.1}
  246. onChange={() => {
  247. this.props.globalState.onUpdateRequiredObservable.notifyObservers();
  248. }}
  249. />
  250. }
  251. </LineContainerComponent>
  252. <LineContainerComponent title="SOURCE">
  253. <CheckBoxLineComponent label="Embed static texture" isSelected={() => this.state.isEmbedded} onSelect={value => {
  254. this.setState({isEmbedded: value});
  255. this.textureBlock.texture = null;
  256. this.updateAfterTextureLoad();
  257. }}/>
  258. {
  259. isInReflectionMode &&
  260. <CheckBoxLineComponent label="Load as cube texture" isSelected={() => this.state.loadAsCubeTexture}
  261. onSelect={value => this.setState({loadAsCubeTexture: value})}/>
  262. }
  263. {
  264. this.state.isEmbedded &&
  265. <FileButtonLineComponent label="Upload" onClick={(file) => this.replaceTexture(file)} accept=".jpg, .png, .tga, .dds, .env" />
  266. }
  267. {
  268. !this.state.isEmbedded &&
  269. <TextInputLineComponent label="Link" globalState={this.props.globalState} value={url} onChange={newUrl => this.replaceTextureWithUrl(newUrl)}/>
  270. }
  271. {
  272. !this.state.isEmbedded && url &&
  273. <ButtonLineComponent label="Refresh" onClick={() => this.replaceTextureWithUrl(url + "?nocache=" + this._generateRandomForCache())}/>
  274. }
  275. {
  276. texture &&
  277. <ButtonLineComponent label="Remove" onClick={() => this.removeTexture()}/>
  278. }
  279. </LineContainerComponent>
  280. <GenericPropertyTabComponent globalState={this.props.globalState} block={this.props.block}/>
  281. </div>
  282. );
  283. }
  284. }