目录

一、合并网格方法

二、详细内容


一、合并网格方法

轻松地将多个网格合并到单个网格,请使用类的MergeMeshes静态Mesh

var newMesh = BABYLON.Mesh.MergeMeshes(options)

options的参数:

属性 描述
arrayOfMeshes 一组网格。它们都应该是相同的材料。
disposeSource(可选) 当为 true(默认)时,源网格将在完成后被处理。
allow32BitsIndices(可选) 当顶点的总和 > 64k 时,必须将其设置为 true。
meshSubclass 可选) 设置后,顶点插入此网格。然后可以将网格合并到一个网格子类中。
subdivideWithSubMeshes(可选) 当为真(默认为假)时,将网格细分为具有网格源的子网格数组。
multiMultiMaterials(可选) 当为 true(默认为 false)时,细分网格并接受多个多材质,忽略 subdivideWithSubMeshes。

由于multiMultiMaterials默认为 false,因此生成的合并网格将仅应用一个材质(取自第一个网格)

注意:当你合并克隆的网格时,你需要在调用函数之前用 computeWorldMatrix 更新网格的世界矩阵。

二、详细内容

注意:本文介绍了内部合并过程。您还可以使用BABYLON.VertexDataobject 及其merge()函数来获得更简单的解决方案。

var mergeMeshes = function (meshName, arrayObj, scene) {var arrayPos = [];var arrayNormal = [];var arrayUv = [];var arrayUv2 = [];var arrayColor = [];var arrayMatricesIndices = [];var arrayMatricesWeights = [];var arrayIndice = [];var savedPosition = [];var savedNormal = [];var newMesh = new BABYLON.Mesh(meshName, scene);var UVKind = true;var UV2Kind = true;var ColorKind = true;var MatricesIndicesKind = true;var MatricesWeightsKind = true;for (var i = 0; i != arrayObj.length; i++) {if (!arrayObj[i].isVerticesDataPresent([BABYLON.VertexBuffer.UVKind])) UVKind = false;if (!arrayObj[i].isVerticesDataPresent([BABYLON.VertexBuffer.UV2Kind])) UV2Kind = false;if (!arrayObj[i].isVerticesDataPresent([BABYLON.VertexBuffer.ColorKind])) ColorKind = false;if (!arrayObj[i].isVerticesDataPresent([BABYLON.VertexBuffer.MatricesIndicesKind])) MatricesIndicesKind = false;if (!arrayObj[i].isVerticesDataPresent([BABYLON.VertexBuffer.MatricesWeightsKind])) MatricesWeightsKind = false;}for (i = 0; i != arrayObj.length; i++) {var ite = 0;var iter = 0;arrayPos[i] = arrayObj[i].getVerticesData(BABYLON.VertexBuffer.PositionKind);arrayNormal[i] = arrayObj[i].getVerticesData(BABYLON.VertexBuffer.NormalKind);if (UVKind) arrayUv = arrayUv.concat(arrayObj[i].getVerticesData(BABYLON.VertexBuffer.UVKind));if (UV2Kind) arrayUv2 = arrayUv2.concat(arrayObj[i].getVerticesData(BABYLON.VertexBuffer.UV2Kind));if (ColorKind) arrayColor = arrayColor.concat(arrayObj[i].getVerticesData(BABYLON.VertexBuffer.ColorKind));if (MatricesIndicesKind) arrayMatricesIndices = arrayMatricesIndices.concat(arrayObj[i].getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind));if (MatricesWeightsKind) arrayMatricesWeights = arrayMatricesWeights.concat(arrayObj[i].getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind));var maxValue = savedPosition.length / 3;arrayObj[i].computeWorldMatrix(true);var worldMatrix = arrayObj[i].getWorldMatrix();for (var ite = 0; ite != arrayPos[i].length; ite += 3) {var vertex = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(arrayPos[i][ite], arrayPos[i][ite + 1], arrayPos[i][ite + 2]), worldMatrix);savedPosition.push(vertex.x);savedPosition.push(vertex.y);savedPosition.push(vertex.z);}for (var iter = 0; iter != arrayNormal[i].length; iter += 3) {var vertex = BABYLON.Vector3.TransformNormal(new BABYLON.Vector3(arrayNormal[i][iter], arrayNormal[i][iter + 1], arrayNormal[i][iter + 2]), worldMatrix);savedNormal.push(vertex.x);savedNormal.push(vertex.y);savedNormal.push(vertex.z);}var tmp = arrayObj[i].getIndices();for (it = 0; it != tmp.length; it++) {arrayIndice.push(tmp[it] + maxValue);}arrayIndice = arrayIndice.concat(tmp);arrayObj[i].dispose(false);}newMesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, savedPosition, false);newMesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, savedNormal, false);if (arrayUv.length > 0) newMesh.setVerticesData(BABYLON.VertexBuffer.UVKind, arrayUv, false);if (arrayUv2.length > 0) newMesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, arrayUv, false);if (arrayColor.length > 0) newMesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, arrayUv, false);if (arrayMatricesIndices.length > 0) newMesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, arrayUv, false);if (arrayMatricesWeights.length > 0) newMesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, arrayUv, false);newMesh.setIndices(arrayIndice);return newMesh;
};

还可以使用CSG类的subtractinverseunionintersect方法构造复杂的网格。

假设要创建一个具有内径和外径的“管道”形状(,不仅仅是一个“管”网格,它是一个没有“厚度”的曲面)。这可以通过首先创建一个“圆柱”网格,然后从它的内部减去一个“管”网格来构建。

