2020-09-15 21:36:39 +08:00
|
|
|
# ✨ VueJS 3 + ViteJS + ThreeJS ⚡
|
2020-09-14 23:04:57 +08:00
|
|
|
|
|
|
|
I wanted to try to write a lib similar to [react-three-fiber](https://github.com/react-spring/react-three-fiber) but for VueJS.
|
|
|
|
|
2020-09-15 21:39:24 +08:00
|
|
|
Demos : https://troisjs.github.io/trois/
|
|
|
|
|
2020-09-15 21:36:39 +08:00
|
|
|
I started from scratch... I don't know if I will have time to maintain this, but feel free to participate :)
|
2020-09-15 21:24:47 +08:00
|
|
|
|
2020-09-15 21:39:24 +08:00
|
|
|
*Trois* is a french word, it means *Three*.
|
2020-09-15 21:24:47 +08:00
|
|
|
|
2020-09-16 21:42:44 +08:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
yarn add troisjs
|
|
|
|
|
2020-09-14 23:04:57 +08:00
|
|
|
## PoC
|
|
|
|
|
2020-09-15 17:30:30 +08:00
|
|
|
I first made a simple *Proof of Concept*, take a look at [Test1.vue](/src/components/Test1.vue) :
|
2020-09-14 23:04:57 +08:00
|
|
|
|
|
|
|
```html
|
2020-09-14 23:10:01 +08:00
|
|
|
<Renderer>
|
2020-09-14 23:04:57 +08:00
|
|
|
<PerspectiveCamera :position="{ z: 100 }"></PerspectiveCamera>
|
2020-09-15 21:24:47 +08:00
|
|
|
<PhongMaterial id="material1" color="#ff0000"></PhongMaterial>
|
|
|
|
<LambertMaterial id="material2" color="#0000ff"></LambertMaterial>
|
2020-09-14 23:04:57 +08:00
|
|
|
<Scene>
|
2020-09-14 23:10:01 +08:00
|
|
|
<PointLight :position="{ y: 50, z: 50 }"></PointLight>
|
2020-09-14 23:06:13 +08:00
|
|
|
<Box ref="box" :size="10" material="material1"></Box>
|
|
|
|
<Sphere ref="sphere" :radius="10" :position="{ x: 50 }" material="material2"></Sphere>
|
2020-09-14 23:04:57 +08:00
|
|
|
</Scene>
|
|
|
|
</Renderer>
|
|
|
|
```
|
|
|
|
|
2020-09-15 21:24:47 +08:00
|
|
|
## InstancedMesh
|
|
|
|
|
|
|
|
Take a look at [Test3.vue](/src/components/Test3.vue).
|
|
|
|
|
|
|
|
### Template
|
|
|
|
|
|
|
|
```html
|
|
|
|
<Renderer ref="renderer" :orbit-ctrl="{ enableDamping: true, dampingFactor: 0.05 }">
|
|
|
|
<Camera :position="{ z: 100 }"></Camera>
|
|
|
|
<PhongMaterial id="material" color="#ffffff"></PhongMaterial>
|
|
|
|
<Scene id="scene1">
|
|
|
|
<PointLight color="#ffffff" :intensity="0.5" :position="{ y: 50, z: 50 }"></PointLight>
|
|
|
|
<PointLight color="#ff0000" :intensity="0.5" :position="{ y: -50, z: 50 }"></PointLight>
|
|
|
|
<InstancedMesh ref="imesh" material="material" :count="1000">
|
|
|
|
<SphereGeometry :radius="2"></SphereGeometry>
|
|
|
|
</InstancedMesh>
|
|
|
|
</Scene>
|
|
|
|
</Renderer>
|
|
|
|
```
|
|
|
|
|
|
|
|
### Script : init instanceMatrix
|
|
|
|
|
|
|
|
```js
|
|
|
|
const { randFloat: rnd, randFloatSpread: rndFS } = MathUtils;
|
|
|
|
const imesh = this.$refs.imesh.mesh;
|
|
|
|
const dummy = new Object3D();
|
|
|
|
for (let i = 0; i < 1000; i++) {
|
|
|
|
dummy.position.set(rndFS(100), rndFS(100), rndFS(100));
|
|
|
|
const scale = rnd(0.2, 1);
|
|
|
|
dummy.scale.set(scale, scale, scale);
|
|
|
|
dummy.updateMatrix();
|
|
|
|
imesh.setMatrixAt(i, dummy.matrix);
|
|
|
|
}
|
|
|
|
imesh.instanceMatrix.needsUpdate = true;
|
|
|
|
```
|
|
|
|
|
2020-09-15 22:03:05 +08:00
|
|
|
## GLTF
|
2020-09-15 21:24:47 +08:00
|
|
|
|
|
|
|
Take a look at [TestGLTF.vue](/src/components/TestGLTF.vue).
|
|
|
|
|
|
|
|
```html
|
|
|
|
<GLTFViewer src="test.glb" :camera-position="{ z: 1 }">
|
|
|
|
<AmbientLight></AmbientLight>
|
|
|
|
</GLTFViewer>
|
|
|
|
```
|
|
|
|
|
2020-09-14 23:04:57 +08:00
|
|
|
## Test
|
|
|
|
|
|
|
|
git clone https://github.com/troisjs/trois
|
|
|
|
cd trois
|
|
|
|
yarn
|
|
|
|
yarn dev
|