three CurvePath 使用小结

73 阅读1分钟

CurvePath 是什么有什么作用

  • CurvePath 是 Curve 的子类
  • 能够将多个线段拼接起来
  • 能将线段做为动画的运动轨迹

使用示例


<template>
    <div id="parkingLot" ref="parkingLot">
    </div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import * as THREE from 'three';
const parkingLot = ref();


onMounted(() => {
    const DOMEl = parkingLot.value;
    // 获取 DOMEl 的宽度和高度,以设置渲染器的大小。
    const width = DOMEl.clientWidth;
    const height = DOMEl.clientHeight;


    const renderer = new THREE.WebGLRenderer();
    renderer.setSize( width, height );
    DOMEl.appendChild( renderer.domElement );
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera( 45, width / height, 1, 2000 );
    camera.position.set( 0, 300, 800 );
    camera.lookAt( 0, 0, 0 );

    // 创建多条曲线
    const curve1 = new THREE.CubicBezierCurve3(
        new THREE.Vector3(-10, 0, 0),
        new THREE.Vector3(-5, 15, 0),
        new THREE.Vector3(5, -15, 0),
        new THREE.Vector3(10, 0, 0)
    );

    const curve2 = new THREE.QuadraticBezierCurve3(
        new THREE.Vector3(10, 0, 0),
        new THREE.Vector3(15, 15, 0),
        new THREE.Vector3(20, 0, 0)
    );
    // 创建 CurvePath 对象并将曲线添加进去
    const path = new THREE.CurvePath();
    path.add(curve1);
    path.add(curve2);

    // 创建路径几何体
    const geometry = new THREE.TubeGeometry(path, 100, 1, 8, false);
    const material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
    const mesh = new THREE.Mesh(geometry, material);

    scene.add(mesh);

    
    // 创建光源
    const light = new THREE.DirectionalLight(0x0000ff, 10);
    light.position.set(10, 10, 10);
    light.castShadow = true;
    scene.add(light);


    renderer.render(scene, camera);

});
</script>
<style lang="scss" scoped="scoped">

    #parkingLot {
        width: 940px;
        height: 940px;
        border: 1px solid #ccc;
        margin: 30px auto;
    }

</style>