Skip to content

Commit cc95f16

Browse files
author
Sascha Braun
committed
rewrite object components for better code reusability: package size has reduced by about 30%
1 parent 0acff8b commit cc95f16

29 files changed

+201
-332
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-threejs-composer",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"main": "build/src/index.js",
55
"typings": "build/src/index.d.ts",
66
"repository": "https://github.com/sascha245/vue-threejs-composer",

samples/components/behaviours/HoverBehaviour.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export class HoverBehaviour extends Mixins(BehaviourComponent) {
1818

1919
public created() {
2020
// access app
21-
const app = this.app();
21+
const app = this.app;
2222

2323
// access scene if behaviour is placed in a scene
24-
const scene = this.scene();
24+
const scene = this.scene;
2525

2626
// access object if behaviour is placed in an object
27-
const object = this.object!();
27+
const object = this.object;
2828

2929
this.m_originalY = this.position.y;
3030

@@ -43,7 +43,7 @@ export class HoverBehaviour extends Mixins(BehaviourComponent) {
4343
}
4444
}
4545

46-
public beforeDestroy() {
46+
public destroyed() {
4747
// dispose everything that needs to be disposed here
4848
}
4949

src/components/AssetBundle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ export class AssetBundle extends Mixins(AppComponent) {
2626
private m_bundle!: BundleHandle;
2727

2828
public mounted() {
29-
this.m_bundle = this.app().assets.bundles.create(this.name);
29+
this.m_bundle = this.app.assets.bundles.create(this.name);
3030
this.m_bundle.onLoad.on(this.onLoad);
3131
this.m_bundle.onUnload.on(this.onUnload);
3232

3333
this.m_bundle.preload = this.preload;
3434
}
3535

3636
public destroyed() {
37-
this.app().assets.bundles.dispose(this.name);
37+
this.app.assets.bundles.dispose(this.name);
3838
}
3939

4040
public render(h: any) {
@@ -62,7 +62,7 @@ export class AssetBundle extends Mixins(AppComponent) {
6262
private getBundles(pDependencies: string | string[]): BundleHandle[] {
6363
const bundles: BundleHandle[] = [];
6464
const dependencies = stringToArray(",", pDependencies);
65-
const app = this.app();
65+
const app = this.app;
6666

6767
dependencies.forEach(name => {
6868
const bundle = app.assets.bundles.get(name);

src/components/Axes.ts

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,19 @@
1-
import { AxesHelper } from "three";
2-
import { Component, Mixins, Prop, Provide } from "vue-property-decorator";
1+
import * as THREE from "three";
2+
import { Component, Mixins, Prop } from "vue-property-decorator";
33

4-
import { ObjectComponent } from "../mixins";
4+
import { Entity } from "./Entity";
55

66
@Component
7-
export class Axes extends Mixins(ObjectComponent) {
7+
export class Axes extends Mixins(Entity) {
88
@Prop({ type: String, default: "" })
99
private name!: string;
1010

1111
@Prop({ type: Number, default: 1 })
1212
private size!: number;
1313

14-
@Provide("object")
15-
private provideObject = this.getObject;
16-
17-
private m_axes!: THREE.AxesHelper;
18-
private m_created = false;
19-
20-
public getObject() {
21-
return this.m_axes;
22-
}
23-
24-
public async created() {
25-
const scene = this.scene() ? this.scene()!.get() : undefined;
26-
if (!scene && !this.object()) {
27-
throw new Error(
28-
"Grid component can only be added as child to an object or mesh component"
29-
);
30-
}
31-
32-
this.m_axes = new AxesHelper(this.size);
33-
this.m_axes.name = this.name;
34-
35-
const parent = this.object ? this.object() : scene;
36-
parent!.add(this.m_axes);
37-
38-
this.m_created = true;
39-
}
40-
41-
public destroyed() {
42-
const scene = this.scene() ? this.scene()!.get() : undefined;
43-
const parent = this.object ? this.object() : scene;
44-
parent!.remove(this.m_axes);
45-
}
46-
47-
public render(h: any) {
48-
if (!this.m_created) {
49-
return null;
50-
}
51-
return h("div", this.$slots.default);
14+
protected async instantiate() {
15+
const axes = new THREE.AxesHelper(this.size);
16+
axes.name = this.name;
17+
return axes;
5218
}
5319
}

src/components/Camera.ts

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,29 @@
1-
import { Component, Mixins, Prop, Provide, Vue } from "vue-property-decorator";
1+
import { Component, Mixins, Prop } from "vue-property-decorator";
22

3-
import { CameraFactory, CameraHandle } from "../core";
4-
import { ObjectComponent } from "../mixins";
3+
import { Application, CameraFactory, CameraHandle } from "../core";
4+
import { Entity } from "./Entity";
55

66
@Component
7-
export class Camera extends Mixins(ObjectComponent) {
7+
export class Camera extends Mixins(Entity) {
88
@Prop({ required: true, type: String })
99
private name!: string;
1010

1111
@Prop({ required: true, type: Function })
1212
public factory!: CameraFactory;
1313

14-
@Provide("object")
15-
public provideObject = this.getObject;
16-
17-
private m_active = false;
18-
private m_camera!: CameraHandle;
19-
20-
public getObject() {
21-
return this.m_camera.get();
22-
}
23-
24-
public onActivate = async () => {
25-
this.m_active = true;
26-
};
27-
public onDeactivate = async () => {
28-
this.m_active = false;
29-
await Vue.nextTick();
30-
};
31-
32-
public async created() {
33-
const app = this.app();
34-
const sceneHandle = this.scene();
35-
const scene = sceneHandle ? this.scene()!.get() : undefined;
36-
if (!scene) {
37-
throw new Error(
38-
"Camera component can only be added as child to an object or scene component"
39-
);
40-
}
14+
private m_handle!: CameraHandle;
4115

16+
protected async instantiate(app: Application) {
4217
const camera = await this.factory(app);
4318
camera.name = this.name;
4419

45-
this.m_camera = sceneHandle!.cameras.create(this.name);
46-
this.m_camera.set(camera);
47-
this.m_camera.onActivate.on(this.onActivate);
48-
this.m_camera.onDeactivate.on(this.onDeactivate);
49-
50-
this.m_active = true;
51-
52-
const parent = this.object ? this.object() : scene;
53-
parent!.add(this.m_camera.get()!);
54-
}
20+
this.m_handle = this.scene!.cameras.create(this.name);
21+
this.m_handle.set(camera);
5522

56-
public destroyed() {
57-
const sceneHandle = this.scene();
58-
const scene = sceneHandle ? this.scene()!.get() : undefined;
59-
const parent = this.object ? this.object() : scene;
60-
parent!.remove(this.m_camera.get()!);
61-
sceneHandle!.cameras.dispose(this.name);
23+
return camera;
6224
}
6325

64-
public render(h: any) {
65-
if (!this.m_active) {
66-
return null;
67-
}
68-
return h("div", this.$slots.default);
26+
protected destroy() {
27+
this.scene!.cameras.dispose(this.name);
6928
}
7029
}

src/components/Entity.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Camera, Object3D } from "three";
2+
import { Component, Mixins, Provide } from "vue-property-decorator";
3+
4+
import { Application } from "../core";
5+
import { ObjectComponent, ObjectType } from "../mixins";
6+
import { Provider } from "../utils/provider";
7+
8+
@Component
9+
export class Entity extends Mixins(ObjectComponent) {
10+
@Provide("object")
11+
public provideObject = Provider.defaultValue<ObjectType>();
12+
13+
private m_object!: ObjectType;
14+
private m_created = false;
15+
16+
protected instantiate(app: Application): Promise<ObjectType> {
17+
return Promise.resolve(new Object3D());
18+
}
19+
protected destroy() {}
20+
21+
private async created() {
22+
const scene = this.scene ? this.scene.get() : undefined;
23+
if (!scene) {
24+
const message = `${
25+
this.$options.name
26+
} component must be placed in a scene component`;
27+
throw {
28+
message,
29+
code: "undefined_scene"
30+
};
31+
}
32+
33+
this.m_object = await this.instantiate(this.app);
34+
const parent = this.object ? this.object : scene;
35+
parent!.add(this.m_object);
36+
37+
Provider.setValue<ObjectType>(this.provideObject, this.m_object);
38+
39+
this.m_created = true;
40+
}
41+
42+
private destroyed() {
43+
if (this.scene) {
44+
this.destroy();
45+
const scene = this.scene ? this.scene.get() : undefined;
46+
const parent = this.object ? this.object : scene;
47+
parent!.remove(this.m_object);
48+
}
49+
}
50+
51+
private render(h: any) {
52+
if (!this.m_created) {
53+
return null;
54+
}
55+
return h("div", this.$slots.default);
56+
}
57+
}

src/components/Fog.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class Fog extends Mixins(SceneComponent) {
2828

2929
@Watch("exp2")
3030
public watchExp() {
31-
const scene = this.scene() ? this.scene()!.get() : undefined;
31+
const scene = this.scene ? this.scene!.get() : undefined;
3232
if (this.exp2) {
3333
this.m_fog = new ThreeFogExp2(this.color, this.density);
3434
} else {
@@ -59,7 +59,7 @@ export class Fog extends Mixins(SceneComponent) {
5959
private m_fog!: IFog;
6060

6161
public created() {
62-
const scene = this.scene() ? this.scene()!.get() : undefined;
62+
const scene = this.scene ? this.scene!.get() : undefined;
6363
if (!scene) {
6464
throw new Error(
6565
"Fog component can only be added as a child to a scene component"
@@ -78,7 +78,7 @@ export class Fog extends Mixins(SceneComponent) {
7878
}
7979

8080
public destroyed() {
81-
const scene = this.scene() ? this.scene()!.get() : undefined;
81+
const scene = this.scene ? this.scene!.get() : undefined;
8282
scene!.fog = null;
8383
}
8484

src/components/Geometry.ts

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

1414
public async created() {
15-
const geometry = this.factory(this.app());
15+
const geometry = this.factory(this.app);
1616
if (this.bundle()) {
1717
this.bundle()!.registerAsset(this.name, geometry);
1818
}
19-
this.app().assets.geometries.set(this.name, geometry);
19+
this.app.assets.geometries.set(this.name, geometry);
2020
}
2121

2222
public async destroyed() {
2323
if (this.bundle()) {
2424
this.bundle()!.unregisterAsset(this.name);
2525
}
26-
this.app().assets.geometries.dispose(this.name);
26+
this.app.assets.geometries.dispose(this.name);
2727
}
2828

2929
public render(h: any) {

src/components/Grid.ts

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import * as THREE from "three";
22
import { Component, Mixins, Prop, Provide } from "vue-property-decorator";
33

44
import { ObjectComponent } from "../mixins";
5+
import { Entity } from "./Entity";
56

67
@Component
7-
export class Grid extends Mixins(ObjectComponent) {
8+
export class Grid extends Mixins(Entity) {
89
@Prop({ type: String, default: "" })
910
private name!: string;
1011

@@ -14,43 +15,9 @@ export class Grid extends Mixins(ObjectComponent) {
1415
@Prop({ type: Number, default: 10 })
1516
private divisions!: number;
1617

17-
@Provide("object")
18-
private provideObject = this.getObject;
19-
20-
private m_grid!: THREE.GridHelper;
21-
private m_created = false;
22-
23-
public getObject(): THREE.Object3D {
24-
return this.m_grid;
25-
}
26-
27-
public async created() {
28-
const scene = this.scene() ? this.scene()!.get() : undefined;
29-
if (!scene && !this.object()) {
30-
throw new Error(
31-
"Grid component can only be added as child to an object or mesh component"
32-
);
33-
}
34-
35-
this.m_grid = new THREE.GridHelper(this.size, this.divisions);
36-
this.m_grid.name = this.name;
37-
38-
const parent = this.object ? this.object() : scene;
39-
parent!.add(this.m_grid);
40-
41-
this.m_created = true;
42-
}
43-
44-
public destroyed() {
45-
const scene = this.scene() ? this.scene()!.get() : undefined;
46-
const parent = this.object ? this.object() : scene;
47-
parent!.remove(this.m_grid);
48-
}
49-
50-
public render(h: any) {
51-
if (!this.m_created) {
52-
return null;
53-
}
54-
return h("div", this.$slots.default);
18+
protected async instantiate() {
19+
const grid = new THREE.GridHelper(this.size, this.divisions);
20+
grid.name = this.name;
21+
return grid;
5522
}
5623
}

0 commit comments

Comments
 (0)