Skip to content

Commit c9df5bd

Browse files
alexchuberryantrem
andauthored
Inspector V2: Tools Pane Init (#16798)
Partial Tools pane implementation. Have left TODO comments for missing sections. The idea is that the components of the Tools pane are lumped into three extensible categories, which can be installed via the Extensions Manager: Export, Capture, and Import tools. Images: <img width="974" height="686" alt="image" src="https://github.com/user-attachments/assets/c9b9c23f-6ef4-407a-bb89-7c3e531cbb39" /> <img width="572" height="1377" alt="image" src="https://github.com/user-attachments/assets/21d424a2-0759-42d7-9d87-9222199bbdd9" /> --------- Co-authored-by: Ryan Tremblay <ryan.tremblay@microsoft.com>
1 parent 5f0612d commit c9df5bd

File tree

13 files changed

+601
-19
lines changed

13 files changed

+601
-19
lines changed

packages/dev/inspector-v2/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@dev/gui": "^1.0.0",
2222
"@dev/loaders": "1.0.0",
2323
"@dev/materials": "^1.0.0",
24+
"@dev/serializers": "1.0.0",
2425
"@fluentui/react-components": "^9.62.0",
2526
"@fluentui/react-icons": "^2.0.271",
2627
"@pmmmwh/react-refresh-webpack-plugin": "^0.6.0",
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { ButtonLine } from "shared-ui-components/fluent/hoc/buttonLine";
2+
import { useState, useRef, useCallback } from "react";
3+
import type { FunctionComponent } from "react";
4+
import { Tools } from "core/Misc/tools";
5+
import type { Scene } from "core/scene";
6+
import { SyncedSliderPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/syncedSliderPropertyLine";
7+
import type { IScreenshotSize } from "core/Misc/interfaces/screenshotSize";
8+
import { SwitchPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/switchPropertyLine";
9+
import { VideoRecorder } from "core/Misc/videoRecorder";
10+
import { captureEquirectangularFromScene } from "core/Misc/equirectangularCapture";
11+
import { Collapse } from "shared-ui-components/fluent/primitives/collapse";
12+
import { CameraRegular, RecordRegular, RecordStopRegular } from "@fluentui/react-icons";
13+
14+
export const CaptureRttTools: FunctionComponent<{ scene: Scene }> = ({ scene }) => {
15+
const [useWidthHeight, setUseWidthHeight] = useState(false);
16+
const [screenshotSize, setScreenshotSize] = useState<IScreenshotSize>({ precision: 1 });
17+
18+
const captureRender = useCallback(async () => {
19+
const sizeToUse: IScreenshotSize = { ...screenshotSize };
20+
if (!useWidthHeight) {
21+
sizeToUse.width = undefined;
22+
sizeToUse.height = undefined;
23+
}
24+
25+
if (scene.activeCamera) {
26+
Tools.CreateScreenshotUsingRenderTarget(scene.getEngine(), scene.activeCamera, sizeToUse, undefined, undefined, 4);
27+
}
28+
}, [scene, screenshotSize, useWidthHeight]);
29+
30+
return (
31+
<>
32+
<ButtonLine label="Capture" icon={CameraRegular} onClick={captureRender} />
33+
<SyncedSliderPropertyLine
34+
label="Precision"
35+
value={screenshotSize.precision ?? 1}
36+
onChange={(value) => setScreenshotSize({ ...screenshotSize, precision: value ?? 1 })}
37+
min={0.1}
38+
max={10}
39+
step={0.1}
40+
/>
41+
<SwitchPropertyLine label="Use Custom Width/Height" value={useWidthHeight} onChange={(value) => setUseWidthHeight(value)} />
42+
<Collapse visible={useWidthHeight}>
43+
<SyncedSliderPropertyLine
44+
label="Width"
45+
value={screenshotSize.width ?? 512}
46+
onChange={(data) => setScreenshotSize({ ...screenshotSize, width: data ?? 512 })}
47+
min={1}
48+
step={1}
49+
/>
50+
<SyncedSliderPropertyLine
51+
label="Height"
52+
value={screenshotSize.height ?? 512}
53+
onChange={(data) => setScreenshotSize({ ...screenshotSize, height: data ?? 512 })}
54+
min={1}
55+
step={1}
56+
/>
57+
</Collapse>
58+
</>
59+
);
60+
};
61+
62+
export const CaptureScreenshotTools: FunctionComponent<{ scene: Scene }> = ({ scene }) => {
63+
const [isRecording, setIsRecording] = useState(false);
64+
const videoRecorder = useRef<VideoRecorder>();
65+
66+
const captureScreenshot = useCallback(() => {
67+
if (scene.activeCamera) {
68+
Tools.CreateScreenshot(scene.getEngine(), scene.activeCamera, { precision: 1 });
69+
}
70+
}, [scene]);
71+
72+
const captureEquirectangularAsync = useCallback(async () => {
73+
if (scene.activeCamera) {
74+
await captureEquirectangularFromScene(scene, { size: 1024, filename: "equirectangular_capture.png" });
75+
}
76+
}, [scene]);
77+
78+
const recordVideoAsync = useCallback(async () => {
79+
if (videoRecorder.current && videoRecorder.current.isRecording) {
80+
videoRecorder.current.stopRecording();
81+
setIsRecording(false);
82+
return;
83+
}
84+
85+
if (!videoRecorder.current) {
86+
videoRecorder.current = new VideoRecorder(scene.getEngine());
87+
}
88+
89+
void videoRecorder.current.startRecording();
90+
setIsRecording(true);
91+
}, [scene]);
92+
93+
return (
94+
<>
95+
<ButtonLine label="Capture" icon={CameraRegular} onClick={captureScreenshot} />
96+
<ButtonLine label="Capture Equirectangular" icon={CameraRegular} onClick={captureEquirectangularAsync} />
97+
<ButtonLine label={isRecording ? "Stop Recording" : "Record Video"} icon={isRecording ? RecordStopRegular : RecordRegular} onClick={recordVideoAsync} />
98+
</>
99+
);
100+
};
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
import { NumberDropdownPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/dropdownPropertyLine";
2+
import { SceneSerializer } from "core/Misc/sceneSerializer";
3+
import { Tools } from "core/Misc/tools";
4+
import { EnvironmentTextureTools } from "core/Misc/environmentTextureTools";
5+
import type { CubeTexture } from "core/Materials/Textures/cubeTexture";
6+
import { Logger } from "core/Misc/logger";
7+
import { useCallback, useState } from "react";
8+
import type { FunctionComponent } from "react";
9+
import type { Scene } from "core/scene";
10+
import { ButtonLine } from "shared-ui-components/fluent/hoc/buttonLine";
11+
import { SyncedSliderPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/syncedSliderPropertyLine";
12+
import type { Node } from "core/node";
13+
import { Mesh } from "core/Meshes/mesh";
14+
import type { PBRMaterial } from "core/Materials/PBR/pbrMaterial";
15+
import type { StandardMaterial } from "core/Materials/standardMaterial";
16+
import type { BackgroundMaterial } from "core/Materials/Background/backgroundMaterial";
17+
import { Texture } from "core/Materials/Textures/texture";
18+
import { Camera } from "core/Cameras/camera";
19+
import { Light } from "core/Lights/light";
20+
import { SwitchPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/switchPropertyLine";
21+
22+
import { MakeLazyComponent } from "shared-ui-components/fluent/primitives/lazyComponent";
23+
import { Collapse } from "shared-ui-components/fluent/primitives/collapse";
24+
import { ArrowDownloadRegular } from "@fluentui/react-icons";
25+
26+
const EnvExportImageTypes = [
27+
{ label: "PNG", value: 0, imageType: "image/png" },
28+
{ label: "WebP", value: 1, imageType: "image/webp" },
29+
] as const;
30+
31+
interface IBabylonExportOptionsState {
32+
imageTypeIndex: number;
33+
imageQuality: number;
34+
iblDiffuse: boolean;
35+
}
36+
37+
export const ExportBabylonTools: FunctionComponent<{ scene: Scene }> = ({ scene }) => {
38+
const [babylonExportOptions, setBabylonExportOptions] = useState<Readonly<IBabylonExportOptionsState>>({
39+
imageTypeIndex: 0,
40+
imageQuality: 0.8,
41+
iblDiffuse: false,
42+
});
43+
44+
const exportBabylon = useCallback(async () => {
45+
const strScene = JSON.stringify(SceneSerializer.Serialize(scene));
46+
const blob = new Blob([strScene], { type: "octet/stream" });
47+
Tools.Download(blob, "scene.babylon");
48+
}, [scene]);
49+
50+
const createEnvTexture = useCallback(async () => {
51+
if (!scene.environmentTexture) {
52+
return;
53+
}
54+
55+
try {
56+
const buffer = await EnvironmentTextureTools.CreateEnvTextureAsync(scene.environmentTexture as CubeTexture, {
57+
imageType: EnvExportImageTypes[babylonExportOptions.imageTypeIndex].imageType,
58+
imageQuality: babylonExportOptions.imageQuality,
59+
disableIrradianceTexture: !babylonExportOptions.iblDiffuse,
60+
});
61+
const blob = new Blob([buffer], { type: "octet/stream" });
62+
Tools.Download(blob, "environment.env");
63+
} catch (error: any) {
64+
Logger.Error(error);
65+
alert(error);
66+
}
67+
}, [scene, babylonExportOptions]);
68+
69+
return (
70+
<>
71+
<ButtonLine label="Export to Babylon" icon={ArrowDownloadRegular} onClick={exportBabylon} />
72+
{!scene.getEngine().premultipliedAlpha && scene.environmentTexture && scene.environmentTexture._prefiltered && scene.activeCamera && (
73+
<>
74+
<ButtonLine label="Generate .env texture" icon={ArrowDownloadRegular} onClick={createEnvTexture} />
75+
{scene.environmentTexture.irradianceTexture && (
76+
<SwitchPropertyLine
77+
key="iblDiffuse"
78+
label="Diffuse Texture"
79+
description="Export diffuse texture for IBL"
80+
value={babylonExportOptions.iblDiffuse}
81+
onChange={(value: boolean) => {
82+
setBabylonExportOptions((prev) => ({ ...prev, iblDiffuse: value }));
83+
}}
84+
/>
85+
)}
86+
<NumberDropdownPropertyLine
87+
label="Image type"
88+
options={EnvExportImageTypes}
89+
value={babylonExportOptions.imageTypeIndex}
90+
onChange={(val) => {
91+
setBabylonExportOptions((prev) => ({ ...prev, imageTypeIndex: val as number }));
92+
}}
93+
/>
94+
<Collapse visible={babylonExportOptions.imageTypeIndex > 0}>
95+
<SyncedSliderPropertyLine
96+
label="Quality"
97+
value={babylonExportOptions.imageQuality}
98+
onChange={(value) => setBabylonExportOptions((prev) => ({ ...prev, imageQuality: value }))}
99+
min={0}
100+
max={1}
101+
/>
102+
</Collapse>
103+
</>
104+
)}
105+
</>
106+
);
107+
};
108+
109+
interface IGltfExportOptionsState {
110+
exportDisabledNodes: boolean;
111+
exportSkyboxes: boolean;
112+
exportCameras: boolean;
113+
exportLights: boolean;
114+
}
115+
116+
export const ExportGltfTools = MakeLazyComponent(async () => {
117+
// Defer importing anything from the serializers package until this component is actually mounted.
118+
const { GLTF2Export } = await import("serializers/glTF/2.0/glTFSerializer");
119+
120+
return (props: { scene: Scene }) => {
121+
const [isExportingGltf, setIsExportingGltf] = useState(false);
122+
const [gltfExportOptions, setGltfExportOptions] = useState<Readonly<IGltfExportOptionsState>>({
123+
exportDisabledNodes: false,
124+
exportSkyboxes: false,
125+
exportCameras: false,
126+
exportLights: false,
127+
});
128+
129+
const exportGLTF = useCallback(async () => {
130+
setIsExportingGltf(true);
131+
132+
const shouldExport = (node: Node): boolean => {
133+
if (!gltfExportOptions.exportDisabledNodes) {
134+
if (!node.isEnabled()) {
135+
return false;
136+
}
137+
}
138+
139+
if (!gltfExportOptions.exportSkyboxes) {
140+
if (node instanceof Mesh) {
141+
if (node.material) {
142+
const material = node.material as PBRMaterial | StandardMaterial | BackgroundMaterial;
143+
const reflectionTexture = material.reflectionTexture;
144+
if (reflectionTexture && reflectionTexture.coordinatesMode === Texture.SKYBOX_MODE) {
145+
return false;
146+
}
147+
}
148+
}
149+
}
150+
151+
if (!gltfExportOptions.exportCameras) {
152+
if (node instanceof Camera) {
153+
return false;
154+
}
155+
}
156+
157+
if (!gltfExportOptions.exportLights) {
158+
if (node instanceof Light) {
159+
return false;
160+
}
161+
}
162+
163+
return true;
164+
};
165+
166+
try {
167+
const glb = await GLTF2Export.GLBAsync(props.scene, "scene", { shouldExportNode: (node) => shouldExport(node) });
168+
glb.downloadFiles();
169+
} catch (reason) {
170+
Logger.Error(`Failed to export GLB: ${reason}`);
171+
} finally {
172+
setIsExportingGltf(false);
173+
}
174+
}, [gltfExportOptions, props.scene]);
175+
176+
return (
177+
<>
178+
<SwitchPropertyLine
179+
key="GLTFExportDisabledNodes"
180+
label="Export Disabled Nodes"
181+
description="Whether to export nodes that are disabled in the scene."
182+
value={gltfExportOptions.exportDisabledNodes}
183+
onChange={(checked: boolean) => setGltfExportOptions({ ...gltfExportOptions, exportDisabledNodes: checked })}
184+
/>
185+
<SwitchPropertyLine
186+
key="GLTFExportSkyboxes"
187+
label="Export Skyboxes"
188+
description="Whether to export skybox nodes in the scene."
189+
value={gltfExportOptions.exportSkyboxes}
190+
onChange={(checked: boolean) => setGltfExportOptions({ ...gltfExportOptions, exportSkyboxes: checked })}
191+
/>
192+
<SwitchPropertyLine
193+
key="GLTFExportCameras"
194+
label="Export Cameras"
195+
description="Whether to export cameras in the scene."
196+
value={gltfExportOptions.exportCameras}
197+
onChange={(checked: boolean) => setGltfExportOptions({ ...gltfExportOptions, exportCameras: checked })}
198+
/>
199+
<SwitchPropertyLine
200+
key="GLTFExportLights"
201+
label="Export Lights"
202+
description="Whether to export lights in the scene."
203+
value={gltfExportOptions.exportLights}
204+
onChange={(checked: boolean) => setGltfExportOptions({ ...gltfExportOptions, exportLights: checked })}
205+
/>
206+
<ButtonLine label="Export to GLB" icon={ArrowDownloadRegular} onClick={exportGLTF} disabled={isExportingGltf} />
207+
</>
208+
);
209+
};
210+
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { useState } from "react";
2+
import type { FunctionComponent } from "react";
3+
import type { Scene } from "core/scene";
4+
import type { DropdownOption } from "shared-ui-components/fluent/primitives/dropdown";
5+
import { ImportAnimationsAsync, SceneLoaderAnimationGroupLoadingMode } from "core/Loading/sceneLoader";
6+
import { FilesInput } from "core/Misc/filesInput";
7+
import { Logger } from "core/Misc";
8+
import { SwitchPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/switchPropertyLine";
9+
import { NumberDropdownPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/dropdownPropertyLine";
10+
import { FileUploadLine } from "shared-ui-components/fluent/hoc/fileUploadLine";
11+
import { Collapse } from "shared-ui-components/fluent/primitives/collapse";
12+
13+
const AnimationGroupLoadingModes = [
14+
{ label: "Clean", value: SceneLoaderAnimationGroupLoadingMode.Clean },
15+
{ label: "Stop", value: SceneLoaderAnimationGroupLoadingMode.Stop },
16+
{ label: "Sync", value: SceneLoaderAnimationGroupLoadingMode.Sync },
17+
{ label: "NoSync", value: SceneLoaderAnimationGroupLoadingMode.NoSync },
18+
] as const satisfies DropdownOption<number>[];
19+
20+
export const ImportAnimationsTools: FunctionComponent<{ scene: Scene }> = ({ scene }) => {
21+
const [importDefaults, setImportDefaults] = useState({
22+
overwriteAnimations: true,
23+
animationGroupLoadingMode: SceneLoaderAnimationGroupLoadingMode.Clean,
24+
});
25+
26+
const importAnimations = (event: FileList) => {
27+
const reloadAsync = async function (sceneFile: File) {
28+
if (sceneFile) {
29+
try {
30+
await ImportAnimationsAsync(sceneFile, scene, {
31+
overwriteAnimations: importDefaults.overwriteAnimations,
32+
animationGroupLoadingMode: importDefaults.animationGroupLoadingMode,
33+
});
34+
35+
if (scene.animationGroups.length > 0) {
36+
const currentGroup = scene.animationGroups[0];
37+
currentGroup.play(true);
38+
}
39+
} catch (error) {
40+
Logger.Error(`Error importing animations: ${error}`);
41+
}
42+
}
43+
};
44+
45+
const filesInputAnimation = new FilesInput(scene.getEngine(), scene, null, null, null, null, null, reloadAsync, null);
46+
filesInputAnimation.loadFiles(event);
47+
filesInputAnimation.dispose();
48+
};
49+
50+
return (
51+
<>
52+
<FileUploadLine label="Import Animations" accept="gltf" onClick={(evt: FileList) => importAnimations(evt)} />
53+
<SwitchPropertyLine
54+
label="Overwrite Animations"
55+
value={importDefaults.overwriteAnimations}
56+
onChange={(value) => {
57+
setImportDefaults({ ...importDefaults, overwriteAnimations: value });
58+
}}
59+
/>
60+
<Collapse visible={!importDefaults.overwriteAnimations}>
61+
<NumberDropdownPropertyLine
62+
label="Animation Merge Mode"
63+
options={AnimationGroupLoadingModes}
64+
value={importDefaults.animationGroupLoadingMode}
65+
onChange={(value) => {
66+
setImportDefaults({ ...importDefaults, animationGroupLoadingMode: value });
67+
}}
68+
/>
69+
</Collapse>
70+
</>
71+
);
72+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ExtensibleAccordion } from "../extensibleAccordion";
2+
import type { Scene } from "core/scene";
3+
4+
export const ToolsPane: typeof ExtensibleAccordion<Scene> = (props) => {
5+
return <ExtensibleAccordion {...props} />;
6+
};

0 commit comments

Comments
 (0)