-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(texture): move texture loading into worker (#32)
- Loading branch information
Showing
5 changed files
with
115 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import SceneWorkerController from '../../worker/SceneWorkerController.js'; | ||
import { TextureSpec } from './types.js'; | ||
import { AssetHost } from '../../asset.js'; | ||
|
||
const createWorker = () => | ||
new Worker(new URL('./worker.js', import.meta.url), { | ||
name: 'texture-loader', | ||
type: 'module', | ||
}); | ||
|
||
type TextureLoaderOptions = { | ||
host: AssetHost; | ||
}; | ||
|
||
class TextureLoader extends SceneWorkerController { | ||
constructor(options: TextureLoaderOptions) { | ||
super(createWorker, { host: options.host }); | ||
} | ||
|
||
loadSpec(path: string): Promise<TextureSpec> { | ||
return this.request('loadSpec', path); | ||
} | ||
} | ||
|
||
export default TextureLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Blp } from '@wowserhq/format'; | ||
import { TextureSpec } from './types.js'; | ||
import SceneWorker from '../../worker/SceneWorker.js'; | ||
import { AssetHost, loadAsset } from '../../asset.js'; | ||
|
||
type TextureLoaderWorkerOptions = { | ||
host: AssetHost; | ||
}; | ||
|
||
class TextureLoaderWorker extends SceneWorker { | ||
#host: AssetHost; | ||
|
||
initialize(options: TextureLoaderWorkerOptions) { | ||
this.#host = options.host; | ||
} | ||
|
||
async loadSpec(path: string) { | ||
const blpData = await loadAsset(this.#host, path); | ||
const blp = new Blp().load(blpData); | ||
|
||
const images = blp.getImages(); | ||
const format = images[0].format; | ||
|
||
const mipmaps = new Array(images.length); | ||
const buffers = new Set<ArrayBuffer>(); | ||
|
||
for (let i = 0; i < images.length; i++) { | ||
const image = images[i]; | ||
|
||
mipmaps[i] = { | ||
width: image.width, | ||
height: image.height, | ||
data: image.data, | ||
}; | ||
|
||
buffers.add(image.data.buffer); | ||
} | ||
|
||
const spec: TextureSpec = { | ||
width: blp.width, | ||
height: blp.height, | ||
format, | ||
mipmaps, | ||
}; | ||
|
||
const transfer = [...buffers]; | ||
|
||
return [spec, transfer]; | ||
} | ||
} | ||
|
||
export default TextureLoaderWorker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { BLP_IMAGE_FORMAT } from '@wowserhq/format'; | ||
|
||
type TextureMipmapSpec = { | ||
width: number; | ||
height: number; | ||
data: Uint8Array; | ||
}; | ||
|
||
type TextureSpec = { | ||
width: number; | ||
height: number; | ||
format: BLP_IMAGE_FORMAT; | ||
mipmaps: TextureMipmapSpec[]; | ||
}; | ||
|
||
export { TextureSpec }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import TextureLoaderWorker from './TextureLoaderWorker.js'; | ||
|
||
const worker = new TextureLoaderWorker(); |