texturePropertyGridComponent.tsx 17 KB

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