-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[Fluent/InspectorV2] UploadTexture and ChooseTexture shared components #17557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
georginahalpern
merged 15 commits into
BabylonJS:master
from
georginahalpern:textureLink
Dec 16, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
37aac9e
textureuploadline
6ce7882
texture upload
0cbf1e6
chooseTexture
70876bd
cleanup
e00ed53
use refaactored textureupload
743b6e0
todo comment
3403224
comment
407a864
build errors
09f5ed0
lint
a877d12
more linting
efb3649
pr comments
9840404
generic typing
57f8d1e
merge w main
7b6b89c
lint
6d99fd6
add comboboxpropertyline
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/dev/inspector-v2/src/components/properties/textures/boundTextureProperty.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { ChooseTexturePropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/chooseTexturePropertyLine"; | ||
| import { useProperty } from "../../../hooks/compoundPropertyHooks"; | ||
| import { usePropertyChangedNotifier } from "../../../contexts/propertyContext"; | ||
| import type { Scene } from "core/scene"; | ||
| import type { BaseTexture } from "core/Materials/Textures/baseTexture"; | ||
| import type { Nullable } from "core/types"; | ||
|
|
||
| /** | ||
| * Type alias for objects with texture properties | ||
| */ | ||
| type TextureHolder<K extends string> = Record<K, Nullable<BaseTexture>>; | ||
|
|
||
| /** | ||
| * Props for BoundTextureProperty | ||
| */ | ||
| type BoundTexturePropertyProps<K extends string> = { | ||
| label: string; | ||
| target: TextureHolder<K>; | ||
| propertyKey: K; | ||
| scene: Scene; | ||
| cubeOnly?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Helper to bind texture properties without needing defaultValue | ||
| * @param props - The required properties | ||
| * @returns ChooseTexturePropertyLine component | ||
| */ | ||
| export function BoundTextureProperty<K extends string>(props: BoundTexturePropertyProps<K>) { | ||
| const { label, target, propertyKey, scene, cubeOnly } = props; | ||
| const value = useProperty(target, propertyKey); | ||
| const notifyPropertyChanged = usePropertyChangedNotifier(); | ||
|
|
||
| return ( | ||
| <ChooseTexturePropertyLine | ||
| label={label} | ||
| value={value} | ||
| onChange={(texture) => { | ||
| const oldValue = target[propertyKey]; | ||
| target[propertyKey] = texture; | ||
| notifyPropertyChanged(target, propertyKey, oldValue, texture); | ||
| }} | ||
| scene={scene} | ||
| cubeOnly={cubeOnly} | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 12 additions & 22 deletions
34
packages/dev/sharedUiComponents/src/fluent/hoc/fileUploadLine.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,25 @@ | ||
| import { useRef } from "react"; | ||
| import type { FunctionComponent } from "react"; | ||
| import { ButtonLine } from "./buttonLine"; | ||
| import { LineContainer } from "./propertyLines/propertyLine"; | ||
| import { UploadButton } from "../primitives/uploadButton"; | ||
| import type { ButtonProps } from "../primitives/button"; | ||
| import { ArrowUploadRegular } from "@fluentui/react-icons"; | ||
|
|
||
| type FileUploadLineProps = Omit<ButtonProps, "onClick" | "label"> & { | ||
| onClick: (files: FileList) => void; | ||
| label: string; // Require a label when button is the entire line (by default, label is optional on a button) | ||
| label: string; // Require a label when button is the entire line (by default, label is optional on an UploadButton | ||
| accept: string; | ||
| }; | ||
|
|
||
| export const FileUploadLine: FunctionComponent<FileUploadLineProps> = (props) => { | ||
| /** | ||
| * A full-width line with an upload button. | ||
| * For just the button without the line wrapper, use UploadButton directly. | ||
| * @returns An UploadButton wrapped in a LineContainer | ||
| */ | ||
| export const FileUploadLine: FunctionComponent<FileUploadLineProps> = ({ onClick, label, accept, ...buttonProps }) => { | ||
| FileUploadLine.displayName = "FileUploadLine"; | ||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| const handleButtonClick = () => { | ||
| inputRef.current?.click(); | ||
| }; | ||
|
|
||
| const handleChange = (evt: React.ChangeEvent<HTMLInputElement>) => { | ||
| const files = evt.target.files; | ||
| if (files && files.length) { | ||
| props.onClick(files); | ||
| } | ||
| evt.target.value = ""; | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <ButtonLine onClick={handleButtonClick} icon={ArrowUploadRegular} label={props.label}></ButtonLine> | ||
| <input ref={inputRef} type="file" accept={props.accept} style={{ display: "none" }} onChange={handleChange} /> | ||
| </> | ||
| <LineContainer> | ||
| <UploadButton onUpload={onClick} accept={accept} label={label} {...buttonProps} /> | ||
| </LineContainer> | ||
| ); | ||
| }; |
26 changes: 26 additions & 0 deletions
26
packages/dev/sharedUiComponents/src/fluent/hoc/propertyLines/chooseTexturePropertyLine.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import type { FunctionComponent } from "react"; | ||
| import type { BaseTexture } from "core/Materials/Textures/baseTexture"; | ||
| import type { Nullable } from "core/types"; | ||
| import type { PropertyLineProps } from "./propertyLine"; | ||
| import type { ChooseTextureProps } from "../../primitives/chooseTexture"; | ||
|
|
||
| import { PropertyLine } from "./propertyLine"; | ||
| import { ChooseTexture } from "../../primitives/chooseTexture"; | ||
|
|
||
| type ChooseTexturePropertyLineProps = PropertyLineProps<Nullable<BaseTexture>> & ChooseTextureProps; | ||
|
|
||
| /** | ||
| * A property line with a ComboBox for selecting from existing scene textures | ||
| * and a button for uploading new texture files. | ||
| * @param props - ChooseTextureProps & PropertyLineProps | ||
| * @returns property-line wrapped ChooseTexture component | ||
| */ | ||
| export const ChooseTexturePropertyLine: FunctionComponent<ChooseTexturePropertyLineProps> = (props) => { | ||
| ChooseTexturePropertyLine.displayName = "ChooseTexturePropertyLine"; | ||
|
|
||
| return ( | ||
| <PropertyLine {...props}> | ||
| <ChooseTexture {...props} /> | ||
| </PropertyLine> | ||
| ); | ||
| }; |
23 changes: 23 additions & 0 deletions
23
packages/dev/sharedUiComponents/src/fluent/hoc/propertyLines/comboBoxPropertyLine.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { FunctionComponent } from "react"; | ||
| import type { PropertyLineProps } from "./propertyLine"; | ||
| import type { ComboBoxProps } from "../../primitives/comboBox"; | ||
|
|
||
| import { PropertyLine } from "./propertyLine"; | ||
| import { ComboBox } from "../../primitives/comboBox"; | ||
|
|
||
| type ComboBoxPropertyLineProps = ComboBoxProps & PropertyLineProps<string>; | ||
|
|
||
| /** | ||
| * A property line with a filterable ComboBox | ||
| * @param props - ComboBoxProps & PropertyLineProps | ||
| * @returns property-line wrapped ComboBox component | ||
| */ | ||
| export const ComboBoxPropertyLine: FunctionComponent<ComboBoxPropertyLineProps> = (props) => { | ||
| ComboBoxPropertyLine.displayName = "ComboBoxPropertyLine"; | ||
|
|
||
| return ( | ||
| <PropertyLine {...props}> | ||
| <ComboBox {...props} /> | ||
| </PropertyLine> | ||
| ); | ||
| }; |
109 changes: 109 additions & 0 deletions
109
packages/dev/sharedUiComponents/src/fluent/hoc/textureUpload.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import type { FunctionComponent } from "react"; | ||
| import { useCallback } from "react"; | ||
| import type { BaseTexture } from "core/Materials/Textures/baseTexture"; | ||
| import type { Scene } from "core/scene"; | ||
| import { Texture } from "core/Materials/Textures/texture"; | ||
| import { CubeTexture } from "core/Materials/Textures/cubeTexture"; | ||
| import { ReadFile } from "core/Misc/fileTools"; | ||
| import { UploadButton } from "../primitives/uploadButton"; | ||
|
|
||
| type TextureUploadUpdateProps = { | ||
| /** | ||
| * Existing texture to update via updateURL | ||
| */ | ||
| texture: BaseTexture; | ||
| /** | ||
| * Callback after texture is updated | ||
| */ | ||
| onChange?: (texture: BaseTexture) => void; | ||
| scene?: never; | ||
| cubeOnly?: never; | ||
| }; | ||
|
|
||
| type TextureUploadCreateProps = { | ||
| /** | ||
| * The scene to create the texture in | ||
| */ | ||
| scene: Scene; | ||
| /** | ||
| * Callback when a new texture is created | ||
| */ | ||
| onChange: (texture: BaseTexture) => void; | ||
| /** | ||
| * Whether to create cube textures | ||
| */ | ||
| cubeOnly?: boolean; | ||
| texture?: never; | ||
| }; | ||
|
|
||
| type TextureUploadProps = TextureUploadUpdateProps | TextureUploadCreateProps; | ||
|
|
||
| /** | ||
| * A button that uploads a file and either: | ||
| * - Updates an existing Texture or CubeTexture via updateURL (if texture prop is provided) | ||
| * - Creates a new Texture or CubeTexture (if scene/onChange props are provided) | ||
| * @param props TextureUploadProps | ||
| * @returns UploadButton component that handles texture upload | ||
| */ | ||
| export const TextureUpload: FunctionComponent<TextureUploadProps> = (props) => { | ||
| TextureUpload.displayName = "TextureUpload"; | ||
| const label = props.texture ? "Upload Texture" : undefined; | ||
| // TODO: This should probably be dynamically fetching a list of supported texture extensions | ||
| const accept = ".jpg, .png, .tga, .dds, .env, .exr"; | ||
| const handleUpload = useCallback( | ||
| (files: FileList) => { | ||
| const file = files[0]; | ||
| if (!file) { | ||
| return; | ||
| } | ||
|
|
||
| ReadFile( | ||
| file, | ||
| (data) => { | ||
| const blob = new Blob([data], { type: "octet/stream" }); | ||
|
|
||
| // Update existing texture | ||
| if (props.texture) { | ||
| const { texture, onChange } = props; | ||
| const reader = new FileReader(); | ||
| reader.readAsDataURL(blob); | ||
| reader.onloadend = () => { | ||
| const base64data = reader.result as string; | ||
|
|
||
| if (texture instanceof CubeTexture) { | ||
| let extension: string | undefined = undefined; | ||
| if (file.name.toLowerCase().indexOf(".dds") > 0) { | ||
| extension = ".dds"; | ||
| } else if (file.name.toLowerCase().indexOf(".env") > 0) { | ||
| extension = ".env"; | ||
| } | ||
| texture.updateURL(base64data, extension, () => onChange?.(texture)); | ||
| } else if (texture instanceof Texture) { | ||
| texture.updateURL(base64data, null, () => onChange?.(texture)); | ||
| } | ||
| }; | ||
| } else { | ||
| // Create new texture | ||
| const { scene, cubeOnly, onChange } = props; | ||
| const url = URL.createObjectURL(blob); | ||
| const extension = file.name.split(".").pop()?.toLowerCase(); | ||
|
|
||
| // Revoke the object URL after texture loads to prevent memory leak | ||
| const revokeUrl = () => URL.revokeObjectURL(url); | ||
|
|
||
| const newTexture = cubeOnly | ||
| ? new CubeTexture(url, scene, [], false, undefined, revokeUrl, undefined, undefined, false, extension ? "." + extension : undefined) | ||
| : new Texture(url, scene, false, false, undefined, revokeUrl); | ||
|
|
||
| onChange(newTexture); | ||
| } | ||
| }, | ||
| undefined, | ||
| true | ||
| ); | ||
| }, | ||
| [props] | ||
| ); | ||
|
|
||
| return <UploadButton onUpload={handleUpload} accept={accept} title={"Upload Texture"} label={label} />; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.