Skip to content
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

fix: enhance texture and loader handling with automatic invalidation when resources are loaded #915

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- run: npm i -g --force corepack && corepack enable
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
Expand Down
7 changes: 5 additions & 2 deletions playground/vue/src/components/BlenderCube.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<script setup lang="ts">
import { useGLTF } from '@tresjs/cientos'
import { dispose } from '@tresjs/core'
import { dispose, useTexture } from '@tresjs/core'
import { useControls } from '@tresjs/leches'

const { nodes } = await useGLTF('https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf/blender-cube.glb', { draco: true })
const model = nodes.Cube

model.position.set(0, 1, 0)

const texture = await useTexture(['https://raw.githubusercontent.com/Tresjs/assets/main/textures/black-rock/Rock035_2K_Displacement.jpg'])

model.children[0].material.map = texture

useControls({
disposeBtn: {
label: 'Dispose',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<script setup lang="ts">
import { OrbitControls } from '@tresjs/cientos'
import { useTres } from '@tresjs/core'
import { ref, watch } from 'vue'
import { ref } from 'vue'
import BlenderCube from '../../../components/BlenderCube.vue'

const { invalidate } = useTres()

const blenderCubeRef = ref()

watch(blenderCubeRef, (prev, next) => {
if (!next) { return }
invalidate()
})

function onControlChange() {
invalidate()
}
Expand Down
4 changes: 3 additions & 1 deletion playground/vue/src/pages/advanced/on-demand/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function onRender() {
clear-color="#82DBC5"
@render="onRender"
>
<OnDemandExperience />
<Suspense>
<OnDemandExperience />
</Suspense>
</TresCanvas>
</template>
5 changes: 4 additions & 1 deletion src/composables/useLoader/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Loader, LoadingManager, Object3D } from 'three'
import type { TresObject } from '../../types'
import { useLogger } from '..'
import { useLogger, useTresContext } from '..'

export interface TresLoader<T> extends Loader {
load: (
Expand Down Expand Up @@ -73,6 +73,8 @@ export async function useLoader<T>(
cb?: (proto: TresLoader<T>) => void,
): Promise<T | T[]> {
const { logError } = useLogger()
const { invalidate } = useTresContext()

const proto = new Loader()
if (cb) {
cb(proto)
Expand All @@ -89,6 +91,7 @@ export async function useLoader<T>(
if (data.scene) {
Object.assign(data, traverseObjects(data.scene))
}
invalidate()
resolve(data as T | T[])
},
onProgress,
Expand Down
7 changes: 6 additions & 1 deletion src/composables/useTexture/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { LoadingManager, Texture } from 'three'
import { TextureLoader } from 'three'
import { isArray } from '../../utils'
import { useTresContext } from '../useTresContextProvider'

export interface PBRMaterialOptions {
/**
Expand Down Expand Up @@ -117,6 +118,7 @@ export async function useTexture(
manager?: LoadingManager,
): Promise<Texture | Texture[] | PBRTextureMaps> {
const textureLoader = new TextureLoader(manager)
const { invalidate } = useTresContext()

/**
* Load a texture.
Expand All @@ -127,7 +129,10 @@ export async function useTexture(
const loadTexture = (url: string): Promise<Texture> => new Promise((resolve, reject) => {
textureLoader.load(
url,
texture => resolve(texture),
(texture) => {
resolve(texture)
invalidate()
},
() => null,
() => {
reject(new Error('[useTextures] - Failed to load texture'))
Expand Down