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

Three.js 设置模型材质纹理贴图和修改材质颜色,材质透明度,材质网格

相关API的使用:

1 traverse (模型循环遍历方法)

2. THREE.TextureLoader(用于加载和处理图片纹理)

3. THREE.MeshLambertMaterial(用于创建材质)

4. getObjectByProperty(通过材质的属性值获取材质信息)

在上一篇 Three.js加载外部glb,fbx,gltf,obj 模型文件 的文章基础上加入onSetSystemModelMap (设置模型材质方法)和onSetModelMaterial(设置材质属性方法)
首先在 setModel 方法里获取到当前模型所有的材质 (traverse

setModel({ filePath, fileType, scale,  position }) {return new Promise((resolve, reject) => {const loader = this.fileLoaderMap[fileType]loader.load(filePath, (result) => {//加载不同类型的文件switch (fileType) {case 'glb':this.model = result.scene		break;case 'fbx':this.model = resultbreak;case 'gltf':this.model = result.scenebreak;case 'obj':this.model = resultbreak;default:break;}this.model.traverse((v) => {const { uuid } = vif (v.isMesh &&  v.materia) {this.modelMaterialList.push(v)}})// 设置模型大小if (scale) {this.model.scale.set(scale, scale, scale);}// 设置模型位置 if (position) {const { x, y, z } = positionthis.model.position.set(x, y, z)}// 设置相机位置this.camera.position.set(0, 2, 6)// 设置相机坐标系this.camera.lookAt(0, 0, 0)// 将模型添加到场景中去   this.scene.add(this.model)resolve(true)}, () => {}, (err) => {console.log(err)reject()})})}

通过 getObjectByProperty 方法传入 uuid 获取到当前材质信息
设置模型贴图

	onSetSystemModelMap({ url }) {// 当前uuid const uuid = store.state.selectMesh.uuid// 通过uuid 获取需要设置的材质const mesh = this.scene.getObjectByProperty('uuid', uuid)const { name, color } = mesh.material// 获取到贴图 url(图片实际地址)const mapTexture = new THREE.TextureLoader().load(url)mesh.material = new THREE.MeshLambertMaterial({map: mapTexture,transparent: true, // 允许材质可透明color,name,})}

设置材质属性

	// 设置材质属性onSetModelMaterial(config) {const { color, wireframe, depthWrite, opacity } = configconst uuid = store.state.selectMesh.uuidconst mesh = this.scene.getObjectByProperty('uuid', uuid)if (mesh && mesh.material) {//设置材质颜色mesh.material.color.set(new THREE.Color(color))//设置网格mesh.material.wireframe = wireframe// 设置深度写入mesh.material.depthWrite = depthWrite//设置透明度mesh.material.transparent = truemesh.material.opacity = opacity}}

完整的代码可参考:https://gitee.com/ZHANG_6666/Three.js3D/blob/master/src/views/renderModel.js

界面效果:
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/122949.html

相关文章:

  • docker 安装mongodb 虚拟机安装mongodb
  • 在SockJS+Spring Websocket中convertAndSendToUser中的“用户”来自哪里?
  • 【软件测试】我的2023面试经验谈
  • SpringBoot 整合JDBC
  • TypeScript使用npm安装报错问题
  • 2023国赛数学建模思路 - 复盘:人力资源安排的最优化模型
  • 学习pytorch 3 tensorboard的使用
  • Linux 命令篇
  • OpenCV-SIFT算法详解
  • Java中的接口到底是什么?
  • Jpa与Druid线程池及Spring Boot整合(一): spring-boot-starter-data-jpa 搭建持久层
  • helm部署vmalert
  • 加工厂数字孪生3D可视化展示系统重塑管理模式
  • php从静态资源到动态内容
  • JavaScript:模块化【CommonJS与ES6】
  • Redis—持久化
  • 【设计模式】代理模式
  • mac arm 通过brew搭建 php+nginx+mysql+xdebug
  • 软信天成:告别手动编码,实现智能自动化云数据管理
  • 易基因:ChIP-seq等揭示转录因子NRF1调控原始生殖细胞发育、增殖和存活的表观遗传机制|科研进展
  • 35岁,体能断崖?你需要健康的生活习惯
  • mysql 习题总结
  • IL汇编语言做一个窗体
  • 不用技术代码,分班查询系统怎么做?
  • 【Mybatis】调试查看执行的 SQL 语句
  • 【多视重建】从Zero-123到One-2-3-45:多视角生成
  • (四)Unity开发Vision Pro——参考文档
  • 【Linux】简单线程池的设计与实现 -- 单例模式
  • [RoarCTF 2019Online Proxy]sql巧妙盲注
  • flutter开发实战-just_audio实现播放音频暂停音频设置音量等