Skip to content

Commit

Permalink
feat: 🎨 allow onSuccess and onError callbacks on useAssetManager tasks (
Browse files Browse the repository at this point in the history
#211)

* fix: 🐛 fix html loosing anchor node - camera transformation matrix is 0 on first render, led to vec being [NaN, NaN], therefore just added an isNaN check.

* fix: 🐛 fix visible behind camera frustum

this should fix html still being visible, when object is behind camera frustum

* feat: 🎨 allow onSuccess and onError callbacks on useAssetManager tasks

* fix: 🎨 add onError callback to useAssetManager
  • Loading branch information
dennemark committed Mar 17, 2022
1 parent 34cfc0a commit 52f5e48
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/react-babylonjs/src/hooks/loaders/useAssetManager.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
AbstractAssetTask,
AssetsManager,
BinaryFileAssetTask,
IAssetsProgressEvent,
MeshAssetTask,
TextureAssetTask,
} from '@babylonjs/core/Misc/assetsManager.js'
import { EventState } from '@babylonjs/core/Misc/observable.js'
Expand All @@ -20,6 +22,8 @@ export type BinaryTask = {
taskType: TaskType.Binary
name: string
url: string
onSuccess?: BinaryFileAssetTask['onSuccess']
onError?: BinaryFileAssetTask['onError']
}

export type MeshTask = {
Expand All @@ -28,6 +32,8 @@ export type MeshTask = {
meshesNames?: any
rootUrl: string
sceneFilename: string
onSuccess?: MeshAssetTask['onSuccess']
onError?: MeshAssetTask['onError']
}

export type TextureTask = {
Expand All @@ -37,6 +43,8 @@ export type TextureTask = {
noMipmap?: boolean
invertY?: boolean
samplingMode?: number
onSuccess?: TextureAssetTask['onSuccess']
onError?: TextureAssetTask['onError']
}

export type Task = BinaryTask | MeshTask | TextureTask
Expand Down Expand Up @@ -174,6 +182,12 @@ const useAssetManagerWithCache = (): ((
case TaskType.Binary:
const binaryTask = assetManager.addBinaryFileTask(task.name, task.url)
newRequests.set(binaryTask, task)
if (task.onSuccess) {
binaryTask.onSuccess = task.onSuccess
}
if (task.onError) {
binaryTask.onError = task.onError
}
break
case TaskType.Mesh:
const meshTask = assetManager.addMeshTask(
Expand All @@ -183,6 +197,12 @@ const useAssetManagerWithCache = (): ((
task.sceneFilename
)
newRequests.set(meshTask, task)
if (task.onSuccess) {
meshTask.onSuccess = task.onSuccess
}
if (task.onError) {
meshTask.onError = task.onError
}
break
case TaskType.Texture:
const textureTask: TextureAssetTask = assetManager.addTextureTask(
Expand All @@ -193,6 +213,12 @@ const useAssetManagerWithCache = (): ((
task.samplingMode
)
newRequests.set(textureTask, task)
if (task.onSuccess) {
textureTask.onSuccess = task.onSuccess
}
if (task.onError) {
textureTask.onError = task.onError
}
break
default:
throw new Error(
Expand Down

0 comments on commit 52f5e48

Please sign in to comment.