当前位置: 首页 > news >正文

直接使用Three.js的 Shape和ExtrudeGeometry创建带孔几何体实现挖孔效果

我们可以通过直接使用Three.js的 ShapeExtrudeGeometry 来创建带孔的几何体,然后将其合并到一个立方体几何体中来实现挖孔效果,而不使用第三方库。以下是一个示例代码:

<!DOCTYPE html>
<html><head><title>Three.js Shape with Holes Created By JoyNop.COM</title><style>body { margin: 0; }canvas { display: block; }</style></head><body><script type="module">import * as THREE from 'https://cdn.skypack.dev/three@0.150.1';// Scene setupconst scene = new THREE.Scene();const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);const renderer = new THREE.WebGLRenderer();renderer.setSize(window.innerWidth, window.innerHeight);document.body.appendChild(renderer.domElement);// Create a shape with a holeconst shape = new THREE.Shape();shape.moveTo(0, 0);shape.lineTo(4, 0);shape.lineTo(4, 4);shape.lineTo(0, 4);shape.lineTo(0, 0);const hole = new THREE.Path();hole.moveTo(1, 1);hole.lineTo(3, 1);hole.lineTo(3, 3);hole.lineTo(1, 3);hole.lineTo(1, 1);shape.holes.push(hole);const extrudeSettings = { depth: 2, bevelEnabled: false };const shapeGeometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);const shapeMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });const shapeMesh = new THREE.Mesh(shapeGeometry, shapeMaterial);// Create a cubeconst cubeGeometry = new THREE.BoxGeometry(4, 4, 4);const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true });const cubeMesh = new THREE.Mesh(cubeGeometry, cubeMaterial);// Position the shape so it intersects with the cubeshapeMesh.position.set(-2, -2, -1);// Add the shape and cube to the scenescene.add(cubeMesh);scene.add(shapeMesh);// Camera positioncamera.position.z = 10;// Render loopfunction animate() {requestAnimationFrame(animate);cubeMesh.rotation.x += 0.01;cubeMesh.rotation.y += 0.01;shapeMesh.rotation.x += 0.01;shapeMesh.rotation.y += 0.01;renderer.render(scene, camera);}animate();</script></body>
</html>

在这里插入图片描述

在这个示例中,我们创建了一个带孔的形状,并将其挤出成几何体。然后我们创建了一个立方体,并将带孔的几何体和立方体添加到场景中。通过调整位置和旋转,可以直观地看到挤出的带孔形状与立方体的相对位置。

虽然这段代码没有使用布尔运算来直接“挖孔”,但你可以通过调整位置和深度,使得带孔形状部分覆盖立方体,从而实现类似的视觉效果。如果你需要真正的布尔运算以移除立方体的部分,你可能需要使用三方库,因为Three.js本身并不直接支持复杂的布尔运算。

相关文章:

  • Go模板页面浏览器显示HTML源码问题
  • 百度OCR初探-python
  • 怎么提升机器人外呼的转化效率
  • linux网络编程poll笔记
  • 实验12 路由重分布
  • STL-常用容器
  • Elastic 索引结构-倒排索引
  • 【AI绘画】Stable Diffusion 3开源
  • Linux3(进程 编辑文件 用户管理 网络)
  • C#A类调用B类的方法,在方法中更新B类的控件
  • c#中上传超过30mb的文件,接口一直报404,小于30mb的却可以上传成功
  • Java多线程-StampedLock(原子读写锁)
  • 小学生期刊知网//《小学生》评职称认可吗?
  • 笔记-前端
  • Android 列表视频滑动自动播放(实现思路)
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • Java的Interrupt与线程中断
  • Node 版本管理
  • Python实现BT种子转化为磁力链接【实战】
  • Redis学习笔记 - pipline(流水线、管道)
  • Webpack 4 学习01(基础配置)
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 优化 Vue 项目编译文件大小
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • ​ubuntu下安装kvm虚拟机
  • ​什么是bug?bug的源头在哪里?
  • ######## golang各章节终篇索引 ########
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (待修改)PyG安装步骤
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (七)glDrawArry绘制
  • (十) 初识 Docker file
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • ***测试-HTTP方法
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .net core 6 集成和使用 mongodb
  • .Net 基于.Net8开发的一个Asp.Net Core Webapi小型易用框架
  • .NET导入Excel数据
  • .Net多线程Threading相关详解
  • .NET框架
  • .vimrc 配置项
  • /dev/sda2 is mounted; will not make a filesystem here!
  • @cacheable 是否缓存成功_Spring Cache缓存注解
  • @RunWith注解作用
  • @四年级家长,这条香港优才计划+华侨生联考捷径,一定要看!
  • [ CTF ]【天格】战队WriteUp- 2022年第三届“网鼎杯”网络安全大赛(青龙组)
  • [<死锁专题>]
  • [17]JAVAEE-HTTP协议
  • [2021ICPC济南 L] Strange Series (Bell 数 多项式exp)
  • [AutoSar]BSW_Com07 CAN报文接收流程的函数调用