From 60053beec8514a3639a3bf1286682d5246836b6e Mon Sep 17 00:00:00 2001 From: skyun Date: Wed, 18 Oct 2023 22:53:00 +0800 Subject: [PATCH] Wed Oct 18 22:53:00 CST 2023 inscode --- src/threejs-tools.js | 36 ++++++++++++++++++++++++++++++++++ src/views/basic.vue | 46 +++++++++++++++++++------------------------- 2 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 src/threejs-tools.js diff --git a/src/threejs-tools.js b/src/threejs-tools.js new file mode 100644 index 0000000..7d30d96 --- /dev/null +++ b/src/threejs-tools.js @@ -0,0 +1,36 @@ +import { ref, onMounted } from 'vue' +import * as THREE from 'three' +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +export class ThreejsDefault { + constructor(container) { + // init scene camera + this.scene = new THREE.Scene() + this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000) + this.camera.position.z = 5 + + // init renderer + this.renderer = new THREE.WebGLRenderer() + this.renderer.setSize(window.innerWidth, window.innerHeight) + + // init controls + this.controls = new OrbitControls(this.camera, this.renderer.domElement); + + this.animateFuncs = [] + const animate = () => { + requestAnimationFrame(animate) + this.animateFuncs.forEach(animFunc => animFunc()) + this.renderer.render(this.scene, this.camera) + } + + onMounted(() => { + container.value.appendChild(this.renderer.domElement) + animate() + }) + } + + + onAnimate(animFunc) { + this.animateFuncs.push(animFunc) + } +} diff --git a/src/views/basic.vue b/src/views/basic.vue index 8a818a3..8139b7c 100644 --- a/src/views/basic.vue +++ b/src/views/basic.vue @@ -4,41 +4,35 @@ -- GitLab