Skip to content

feat(useTexture)!: refactor to be a real composable with a state#949

Closed
alvarosabu wants to merge 12 commits into
nextfrom
feature/922-use-texture
Closed

feat(useTexture)!: refactor to be a real composable with a state#949
alvarosabu wants to merge 12 commits into
nextfrom
feature/922-use-texture

Conversation

@alvarosabu

@alvarosabu alvarosabu commented Feb 28, 2025

Copy link
Copy Markdown
Member

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 (PBR is going to have its own composable)
  • Enhance error handling and async loading behavior

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
@alvarosabu alvarosabu marked this pull request as draft February 28, 2025 16:49
@alvarosabu alvarosabu added p3-significant High-priority enhancement (priority) breaking-change v5 labels Feb 28, 2025
@alvarosabu alvarosabu added this to the v5 milestone Feb 28, 2025
- 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
@pkg-pr-new

pkg-pr-new Bot commented Feb 28, 2025

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@tresjs/core@949

commit: 4e52f43

…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
@alvarosabu alvarosabu marked this pull request as ready for review March 1, 2025 12:24
- 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
@alvarosabu alvarosabu requested a review from Tinoooo March 14, 2025 18:58
@alvarosabu alvarosabu linked an issue Mar 18, 2025 that may be closed by this pull request
4 tasks
@alvarosabu alvarosabu mentioned this pull request Mar 18, 2025
Closed
Comment thread docs/composables/use-pbr-texture.md Outdated
@@ -0,0 +1,233 @@
# usePBRTexture

![PBR Maps explained](https://learnopengl.com/img/pbr/textures.png)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Are we allowed to use their pictures like so?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread docs/composables/use-pbr-texture.md Outdated
Comment thread src/composables/usePBRTexture/component.vue Outdated
Comment thread src/composables/useTexture/index.ts Outdated
Comment thread src/composables/useTexture/index.ts Outdated
Comment thread src/composables/useTexture/index.ts Outdated
Comment thread src/composables/usePBRTexture/index.ts Outdated
emissiveMap: Texture | null
}

export interface PBRTextureResult {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Imo all the async results in the library should have the same structure. Therefore I suggest using a generic type instead.

Comment on lines +22 to +35
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>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I suggest omitting the reactive.

Suggested change
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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I provided a poc here.

<template>
<slot :textures="data"></slot>
<slot
:data="textureData.data"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the component as a poc here.

@alvarosabu alvarosabu requested a review from Tinoooo April 2, 2025 16:48
- 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.
Comment thread src/utils/loaders.ts
* @param {(error: ErrorEvent) => void} options.onError - Optional error callback
* @returns {Texture} The loaded texture
*/
export function loadTextureSync(

@alvarosabu alvarosabu Apr 3, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tinoooo should we export this? I'm hesitant. I prefer people to use useTexture

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not worth it. There is not much the function does.

@alvarosabu alvarosabu mentioned this pull request Apr 4, 2025
17 tasks
@alvarosabu alvarosabu mentioned this pull request Oct 6, 2025
4 tasks
@alvarosabu

Copy link
Copy Markdown
Member Author

We decided to simplify and move this functionality to cientos #1156

@alvarosabu alvarosabu closed this Apr 12, 2025
@alvarosabu alvarosabu deleted the feature/922-use-texture branch February 27, 2026 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change p3-significant High-priority enhancement (priority) v5

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Several composables are not truly composables.

2 participants