提供一段vue下的threejs案例代码
时间: 2023-04-02 10:04:15 浏览: 158
以下是一个简单的 Vue 和 Three.js 的案例代码:
<template>
<div ref="container"></div>
</template>
<script>
import * as THREE from 'three';
export default {
mounted() {
// 创建场景
const scene = new THREE.Scene();
// 创建相机
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
.1,
100
);
camera.position.z = 5;
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
this.$refs.container.appendChild(renderer.domElement);
// 创建立方体
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
// 渲染场景
const animate = function () {
requestAnimationFrame(animate);
cube.rotation.x += .01;
cube.rotation.y += .01;
renderer.render(scene, camera);
};
animate();
},
};
</script>
阅读全文