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

Threejs创建正多边体

        上一章节实现了球体的绘制,这节来绘制多面体,包括正多面体,平面中,每条边一样长组成的图形叫正多边形,这里每个面一样,叫正多面体。如上文一样,先要创建出基础的组件,包括场景,相机,灯光,渲染器。代码如下:

 initScene() {
      this.scene = new THREE.Scene();//创建一个Scene场景
    },
    initLight(){
      const light = new THREE.DirectionalLight(0xffffff, 1);//创建一个灯光
      this.scene.add(light)
    },
    initCamera(){
      //创建一个透视相机,视角为45度,宽高比为window窗口的宽高比,0.1为近面,10000为远面
      this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
      this.camera.position.set(50,50,50);//设置相机位置
      this.camera.lookAt(0,0,0);//设置相机位置
    },
 initRenderer(){//初始化渲染器
      this.renderer = new THREE.WebGLRenderer({ antialias: true });
      this.container = document.getElementById("container");//获取容器
      this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);//设置渲染器的大小
      this.container.appendChild(this.renderer.domElement);//将渲染器添加到容器中
      this.renderer.setClearColor('#FFFFFF', 1.0);//设置背景颜色
    },
    initOrbitControls(){ //创建鼠标控制工具
      this.orbitControls = new OrbitControls(this.camera, this.renderer.domElement);//创建控制器
    },
    initAnimate() { //循环渲染
      requestAnimationFrame(this.initAnimate);
      this.renderer.render(this.scene, this.camera);
    },

首先绘制一个正四面体,正四面体可以想象得到就是一个三棱锥,只有每面是三角形才能四个面组成一个体。

TetrahedronGeometry的构造函数接受两个参数:

  • radius(半径):这个参数定义了四面体的外接球半径,它决定了四面体的整体大小
  • detail(细节级别):这个可选参数用于细分四面体的面,以增加几何形状的细节。默认值为 0,表示不进行细分。

代码如下:

    initTetrahedronGeometry(){ //四面缓冲几何体
      const geometry = new THREE.TetrahedronGeometry( 1 );
      const material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
      const dodecahedron = new THREE.Mesh( geometry, material );
      this.scene.add( dodecahedron );
    },

效果如下:

 

OctahedronGeometry(八面体几何)是 Three.js 中用于创建八面体模型的几何类。八面体是一种具有八个面的多面体,每个面都是等边三角形,TetrahedronGeometry的构造函数接受两个参数:

  • radius(半径):这个参数定义了四面体的外接球半径,它决定了四面体的整体大小
  • detail(细节级别):这个可选参数用于细分四面体的面,以增加几何形状的细节。默认值为 0,表示不进行细分。

代码如下:

initOctahedronGeometry(){  //八面缓冲几何体
      const geometry = new THREE.OctahedronGeometry( 1 );
      const material = new THREE.MeshBasicMaterial( {color: 0x00ff00,wireframe:true} );
      const dodecahedron = new THREE.Mesh( geometry, material );
      this.scene.add( dodecahedron );
    },

效果如下:

 

DodecahedronGeometry(十二面体几何)是 Three.js 中用于创建十二面体模型的几何类。十二面体是一种具有 12 个面的多面体,每个面都是正五边形。

  • radius(半径):确定十二面体的外接球半径,从而决定十二面体的整体大小。
  • detail(细分层级):可选参数,默认值为 0。它控制几何体的细分程度,当 detail 值大于 0 时,十二面体的每个面会被细分成更多的小三角形,增加几何形状的细节和复杂度。数值越大,细分程度越高。

代码如下:

    initDodecahedronGeometry(){  //十二面缓冲几何体
      const geometry = new THREE.DodecahedronGeometry( 1 );
      const material = new THREE.MeshBasicMaterial( {color: 0x00ff00,wireframe:true} );
      const dodecahedron = new THREE.Mesh( geometry, material );
      this.scene.add( dodecahedron );
    },

 效果如下:

 

IcosahedronGeometry(二十面体几何)是 Three.js 中的一个几何类,用于构建二十面体的几何形状。二十面体是一种具有 20 个等边三角形面的多面体。

  • radius(半径):这一参数定义了二十面体的外接球半径,它决定了二十面体的整体大小。
  • detail(细节层级):这是一个可选参数,默认值为 0。通过调整这个参数,可以对二十面体的每个面进行细分,以增加几何形状的细节。当 detail 为 1 时,每个面将被进一步细分成更小的三角形,从而使整个二十面体看起来更加复杂和精细。

代码如下:

    initIcosahedronGeometry(){ //二十面缓冲几何体
      const geometry = new THREE.IcosahedronGeometry( 1 );
      const material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
      const dodecahedron = new THREE.Mesh( geometry, material );
      this.scene.add( dodecahedron );
    },

效果如下:


http://www.kler.cn/news/328107.html

相关文章:

  • 【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL63
  • Git | Dockerized GitLab 安装使用(简单实操版)
  • 经典sql题(十四)炸裂函数的恢复
  • 【AIGC】ChatGPT提示词助力自媒体内容创作升级
  • 鸿蒙NEXT开发-ArkTS(基于最新api12稳定版)
  • 梯度检查点技术的使用
  • LINUX-线程
  • MySql基础34题写题记录(3-10)
  • 【tbNick专享】虚拟机域控、成员服务器、降级等管理
  • pip install kaggle-environments ISSUE:Failed to build vec-noise
  • MicoZone-Git
  • 深度剖析IT技术前沿:编织数字世界的未来篇章
  • 怎么通过AI大模型开发一个网站?
  • SQL第11课——使用子查询
  • 1.1.5 计算机网络的性能指标(下)
  • 作文网源码 范文论文网模板 带会员系统+支付接口+整站数据
  • docker_阿里云镜像仓库
  • 代码随想录算法训练营第56天 | 1、冗余连接,2、冗余连接II
  • 【数学分析笔记】第4章第2节 导数的意义和性质(1)
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-09-29
  • 谷歌发布Imagen 3,超过SD3、DALL・E-3,谷歌发布新RL方法,性能提升巨大,o1模型已证明
  • Python 封装 socket 为 [TCP/UDP/MULTICAST] 客户端
  • powerbi计算销售额同比增长率
  • MySql Explain优化命令使用
  • Vue实战教程:如何用JS封装一个可复用的Loading组件
  • 基于php的律所管理系统
  • leetcode 513 找到左下角的值
  • SQLite3模块使用详解
  • 使用WebClient 快速发起请求(不使用WebClientUtils工具类)
  • 测试面试题:pytest断言时,数据是符点类型,如何断言?