texturePropertyGridComponent.tsx 15 KB

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