Three.js曲线篇 6.雕刻平面大师shape

说shape是平面雕刻大师一点都不为过。shade可以雕刻出各种复杂的平面,包括房子的那种什么c4d设计图纸。然后通过曲线的拉伸,将建筑立体化。

下面这里演示一个简单的使用场景:

完整代码:

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(100, 100, 100);
    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);

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

    // Shape表示一个平面多边形轮廓
    const shape = new THREE.Shape();
    shape.moveTo(10, 0);// 移动到指定点
    shape.lineTo(100, 0);//.currentPoint变为(100, 0)
    shape.lineTo(100, 100);//.currentPoint变为(100, 100)
    shape.lineTo(10, 100);//.currentPoint变为(10, 100)

    // 创建空
    // Shape内孔轮廓
    const path1 = new THREE.Path();// 圆孔1
    path1.absarc(20, 20, 10);
    const path2 = new THREE.Path();// 圆孔2
    path2.absarc(80, 20, 10);
    const path3 = new THREE.Path();// 方形孔
    path3.moveTo(50, 50);
    path3.lineTo(80, 50);
    path3.lineTo(80, 80);
    path3.lineTo(50, 80);

    //三个内孔轮廓分别插入到holes属性中
    shape.holes.push(path1, path2, path3);

    const geometry = new THREE.ExtrudeGeometry(shape, {
        depth: 20,//拉伸长度
        bevelEnabled: false,//禁止倒角
        curveSegments: 50,
    });
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);
    console.log('currentPoint', shape.currentPoint);


    /* ------------------------------动画函数--------------------------------- */
    const animation = () => {
        controls.update();// 如果不调用,就会很卡
        renderer.render(scene, camera);
        requestAnimationFrame(animation);
    }
    animation();
}
相关推荐
溪语流沙4 分钟前
【每天一个CSS | Day19】流光文字与一笔写成的手写签名
前端·javascript·css
逐光者93310 分钟前
STM32——SPI 屏 · Flash · 高速
java·前端·stm32
To_OC18 分钟前
同样是让 AI 写全栈,为什么别人一句话就够了
前端·前端框架·next.js
CDwenhuohuo31 分钟前
electron pc项目打包成桌面端
前端·javascript·electron
前端大斗师33 分钟前
「大于 1000」把 1000 元那单也算进去了:我在 Vue3 订单页对了 10 句话
前端·人工智能·typescript·大模型·原力计划
Jmyd01231 小时前
用 Web VR 引擎搭建 3D 虚拟展厅:从素材上云到多端实时渲染的完整数据链路
前端·3d·vr·三维数字化
刘天远2 小时前
Agent成本核算实现:事件表、状态分布与Python归集
前端·数据库·人工智能·python
欣欣之王来了2 小时前
开发环境准备:Node.js、npm、VS Code安装配置
前端·学习·架构·项目·vue3教程
xiaolinudao1232 小时前
将多个 Excel 表格中的数据合并到单个表中|6 种实现方案全解析
java·前端·excel
Zldaisy3d2 小时前
北京理工大学 l 金属激光增材制造在线监测技术研究进展
数码相机·3d·制造