效果
代码展示
主要思路是先创建指定曲线路径,并将其分为1000个点,然后按照自定义路线从头遍历点的位置,然后修改物体位置以及朝向等,实现三维立体物体沿指定曲线循环移动的效果。这里以vue3代码为例,进行展示
<template><div class="threejs"><div id="containerId" /></div>
</template><script setup>
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls';
import { ref, onMounted } from 'vue';
let scene;const camera = ref();
const cameraCurve = ref();
const renderer = ref();
const container = ref();
const controls = ref();
const pathIndex = ref(1000); //小车的运动轨迹点索引
// const agv = ref();
let cube;function initScene() {scene = new THREE.Scene();
}
function initCamera() {camera.value = new THREE.PerspectiveCamera(65,window.innerWidth / window.innerHeight,0.1,10000);camera.value.position