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

feat : ✨ resolve [TODO] - provide file size of imported mesh #219

Open
wants to merge 3 commits into
base: master
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
38 changes: 32 additions & 6 deletions packages/react-babylonjs/src/hooks/loaders/useSceneLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ const useSceneLoaderWithCache = (): ((
// eslint-disable-next-line react-hooks/rules-of-hooks
const sceneLoaderContext = useContext<SceneLoaderContextType>(SceneLoaderContext)

const pathOrigin = window.location.origin
const isLocalAsset = suspenseKey.includes(pathOrigin) || !/^http/.test(suspenseKey)
let fileFetchFlag = 0
let fileTotalSize = 0;

const createSceneLoader = (): (() => LoadedModel) => {
const taskPromise = new Promise<LoadedModel>((resolve, reject) => {
const loadedModel = new LoadedModel()
Expand Down Expand Up @@ -232,15 +237,36 @@ const useSceneLoaderWithCache = (): ((
originalDispose.call(loadedModel)
}

fileTotalSize = 0
fileFetchFlag = 0
resolve(loadedModel)
},
(event: ISceneLoaderProgressEvent): void => {
if (opts.reportProgress === true && sceneLoaderContext !== undefined) {
sceneLoaderContext!.updateProgress(event)
}
if (opts.onLoadProgress) {
opts.onLoadProgress(event)
}
;(async () => {
if(event.total !== 0 ){
fileTotalSize = event.total
}

if (isLocalAsset && fileTotalSize === 0 && fileFetchFlag === 0) {
fileFetchFlag += 1
try {
const response = await fetch(sceneFilename ? suspenseKey : rootUrl,{
cache: 'force-cache',
})
const { size } = await response.blob()
fileTotalSize = size
} catch (error) {
console.error(`failed to get the file size : ${error}`)
}
}

if (opts.reportProgress === true && sceneLoaderContext !== undefined) {
sceneLoaderContext!.updateProgress({ ...event, total: fileTotalSize })
}
if (opts.onLoadProgress) {
opts.onLoadProgress({ ...event, total: fileTotalSize })
}
})()
},
(_: Scene, message: string, exception?: any): void => {
if (opts.onModelError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const ProgressFallback = (props) => {
let loadProgress = 0;
if (sceneLoaderContext.lastProgress) {
const progress = sceneLoaderContext.lastProgress;
loadProgress = progress.lengthComputable
? progress.loaded / progress.total
: progress.loaded / 10000; // TODO: provide option to input file size for proper loading.
loadProgress = progress.loaded / progress.total;
}

return (
Expand Down