texturePropertyGridComponent.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. interface ITexturePropertyGridComponentProps {
  26. texture: BaseTexture,
  27. lockObject: LockObject,
  28. globalState: GlobalState,
  29. onPropertyChangedObservable?: Observable<PropertyChangedEvent>
  30. }
  31. export class TexturePropertyGridComponent extends React.Component<ITexturePropertyGridComponentProps> {
  32. private _adtInstrumentation: Nullable<AdvancedDynamicTextureInstrumentation>;
  33. private textureLineRef: React.RefObject<TextureLineComponent>;
  34. constructor(props: ITexturePropertyGridComponentProps) {
  35. super(props);
  36. const texture = this.props.texture;
  37. if (!texture || !(texture as any).rootContainer) {
  38. return;
  39. }
  40. const adt = texture as AdvancedDynamicTexture;
  41. this._adtInstrumentation = new AdvancedDynamicTextureInstrumentation(adt);
  42. this._adtInstrumentation!.captureRenderTime = true;
  43. this._adtInstrumentation!.captureLayoutTime = true;
  44. }
  45. componentWillUnmount() {
  46. if (this._adtInstrumentation) {
  47. this._adtInstrumentation.dispose();
  48. this._adtInstrumentation = null;
  49. }
  50. }
  51. updateTexture(file: File) {
  52. const texture = this.props.texture;
  53. Tools.ReadFile(file, (data) => {
  54. var blob = new Blob([data], { type: "octet/stream" });
  55. var url = URL.createObjectURL(blob);
  56. if (texture.isCube) {
  57. let extension: string | undefined = undefined;
  58. if (file.name.toLowerCase().indexOf(".dds") > 0) {
  59. extension = ".dds";
  60. } else if (file.name.toLowerCase().indexOf(".env") > 0) {
  61. extension = ".env";
  62. }
  63. (texture as CubeTexture).updateURL(url, extension, () => this.foreceRefresh());
  64. } else {
  65. (texture as Texture).updateURL(url, null, () => this.foreceRefresh());
  66. }
  67. }, undefined, true);
  68. }
  69. foreceRefresh() {
  70. this.forceUpdate();
  71. (this.textureLineRef.current as TextureLineComponent).updatePreview();
  72. }
  73. render() {
  74. const texture = this.props.texture;
  75. var samplingMode = [
  76. { label: "Nearest", value: Texture.NEAREST_NEAREST },
  77. { label: "Nearest & linear mip", value: Texture.NEAREST_LINEAR },
  78. { label: "Linear", value: Texture.LINEAR_LINEAR_MIPLINEAR },
  79. ];
  80. var coordinatesMode = [
  81. { label: "Explicit", value: Texture.EXPLICIT_MODE },
  82. { label: "Cubic", value: Texture.CUBIC_MODE },
  83. { label: "Inverse cubic", value: Texture.INVCUBIC_MODE },
  84. { label: "Equirectangular", value: Texture.EQUIRECTANGULAR_MODE },
  85. { label: "Fixed equirectangular", value: Texture.FIXED_EQUIRECTANGULAR_MODE },
  86. { label: "Fixed equirectangular mirrored", value: Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE },
  87. { label: "Planar", value: Texture.PLANAR_MODE },
  88. { label: "Projection", value: Texture.PROJECTION_MODE },
  89. { label: "Skybox", value: Texture.SKYBOX_MODE },
  90. { label: "Spherical", value: Texture.SPHERICAL_MODE },
  91. ];
  92. let extension = "";
  93. let url = (texture as Texture).url;
  94. if (url) {
  95. for (var index = url.length - 1; index >= 0; index--) {
  96. if (url[index] === ".") {
  97. break;
  98. }
  99. extension = url[index] + extension;
  100. }
  101. } else {
  102. url = "";
  103. }
  104. let textureUrl = (url.substring(0, 4) === "data" || url.substring(0, 4) === "blob") ? "" : url;
  105. return (
  106. <div className="pane">
  107. <LineContainerComponent globalState={this.props.globalState} title="PREVIEW">
  108. <TextureLineComponent ref={this.textureLineRef} texture={texture} width={256} height={256} globalState={this.props.globalState} />
  109. <FileButtonLineComponent label="Load texture from file" onClick={(file) => this.updateTexture(file)} accept=".jpg, .png, .tga, .dds, .env" />
  110. <TextInputLineComponent label="URL" value={textureUrl} lockObject={this.props.lockObject} onChange={url => {
  111. (texture as Texture).updateURL(url);
  112. this.foreceRefresh();
  113. }} />
  114. </LineContainerComponent>
  115. <CustomPropertyGridComponent globalState={this.props.globalState} target={texture}
  116. lockObject={this.props.lockObject}
  117. onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  118. <LineContainerComponent globalState={this.props.globalState} title="GENERAL">
  119. <TextLineComponent label="Width" value={texture.getSize().width.toString()} />
  120. <TextLineComponent label="Height" value={texture.getSize().height.toString()} />
  121. {
  122. texture.isRenderTarget &&
  123. <ButtonLineComponent label="Scale up" onClick={() => {
  124. let scene = texture.getScene()!;
  125. texture.scale(2);
  126. setTimeout(() => {
  127. this.props.globalState.onSelectionChangedObservable.notifyObservers(scene.getTextureByUniqueID(texture.uniqueId));
  128. });
  129. }} />
  130. }
  131. {
  132. texture.isRenderTarget &&
  133. <ButtonLineComponent label="Scale down" onClick={() => {
  134. let scene = texture.getScene()!;
  135. texture.scale(0.5);
  136. setTimeout(() => {
  137. this.props.globalState.onSelectionChangedObservable.notifyObservers(scene.getTextureByUniqueID(texture.uniqueId));
  138. });
  139. }} />
  140. }
  141. {
  142. extension &&
  143. <TextLineComponent label="File format" value={extension} />
  144. }
  145. <TextLineComponent label="Unique ID" value={texture.uniqueId.toString()} />
  146. <TextLineComponent label="Class" value={texture.getClassName()} />
  147. <TextLineComponent label="Has alpha" value={texture.hasAlpha ? "Yes" : "No"} />
  148. <TextLineComponent label="Is 3D" value={texture.is3D ? "Yes" : "No"} />
  149. <TextLineComponent label="Is 2D array" value={texture.is2DArray ? "Yes" : "No"} />
  150. <TextLineComponent label="Is cube" value={texture.isCube ? "Yes" : "No"} />
  151. <TextLineComponent label="Is render target" value={texture.isRenderTarget ? "Yes" : "No"} />
  152. {
  153. (texture instanceof Texture) &&
  154. <TextLineComponent label="Stored as inverted on Y" value={texture.invertY ? "Yes" : "No"} />
  155. }
  156. <TextLineComponent label="Has mipmaps" value={!texture.noMipmap ? "Yes" : "No"} />
  157. <SliderLineComponent label="UV set" target={texture} propertyName="coordinatesIndex" minimum={0} maximum={3} step={1} onPropertyChangedObservable={this.props.onPropertyChangedObservable} decimalCount={0} />
  158. <OptionsLineComponent label="Mode" options={coordinatesMode} target={texture} propertyName="coordinatesMode" onPropertyChangedObservable={this.props.onPropertyChangedObservable} onSelect={(value) => texture.updateSamplingMode(value)} />
  159. <SliderLineComponent label="Level" target={texture} propertyName="level" minimum={0} maximum={2} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  160. {
  161. texture.updateSamplingMode &&
  162. <OptionsLineComponent label="Sampling" options={samplingMode} target={texture} noDirectUpdate={true} propertyName="samplingMode" onPropertyChangedObservable={this.props.onPropertyChangedObservable} onSelect={(value) => texture.updateSamplingMode(value)} />
  163. }
  164. </LineContainerComponent>
  165. {
  166. (texture as any).rootContainer &&
  167. <LineContainerComponent globalState={this.props.globalState} title="ADVANCED TEXTURE PROPERTIES">
  168. <ValueLineComponent label="Last layout time" value={this._adtInstrumentation!.renderTimeCounter.current} units="ms" />
  169. <ValueLineComponent label="Last render time" value={this._adtInstrumentation!.layoutTimeCounter.current} units="ms" />
  170. <SliderLineComponent label="Render scale" minimum={0.1} maximum={5} step={0.1} target={texture} propertyName="renderScale" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  171. <CheckBoxLineComponent label="Premultiply alpha" target={texture} propertyName="premulAlpha" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  172. <FloatLineComponent lockObject={this.props.lockObject} label="Ideal width" target={texture} propertyName="idealWidth" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  173. <FloatLineComponent lockObject={this.props.lockObject} label="Ideal height" target={texture} propertyName="idealHeight" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  174. <CheckBoxLineComponent label="Use smallest ideal" target={texture} propertyName="useSmallestIdeal" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  175. <CheckBoxLineComponent label="Render at ideal size" target={texture} propertyName="renderAtIdealSize" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  176. <CheckBoxLineComponent label="Invalidate Rect optimization" target={texture} propertyName="useInvalidateRectOptimization" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  177. </LineContainerComponent>
  178. }
  179. <LineContainerComponent globalState={this.props.globalState} title="TRANSFORM">
  180. {
  181. !texture.isCube &&
  182. <div>
  183. <FloatLineComponent lockObject={this.props.lockObject} label="U offset" target={texture} propertyName="uOffset" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  184. <FloatLineComponent lockObject={this.props.lockObject} label="V offset" target={texture} propertyName="vOffset" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  185. <FloatLineComponent lockObject={this.props.lockObject} label="U scale" target={texture} propertyName="uScale" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  186. <FloatLineComponent lockObject={this.props.lockObject} label="V scale" target={texture} propertyName="vScale" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  187. <FloatLineComponent lockObject={this.props.lockObject} label="U angle" useEuler={this.props.globalState.onlyUseEulers} target={texture} propertyName="uAng" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  188. <FloatLineComponent lockObject={this.props.lockObject} label="V angle" useEuler={this.props.globalState.onlyUseEulers} target={texture} propertyName="vAng" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  189. <FloatLineComponent lockObject={this.props.lockObject} label="W angle" useEuler={this.props.globalState.onlyUseEulers} target={texture} propertyName="wAng" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  190. <CheckBoxLineComponent label="Clamp U" isSelected={() => texture.wrapU === Texture.CLAMP_ADDRESSMODE} onSelect={(value) => texture.wrapU = value ? Texture.CLAMP_ADDRESSMODE : Texture.WRAP_ADDRESSMODE} />
  191. <CheckBoxLineComponent label="Clamp V" isSelected={() => texture.wrapV === Texture.CLAMP_ADDRESSMODE} onSelect={(value) => texture.wrapV = value ? Texture.CLAMP_ADDRESSMODE : Texture.WRAP_ADDRESSMODE} />
  192. </div>
  193. }
  194. {
  195. texture.isCube &&
  196. <div>
  197. <SliderLineComponent label="Rotation Y" useEuler={this.props.globalState.onlyUseEulers} minimum={0} maximum={2 * Math.PI} step={0.1} target={texture} propertyName="rotationY" />
  198. </div>
  199. }
  200. </LineContainerComponent>
  201. </div>
  202. );
  203. }
  204. }