function createPipe(diamInner: number, diamOuter: number, length: number, scene: BABYLON.Scene): BABYLON.Mesh {// Create the outer wall using a Cylinder meshconst mOuter = BABYLON.MeshBuilder.CreateCylinder("mOuter",{diameter: diamOuter,height: length,},scene,);// Create the inner wall using a Tube meshconst mInner = BABYLON.MeshBuilder.CreateTube("mInner",{path: [new BABYLON.Vector3(0, -length / 2, 0), new BABYLON.Vector3(0, length / 2, 0)],radius: diamInner / 2,sideOrientation: BABYLON.Mesh.DOUBLESIDE,},scene,);// Create CSG objects from each meshconst outerCSG = BABYLON.CSG.FromMesh(mOuter);const innerCSG = BABYLON.CSG.FromMesh(mInner);// Create a new CSG object by subtracting the inner tube from the outer cylinderconst pipeCSG = outerCSG.subtract(innerCSG);// Create the resulting mesh from the new CSG objectconst mPipe = pipeCSG.toMesh("mPipe", null, scene);// Dispose of the meshes, no longer neededmInner.dispose();mOuter.dispose();scene.removeMesh(mInner);scene.removeMesh(mOuter);// Return the resultreturn mPipe;
}

Babylon.js 第34章 合并网格相关推荐

  1. Babylon.js 第33章 网格高亮与发光

    目录 一.创建高亮网格 1.示例 二.发光网格 1.创建: 2.添加属性 3.完整示例 一.创建高亮网格 let h1=new BABYLON.HighlightLayer('h',scene,{ca ...

  2. Babylon.js 第27章 创建参数化网格

    目录 一.线 1.绘制线 2.绘制曲线 3.3D路径 二.虚线 三.线路系统 四.网格生成器 五.管子 六.挤压形状 1.挤压 2.定制挤压方式 七.车床 八.不规则多边形 九.不规则多边形挤出 1. ...

  3. Babylon.js 第19章 体积光散射后期处理

    一.做一个太阳 var godrays = new BABYLON.VolumetricLightScatteringPostProcess('godrays',1.0,camera,null,100 ...

  4. Babylon.js 第25章 物理渲染

    PBR金属材料 baseColor / baseTexture:基础颜色根据金属度的值有两种不同的解释.当材料是金属时,基色是在法向入射 (F0) 下测得的特定反射率值.对于非金属,基色表示材料反射的 ...

  5. Babylon.js 第13章 背景材料

    一.背景材料 var backgroundMaterial = new BABYLON.BackgroundMaterial("backgroundMaterial", scene ...

  6. Babylon.js 深入 - 第 2 章 - 声音(2)

    Babylon.js 深入 - 第 2 章 - 声音(2) 声音 Babylon.js 声音引擎基于 Web Audio 规范,要使用它,您需要使用与 Web Audio 兼容的浏览器.声音引擎提供环 ...

  7. Babylon.js 入门 - 第 7 章 - 点亮夜晚

    Babylon.js 入门 - 第 7 章 - 点亮夜晚 点亮村庄 村子永远是明亮的会使睡眠变得非常困难,因此我们将通过调暗光线来展示夜间时间.当然人们不想晚上在完全黑暗中行走,所以我们会添加一些路灯 ...

  8. Babylon.js 深入 - 第 3 章 - 行为(2)

    Babylon.js 深入 - 第 3 章 - 行为(2) 行为 行为是 Babylon.js v3.1 引入的一个新的基于组件的工具.行为是一个简单的类,可以附加到一个目标上,它将提供一组特定的功能 ...

  9. Babylon.js 入门 - 第 6 章 - 构建粒子喷泉

    Babylon.js 入门 - 第 6 章 - 构建粒子喷泉 粒子喷泉 每个村庄都需要水,所以让我们安装一个喷泉.我们将使用一个旋转对称的网格来创建它.细颗粒将被回收以产生水效果.为了不浪费水,让我们 ...

最新文章

  1. kafka_2.11-0.10.2.1中的auto.offset.reset
  2. CocoaPods 的使用与一些异常情况的处理
  3. think in java - 第四章 学习笔记
  4. html5开发ria_用于RIA的JavaFX 2与HTML5
  5. Centos7使用Yum安装高版本的LNMP
  6. pyqt5设置按钮,移上去变为手型
  7. javaweb学习总结七(XML语言作用、语法)
  8. java+串行和并行的区别_Java8新特性_并行流与串行流
  9. 程序员如何帮助公司快速上市、获得分红?
  10. redis设计与实现读书笔记(一)
  11. c语言全局钩子,如何编写一个全局钩子
  12. 数据库实验——T-SQL编程
  13. 推挽输出和开漏输出详解
  14. Jenkins+Gitlab+Ansible自动化部署(六)
  15. 荣耀10青春版支持鸿蒙吗,荣耀10青春版详细评测:又一款年轻群体收割机
  16. RocketMQ下载安装及基本使用
  17. HCIA 8-17 笔记
  18. 梁宁:2019年是5G时代,也是革命性的新营销阵地
  19. 时艳强对话Ricky Ng:交易平台新势力 引领行业新变革
  20. Intel.VTune.Performance.Analyzer.v8.0.014 分析软件

热门文章

  1. r语言rank降序_R语言速成之第一章 向量(编辑,排序,10个基本函数)
  2. python安装包区别
  3. Sanitizer的使用
  4. 设置Go Proxy
  5. Blender着色器节点教程 —— Fresnel
  6. 这么牛的毕业生,来当CTO吧!
  7. 2023年-梅宏院士等:大数据技术的四大挑战与十大趋势
  8. git clone: SSL certificate problem: self signed certificate 报错解决
  9. 层流压差质量流量计的主要功能
  10. IDEA访问不了官网?看过来!(超详!超细!)