Three.js曲线篇 8.管道漫游

目录

创建样条曲线

创建管道

透视相机漫游

完整代码


大家不要被这个"管道漫游"这几个字所蒙骗了,学完后大家就知道这个知识点有多脏了。我也是误入歧途,好奇了一下"管道漫游"。好了,现在就给大家展示一下为啥这个只是点脏了。

我也废话少说了,带大家实现这个轨道漫游。

创建样条曲线

javascript 复制代码
    // 3d样条曲线
    const path = new THREE.CatmullRomCurve3([
        new THREE.Vector3(-50, 20, 90),
        new THREE.Vector3(-10, 40, 40),
        new THREE.Vector3(0, 0, 0),
        new THREE.Vector3(60, -60, 0),
        new THREE.Vector3(90, -40, 60),
        new THREE.Vector3(120, 30, 30),
    ]);

穿件完样条曲线后,然后需要 TubeGeometry 来创建管道。

创建管道

javascript 复制代码
    // 创建管道
    const geometry = new THREE.TubeGeometry(path, 200, 5, 30);
    const texLoader = new THREE.TextureLoader();
    const url = new URL('../../assets/images/gd.png', import.meta.url).href;
    const texture = texLoader.load(url);
    texture.wrapS = THREE.RepeatWrapping;
    texture.wrapT = THREE.RepeatWrapping;
    const material = new THREE.MeshBasicMaterial({ map: texture, side: THREE.DoubleSide });
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

透视相机漫游

透视相机漫游,其实就是按照按照样条曲线的轨迹来设置,通过 样条曲线上的一个方法 getSpacedPoints 可以获取样条曲线上一定的点位数量,然后使用循环来设置相机的位置。

javascript 复制代码
    // 从曲线上等间距获取一定数量点坐标
    const pointsArr = path.getSpacedPoints(500);
    let i = 0;

    /* ------------------------------动画函数--------------------------------- */
    const animation = () => {
        if (i < pointsArr.length - 1) {
            // 相机位置设置在当前点位置
            camera.position.copy(pointsArr[i]);
            // 曲线上当前点pointsArr[i]和下一个点pointsArr[i+1]近似模拟当前点曲线切线
            // 设置相机观察点为当前点的下一个点,相机视线和当前点曲线切线重合
            camera.lookAt(pointsArr[i + 1]);
            camera.updateProjectionMatrix();
            controls.target.copy(pointsArr[i + 1]);
            i += 1; //调节速度
        } else {
            i = 0
        }
        controls.update();// 如果不调用,就会很卡
        renderer.render(scene, camera);
        requestAnimationFrame(animation);
    }
    animation();

这里给大家带来的知识点就是利用线条来实现相机漫游的效果。

完整代码

javascript 复制代码
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'


export default (domId) => {
    /* ------------------------------初始化三件套--------------------------------- */
    const dom = document.getElementById(domId);
    const { innerHeight, innerWidth } = window

    const scene = new THREE.Scene();

    const camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 1, 2000);
    camera.position.set(50, 50, 50);
    camera.lookAt(scene.position);

    const renderer = new THREE.WebGLRenderer({
        antialias: true,// 抗锯齿
        alpha: false,// 透明度
        powerPreference: 'high-performance',// 性能
        // logarithmicDepthBuffer: true,// 深度缓冲
    })
    renderer.shadowMap.enabled = true;// 开启阴影
    renderer.shadowMap.type = THREE.PCFSoftShadowMap;// 阴影类型
    renderer.outputEncoding = THREE.sRGBEncoding;// 输出编码
    renderer.toneMapping = THREE.ACESFilmicToneMapping;// 色调映射
    renderer.toneMappingExposure = 1;// 色调映射曝光
    renderer.physicallyCorrectLights = true;// 物理正确灯光
    renderer.setPixelRatio(window.devicePixelRatio);// 设置像素比
    renderer.setSize(innerWidth, innerHeight);// 设置渲染器大小
    dom.appendChild(renderer.domElement);

    // 重置大小
    window.addEventListener('resize', () => {
        const { innerHeight, innerWidth } = window
        camera.aspect = innerWidth / innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(innerWidth, innerHeight);
    })

    /* ------------------------------初始化工具--------------------------------- */
    const controls = new OrbitControls(camera, renderer.domElement) // 相机轨道控制器
    controls.enableDamping = true // 是否开启阻尼
    controls.dampingFactor = 0.05// 阻尼系数
    controls.panSpeed = -1// 平移速度

    const axesHelper = new THREE.AxesHelper(10);
    scene.add(axesHelper);

    /* ------------------------------正题--------------------------------- */

    // 3d样条曲线
    const path = new THREE.CatmullRomCurve3([
        new THREE.Vector3(-50, 20, 90),
        new THREE.Vector3(-10, 40, 40),
        new THREE.Vector3(0, 0, 0),
        new THREE.Vector3(60, -60, 0),
        new THREE.Vector3(90, -40, 60),
        new THREE.Vector3(120, 30, 30),
    ]);
    // 创建管道
    const geometry = new THREE.TubeGeometry(path, 200, 5, 30);
    const texLoader = new THREE.TextureLoader();
    const url = new URL('../../assets/images/gd.png', import.meta.url).href;
    const texture = texLoader.load(url);
    texture.wrapS = THREE.RepeatWrapping;
    texture.wrapT = THREE.RepeatWrapping;
    const material = new THREE.MeshBasicMaterial({ map: texture, side: THREE.DoubleSide });
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

    // 从曲线上等间距获取一定数量点坐标
    const pointsArr = path.getSpacedPoints(500);
    let i = 0;

    /* ------------------------------动画函数--------------------------------- */
    const animation = () => {
        if (i < pointsArr.length - 1) {
            // 相机位置设置在当前点位置
            camera.position.copy(pointsArr[i]);
            // 曲线上当前点pointsArr[i]和下一个点pointsArr[i+1]近似模拟当前点曲线切线
            // 设置相机观察点为当前点的下一个点,相机视线和当前点曲线切线重合
            camera.lookAt(pointsArr[i + 1]);
            camera.updateProjectionMatrix();
            controls.target.copy(pointsArr[i + 1]);
            i += 1; //调节速度
        } else {
            i = 0
        }
        controls.update();// 如果不调用,就会很卡
        renderer.render(scene, camera);
        requestAnimationFrame(animation);
    }
    animation();
}

最后,透露一下为啥脏。

这颜色,这个洞,这个词,总觉得怪怪的。

相关推荐
用户059540174461 小时前
把AI对话记忆存储测试从手工改成Playwright+pytest,覆盖率从20%提到96%,回归时间缩短90%
前端·css
kyriewen1 小时前
别再这样写TypeScript了——Code Review中最常见的8个反模式
前端·javascript·typescript
名字还没想好☜2 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
午安~婉2 小时前
GitHub Token/ GitHub Stats统计显示图异常
前端·github·vercel·github token
码云之上2 小时前
AI Agent 工程化总览篇:从 Prompt 到 Harness
前端·人工智能
2501_926978332 小时前
以说明书 DNA 为模板——完整 AGI 的结构图景
前端·人工智能·经验分享·笔记·ai写作
IT_陈寒2 小时前
Vite静态资源引用这个坑我踩得有点疼
前端·人工智能·后端
程序员黑豆3 小时前
鸿蒙应用开发:AttributeModifier 使用教程
前端·harmonyos
codeniu3 小时前
TRAE Work 实战 | 从"有个想法"到线上Demo,我全程只靠对话就完成了
前端·trae
妙码生花3 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(五十):增加管理员角色组管理
前端·后端·ai编程