Skip to content

Commit c65e34f

Browse files
author
Sascha Braun
committed
fix asset preloading
update bundle assets registration
1 parent 796d6ca commit c65e34f

File tree

16 files changed

+186
-84
lines changed

16 files changed

+186
-84
lines changed

samples/views/Demo.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@
4747

4848

4949
<asset-bundle name="Crate" preload>
50-
<texture name="crateTex" src="/assets/textures/crate.jpg"/>
51-
<standard-material name="cubeMat" map="crateTex"/>
52-
<geometry name="cube" :factory="cubeFactory"/>
50+
<div>
51+
<texture name="crateTex" src="/assets/textures/crate.jpg"/>
52+
<standard-material name="cubeMat" map="crateTex"/>
53+
<geometry name="cube" :factory="cubeFactory"/>
54+
</div>
5355
</asset-bundle>
5456

5557
<asset-bundle dependencies="Crate" name="Scene1" preload>

samples/views/StandardMaterial.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,34 @@ export default class StandardMaterial extends Vue {
2323
@Prop({ type: Number, default: 0.01 })
2424
public metalness!: number;
2525

26-
public factory: MaterialFactory | null = null;
26+
public async factory(app: Application) {
27+
let texture;
28+
if (this.map) {
29+
texture = await app.assets.textures.get(this.map);
30+
}
31+
32+
const mat = new MeshStandardMaterial({
33+
color: this.color,
34+
metalness: this.metalness
35+
});
36+
mat.map = texture as Texture;
37+
return mat;
38+
}
2739

2840
public created() {
29-
this.factory = async (app: Application) => {
30-
let texture;
31-
if (this.map) {
32-
texture = await app.assets.textures.get(this.map);
33-
}
34-
35-
const mat = new MeshStandardMaterial({
36-
color: this.color,
37-
metalness: this.metalness
38-
});
39-
mat.map = texture as Texture;
40-
return mat;
41-
};
41+
console.log("standard material created", this.name);
42+
// this.factory = async (app: Application) => {
43+
// let texture;
44+
// if (this.map) {
45+
// texture = await app.assets.textures.get(this.map);
46+
// }
47+
// const mat = new MeshStandardMaterial({
48+
// color: this.color,
49+
// metalness: this.metalness
50+
// });
51+
// mat.map = texture as Texture;
52+
// console.log("standard material created");
53+
// return mat;
54+
// };
4255
}
4356
}

src/v2/components/AssetBundle.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import Vue, { VNode } from "vue";
2-
import { Component, Mixins, Prop } from "vue-property-decorator";
1+
import { Component, Mixins, Prop, Provide, Vue } from "vue-property-decorator";
32

43
import { BundleHandle } from "../core";
5-
import { AppComponent, isAssetComponent } from "../mixins";
4+
import { AppComponent } from "../mixins";
65
import { stringToArray } from "../utils/toArray";
76

