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

本文介绍了如何使用Babylon.js创建各种参数化形状,包括线、曲线、管子、挤压形状等,并提供了详细的创建方法和示例。通过绘制线、曲线、3D路径,到生成管状物、挤压形状和车削形状,深入理解3D图形编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、线

1、绘制线 

2、绘制曲线

3、3D路径

二、虚线

三、线路系统

四、网格生成器

五、管子

六、挤压形状

1、挤压 

  2、定制挤压方式

七、车床

八、不规则多边形

九、不规则多边形挤出

1、正常挤出 

2、多边形网格生成器


一、线

1、绘制线 

const options = {
    points: myPoints,//3维空间点数组
    updatable: true
}

let lines = BABYLON.MeshBuilder.CreateLines("lines", options, scene);
options.points[0].x +=6; 
options.instance = lines;
lines = BABYLON.MeshBuilder.CreateLines("lines", options);

 options:

选项价值默认值
points(Vector3[]) Vector3 的数组,行的路径REQUIRED
updatable(布尔值)如果网格是可更新的,则为真错误的
instance(LineMesh)要更新的线网格实例无效的
colors(Color4[]) Color4的数组,每个点的颜色无效的
useVertexAlpha(布尔值)如果不需要 alpha 混合则为 false(更快)真的

与网格不同,线条在创建后使用颜色属性而不是材质进行着色。

lines.color = new BABYLON.Color3(1, 0, 0);

mesh:

let lines = BABYLON.Mesh.CreateLines("lines", points, scene);
let lines = BABYLON.Mesh.CreateLines("lines", points, scene, updatable, instance);

2、绘制曲线

三点圆弧:

    let arr=[
        new BABYLON.Vector3(10,1,0),
        new BABYLON.Vector3(10,1,10),
        new BABYLON.Vector3(15,10,20),
    ]
    let arc=BABYLON.Curve3.ArcThru3Points(arr[0],arr[1],arr[2],32,true,true)
    let arcline=BABYLON.MeshBuilder.CreateLines('arc',
        {points:arc.getPoints()})

 arcThru3Points参数:

  • first Vector3弧必须经过的第一个点。
  • second Vector3弧必须经过的第二个点。
  • 第三个 Vector3弧必须经过的第三个点。
  •  越大,步数越大,弧线越详细。
  • 关闭 布尔可选,默认为 false,当 true 从第一点和第三点形成和弦时
  • fullCircle boolean可选,默认为 false,当 true 通过三个点形成完整的圆

此静态方法返回Curve3的一个实例。
只需使用 Curve3 getPoints()方法填充您的数组:getPoints()返回一个连续Vector3的数组。length()方法返回曲线长度。

 二次贝塞尔曲线:

    let arr=[
        new BABYLON.Vector3(10,1,0),
        new BABYLON.Vector3(10,1,10),
        new BABYLON.Vector3(15,10,20),
    ]
    let arc=BABYLON.Curve3.CreateQuadraticBezier(arr[0],arr[1],arr[2],32)
    let arcline=BABYLON.MeshBuilder.CreateLines('arc',
        {points:arc.getPoints()})

CreateQuadraticBezier参数介绍:

  • origin : Vector3原点,
  • control : Vector3控制点,
  • destination : Vector3目标点,
  • nb_of_points:路径数组中想要的点数

同样,只需使用 Curve3 getPoints()方法填充您的数组:getPoints()返回一个连续Vector3的数组。length()方法返回曲线长度。

三次贝塞尔曲线:

    let arr=[
        new BABYLON.Vector3.Zero(),
        new BABYLON.Vector3(30,30,40),
        new BABYLON.Vector3(-60,50,-10),
        new BABYLON.Vector3(30,-50,-20),
        new BABYLON.Vector3(-60,10,-20)
    ]
    let arc=BABYLON.Curve3.CreateCubicBezier(arr[0],arr[1],arr[2],arr[3],32)
    let arcline=BABYLON.MeshBuilder.CreateLines('arc',
        {points:arc.getPoints()})

