|
| 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 | +}); |
0 commit comments