Skip to content

Commit 01b4394

Browse files
author
Sascha Braun
committed
add properties, behaviours and input manager + clean up old code
1 parent 0590bda commit 01b4394

37 files changed

+378
-246
lines changed

README.md

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,125 @@
11
# Vue Three.js
22

3-
/!\ This module is still in development and not usable yet. If you want to help out, you can make a pull request with the appropriate fixes or features.
3+
/!\ This module is still in development and not usable yet as a library.
44

5-
Contains Vuejs bindings for creating and interacting with Threejs scenes and objects
5+
Contains Vuejs bindings for creating and interacting with Threejs scenes and objects in a easy and reactive way.
66

77
**Features:**
88

9-
- Asset manager with preload possibility (not 100% finished yet)
9+
- Only core features will be present in this package
10+
- Asset manager with preload possibility (loading events not done yet)
1011
- Scene manager with scene switching / loading
11-
- Reactive meshes, cameras and lights (not 100% finished yet)
12+
- Reactive meshes, cameras and lights
1213
- Asynchronous asset and mesh loading
13-
- Custom factory functions to create (asynchronously) geometries, materials and lights
14+
- Custom factory functions to load (asynchronously) custom geometries, materials and textures
15+
- Input manager
16+
- Behaviour components for data manipulation
1417

1518
**Todo:**
1619

17-
- Input manager
18-
- Object behaviour components for data manipulation
19-
- Stabilize API and clean up code
20+
- Add delta time ( for now it is equals to 0 all time )
21+
- Change project to library and add samples
2022
- Publish first pre-release on npm
2123

2224
## Usage
2325