参数:

  • origin : Vector3原点,
  • control1 : Vector3第一个控制点,
  • control2 : Vector3第二个控制点,
  • destination : Vector3目标点,
  • nb_of_points:对数组中所需的最终曲线点数进行编号

此静态方法返回Curve3的一个实例。
只需使用 Curve3 getPoints()方法填充您的数组:getPoints()返回一个连续Vector3的数组。length()
方法返回曲线长度。

埃尔米特线:

    let arr=[
        new BABYLON.Vector3.Zero(),
        new BABYLON.Vector3(10,30,40),
        new BABYLON.Vector3(-60,10,-10),
        new BABYLON.Vector3(-30,-50,20),
    ]
    let arc=BABYLON.Curve3.CreateHermiteSpline(arr[0],arr[1],arr[2],arr[3],32)
    let arcline=BABYLON.MeshBuilder.CreateLines('arc',
        {points:arc.getPoints()})

参数:

  • p1 : Vector3原点,
  • t1 : Vector3原点正切向量,
  • p2 : Vector3目标点,
  • t2 : Vector3目标切线向量,
  • nbPoints:对数组中所需的最终曲线点数进行编号

此静态方法返回Curve3的一个实例。
只需使用 Curve3 getPoints()方法填充您的数组:getPoints()返回一个连续Vector3的数组。length()
方法返回曲线长度。

三次埃尔米特线:

    let arr=[
        new BABYLON.Vector3.Zero(),
        new BABYLON.Vector3(10,30,40),
        new BABYLON.Vector3(-60,10,-10),
        new BABYLON.Vector3(-30,-50,20),
    ]
    let arc=BABYLON.Curve3.CreateCatmullRomSpline(arr,20,32,true)
    let arcline=BABYLON.MeshBuilder.CreateLines('arc',
        {points:arc.getPoints()})

参数:

  • points : Vector3曲线将通过的 Vector3 数组(控制点),
  • nbPoints编号每个控制点之间的所需曲线点数,
  • closed布尔可选,默认为false;可从 BJS V3.3 获得;当 true 形成闭合曲线时。

此静态方法返回Curve3的一个实例。
只需使用 Curve3 getPoints()方法填充您的数组:getPoints()返回一个连续Vector3的数组。length()
方法返回曲线长度。

创建自定义曲线:

自定义曲线可以使用起点连接上段曲线的终点。

var mySinus = [];
for (var i = 0; i < 30; i++) {
 mySinus.push( new BABYLON.Vector3(i, Math.sin(i / 10), 0) );
}
var mySinusCurve = new BABYLON.Curve3(mySinus);
var myFullCurve = mySinusCurve.continue(bezier3).continue(bezier2);

3、3D路径

var points = [v1, v2, ..., vn]; 
var path3d = new BABYLON.Path3D(points);
	var path3d = new BABYLON.Path3D(points);
    //切线
	var tangents = path3d.getTangents();
    //法线
	var normals = path3d.getNormals();
    //副法线
	var binormals = path3d.getBinormals();
    //曲线
	var curve = path3d.getCurve();

方法:

getTangentsgetNormals还有getBinormals一些其他的方法Path3D,getCurve方法返回为创建对象而提供getCurve的初始数组的副本。

var distances = path3d.getDistances();

getDistances方法按点的顺序返回从一个曲线点到下一个曲线点的距离数组,其中 0 是第一个距离。

slice方法返回一个新的 Path3D,它是原始路径的子路径(切片)。它采用从 0.0 到 1.0 的开始结束位置,或从 1.0 回绕的负值

为了避免内存重新分配(例如在渲染循环中),因为给定的点数Path3D组是在内部复制的,您可以使用其update()方法更新现有对象。

getClosestPositionTo函数返回从 0.0 到 1.0 的沿任意 Vector3 的路径最近的虚拟点的位置。

 

除了第一个法线外,还有两个参数:

raw, 布尔值,默认为false。如果为真,则切线、法线和副法线不会被归一化。用于描述路径加速度或速度。alignTangentsWithPath, 布尔值,默认为false。如果为真,切线将与路径对齐。

 

二、虚线

