feat(useTexture)!: refactor to be a real composable with a state#949
feat(useTexture)!: refactor to be a real composable with a state#949alvarosabu wants to merge 12 commits into
Conversation
BREAKING CHANGE: useTexture no longer returns the plain texture, it now returns and object with reactive data (texture|s), isLoading, error) and a load method, can be used both sync and async (suspense) - Completely rewrite `useTexture` composable with more robust loading mechanism - Add comprehensive type support for single and multiple texture loading - Implement reactive loading states (isLoading, error) - Update component and tests to match new composable implementation - Remove deprecated texture loading patterns - Enhance error handling and async loading behavior
- Update `useTexture` to return a Promise-like object - Enhance type definitions to support async/await usage - Maintain existing reactive state and loading mechanism - Improve type safety and composable flexibility
commit: |
…osable - Create detailed documentation for `useTexture` composable - Add extensive code examples covering various use cases - Include API reference and feature highlights - Provide insights into loading textures with Tres.js - Enhance documentation with practical examples and best practices
- Implement `usePBRTexture` composable for simplified PBR texture management - Support concurrent texture loading with async/await and reactive states - Add comprehensive documentation with usage examples and API reference - Create playground examples demonstrating sync and async texture loading - Implement robust error handling and loading state tracking - Enhance type safety with detailed TypeScript interfaces
- Remove unnecessary `nextTick` import from Vue - Simplify test file dependencies - Maintain clean and focused test imports
- Remove the unused `error` variable from the `useTexture` composable - Simplify the component by eliminating unnecessary error tracking - Maintain clean and focused code structure
- Implement `UsePBRTexture` component for declarative PBR texture loading - Add new component to `src/composables/usePBRTexture/component.vue` - Update documentation with component usage example - Enhance playground with new PBR texture component example - Expose component in composables index for easy importing - Support scoped slot for flexible texture rendering
| @@ -0,0 +1,233 @@ | |||
| # usePBRTexture | |||
|
|
|||
|  | |||
There was a problem hiding this comment.
question: Are we allowed to use their pictures like so?
There was a problem hiding this comment.
Their website do not mention any restriction on their image, but the downloadable versions of the Learn OpenGL book are licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License, which allows for sharing and adapting the material for non-commercial purposes, provided appropriate credit>
But I think I will create one myself
| emissiveMap: Texture | null | ||
| } | ||
|
|
||
| export interface PBRTextureResult { |
There was a problem hiding this comment.
suggestion: Imo all the async results in the library should have the same structure. Therefore I suggest using a generic type instead.
| const textureData = reactive<PBRTextureResult>(usePBRTexture(props.paths, props.manager)) | ||
|
|
||
| // Handle loading state | ||
| textureData.promise | ||
| .then(() => emit('loaded')) | ||
| .catch(err => emit('error', err)) | ||
| </script> | ||
|
|
||
| <template> | ||
| <slot | ||
| :data="textureData.data" | ||
| :is-loading="textureData.isLoading" | ||
| :error="textureData.error" | ||
| ></slot> |
There was a problem hiding this comment.
suggestion: I suggest omitting the reactive.
| const textureData = reactive<PBRTextureResult>(usePBRTexture(props.paths, props.manager)) | |
| // Handle loading state | |
| textureData.promise | |
| .then(() => emit('loaded')) | |
| .catch(err => emit('error', err)) | |
| </script> | |
| <template> | |
| <slot | |
| :data="textureData.data" | |
| :is-loading="textureData.isLoading" | |
| :error="textureData.error" | |
| ></slot> | |
| const { isLoading, error, data, promise } = usePBRTexture(props.paths, props.manager) | |
| // Handle loading state | |
| promise | |
| .then(() => emit('loaded')) | |
| .catch(err => emit('error', err)) | |
| </script> | |
| <template> | |
| <slot | |
| :data="data" | |
| :is-loading="isLoading" | |
| :error="error" | |
| ></slot> |
| const data = await reactive(useTexture(props)) | ||
| // Type guard to handle the union type | ||
| const textureData = Array.isArray(props.path) | ||
| ? reactive<UseTextureReturn<Texture[]>>(useTexture(props.path, props.manager)) |
There was a problem hiding this comment.
suggestion: The reactive should not be responsible for typing what useTexture returns. The return type should be solely defined by useTexture itself. Therefore useTexture should have a generic return type.
| <template> | ||
| <slot :textures="data"></slot> | ||
| <slot | ||
| :data="textureData.data" |
There was a problem hiding this comment.
suggestion: The type of data is Texture | Texture[] | null right now. This is not desirable as the type is directly dependent to the type of the prop path. I suggest making this component a generic component so the return type is well defined.
Co-authored-by: Tino Koch <17991193+Tinoooo@users.noreply.github.com>
- Deleted the `usePBRTexture` composable and its associated files, including documentation and example components, to streamline the codebase. - Updated the VitePress configuration to remove references to the now-removed composable. - This change simplifies the composables directory and reduces maintenance overhead.
- Added a `disposeTexture` function to safely dispose of textures when they are no longer needed, preventing memory leaks. - Updated the `useTexture` composable to call `disposeTexture` before loading new textures and during component unmounting. - Removed the immediate option from the texture watcher in `SimpleTexture.vue` for cleaner logic.
| * @param {(error: ErrorEvent) => void} options.onError - Optional error callback | ||
| * @returns {Texture} The loaded texture | ||
| */ | ||
| export function loadTextureSync( |
There was a problem hiding this comment.
@Tinoooo should we export this? I'm hesitant. I prefer people to use useTexture
There was a problem hiding this comment.
I think it is not worth it. There is not much the function does.
|
We decided to simplify and move this functionality to cientos #1156 |
BREAKING CHANGE: useTexture no longer returns the plain texture, it now returns and object with reactive data (texture|s), isLoading, error) and a load method, can be used both sync and async (suspense)
useTexturecomposable with more robust loading mechanism