texturePropertyGridComponent.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import * as React from "react";
  2. import { Nullable } from "babylonjs/types";
  3. import { Tools } from "babylonjs/Misc/tools";
  4. import { Observable } from "babylonjs/Misc/observable";
  5. import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
  6. import { Texture } from "babylonjs/Materials/Textures/texture";
  7. import { CubeTexture } from "babylonjs/Materials/Textures/cubeTexture";
  8. import { PropertyChangedEvent } from "../../../../propertyChangedEvent";
  9. import { LineContainerComponent } from "../../../lineContainerComponent";
  10. import { SliderLineComponent } from "../../../lines/sliderLineComponent";
  11. import { TextLineComponent } from "../../../../../sharedUiComponents/lines/textLineComponent";
  12. import { CheckBoxLineComponent } from "../../../lines/checkBoxLineComponent";
  13. import { TextureLineComponent } from "../../../lines/textureLineComponent";
  14. import { FloatLineComponent } from "../../../lines/floatLineComponent";
  15. import { OptionsLineComponent } from "../../../lines/optionsLineComponent";
  16. import { FileButtonLineComponent } from "../../../../../sharedUiComponents/lines/fileButtonLineComponent";
  17. import { LockObject } from "../lockObject";
  18. import { ValueLineComponent } from "../../../../../sharedUiComponents/lines/valueLineComponent";
  19. import { GlobalState } from "../../../../../components/globalState";
  20. import { AdvancedDynamicTextureInstrumentation } from "babylonjs-gui/2D/adtInstrumentation";
  21. import { AdvancedDynamicTexture } from "babylonjs-gui/2D/advancedDynamicTexture";
  22. import { CustomPropertyGridComponent } from '../customPropertyGridComponent';
  23. import { ButtonLineComponent } from '../../../../../sharedUiComponents/lines/buttonLineComponent';
  24. import { TextInputLineComponent } from '../../../lines/textInputLineComponent';
  25. import { AnimationGridComponent } from '../animations/animationPropertyGridComponent';
  26. import { PopupComponent } from '../../../../popupComponent';
  27. import { TextureEditorComponent } from './textures/textureEditorComponent';
  28. interface ITexturePropertyGridComponentProps {
  29. texture: BaseTexture,
  30. lockObject: LockObject,
  31. globalState: GlobalState,
  32. onPropertyChangedObservable?: Observable<PropertyChangedEvent>
  33. }
  34. interface ITexturePropertyGridComponentState {
  35. isTextureEditorOpen : boolean,
  36. textureEditing : Nullable<BaseTexture>
  37. }
  38. export class TexturePropertyGridComponent extends React.Component<ITexturePropertyGridComponentProps,ITexturePropertyGridComponentState> {
  39. private _adtInstrumentation: Nullable<AdvancedDynamicTextureInstrumentation>;
  40. private popoutWindowRef : React.RefObject<PopupComponent>;
  41. private textureLineRef: React.RefObject<TextureLineComponent>;
  42. private _textureInspectorSize = {width: 1024, height: 490};
  43. constructor(props: ITexturePropertyGridComponentProps) {
  44. super(props);
  45. this.state = {
  46. isTextureEditorOpen: false,
  47. textureEditing: null
  48. }
  49. const texture = this.props.texture;
  50. this.textureLineRef = React.createRef();
  51. this.popoutWindowRef = React.createRef();
  52. if (!texture || !(texture as any).rootContainer) {
  53. return;
  54. }
  55. const adt = texture as AdvancedDynamicTexture;
  56. this._adtInstrumentation = new AdvancedDynamicTextureInstrumentation(adt);
  57. this._adtInstrumentation!.captureRenderTime = true;
  58. this._adtInstrumentation!.captureLayoutTime = true;
  59. this.onOpenTextureEditor.bind(this);
  60. this.onCloseTextureEditor.bind(this);
  61. this.openTextureEditor.bind(this);
  62. }
  63. componentWillUnmount() {
  64. if (this._adtInstrumentation) {
  65. this._adtInstrumentation.dispose();
  66. this._adtInstrumentation = null;
  67. }
  68. }
  69. updateTexture(file: File) {
  70. const texture = this.props.texture;
  71. Tools.ReadFile(file, (data) => {
  72. var blob = new Blob([data], { type: "octet/stream" });
  73. var reader = new FileReader();
  74. reader.readAsDataURL(blob);
  75. reader.onloadend = () => {
  76. let base64data = reader.result as string;
  77. if (texture.isCube) {
  78. let extension: string | undefined = undefined;
  79. if (file.name.toLowerCase().indexOf(".dds") > 0) {
  80. extension = ".dds";
  81. } else if (file.name.toLowerCase().indexOf(".env") > 0) {
  82. extension = ".env";
  83. }
  84. (texture as CubeTexture).updateURL(base64data, extension, () => this.forceRefresh());
  85. } else {
  86. (texture as Texture).updateURL(base64data, null, () => this.forceRefresh());
  87. }
  88. };
  89. }, undefined, true);
  90. }
  91. openTextureEditor() {
  92. if (this.state.isTextureEditorOpen) {
  93. this.onCloseTextureEditor(() => this.openTextureEditor());
  94. return;
  95. }
  96. this.setState({
  97. isTextureEditorOpen: true,
  98. textureEditing: this.props.texture
  99. });
  100. }
  101. onOpenTextureEditor(window: Window) {}
  102. onCloseTextureEditor(callback?: {() : void}) {
  103. this.setState({
  104. isTextureEditorOpen: false,
  105. textureEditing: null
  106. }, callback);
  107. }
  108. forceRefresh() {
  109. this.forceUpdate();
  110. (this.textureLineRef.current as TextureLineComponent).updatePreview();
  111. }
  112. render() {
  113. const texture = this.props.texture;
  114. var samplingMode = [
  115. { label: "Nearest", value: Texture.NEAREST_NEAREST }, // 1
  116. { label: "Linear", value: Texture.LINEAR_LINEAR }, // 2
  117. { label: "Linear & linear mip", value: Texture.LINEAR_LINEAR_MIPLINEAR }, // 3
  118. { label: "Linear & nearest mip", value: Texture.LINEAR_LINEAR_MIPNEAREST }, // 11
  119. { label: "Nearest & linear mip", value: Texture.NEAREST_NEAREST_MIPLINEAR }, // 8
  120. { label: "Nearest & nearest mip", value: Texture.NEAREST_NEAREST_MIPNEAREST }, // 4
  121. { label: "Nearest/Linear", value: Texture.NEAREST_LINEAR }, // 7
  122. { label: "Nearest/Linear & linear mip", value: Texture.NEAREST_LINEAR_MIPLINEAR }, // 6
  123. { label: "Nearest/Linear & nearest mip", value: Texture.NEAREST_LINEAR_MIPNEAREST }, // 5
  124. { label: "Linear/Nearest", value: Texture.LINEAR_NEAREST }, // 12
  125. { label: "Linear/Nearest & linear mip", value: Texture.LINEAR_NEAREST_MIPLINEAR }, // 10
  126. { label: "Linear/Nearest & nearest mip", value: Texture.LINEAR_NEAREST_MIPNEAREST }, // 9
  127. ];
  128. var coordinatesMode = [
  129. { label: "Explicit", value: Texture.EXPLICIT_MODE },
  130. { label: "Cubic", value: Texture.CUBIC_MODE },
  131. { label: "Inverse cubic", value: Texture.INVCUBIC_MODE },
  132. { label: "Equirectangular", value: Texture.EQUIRECTANGULAR_MODE },
  133. { label: "Fixed equirectangular", value: Texture.FIXED_EQUIRECTANGULAR_MODE },
  134. { label: "Fixed equirectangular mirrored", value: Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE },
  135. { label: "Planar", value: Texture.PLANAR_MODE },
  136. { label: "Projection", value: Texture.PROJECTION_MODE },
  137. { label: "Skybox", value: Texture.SKYBOX_MODE },
  138. { label: "Spherical", value: Texture.SPHERICAL_MODE },
  139. ];
  140. let extension = "";
  141. let url = (texture as Texture).url;
  142. let textureUrl = (!url || url.substring(0, 4) === "data" || url.substring(0, 4) === "blob") ? "" : url;
  143. if (textureUrl) {
  144. for (var index = textureUrl.length - 1; index >= 0; index--) {
  145. if (textureUrl[index] === ".") {
  146. break;
  147. }
  148. extension = textureUrl[index] + extension;
  149. }
  150. }
  151. return (
  152. <div className="pane">
  153. <LineContainerComponent globalState={this.props.globalState} title="PREVIEW">
  154. <TextureLineComponent ref={this.textureLineRef} texture={texture} width={256} height={256} globalState={this.props.globalState} />
  155. <FileButtonLineComponent label="Load texture from file" onClick={(file) => this.updateTexture(file)} accept=".jpg, .png, .tga, .dds, .env" />
  156. <ButtonLineComponent label="Edit" onClick={() => this.openTextureEditor()} />
  157. <TextInputLineComponent label="URL" value={textureUrl} lockObject={this.props.lockObject} onChange={url => {
  158. (texture as Texture).updateURL(url);
  159. this.forceRefresh();
  160. }} />
  161. </LineContainerComponent>
  162. {this.state.isTextureEditorOpen && (
  163. <PopupComponent
  164. id='texture-editor'
  165. title='Texture Inspector'
  166. size={this._textureInspectorSize}
  167. onOpen={this.onOpenTextureEditor}
  168. onClose={() => this.onCloseTextureEditor}
  169. ref={this.popoutWindowRef}
  170. >
  171. <TextureEditorComponent
  172. texture={this.props.texture}
  173. url={textureUrl}
  174. window={this.popoutWindowRef}
  175. onUpdate={() => this.forceRefresh()}
  176. />
  177. </PopupComponent>)}
  178. <CustomPropertyGridComponent globalState={this.props.globalState} target={texture}
  179. lockObject={this.props.lockObject}
  180. onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  181. <LineContainerComponent globalState={this.props.globalState} title="GENERAL">
  182. <TextLineComponent label="Width" value={texture.getSize().width.toString()} />
  183. <TextLineComponent label="Height" value={texture.getSize().height.toString()} />
  184. {
  185. texture.isRenderTarget &&
  186. <ButtonLineComponent label="Scale up" onClick={() => {
  187. let scene = texture.getScene()!;
  188. texture.scale(2);
  189. setTimeout(() => {
  190. this.props.globalState.onSelectionChangedObservable.notifyObservers(scene.getTextureByUniqueID(texture.uniqueId));
  191. });
  192. }} />
  193. }
  194. {
  195. texture.isRenderTarget &&
  196. <ButtonLineComponent label="Scale down" onClick={() => {
  197. let scene = texture.getScene()!;
  198. texture.scale(0.5);
  199. setTimeout(() => {
  200. this.props.globalState.onSelectionChangedObservable.notifyObservers(scene.getTextureByUniqueID(texture.uniqueId));
  201. });
  202. }} />
  203. }
  204. {
  205. extension &&
  206. <TextLineComponent label="File format" value={extension} />
  207. }
  208. <TextLineComponent label="Unique ID" value={texture.uniqueId.toString()} />
  209. <TextLineComponent label="Class" value={texture.getClassName()} />
  210. <TextLineComponent label="Has alpha" value={texture.hasAlpha ? "Yes" : "No"} />
  211. <TextLineComponent label="Is 3D" value={texture.is3D ? "Yes" : "No"} />
  212. <TextLineComponent label="Is 2D array" value={texture.is2DArray ? "Yes" : "No"} />
  213. <TextLineComponent label="Is cube" value={texture.isCube ? "Yes" : "No"} />
  214. <TextLineComponent label="Is render target" value={texture.isRenderTarget ? "Yes" : "No"} />
  215. {
  216. (texture instanceof Texture) &&
  217. <TextLineComponent label="Stored as inverted on Y" value={texture.invertY ? "Yes" : "No"} />
  218. }
  219. <TextLineComponent label="Has mipmaps" value={!texture.noMipmap ? "Yes" : "No"} />
  220. <SliderLineComponent label="UV set" target={texture} propertyName="coordinatesIndex" minimum={0} maximum={3} step={1} onPropertyChangedObservable={this.props.onPropertyChangedObservable} decimalCount={0} />
  221. <OptionsLineComponent label="Mode" options={coordinatesMode} target={texture} propertyName="coordinatesMode" onPropertyChangedObservable={this.props.onPropertyChangedObservable} onSelect={(value) => texture.coordinatesMode = value} />
  222. <SliderLineComponent label="Level" target={texture} propertyName="level" minimum={0} maximum={2} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  223. {
  224. texture.updateSamplingMode &&
  225. <OptionsLineComponent label="Sampling" options={samplingMode} target={texture} noDirectUpdate={true} propertyName="samplingMode" onPropertyChangedObservable={this.props.onPropertyChangedObservable} onSelect={(value) => texture.updateSamplingMode(value)} />
  226. }
  227. </LineContainerComponent>
  228. {
  229. texture.getScene() &&
  230. <AnimationGridComponent globalState={this.props.globalState} animatable={texture} scene={texture.getScene()!} lockObject={this.props.lockObject} />
  231. }
  232. {
  233. (texture as any).rootContainer && this._adtInstrumentation &&
  234. <LineContainerComponent globalState={this.props.globalState} title="ADVANCED TEXTURE PROPERTIES">
  235. <ValueLineComponent label="Last layout time" value={this._adtInstrumentation!.renderTimeCounter.current} units="ms" />
  236. <ValueLineComponent label="Last render time" value={this._adtInstrumentation!.layoutTimeCounter.current} units="ms" />
  237. <SliderLineComponent label="Render scale" minimum={0.1} maximum={5} step={0.1} target={texture} propertyName="renderScale" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  238. <CheckBoxLineComponent label="Premultiply alpha" target={texture} propertyName="premulAlpha" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  239. <FloatLineComponent lockObject={this.props.lockObject} label="Ideal width" target={texture} propertyName="idealWidth" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  240. <FloatLineComponent lockObject={this.props.lockObject} label="Ideal height" target={texture} propertyName="idealHeight" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  241. <CheckBoxLineComponent label="Use smallest ideal" target={texture} propertyName="useSmallestIdeal" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  242. <CheckBoxLineComponent label="Render at ideal size" target={texture} propertyName="renderAtIdealSize" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  243. <CheckBoxLineComponent label="Invalidate Rect optimization" target={texture} propertyName="useInvalidateRectOptimization" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  244. </LineContainerComponent>
  245. }
  246. <LineContainerComponent globalState={this.props.globalState} title="TRANSFORM">
  247. {
  248. !texture.isCube &&
  249. <div>
  250. <FloatLineComponent lockObject={this.props.lockObject} label="U offset" target={texture} propertyName="uOffset" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  251. <FloatLineComponent lockObject={this.props.lockObject} label="V offset" target={texture} propertyName="vOffset" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  252. <FloatLineComponent lockObject={this.props.lockObject} label="U scale" target={texture} propertyName="uScale" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  253. <FloatLineComponent lockObject={this.props.lockObject} label="V scale" target={texture} propertyName="vScale" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  254. <FloatLineComponent lockObject={this.props.lockObject} label="U angle" useEuler={this.props.globalState.onlyUseEulers} target={texture} propertyName="uAng" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  255. <FloatLineComponent lockObject={this.props.lockObject} label="V angle" useEuler={this.props.globalState.onlyUseEulers} target={texture} propertyName="vAng" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  256. <FloatLineComponent lockObject={this.props.lockObject} label="W angle" useEuler={this.props.globalState.onlyUseEulers} target={texture} propertyName="wAng" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  257. <CheckBoxLineComponent label="Clamp U" isSelected={() => texture.wrapU === Texture.CLAMP_ADDRESSMODE} onSelect={(value) => texture.wrapU = value ? Texture.CLAMP_ADDRESSMODE : Texture.WRAP_ADDRESSMODE} />
  258. <CheckBoxLineComponent label="Clamp V" isSelected={() => texture.wrapV === Texture.CLAMP_ADDRESSMODE} onSelect={(value) => texture.wrapV = value ? Texture.CLAMP_ADDRESSMODE : Texture.WRAP_ADDRESSMODE} />
  259. </div>
  260. }
  261. {
  262. texture.isCube &&
  263. <div>
  264. <SliderLineComponent label="Rotation Y" useEuler={this.props.globalState.onlyUseEulers} minimum={0} maximum={2 * Math.PI} step={0.1} target={texture} propertyName="rotationY" />
  265. </div>
  266. }
  267. </LineContainerComponent>
  268. </div>
  269. );
  270. }
  271. }