const options = {
    points: myPoints,//3为空间点数组
    updatable: true
}

let dashedlines = BABYLON.MeshBuilder.CreateDashedLines("dashedlines",
    options, scene);  

options.points[0].x +=6; 
options.instance = lines;
lines = BABYLON.MeshBuilder.CreateDashedLines("dashedlines", options);
选项价值默认值
points(Vector3[]) Vector3 的数组,行的路径REQUIRED
dashSize(数字) 破折号的大小3
gapSize(数量) 间隙大小1
dashNb(数字) 预期的破折号数200
updatable(布尔值)如果网格是可更新的,则为真错误的
instance(LineMesh)要更新的线网格实例无效的

mesh: 

const dashedlines = BABYLON.Mesh.CreateDashedLines("dashedLines",
     vector3 array, dashSize, gapSize, dashNb, scene);
const dashedlines = BABYLON.Mesh.CreateDashedLines("dashedLines",
     vector3 array, dashSize, gapSize, dashNb, scene, updatable, instance);

三、线路系统

let lineSystem = BABYLON.MeshBuilder.CreateLineSystem("lineSystem", 
    {lines: myArray}, scene);

lineSystem = BABYLON.MeshBuilder.CreateLineSystem("lineSystem", 
    {lines: myArray, instance: lineSystem});
选项价值默认值
lines(Vector3[][]) 行数组,每行是一个连续的 Vector3数组
updatable(布尔值)如果网格是可更新的,则为真错误的
instance(LineMesh)要更新的线系统网格的实例无效的
colors(Color4[]) Color4的数组,每个点的颜色无效的
useVertexAlpha(布尔值)如果不需要 alpha 混合则为 false(更快)真的

四、网格生成器

const options = {
    pathArray: myPaths,//一个数组,其元素均为三维数组,三维数组存储网格顶点
    updatable: true
}

let ribbon = BABYLON.MeshBuilder.CreateRibbon("ribbon", options, scene);

options.pathArray: myNewPaths;
myNewPaths[i].length === myPaths[i] for all i
options.instance = true;

ribbon = BABYLON.MeshBuilder.CreateRibbon("ribbon", 
    {pathArray: myNewPath, instance: ribbon});