87
@Component
@@ -16,6 +15,13 @@ export class AssetBundle extends Mixins(AppComponent) {
1615
@Prop({ type: [String, Array], default: () => [] })
1716
public dependencies!: string | string[];
1817

18+
@Provide("bundle")
19+
private provideBundle = this.getBundle;
20+
21+
private getBundle() {
22+
return this.m_bundle;
23+
}
24+
1925
private m_active = false;
2026
private m_bundle!: BundleHandle;
2127

@@ -48,24 +54,20 @@ export class AssetBundle extends Mixins(AppComponent) {
4854
await Vue.nextTick();
4955
await deps;
5056

51-
this.registerAssets(this.$slots.default);
57+
console.log("registered bundle", this.name, this.m_bundle.countAssets());
5258
}
5359

5460
private async onUnload(): Promise<void> {
5561
this.m_active = false;
5662
await Vue.nextTick();
57-
}
5863

59-
private registerAssets(nodes: VNode[] | undefined) {
60-
if (nodes) {
61-
for (const node of nodes) {
62-
const component = node.componentInstance;
63-
if (component && isAssetComponent(component)) {
64-
this.m_bundle.registerAsset(component.asset);
65-
}
66-
this.registerAssets(node.children);
67-
}
68-
}
64+
const assets = this.app().assets;
65+
console.log(
66+
assets.textures.size(),
67+
assets.materials.size(),
68+
assets.geometries.size(),
69+
assets.models.size()
70+
);
6971
}
7072

7173
private getBundles(pDependencies: string | string[]): BundleHandle[] {

src/v2/components/Camera.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { Component, Mixins, Prop, Provide } from "vue-property-decorator";
1+
import { Component, Mixins, Prop, Provide, Vue } from "vue-property-decorator";
22

33
import { CameraFactory, CameraHandle } from "../core";
44
import { ObjectComponent } from "../mixins";
55

6-
76
@Component
87
export class Camera extends Mixins(ObjectComponent) {
98
@Prop({ required: true, type: String })
@@ -27,6 +26,7 @@ export class Camera extends Mixins(ObjectComponent) {
2726
};
2827
public onDeactivate = async () => {
2928
this.m_active = false;
29+
await Vue.nextTick();
3030
};
3131

3232
public async created() {
@@ -41,10 +41,13 @@ export class Camera extends Mixins(ObjectComponent) {
4141
this.m_camera.onDeactivate.on(this.onDeactivate);
4242

4343
this.m_active = true;
44+
45+
console.log("camera created", this.name);
4446
}
4547

4648
public beforeDestroy() {
4749
this.app().cameras.dispose(this.name);
50+
console.log("camera disposed", this.name);
4851
}
4952

5053
public render(h: any) {

src/v2/components/Geometry.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ export class Geometry extends Mixins(AssetComponent) {
1212
public factory!: GeometryFactory;
1313

1414
public async created() {
15-
this.asset = this.factory(this.app());
16-
this.app().assets.geometries.set(this.name, this.asset as Promise<
17-
GeometryType
18-
>);
15+
const geometry = this.factory(this.app());
16+
if (this.bundle()) {
17+
this.bundle()!.registerAsset(this.name, geometry);
18+
}
19+
this.app().assets.geometries.set(this.name, geometry);
1920
}
2021

2122
public async beforeDestroy() {
23+
if (this.bundle()) {
24+
this.bundle()!.unregisterAsset(this.name);
25+
}
2226
this.app().assets.geometries.dispose(this.name);
2327
}
2428

src/v2/components/Material.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ export class Material extends Mixins(AssetComponent) {
1212
private factory!: MaterialFactory;
1313

1414
public created() {
15-
this.asset = this.factory(this.app());
16-
this.app().assets.materials.set(this.name, this.asset as Promise<
17-
MaterialType
18-
>);
15+
const material = this.factory(this.app());
16+
if (this.bundle()) {
17+
this.bundle()!.registerAsset(this.name, material);
18+
}
19+
this.app().assets.materials.set(this.name, material);
20+
console.log("material created", this.name);
1921
}
2022

2123
public async beforeDestroy() {
24+
if (this.bundle()) {
25+
this.bundle()!.unregisterAsset(this.name);
26+
}
2227
this.app().assets.materials.dispose(this.name);
28+
console.log("material disposed", this.name);
2329
}
2430

2531
public render(h: any) {

src/v2/components/Model.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export class Model extends Mixins(AssetComponent) {
1818
@Prop({ type: [String, Array], default: () => [] })
1919
public materials!: string | string[];
2020

21+
private m_model!: Promise<ModelType>;
22+
2123
public async created() {
2224
const app = this.app();
23-
2425
if (!this.factory && !this.src) {
2526
throw new Error(
2627
`Model "${
@@ -30,17 +31,25 @@ export class Model extends Mixins(AssetComponent) {
3031
}
3132

3233
if (this.src) {
33-
this.asset = app.loader.load(this.src, this.name);
34+
this.m_model = app.loader.load(this.src, this.name) as Promise<ModelType>;
3435
} else {
35-
this.asset = this.factory(app);
36+
this.m_model = this.factory(app);
3637
}
3738
this.overrideMaterials();
3839

39-
app.assets.models.set(this.name, this.asset as Promise<ModelType>);
40+
if (this.bundle()) {
41+
this.bundle()!.registerAsset(this.name, this.m_model);
42+
}
43+
app.assets.models.set(this.name, this.m_model);
44+
console.log("model created", this.name);
4045
}
4146

4247
public async beforeDestroy() {
48+
if (this.bundle()) {
49+
this.bundle()!.unregisterAsset(this.name);
50+
}
4351
this.app().assets.models.dispose(this.name);
52+
console.log("model disposed", this.name);
4453
}
4554

4655
public render(h: any) {
@@ -50,7 +59,7 @@ export class Model extends Mixins(AssetComponent) {
5059
private overrideMaterials() {
5160
const materials = this.getMaterialPromises(this.materials);
5261
if (materials) {
53-
this.asset = this.asset.then(async asset => {
62+
this.m_model = this.m_model.then(async asset => {
5463
const mats = await Promise.all(materials);
5564
const model = asset as ModelType;
5665

src/v2/components/Texture.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,35 @@ export class Texture extends Mixins(AssetComponent) {
1414
@Prop({ type: String })
1515
public src?: string;
1616

17+
private m_texture!: Promise<TextureType>;
18+
1719
public created() {
1820
const app = this.app();
1921
if (this.factory) {
20-
this.asset = this.factory(app);
22+
this.m_texture = this.factory(app);
2123
} else if (this.src) {
22-
this.asset = app.loader.texture(this.src, this.name);
24+
this.m_texture = app.loader.texture(this.src, this.name);
2325
} else {
2426
throw new Error(
2527
`Texture "${
2628
this.name
2729
}" could not be loaded: no "src" or "factory" props given`
2830
);
2931
}
30-
app.assets.textures.set(this.name, this.asset as Promise<TextureType>);
32+
33+
if (this.bundle()) {
34+
this.bundle()!.registerAsset(this.name, this.m_texture);
35+
}
36+
app.assets.textures.set(this.name, this.m_texture);
37+
console.log("texture created", this.name);
3138
}
3239

3340
public async beforeDestroy() {
41+
if (this.bundle()) {
42+
this.bundle()!.unregisterAsset(this.name);
43+
}
3444
this.app().assets.textures.dispose(this.name);
45+
console.log("texture disposed", this.name);
3546
}
3647

3748
public render(h: any) {

src/v2/core/AssetMap.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HandlerMapErrors } from "./Errors";
1+
import { AssetMapErrors, HandlerMapErrors } from "./Errors";
22

33
interface DisposableAsset {
44
dispose?: () => void;
@@ -7,9 +7,14 @@ interface DisposableAsset {
77
export class AssetMap<T> {
88
protected _data = new Map<string, Promise<T>>();
99

10+
public size() {
11+
return this._data.size;
12+
}
13+
1014
public set(name: string, asset: Promise<T>): void {
1115
if (this._data.has(name)) {
12-
throw HandlerMapErrors.ALREADY_EXISTS;
16+
console.log("asset already exists in map", name);
17+
throw AssetMapErrors.ALREADY_EXISTS;
1318
}
1419
this._data.set(name, asset);
1520
}
@@ -25,9 +30,10 @@ export class AssetMap<T> {
2530
return;
2631
}
2732

28-
const handler = this._data.get(name);
29-
if (handler) {
30-
this.disposeHook(handler);
33+
const asset = this._data.get(name);
34+
if (asset) {
35+
this.disposeHook(asset);
36+
this._data.delete(name);
3137
}
3238
}
3339

0 commit comments

Comments
 (0)