gltfComponent.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import * as React from "react";
  2. import { Scene } from "babylonjs/scene";
  3. import { LineContainerComponent } from "../../lineContainerComponent";
  4. import { CheckBoxLineComponent } from "../../lines/checkBoxLineComponent";
  5. import { GlobalState } from "../../../globalState";
  6. import { FloatLineComponent } from "../../lines/floatLineComponent";
  7. import { OptionsLineComponent } from "../../lines/optionsLineComponent";
  8. import { MessageLineComponent } from "../../lines/messageLineComponent";
  9. import { faCheck, faTimesCircle } from "@fortawesome/free-solid-svg-icons";
  10. import { TextLineComponent } from "../../lines/textLineComponent";
  11. import { GLTFLoaderCoordinateSystemMode, GLTFLoaderAnimationStartMode } from "babylonjs-loaders/glTF/index";
  12. import { Nullable } from "babylonjs/types";
  13. import { Observer } from "babylonjs/Misc/observable";
  14. import { IGLTFValidationResults } from "babylonjs-gltf2interface";
  15. interface IGLTFComponentProps {
  16. scene: Scene;
  17. globalState: GlobalState;
  18. }
  19. export class GLTFComponent extends React.Component<IGLTFComponentProps> {
  20. private _onValidationResultsUpdatedObserver: Nullable<Observer<Nullable<IGLTFValidationResults>>> = null;
  21. openValidationDetails() {
  22. const validationResults = this.props.globalState.validationResults;
  23. const win = window.open("", "_blank");
  24. if (win) {
  25. // TODO: format this better and use generator registry (https://github.com/KhronosGroup/glTF-Generator-Registry)
  26. win.document.title = "glTF Validation Results";
  27. win.document.body.innerText = JSON.stringify(validationResults, null, 2);
  28. win.document.body.style.whiteSpace = "pre";
  29. win.document.body.style.fontFamily = `monospace`;
  30. win.document.body.style.fontSize = `14px`;
  31. win.focus();
  32. }
  33. }
  34. prepareText(singularForm: string, count: number) {
  35. if (count) {
  36. return `${count} ${singularForm}s`;
  37. }
  38. return `${singularForm}`;
  39. }
  40. componentDidMount() {
  41. if (this.props.globalState) {
  42. this._onValidationResultsUpdatedObserver = this.props.globalState.onValidationResultsUpdatedObservable.add(() => {
  43. this.forceUpdate();
  44. });
  45. }
  46. }
  47. componentWillUnmount() {
  48. if (this.props.globalState) {
  49. if (this._onValidationResultsUpdatedObserver) {
  50. this.props.globalState.onValidationResultsUpdatedObservable.remove(this._onValidationResultsUpdatedObserver);
  51. }
  52. }
  53. }
  54. renderValidation() {
  55. const validationResults = this.props.globalState.validationResults;
  56. if (!validationResults) {
  57. return null;
  58. }
  59. const issues = validationResults.issues;
  60. return (
  61. <LineContainerComponent globalState={this.props.globalState} title="GLTF VALIDATION" closed={!issues.numErrors && !issues.numWarnings}>
  62. {issues.numErrors !== 0 && <MessageLineComponent text="Your file has some validation issues" icon={faTimesCircle} color="Red" />}
  63. {issues.numErrors === 0 && <MessageLineComponent text="Your file is a valid glTF file" icon={faCheck} color="Green" />}
  64. <TextLineComponent label="Errors" value={issues.numErrors.toString()} />
  65. <TextLineComponent label="Warnings" value={issues.numWarnings.toString()} />
  66. <TextLineComponent label="Infos" value={issues.numInfos.toString()} />
  67. <TextLineComponent label="Hints" value={issues.numHints.toString()} />
  68. <TextLineComponent label="More details" value="Click here" onLink={() => this.openValidationDetails()} />
  69. </LineContainerComponent>
  70. );
  71. }
  72. render() {
  73. const extensionStates = this.props.globalState.glTFLoaderExtensionDefaults;
  74. const loaderState = this.props.globalState.glTFLoaderDefaults;
  75. var animationStartMode =
  76. (typeof GLTFLoaderAnimationStartMode !== "undefined"
  77. ? [
  78. { label: "None", value: GLTFLoaderAnimationStartMode.NONE },
  79. { label: "First", value: GLTFLoaderAnimationStartMode.FIRST },
  80. { label: "ALL", value: GLTFLoaderAnimationStartMode.ALL },
  81. ]
  82. : [
  83. { label: "None", value: 0 },
  84. { label: "First", value: 1 },
  85. { label: "ALL", value: 2 },
  86. ]);
  87. var coordinateSystemMode =
  88. typeof GLTFLoaderCoordinateSystemMode !== "undefined"
  89. ? [
  90. { label: "Auto", value: GLTFLoaderCoordinateSystemMode.AUTO },
  91. { label: "Right handed", value: GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED },
  92. ]
  93. : [
  94. { label: "Auto", value: 0 },
  95. { label: "Right handed", value: 1 },
  96. ];
  97. return (
  98. <div>
  99. <LineContainerComponent globalState={this.props.globalState} title="GLTF LOADER" closed={true}>
  100. <OptionsLineComponent label="Animation start mode" options={animationStartMode} target={loaderState} propertyName="animationStartMode" />
  101. <CheckBoxLineComponent label="Capture performance counters" target={loaderState} propertyName="capturePerformanceCounters" />
  102. <CheckBoxLineComponent label="Compile materials" target={loaderState} propertyName="compileMaterials" />
  103. <CheckBoxLineComponent label="Compile shadow generators" target={loaderState} propertyName="compileShadowGenerators" />
  104. <OptionsLineComponent label="Coordinate system" options={coordinateSystemMode} target={loaderState} propertyName="coordinateSystemMode" />
  105. <CheckBoxLineComponent label="Enable logging" target={loaderState} propertyName="loggingEnabled" />
  106. <CheckBoxLineComponent label="Transparency as coverage" target={loaderState} propertyName="transparencyAsCoverage" />
  107. <CheckBoxLineComponent label="Use clip plane" target={loaderState} propertyName="useClipPlane" />
  108. <CheckBoxLineComponent label="Validate" target={loaderState} propertyName="validate" />
  109. <MessageLineComponent text="You need to reload your file to see these changes" />
  110. </LineContainerComponent>
  111. <LineContainerComponent globalState={this.props.globalState} title="GLTF EXTENSIONS" closed={true}>
  112. <CheckBoxLineComponent label="MSFT_lod" isSelected={() => extensionStates["MSFT_lod"].enabled} onSelect={(value) => (extensionStates["MSFT_lod"].enabled = value)} />
  113. <FloatLineComponent label="Maximum LODs" target={extensionStates["MSFT_lod"]} propertyName="maxLODsToLoad" additionalClass="gltf-extension-property" isInteger={true} />
  114. <CheckBoxLineComponent label="MSFT_minecraftMesh" isSelected={() => extensionStates["MSFT_minecraftMesh"].enabled} onSelect={(value) => (extensionStates["MSFT_minecraftMesh"].enabled = value)} />
  115. <CheckBoxLineComponent label="MSFT_sRGBFactors" isSelected={() => extensionStates["MSFT_sRGBFactors"].enabled} onSelect={(value) => (extensionStates["MSFT_sRGBFactors"].enabled = value)} />
  116. <CheckBoxLineComponent label="MSFT_audio_emitter" isSelected={() => extensionStates["MSFT_audio_emitter"].enabled} onSelect={(value) => (extensionStates["MSFT_audio_emitter"].enabled = value)} />
  117. <CheckBoxLineComponent label="KHR_xmp" isSelected={() => extensionStates["KHR_xmp"].enabled} onSelect={(value) => (extensionStates["KHR_xmp"].enabled = value)} />
  118. <CheckBoxLineComponent label="KHR_draco_mesh_compression" isSelected={() => extensionStates["KHR_draco_mesh_compression"].enabled} onSelect={(value) => (extensionStates["KHR_draco_mesh_compression"].enabled = value)} />
  119. <CheckBoxLineComponent label="KHR_mesh_quantization" isSelected={() => extensionStates["KHR_mesh_quantization"].enabled} onSelect={(value) => (extensionStates["KHR_mesh_quantization"].enabled = value)} />
  120. <CheckBoxLineComponent label="KHR_materials_pbrSpecularGloss..." isSelected={() => extensionStates["KHR_materials_pbrSpecularGlossiness"].enabled} onSelect={(value) => (extensionStates["KHR_materials_pbrSpecularGlossiness"].enabled = value)} />
  121. <CheckBoxLineComponent label="KHR_materials_clearcoat" isSelected={() => extensionStates["KHR_materials_clearcoat"].enabled} onSelect={(value) => (extensionStates["KHR_materials_clearcoat"].enabled = value)} />
  122. <CheckBoxLineComponent label="KHR_materials_ior" isSelected={() => extensionStates["KHR_materials_ior"].enabled} onSelect={(value) => (extensionStates["KHR_materials_ior"].enabled = value)} />
  123. <CheckBoxLineComponent label="KHR_materials_sheen" isSelected={() => extensionStates["KHR_materials_sheen"].enabled} onSelect={(value) => (extensionStates["KHR_materials_sheen"].enabled = value)} />
  124. <CheckBoxLineComponent label="KHR_materials_specular" isSelected={() => extensionStates["KHR_materials_specular"].enabled} onSelect={(value) => (extensionStates["KHR_materials_specular"].enabled = value)} />
  125. <CheckBoxLineComponent label="KHR_materials_unlit" isSelected={() => extensionStates["KHR_materials_unlit"].enabled} onSelect={(value) => (extensionStates["KHR_materials_unlit"].enabled = value)} />
  126. <CheckBoxLineComponent label="KHR_materials_variants" isSelected={() => extensionStates["KHR_materials_variants"].enabled} onSelect={(value) => (extensionStates["KHR_materials_variants"].enabled = value)} />
  127. <CheckBoxLineComponent label="KHR_materials_transmission" isSelected={() => extensionStates["KHR_materials_transmission"].enabled} onSelect={(value) => (extensionStates["KHR_materials_transmission"].enabled = value)} />
  128. <CheckBoxLineComponent label="KHR_lights_punctual" isSelected={() => extensionStates["KHR_lights_punctual"].enabled} onSelect={(value) => (extensionStates["KHR_lights_punctual"].enabled = value)} />
  129. <CheckBoxLineComponent label="KHR_texture_basisu" isSelected={() => extensionStates["KHR_texture_basisu"].enabled} onSelect={(value) => (extensionStates["KHR_texture_basisu"].enabled = value)} />
  130. <CheckBoxLineComponent label="KHR_texture_transform" isSelected={() => extensionStates["KHR_texture_transform"].enabled} onSelect={(value) => (extensionStates["KHR_texture_transform"].enabled = value)} />
  131. <CheckBoxLineComponent label="EXT_lights_image_based" isSelected={() => extensionStates["EXT_lights_image_based"].enabled} onSelect={(value) => (extensionStates["EXT_lights_image_based"].enabled = value)} />
  132. <CheckBoxLineComponent label="EXT_mesh_gpu_instancing" isSelected={() => extensionStates["EXT_mesh_gpu_instancing"].enabled} onSelect={(value) => (extensionStates["EXT_mesh_gpu_instancing"].enabled = value)} />
  133. <CheckBoxLineComponent label="EXT_texture_webp" isSelected={() => extensionStates["EXT_texture_webp"].enabled} onSelect={(value) => (extensionStates["EXT_texture_webp"].enabled = value)} />
  134. <MessageLineComponent text="You need to reload your file to see these changes" />
  135. </LineContainerComponent>
  136. {loaderState["validate"] && this.props.globalState.validationResults && this.renderValidation()}
  137. </div>
  138. );
  139. }
  140. }