选项价值默认值
pathArray(Vector3[][]) Vector3 数组数组,路径数组REQUIRED
closeArray(布尔值) 强制功能区加入其最后和第一条路径错误的
closePath(布尔值) 强制每个功能区路径连接其最后一个点和第一个点错误的
offset(数字) 如果 pathArray 只有一个路径使用路径长度的一半
updatable(布尔值)如果网格是可更新的,则为真错误的
sideOrientation(数字)侧向默认侧
frontUVs(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
backUVs(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
instance(LineMesh)要更新的功能区实例无效的
invertUV(布尔值)在几何构造时交换 U 和 V 坐标(纹理旋转 90°)错误的

mesh:

const ribbon = BABYLON.Mesh.CreateRibbon("ribbon", 
    pathArray, closeArray, closePath, offset, scene);
const ribbon = BABYLON.Mesh.CreateRibbon("ribbon", 
    pathArray, closeArray, closePath, offset, scene, 
    updatable, sideOrientation, instance);

五、管子

const options = {//vec3 array,
    updatable: true
}

let tube = BABYLON.MeshBuilder.CreateTube("tube", options, scene);

options.path[0].x +=6; 
options.instance = tube;
tube = BABYLON.MeshBuilder.CreateTubes("tube", options);
选项价值默认值
path(Vector3[]) Vector3 的数组,管子的路径REQUIRED
radius(number) 管的半径1
tessellation(number) 径向段数64
radiusFunction( function(index, distance) ) 从(index, distance)参数返回半径值的函数无效的
cap(编号)管帽:NO_CAP、CAP_START、CAP_END、CAP_ALLNO_CAP
arc(number)管周长在 0 和 1 之间的比率1
updatable(布尔值)如果网格是可更新的,则为真错误的
sideOrientation(数字)侧向默认侧
frontUVs(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
backUVs(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
backUVs(LineMesh)要更新的管的实例无效的
invertUV(布尔值)在几何构造时交换 U 和 V 坐标(纹理旋转 90°)错误的

mesh:

let tube = BABYLON.Mesh.CreateTube("tube", path, radius, 
    tesselation, optional radiusFunction, cap, scene);
let tube = BABYLON.Mesh.CreateTube("tube", path, radius, 
    tesselation, optional radiusFunction, cap, scene, 
    updatable, sideOrientation, instance);

六、挤压形状

1、挤压 

const options = {
    shape: myPoints, //z=0的三维空间数组
    path: myPath, //3维空间数组
    updatable: true
}

let extruded = BABYLON.MeshBuilder.ExtrudeShape("ext", options, scene); 

options.shape = newShape;
options.path = newPath;
options.instance = extruded;
extruded = BABYLON.MeshBuilder.ExtrudeShape("ext", options); 
选项价值默认值
shape(Vector3[]) Vector3 数组,你要拉伸的形状需要
path(Vector3[]) Vector3 的数组,需要拉伸轴
scale(数字) 缩放形状的值1
rotation(数字) 沿着路径每一步旋转形状的值0
cap(数量)挤压帽:NO_CAP, CAP_START, CAP_END, CAP_ALLNO_CAP
closeShape(boolean)关闭形状,无需将 shape[0] 推入形状数组错误的
closePath(boolean)关闭路径,无需将 path[0] 推入路径数组错误的
updatable(布尔值)如果网格是可更新的,则为真错误的
sideOrientation(数字)侧向默认侧
frontUVs(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
backUVs(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
instance(LineMesh)要更新的拉伸形状的实例无效的
invertUV(布尔值)在几何构造时交换 U 和 V 坐标(纹理旋转 90°)错误的

mesh:

let extrusion = BABYLON.Mesh.ExtrudeShape(name, shape, path,
     scale, rotation, cap, scene);
let extrusion = BABYLON.Mesh.ExtrudeShape(name, shape, path, 
    scale, rotation, cap, scene, updatable, sideOrientation, instance);

  2、定制挤压方式

const options = {
  shape: myPoints,
  path: myPath, 
  rotationFunction: rotFn,
  scaleFunction: scaleFn,
  updatable: true,
};

let extruded = BABYLON.MeshBuilder.ExtrudeShapeCustom("ext", options, scene); 
options.shape = newShape;
options.path = newPath;
options.instance = extruded;
options.rotationFunction = newRotFn;
options.scaleFunction = newScaleFn;
extruded = BABYLON.MeshBuilder.ExtrudeShapeCustom("ext", options);
选项价值默认值
shape(Vector3[]) Vector3 数组,你要拉伸的形状需要
path(Vector3[]) Vector3的数组,需要拉伸轴
scaleFunction( function(i, distance) )从(i, distance)参数返回比例值的函数{返回 1;}
rotationFunction( function(i, distance) )从(i, distance)参数返回旋转值的函数{返回 0;}
closeShape(boolean)关闭形状,替换ribbonClosePath错误的
closePath(boolean)关闭路径,替换ribbonCloseArray错误的
ribbonClosePath(布尔值)底层功能区closePath参数值已弃用错误的
ribbonCloseArray(布尔值)底层功能区closeArray参数值已弃用错误的
cap(数量)挤压帽:NO_CAP, CAP_START, CAP_END, CAP_ALLNO_CAP
updatable(布尔值)如果网格是可更新的,则为真错误的
sideOrientation(数字)侧向默认侧
frontUVs(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
backUVs(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
instance(LineMesh)要更新的拉伸形状的实例无效的
invertUV(布尔值)在几何构造时交换 U 和 V 坐标(纹理旋转 90°)错误的

mesh:

let extrusion = BABYLON.Mesh.ExtrudeShapeCustom("extrusion", shape,
    path, scaleFunction, rotateFunction, ribbonCloseArray,
    ribbonClosePath, cap, scene);
let extrusion = BABYLON.Mesh.ExtrudeShapeCustom("extrusion", shape,
    path, scaleFunction, rotateFunction, ribbonCloseArray,
    ribbonClosePath, cap, scene, updatable, sideOrientation, instance); 

let extrusion = BABYLON.Mesh.ExtrudeShapeCustom(
  "extrusion",
  shape,
  path,
  () => {
    return 1;
  },
  () => {
    return 0;
  },
  ribbonCloseArray,
  ribbonClosePath,
  cap,
  scene,
);

七、车床

const lathe = BABYLON.MeshBuilder.CreateLathe("lathe", options, scene);
选项价值默认值
shape(Vector3[]) Vector3的数组,你要转的形状REQUIRED
radius(数字) 车床半径值1
tessellation(number) 围绕车床的迭代次数,车床边数64
arc(数字)圆周率在 0 和 1 之间。取比例值1
cap(编号)管帽:NO_CAP、CAP_START、CAP_END、CAP_ALLNO_CAP
closed(boolean)false打开/关闭车床圆周,使用时应设置为arc真的
updatable(布尔值)如果网格是可更新的,则为真错误的
sideOrientation(数字)侧向默认侧
fRontUVS(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
backUVS(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
insertUV(布尔值)在几何构造时交换 U 和 V 坐标(纹理旋转 90°)错误的

mesh:

let lathe = BABYLON.Mesh.CreateLathe("lathe", shape, radius,
    tessellation, scene);
let lathe = BABYLON.Mesh.CreateLathe("lathe", shape, radius, 
    tessellation, scene, updatable, sideOrientation);

八、不规则多边形

const polygon = BABYLON.MeshBuilder.CreatePolygon("polygon", options, scene);
选项价值默认值
shape(Vector3[]) Vector3 数组,你要构建的形状需要
holes(Vector3[][]) 孔数组,每个孔是一个连续的 Vector3 数组[]
updatable(布尔值)如果网格是可更新的,则为真错误的
sideOrientation(数字)侧向默认侧
frontUVS(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)
backUVS(Vector4) 仅当 sideOrientation:BABYLON.Mesh.DOUBLESIDE 是一个选项时Vector4(0,0, 1,1)

mesh:

let polygon = BABYLON.Mesh.CreatePolygon("polygon", shape, scene);
let polygon = BABYLON.Mesh.CreatePolygon("polygon", shape,
    scene, holes, updatable, sideOrientation);

九、不规则多边形挤出

1、正常挤出 

const extrudedPolygon = BABYLON.MeshBuilder.ExtrudePolygon(
    "polygon", options, scene); 
选项价值默认值
shape(Vector3[]) Vector3的数组,你要转的形状REQUIRED
depth(数字) 挤压深度要求
faceColors(Color4[]) 3 个Color4的数组,每个盒子面一个每边的 Color4(1, 1, 1, 1)
faceUV(Vector4[]) 3 个Vector4的数组,每个盒子面一个每边的 UV(0, 0, 1, 1)
wrap(布尔值)当错误纹理映射到每个单独的侧面时,使用 faceUV[1] 将纹理映射到侧面,当真实包裹在所有侧面时错误的
holes(Vector3[][]) 孔数组,每个孔是一个连续的 Vector3 数组[]
updatable(布尔值)如果网格是可更新的,则为真错误的
sideOrientation(数字)侧向默认侧

mesh:

let polygon = BABYLON.Mesh.CreatePolygon("polygon", shape,
    depth, scene);
let polygon = BABYLON.Mesh.CreatePolygon("polygon", shape,
    depth, scene, holes, updatable, sideOrientation);

2、多边形网格生成器

const polygon_triangulation = new BABYLON.PolygonMeshBuilder(
    "name", vector2 array, scene);
const polygon = polygon_triangulation.build();
var polygon_triangulation = new BABYLON.PolygonMeshBuilder(
    "name", Path2, scene);
var polygon = polygon_triangulation.build(false, 3);

添加孔:

polygon_triangulation.addHole(hole1);
polygon_triangulation.addHole(hole2);
polygon_triangulation.addHole(hole3);
var polygon = polygon_triangulation.build(true, 1.4);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值