24-
Coming once the API has been stabilized
26+
####Define your scenes
27+
28+
```html
29+
<div>
30+
<canvas ref="canvas" class="webglCanvas"></canvas>
31+
<div v-if="canvas">
32+
<three :canvas="canvas" antialias>
33+
34+
<scene name="MyScene" active>
35+
<template slot="preload">
36+
<material name="cubeMat" :factory="cubeMaterialFactory"/>
37+
<geometry name="cube" :factory="cubeFactory"/>
38+
39+
<material name="waterMat" :factory="waterMaterialFactory"/>
40+
<geometry name="plane" :factory="planeFactory"/>
41+
</template>
42+
43+
<camera name="mainCamera" :factory="cameraFactory">
44+
<position :value="scene1.camera.position"/>
45+
<rotation :value="scene1.camera.rotation" rad/>
46+
</camera>
47+
48+
<light name="light" :factory="lightFactory">
49+
<position :value="{x: 0, y: 10, z: 0}"/>
50+
<shadows cast/>
51+
</light>
52+
53+
<mesh name="waterPlane" geometry="plane" material="waterMat">
54+
<rotation :value="{ x: -90, y: 0, z: 0 }"/>
55+
<shadows receive/>
56+
</mesh>
57+
58+
<mesh v-for="field in scene1.fields"
59+
:key="field.id"
60+
:name="'field-'+field.id"
61+
geometry="cube"
62+
material="cubeMat"
63+
>
64+
<position :value="{ x: field.x * 2, y: 0, z: field.y * 2}"/>
65+
<scale :value="{ x: 1.2, y: 0.7, z: 1.2}"/>
66+
<shadows cast receive/>
67+
</mesh>
68+
69+
</scene>
70+
71+
</three>
72+
</div>
73+
```
74+
75+
####Define custom behaviours
76+
77+
```ts
78+
import { Component, Mixins, Prop } from "vue-property-decorator";
79+
80+
import { Behaviour } from "vue-threejs-composer";
81+
82+
@Component
83+
export class MyBehaviour extends Mixins(Behaviour) {
84+
@Prop()
85+
public data!: {
86+
position: Vec3;
87+
rotation: Vec3;
88+
};
89+
90+
private m_moveUp = true;
91+
92+
public created() {
93+
// access app
94+
const app = this.app();
95+
96+
// access scene if behaviour is placed in a scene
97+
const scene = this.scene();
98+
99+
// access object if behaviour is placed in an object
100+
const object = this.object();
101+
102+
// once your component is ready
103+
this.ready();
104+
}
105+
106+
// lifecycle function called before each frame (optional)
107+
public update(deltaTime: number) {
108+
this.data.position.y += 0.01 * (this.m_moveUp ? 1 : -1);
109+
110+
if (this.data.position.y > 10) {
111+
this.m_moveUp = false;
112+
} else if (this.data.position.y < 0) {
113+
this.m_moveUp = true;
114+
}
115+
}
116+
117+
public beforeDestroy() {
118+
// dispose everything that needs to be disposed here
119+
}
120+
121+
public render(h: any) {
122+
return h("div");
123+
}
124+
}
125+
```

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
"test:unit": "vue-cli-service test:unit"
1010
},
1111
"dependencies": {
12-
"@types/events": "^1.2.0",
1312
"@types/three": "^0.92.24",
14-
"events": "^3.0.0",
1513
"three": "^0.97.0",
1614
"vue": "^2.5.17",
1715
"vue-class-component": "^6.0.0",

src/views/About.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import * as THREE from "three";
22
import { Component, Vue } from "vue-property-decorator";
33

4-
import { components, GeometryFactory, LightFactory, MaterialFactory } from "@/vue-three";
4+
import {
5+
CameraFactory, components, GeometryFactory, LightFactory, MaterialFactory
6+
} from "@/vue-three";
57

68
import { MyBehaviour } from "./MyBehaviour";
79

8-
console.log(components);
9-
1010
@Component({
1111
components: {
12-
...components
12+
...components,
13+
MyBehaviour
1314
}
1415
})
1516
export default class About extends Vue {
@@ -19,24 +20,39 @@ export default class About extends Vue {
1920
public waterMaterialFactory: MaterialFactory | null = null;
2021

2122
public lightFactory: LightFactory | null = null;
22-
23-
public behaviour = MyBehaviour;
23+
public cameraFactory: CameraFactory | null = null;
2424

2525
public canvas: HTMLCanvasElement | null = null;
2626

2727
public scene1 = {
2828
name: "First scene",
2929
active: true,
30+
camera: {
31+
position: {
32+
x: 0,
33+
y: 10,
34+
z: 0
35+
},
36+
rotation: {
37+
x: 0,
38+
y: 0,
39+
z: 0
40+
}
41+
},
42+
3043
fields: [
3144
{
45+
id: "someId-0",
3246
x: 0,
3347
y: 0
3448
},
3549
{
50+
id: "someId-1",
3651
x: 1,
3752
y: 0
3853
},
3954
{
55+
id: "someId-2",
4056
x: 2,
4157
y: 0
4258
}
@@ -97,6 +113,18 @@ export default class About extends Vue {
97113

98114
return light;
99115
};
116+
117+
this.cameraFactory = async ({ width, height }) => {
118+
const viewAngle = 60;
119+
const nearClipping = 0.1;
120+
const farClipping = 1000;
121+
return new THREE.PerspectiveCamera(
122+
viewAngle,
123+
width / height,
124+
nearClipping,
125+
farClipping
126+
);
127+
};
100128
}
101129

102130
public mounted() {

src/views/About.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<div>
77
<button @click="changeScene(scene1)">Scene 1</button>
88
<button @click="changeScene(scene2)">Scene 2</button>
9-
109
</div>
10+
1111
<canvas ref="canvas" class="webglCanvas"></canvas>
1212
<div v-if="canvas">
1313
<three :canvas="canvas" antialias>
@@ -21,11 +21,11 @@
2121
<geometry name="plane" :factory="planeFactory"/>
2222
</template>
2323

24-
<behaviour :value="behaviour" :data="{}"/>
24+
<camera name="mainCamera" :factory="cameraFactory">
25+
<position :value="scene1.camera.position"/>
26+
<rotation :value="scene1.camera.rotation" rad/>
2527

26-
<camera name="mainCamera">
27-
<position :value="{x: 0, y: 10, z: 0}"/>
28-
<behaviour :value="behaviour"/>
28+
<my-behaviour :data="scene1.camera"/>
2929
</camera>
3030

3131
<light name="light" :factory="lightFactory">
@@ -38,9 +38,9 @@
3838
<shadows receive/>
3939
</mesh>
4040

41-
<mesh v-for="(field, index) in scene1.fields"
42-
:key="field.x + ' ' + field.y"
43-
:name="'field-'+index"
41+
<mesh v-for="field in scene1.fields"
42+
:key="field.id"
43+
:name="'field-'+field.id"
4444
geometry="cube"
4545
material="cubeMat"
4646
>

src/views/MyBehaviour.ts

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,60 @@
1-
import { ThreeApplication } from "@/vue-three/core";
2-
import { BehaviourScript } from "@/vue-three/core/BehaviourScript";
1+
import * as THREE from "three";
2+
import { Component, Mixins, Prop } from "vue-property-decorator";
33

4-
export class MyBehaviour extends BehaviourScript {
5-
public onInitialize(app: ThreeApplication, data: any) {
6-
console.log("my behaviour props", app, data);
4+
import { Behaviour } from "@/vue-three";
5+
6+
import { OrbitControls } from "./OrbitControls";
7+
8+
interface Vec3 {
9+
x: number;
10+
y: number;
11+
z: number;
12+
}
13+
14+
@Component
15+
export class MyBehaviour extends Mixins(Behaviour) {
16+
@Prop()
17+
public data!: {
18+
position: Vec3;
19+
rotation: Vec3;
20+
};
21+
22+
private controls!: OrbitControls;
23+
private camera!: THREE.PerspectiveCamera;
24+
25+
public created() {
26+
console.log("created my behaviour", this);
27+
if (!this.data) {
28+
throw new Error("Could not initialize MyBehaviour: data is missing");
29+
}
30+
this.camera = new THREE.PerspectiveCamera();
31+
this.controls = new OrbitControls(
32+
this.camera,
33+
this.app().renderer.domElement
34+
);
35+
this.camera.position.set(
36+
this.data!.position.x,
37+
this.data!.position.y,
38+
this.data!.position.z
39+
);
40+
this.camera.rotation.set(
41+
this.data!.rotation.x,
42+
this.data!.rotation.y,
43+
this.data!.rotation.z
44+
);
45+
this.data!.position = this.camera.position;
46+
this.data!.rotation = this.camera.rotation;
47+
this.controls.update();
48+
49+
this.ready();
750
}
851

9-
public onUpdate() {
10-
// console.log("my behaviour update");
52+
public beforeDestroy() {
53+
console.log("before destroy mybehaviour");
54+
this.controls.dispose();
1155
}
1256

13-
public onDestroy() {
14-
console.log("my behaviour destroy");
57+
public render(h: any) {
58+
return h("div");
1559
}
1660
}
File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Component, Mixins } from "vue-property-decorator";
2+
3+
import { ThreeComponent, ThreeObjectComponent, ThreeSceneComponent } from "./base";
4+
5+
@Component
6+
export class Behaviour extends Mixins(
7+
ThreeComponent,
8+
ThreeSceneComponent,
9+
ThreeObjectComponent
10+
) {
11+
public ready() {
12+
if ((this as any).update) {
13+
this.app().on("update", (this as any).update);
14+
}
15+
}
16+
17+
public beforeDestroy() {
18+
console.log("before destroy behaviour");
19+
if ((this as any).update) {
20+
this.app().off("update", (this as any).update);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)