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

Three.js之模型对象、材质

参考资料

  • 三维向量Vector3与模型位置
  • 克隆.clone()和复制.copy()

知识点

注:基于Three.jsv0.155.0

  • 三维向量Vector3与模型位置
  • 欧拉Euler与角度属性.rotation
  • 模型材质颜色(Color对象)
  • 模型材质父类Material:透明、面属性
  • 模型材质和几何体属性
  • 克隆.clone()和复制.copy()

代码实现

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Three.js</title>
</head><body></body><!-- 具体路径配置,你根据自己文件目录设置,我的是课件中源码形式 --><script type="importmap">{"imports": {"three": "./js/three.module.js","three/addons/": "../three.js/examples/jsm/"}}</script><script type="module">import * as THREE from 'three';import { OrbitControls } from 'three/addons/controls/OrbitControls.js';const width = 800const height = 500// 场景const scene = new THREE.Scene();// 几何体const geometry = new THREE.BoxGeometry(100, 100, 100);// 材质 const material = new THREE.MeshBasicMaterial({color:0x0000ff,});// 网格模型:物体const mesh = new THREE.Mesh(geometry, material);// 位置属性mesh.position.set(0, 0, 0);// mesh.position.x = 100;// mesh.scale.set(2, 2, 2);// mesh.scale.x = 2;// mesh.translateOnAxis(new THREE.Vector3(1, 0, 1), 100);// mesh.translateX(100);scene.add(mesh);// 角度属性// mesh.rotation.x = Math.PI / 4;// mesh.rotation.set(0, 0, Math.PI / 4);// mesh.rotateX(Math.PI / 4);console.log('🚀 ~ file: 3.1三维向量Vector3与模型位置.html:46 ~ mesh.rolation:', mesh.rotation)// 材料颜色// mesh.material.color.set('red');// mesh.material.color.set('#ff0000');// mesh.material.color.set(0xff0000);mesh.material.color.r = 1;// 材料透明度mesh.material.opacity = 0.8;mesh.material.transparent = true; // 是否透明// 材料面属性// mesh.material.side = THREE.FrontSide; // 正面可见mesh.material.side = THREE.DoubleSide; // 两面可见console.log('🚀 ~ file: 3.1三维向量Vector3与模型位置.html:66 ~ mesh.material.side:', mesh.material.side) // 2console.log('🚀 ~ file: 3.1三维向量Vector3与模型位置.html:63 ~ mesh.material:', mesh.material)console.log('🚀 ~ file: 3.1三维向量Vector3与模型位置.html:89 ~ mesh.geometry:', mesh.geometry)// 克隆、复制const mesh2 = mesh.clone();mesh2.position.set(200, 0, 0);mesh2.material = mesh.material.clone();mesh2.material.color.set(0xff0000);mesh2.position.copy(mesh.position);mesh2.position.y += 150;scene.add(mesh2);// 坐标系const axes = new THREE.AxesHelper(200);scene.add(axes);// 相机const camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);camera.position.set(200, 200, 200);camera.lookAt(scene.position);// 渲染器const renderer = new THREE.WebGLRenderer();renderer.setSize(width, height);renderer.render(scene, camera);document.body.appendChild(renderer.domElement);// 控制器const controls = new OrbitControls(camera, renderer.domElement);controls.addEventListener('change', () => {renderer.render(scene, camera);});// 渲染循环function render() {mesh.rotateY(0.01);// mesh旋转动画// 同步mesh2和mesh的姿态角度一样,不管mesh姿态角度怎么变化,mesh2始终保持同步mesh2.rotation.copy(mesh.rotation);renderer.render(scene, camera);requestAnimationFrame(render);}render();</script>
</html>
http://www.lryc.cn/news/150445.html

相关文章:

  • uniapp启动微信小程序开发者工具报错Enable IDE Service (y/N) 
  • Gitee注册和使用
  • 【Flutter】下载安装Flutter并使用学习dart语言
  • HTTP返回状态值详解整理
  • python web 开发与 Node.js + Express 创建web服务器入门
  • OpenCV(三):Mat类数据的读取
  • 区块链BaaS篇
  • C++|观察者模式
  • 【java 入侵 C# 之路】1-入门
  • 【git】从一个git仓库迁移到另外一个git仓库
  • 控制goroutine 的并发执行数量
  • 深入解析即时通讯App开发中的关键技术
  • ClickHouse进阶(三):ClickHouse 索引
  • 四、MySQL(表操作)如何添加字段?修改表?删除字段?修改表名?删除表?格式化某张表?
  • docker启动paddlespeech服务,并使用接口调用
  • 如何训练ChatGPT以生成音乐和创意艺术作品?
  • 北约报告:2023-2043,下一代量子技术的发展与挑战
  • arm版Linux下安装es集群
  • vConsole调试工具的三种使用方式
  • 不用订阅,不用破解,永久免费使用Axure最新版教程
  • 合宙Air724UG LuatOS-Air LVGL API控件--复选框 (Checkbox)
  • 使用nps实现内网穿透
  • 时序预测 | MATLAB实现TCN-BiGRU时间卷积双向门控循环单元时间序列预测
  • websocket和uni-app里使用websocket
  • Opencv-C++笔记 (18) : 轮廓和凸包
  • 【半监督医学图像分割】2022-MedIA-UWI
  • python发送邮件
  • gitee上传本地项目bug
  • 自然语言处理2-NLP
  • 穿上App外衣,保持Web灵魂——PWA温故