Skip to content

Commit 16e2233

Browse files
authored
Merge pull request #1 from sascha245/feature/multiple_renderer
Feature/multiple renderer
2 parents 5e24c85 + eaae026 commit 16e2233

19 files changed

+584
-255
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,4 @@ This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md
185185
## Acknowledgments
186186

187187
- First version of a input manager based on [pinput](https://github.com/ichub/pinput)
188+
- Handling of multiple renderer inspired by [vue-gl](https://github.com/vue-gl/vue-gl)

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-threejs-composer",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"main": "build/src/index.js",
55
"typings": "build/src/index.d.ts",
66
"repository": "https://github.com/sascha245/vue-threejs-composer",
@@ -12,7 +12,7 @@
1212
],
1313
"scripts": {
1414
"serve": "vue-cli-service serve ./samples/main.ts",
15-
"build": "tsc -p tsconfig.prod.json",
15+
"build": "rimraf ./build && tsc -p tsconfig.prod.json",
1616
"lint": "vue-cli-service lint",
1717
"test:unit": "vue-cli-service test:unit",
1818
"validate": "npm run build && npm run test:unit && npm pack --dry-run"
@@ -33,6 +33,7 @@
3333
"@vue/test-utils": "^1.0.0-beta.20",
3434
"babel-core": "7.0.0-bridge.0",
3535
"lint-staged": "^7.2.2",
36+
"rimraf": "^2.6.2",
3637
"three": "^0.97.0",
3738
"ts-jest": "^23.0.0",
3839
"tslint-config-prettier": "^1.15.0",

samples/views/Demo.ts

+48-30
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import {
77
Application, AssetTypes, CameraFactory, components, GeometryFactory, LightFactory,
88
MaterialFactory
99
} from "../../src";
10+
import { HoverBehaviour } from "./HoverBehaviour";
1011
import { MyBehaviour } from "./MyBehaviour";
1112

1213
(window as any).THREE = THREE;
1314

1415
@Component({
1516
components: {
1617
...components,
17-
MyBehaviour
18+
MyBehaviour,
19+
HoverBehaviour
1820
}
1921
})
2022
export default class About extends Vue {
@@ -42,10 +44,10 @@ export default class About extends Vue {
4244
public cameraFactory: CameraFactory | null = null;
4345

4446
public canvas: HTMLCanvasElement | null = null;
47+
public canvas2: HTMLCanvasElement | null = null;
4548

4649
public scene1 = {
47-
name: "First scene",
48-
active: true,
50+
name: "scene1",
4951
camera: {
5052
position: {
5153
x: 0,
@@ -58,34 +60,18 @@ export default class About extends Vue {
5860
z: 0
5961
}
6062
},
61-
62-
fields: [
63-
{
64-
id: "someId-0",
65-
x: 0,
66-
y: 0
67-
},
68-
{
69-
id: "someId-1",
70-
x: 1,
71-
y: 0
72-
},
73-
{
74-
id: "someId-2",
75-
x: 2,
76-
y: 0
77-
}
78-
]
63+
fields: new Array()
7964
};
8065
public scene2 = {
81-
name: "Second scene",
82-
active: true
66+
name: "scene2"
8367
};
8468

8569
public isLoading = true;
8670
public loadingAmount = 0;
8771
public loadingTotal = 0;
8872

73+
public activeScene = this.scene1.name;
74+
8975
public scenes = [this.scene1, this.scene2];
9076

9177
public startLoading() {
@@ -103,15 +89,27 @@ export default class About extends Vue {
10389
}
10490

10591
public changeScene(pScene: any) {
106-
this.scenes.forEach(scene => {
107-
scene.active = false;
108-
});
109-
pScene.active = true;
92+
this.activeScene = pScene.name;
93+
// this.scenes.forEach(scene => {
94+
// scene.active = false;
95+
// });
96+
// pScene.active = true;
11097
console.log("about scene change", this.scenes);
11198
}
11299

113100
public created() {
114-
console.log(this.scene1);
101+
let idx = 0;
102+
for (let x = 0; x < 5; ++x) {
103+
for (let z = 0; z < 5; ++z) {
104+
this.scene1.fields.push({
105+
x,
106+
z,
107+
id: `field_${idx}`,
108+
y: 0
109+
});
110+
++idx;
111+
}
112+
}
115113

116114
this.cubeFactory = async () => {
117115
// await new Promise(r => setTimeout(r, 2000));
@@ -158,13 +156,13 @@ export default class About extends Vue {
158156
return light;
159157
};
160158

161-
this.cameraFactory = async ({ width, height }) => {
159+
this.cameraFactory = async () => {
162160
const viewAngle = 60;
163161
const nearClipping = 0.1;
164162
const farClipping = 1000;
165163
return new THREE.PerspectiveCamera(
166164
viewAngle,
167-
width / height,
165+
window.innerWidth / window.innerHeight,
168166
nearClipping,
169167
farClipping
170168
);
@@ -173,5 +171,25 @@ export default class About extends Vue {
173171

174172
public mounted() {
175173
this.canvas = this.$refs.canvas as HTMLCanvasElement;
174+
this.canvas2 = this.$refs.canvas2 as HTMLCanvasElement;
176175
}
177176
}
177+
178+
// <!-- Asset management update ideas: allow to split up and depend on asset bundles (that can be either preloaded or loaded during runtime) -->
179+
// <!-- <assets-bundle name="PolygonMini" preload>
180+
// <texture name="PolygonMini_Tex" src="/assets/textures/PolygonMinis_Texture_01.png"/>
181+
// <material name="PolygonMini_Mat" :factory="polygonMaterialFactory"/>
182+
183+
// <model name="grassModel" src="/assets/models/SM_Env_Grass_01.fbx" materials="PolygonMini_Mat"/>
184+
// <model name="PM_column" src="/assets/models/SM_Tile_Hex_Column_02.fbx" materials="PolygonMini_Mat"/>
185+
// <model name="PM_flat" src="/assets/models/SM_Tile_Hex_Flat_01.fbx" materials="PolygonMini_Mat"/>
186+
// </assets-bundle>
187+
188+
// <assets-bundle name="crate" preload>
189+
// <texture name="crateTex" src="/assets/textures/crate.jpg"/>
190+
// </assets-bundle>
191+
192+
// <assets-bundle name="Scene1" dependencies="crate" preload>
193+
// <geometry name="cube" :factory="cubeFactory"/>
194+
// <material name="cubeMat" :factory="cubeMaterialFactory"/>
195+
// </assets-bundle> -->

samples/views/Demo.vue

+39-17
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
<button @click="changeScene(scene2)">Scene 2</button>
99
</div>
1010

11-
<div class="screen">
12-
<canvas ref="canvas" class="screen-canvas"></canvas>
13-
<div class="screen-loading" v-if="isLoading">
14-
<div>
15-
<p>Loading...</p>
16-
<p>{{loadingAmount}} / {{loadingTotal}}</p>
11+
<div>
12+
<div class="screen">
13+
<canvas ref="canvas" class="screen-canvas"></canvas>
14+
<div class="screen-loading" v-if="isLoading">
15+
<div>
16+
<p>Loading...</p>
17+
<p>{{loadingAmount}} / {{loadingTotal}}</p>
18+
</div>
1719
</div>
1820
</div>
1921
</div>
22+
2023
<div v-if="canvas">
21-
<three :canvas="canvas" antialias>
24+
<three>
25+
<renderer :canvas="canvas" camera="main" :scene="activeScene" antialias shadows/>
2226

23-
<scene :name="scene1.name" :active.sync="scene1.active" @load="startLoading" @load-progress="loadingProgress" @loaded="finishLoading">
27+
<scene name="scene1" @load="startLoading" @load-progress="loadingProgress" @loaded="finishLoading">
2428
<template slot="preload">
2529
<div>
2630
<texture name="crateTex" src="/assets/textures/crate.jpg"/>
@@ -43,15 +47,20 @@
4347

4448
<fog exp2/>
4549

46-
<camera :factory="cameraFactory">
50+
<camera name="main" :factory="cameraFactory">
4751
<position :value="scene1.camera.position"/>
4852
<rotation :value="scene1.camera.rotation" rad/>
4953

5054
<my-behaviour :data="scene1.camera"/>
5155
</camera>
5256

57+
<camera name="secondary" :factory="cameraFactory">
58+
<position :value="{ x: 0, y: 10, z: 0 }"/>
59+
<rotation :value="{ x: -90, y: 0, z: 0 }"/>
60+
</camera>
61+
5362
<light name="sun" :factory="lightFactory">
54-
<position :value="{x: 0, y: 10, z: 0}"/>
63+
<position :value="{x: -5, y: 10, z: -5}"/>
5564
<shadows cast/>
5665
</light>
5766

@@ -95,15 +104,17 @@
95104
geometry="cube"
96105
material="cubeMat"
97106
>
98-
<position :value="{ x: field.x * 2, y: 0.5, z: field.y * 2}"/>
107+
<position :value="{ x: field.x * 2, y: field.y + 5, z: field.z * -2}"/>
99108
<scale :value="{ x: 1.2, y: 1.2, z: 1.2}"/>
100109
<shadows cast receive/>
110+
111+
<!-- <hover-behaviour :position="field" :distance="5"/> -->
101112
</mesh>
102113
</group>
103114

104115
</scene>
105116

106-
<scene :name="scene2.name" :active.sync="scene2.active" @load="startLoading" @load-progress="loadingProgress" @loaded="finishLoading">
117+
<scene name="scene2" @load="startLoading" @load-progress="loadingProgress" @loaded="finishLoading">
107118
<!-- <template slot="preload">
108119
<material name="scene2_mat" :factory="materialFactory"/>
109120
<geometry name="scene2_field" :factory="geometryFactory"/>
@@ -121,16 +132,27 @@
121132
</template>
122133

123134
<style>
135+
.fullscreen {
136+
position: absolute;
137+
top: 0;
138+
left: 0;
139+
width: 100%;
140+
height: 100vh;
141+
overflow: hidden;
142+
z-index: -1;
143+
}
144+
124145
.screen {
125146
position: relative;
126-
width: 80%;
127-
height: auto;
128-
margin: auto;
129-
max-height: 100vh;
147+
width: 100%;
148+
height: 100%;
149+
/* height: auto; */
150+
/* margin: auto; */
151+
/* max-height: 100vh; */
130152
}
131153
.screen-canvas {
132154
width: 100% !important;
133-
height: auto !important;
155+
height: 100% !important;
134156
max-height: 100vh;
135157
}
136158
.screen-loading {

samples/views/HoverBehaviour.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Component, Mixins, Prop } from "vue-property-decorator";
2+
3+
import { Behaviour } from "../../src";
4+
5+
@Component
6+
export class HoverBehaviour extends Mixins(Behaviour) {
7+
@Prop({ required: true, type: Object })
8+
public position!: { y: number };
9+
10+
@Prop({ type: Number, default: 1 })
11+
public distance!: number;
12+
13+
@Prop({ type: Number, default: 1 })
14+
public speed!: number;
15+
16+
private m_moveUp = true;
17+
private m_originalY = 0;
18+
19+
public created() {
20+
// access app
21+
const app = this.app();
22+
23+
// access scene if behaviour is placed in a scene
24+
const scene = this.scene();
25+
26+
// access object if behaviour is placed in an object
27+
const object = this.object!();
28+
29+
this.m_originalY = this.position.y;
30+
31+
// once your component is ready
32+
this.ready();
33+
}
34+
35+
// lifecycle function called before each frame (optional)
36+
public update(deltaTime: number) {
37+
this.position.y += deltaTime * (this.m_moveUp ? 1 : -1) * this.speed;
38+
39+
if (this.position.y > this.m_originalY + this.distance) {
40+
this.m_moveUp = false;
41+
} else if (this.position.y < this.m_originalY - this.distance) {
42+
this.m_moveUp = true;
43+
}
44+
}
45+
46+
public beforeDestroy() {
47+
// dispose everything that needs to be disposed here
48+
}
49+
50+
public render(h: any) {
51+
return h("div");
52+
}
53+
}

samples/views/MyBehaviour.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ export class MyBehaviour extends Mixins(Behaviour) {
2626
throw new Error("Could not initialize MyBehaviour: data is missing");
2727
}
2828
this.camera = new THREE.PerspectiveCamera();
29-
this.controls = new OrbitControls(
30-
this.camera,
31-
this.app().renderer.domElement
32-
);
29+
this.controls = new OrbitControls(this.camera);
3330
this.camera.position.set(
3431
this.data!.position.x,
3532
this.data!.position.y,

0 commit comments

Comments
 (0)