Skip to content

Commit 2055f24

Browse files
author
Sascha Braun
committed
add scene loading events and handle nested assets preloading (nested assets weren't detected previously)
1 parent cdbbb45 commit 2055f24

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

README.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ However, for now, there are still some shipped in features. Some features are bo
2323

2424
**Todo:**
2525

26-
- Add delta time ( for now it is not set and is always equals to 0 )
27-
28-
- Add scene loading events for tracking loading progress
2926
- Remove scene active prop (can be done with an v-if attribute)
30-
- Add scene preload nested assets handling
31-
- Add cube texture component
3227

28+
- Add cube texture component
3329
- Add default materials and geometries
3430

3531
## Usage
@@ -123,7 +119,6 @@ export class MyBehaviour extends Mixins(Behaviour) {
123119
@Prop()
124120
public data!: {
125121
position: Vec3;
126-
rotation: Vec3;
127122
};
128123

129124
private m_moveUp = true;
@@ -144,7 +139,8 @@ export class MyBehaviour extends Mixins(Behaviour) {
144139

145140
// lifecycle function called before each frame (optional)
146141
public update(deltaTime: number) {
147-
this.data.position.y += 0.01 * (this.m_moveUp ? 1 : -1);
142+
const speed = 5; // 5 y / second
143+
this.data.position.y += deltaTime * (this.m_moveUp ? 1 : -1) * speed;
148144

149145
if (this.data.position.y > 10) {
150146
this.m_moveUp = false;

samples/views/Demo.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Component, Vue } from "vue-property-decorator";
44
import {
55
CameraFactory, components, GeometryFactory, LightFactory, MaterialFactory
66
} from "../../src";
7-
87
import { MyBehaviour } from "./MyBehaviour";
98

109
@Component({
@@ -65,6 +64,16 @@ export default class About extends Vue {
6564

6665
public scenes = [this.scene1, this.scene2];
6766

67+
public startLoading() {
68+
console.log("start loading");
69+
}
70+
public finishLoading() {
71+
console.log("finish loading");
72+
}
73+
public loadingProgress(amount: number, total: number) {
74+
console.log("loading progress", `${amount} / ${total}`);
75+
}
76+
6877
public changeScene(pScene: any) {
6978
this.scenes.forEach(scene => {
7079
scene.active = false;
@@ -77,7 +86,7 @@ export default class About extends Vue {
7786
console.log(this.scene1);
7887

7988
this.cubeFactory = async () => {
80-
// await new Promise(r => setTimeout(r, 3000));
89+
await new Promise(r => setTimeout(r, 2000));
8190
return new THREE.BoxBufferGeometry(1, 2, 1);
8291
};
8392
this.planeFactory = async () => {

samples/views/Demo.vue

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
<div v-if="canvas">
1313
<three :canvas="canvas" antialias>
1414

15-
<scene :name="scene1.name" :active.sync="scene1.active">
15+
<scene :name="scene1.name" :active.sync="scene1.active" @load="startLoading" @load-progress="loadingProgress" @loaded="finishLoading">
1616
<template slot="preload">
17-
<material name="cubeMat" :factory="cubeMaterialFactory"/>
18-
<geometry name="cube" :factory="cubeFactory"/>
17+
<div>
18+
<material name="cubeMat" :factory="cubeMaterialFactory"/>
19+
<material name="waterMat" :factory="waterMaterialFactory"/>
20+
</div>
1921

20-
<material name="waterMat" :factory="waterMaterialFactory"/>
22+
<geometry name="cube" :factory="cubeFactory"/>
2123
<geometry name="plane" :factory="planeFactory"/>
24+
2225
</template>
2326

2427
<fog exp2/>

src/components/Scene.tsx

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as THREE from "three";
2+
import { VNode } from "vue";
23
import { Component, Mixins, Prop, Provide, Vue, Watch } from "vue-property-decorator";
34

45
import { AssetType } from "../types";
5-
import { ThreeAssetComponent, ThreeComponent } from "./base";
6+
import { isThreeAssetComponent, ThreeAssetComponent, ThreeComponent } from "./base";
67

78
@Component
89
export class Scene extends Mixins(ThreeComponent) {
@@ -70,19 +71,37 @@ export class Scene extends Mixins(ThreeComponent) {
7071
this.m_isReady = true;
7172
}
7273

73-
public async preloadAssets() {
74-
const assets: Array<Promise<AssetType>> = [];
75-
const preloadNodes = this.$slots.preload;
76-
if (preloadNodes) {
77-
for (const node of preloadNodes) {
74+
private recursivePreload(
75+
data: { counter: number; assets: Array<Promise<AssetType>> },
76+
nodes: VNode[] | undefined
77+
) {
78+
if (nodes) {
79+
for (const node of nodes) {
7880
const component = node.componentInstance;
79-
if (component instanceof ThreeAssetComponent) {
80-
assets.push(component.asset);
81+
if (component && isThreeAssetComponent(component)) {
82+
const p = component.asset.then(asset => {
83+
++data.counter;
84+
this.$emit("load-progress", data.counter, data.assets.length);
85+
return asset;
86+
});
87+
data.assets.push(p);
8188
}
89+
this.recursivePreload(data, node.children);
8290
}
8391
}
92+
}
93+
94+
public async preloadAssets() {
95+
this.$emit("load");
96+
97+
const data: { counter: number; assets: Array<Promise<AssetType>> } = {
98+
assets: [],
99+
counter: 0
100+
};
101+
this.recursivePreload(data, this.$slots.preload);
84102

85-
return Promise.all(assets);
103+
await Promise.all(data.assets);
104+
this.$emit("loaded");
86105
}
87106

88107
public mounted() {
@@ -109,7 +128,7 @@ export class Scene extends Mixins(ThreeComponent) {
109128
);
110129

111130
return (
112-
<div className="scene">
131+
<div class="scene">
113132
<h2>Scene {this.name}</h2>
114133
<div>
115134
<h3>Preload</h3>

src/components/base/ThreeAssetComponent.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ import { AssetType } from "../../types";
66
export class ThreeAssetComponent extends Vue {
77
public asset!: Promise<AssetType>;
88
}
9+
10+
export function isThreeAssetComponent(
11+
component: Vue
12+
): component is ThreeAssetComponent {
13+
return (component as any).asset instanceof Promise;
14+
}

0 commit comments

Comments
 (0)