Moved to composition API

This commit is contained in:
2022-01-29 13:55:47 +03:00
parent fdc17340cf
commit ddf3997cb3
2 changed files with 48 additions and 62 deletions

View File

@@ -1,24 +1,3 @@
# vue-three-d-mockup # vue-three-d-mockup
## Project setup Check out the [demo](https://anatolykopyl.github.io/vue-three-d-mockup/)
```
npm install
```
### Compiles and hot-reloads for development
```
npm run serve
```
### Compiles and minifies for production
```
npm run build
```
### Lints and fixes files
```
npm run lint
```
### Customize configuration
See [Configuration Reference](https://cli.vuejs.org/config/).

View File

@@ -1,8 +1,10 @@
<template> <template>
<div ref="canvas" /> <div ref="container" />
</template> </template>
<script> <script>
import { ref, onMounted } from 'vue';
import * as THREE from 'three'; import * as THREE from 'three';
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader'; import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader';
import phoneObj from './assets/iphone.obj'; import phoneObj from './assets/iphone.obj';
@@ -19,32 +21,32 @@ export default {
default: 'hsl(0, 100%, 100%)', default: 'hsl(0, 100%, 100%)',
}, },
}, },
data() { setup(props) {
return { const idle = ref(true);
idle: true, const container = ref(null);
}; let camera;
}, let scene;
methods: { let phone;
init() { let renderer;
const container = this.$refs.canvas;
function init() {
const environmentInit = () => { const environmentInit = () => {
this.camera = new THREE.PerspectiveCamera( camera = new THREE.PerspectiveCamera(
45, container.clientWidth / container.clientHeight, 0.1, 10000, 45, container.value.clientWidth / container.value.clientHeight, 0.1, 10000,
); );
this.scene = new THREE.Scene(); scene = new THREE.Scene();
const light = new THREE.DirectionalLight(this.lightClr); const light = new THREE.DirectionalLight(props.lightClr);
this.scene.add(light); scene.add(light);
light.position.set(0, 0, 300); light.position.set(0, 0, 300);
this.camera.position.set(0, 0, 200); camera.position.set(0, 0, 200);
}; };
const phoneInit = () => { const phoneInit = () => {
const loader = new OBJLoader(); const loader = new OBJLoader();
const texture = new THREE.TextureLoader().load(this.screenImg); const texture = new THREE.TextureLoader().load(props.screenImg);
const material = new THREE.MeshLambertMaterial({ map: texture }); const material = new THREE.MeshLambertMaterial({ map: texture });
const scale = 6; const scale = 6;
const screen = new THREE.Mesh( const screen = new THREE.Mesh(
@@ -53,15 +55,15 @@ export default {
screen.translateZ(6); screen.translateZ(6);
screen.translateX(1); screen.translateX(1);
this.phone = new THREE.Group(); phone = new THREE.Group();
this.phone.add(screen); phone.add(screen);
loader.load( loader.load(
phoneObj, phoneObj,
(body) => { (body) => {
body.position.y = -60; body.position.y = -60;
this.phone.add(body); phone.add(body);
this.scene.add(this.phone); scene.add(phone);
}, },
); );
}; };
@@ -69,27 +71,32 @@ export default {
environmentInit(); environmentInit();
phoneInit(); phoneInit();
this.renderer = new THREE.WebGLRenderer({ antialias: true }); renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setSize(container.clientWidth, container.clientHeight); renderer.setSize(container.value.clientWidth, container.value.clientHeight);
container.appendChild(this.renderer.domElement); container.value.appendChild(renderer.domElement);
},
animate() {
requestAnimationFrame(this.animate);
if (this.phone) {
this.idleAnimation();
} }
this.renderer.render(this.scene, this.camera);
},
idleAnimation() {
this.phone.rotation.y += 0.02;
},
followAnimation() {
}, function idleAnimation() {
}, phone.rotation.y += 0.02;
mounted() { }
this.init();
this.animate(); function animate() {
requestAnimationFrame(animate);
if (phone) {
idleAnimation();
}
renderer.render(scene, camera);
}
onMounted(() => {
init();
animate();
});
return {
idle,
container,
};
}, },
}; };
</script> </script>