且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 SceneKit 在两点之间画一条线

更新时间:2023-01-18 10:25:11

有很多方法可以做到这一点.

There are lots of ways to do this.

如前所述,您的自定义几何方法有一些缺点.您应该能够通过为其材料提供 doubleSided 属性.不过,您仍然可能会遇到二维问题.

As noted, your custom geometry approach has some disadvantages. You should be able to correct the problem of it being invisible from one side by giving its material the doubleSided property. You still may have issues with it being two-dimensional, though.

您还可以修改自定义几何图形以包含更多三角形,从而获得具有三个或更多边的管状形状,而不是平面矩形.或者只在几何源中有两个点,并使用 SCNGeometryPrimitiveTypeLine 几何元素类型让 Scene Kit 在它们之间绘制线段.(尽管使用线条绘制渲染样式的灵活性不如使用着色多边形.)

You could also modify your custom geometry to include more triangles, so you get a tube shape with three or more sides instead of a flat rectangle. Or just have two points in your geometry source, and use the SCNGeometryPrimitiveTypeLine geometry element type to have Scene Kit draw a line segment between them. (Though you won't get as much flexibility in rendering styles with line drawing as with shaded polygons.)

您还可以使用您提到的 SCNCylinder 方法(或任何其他内置基本形状).请记住,几何是在它们自己的本地(又名模型)坐标空间中定义的,Scene Kit 相对于节点定义的坐标空间进行解释.换句话说,您可以定义一个在所有维度上宽度为 1.0 个单位的圆柱体(或盒子、胶囊或平面或其他任何东西),然后使用包含该几何体的 SCNNode 的旋转/缩放/位置或变换使它长,薄,并在您想要的两点之间拉伸.(另请注意,由于您的线条会非常细,您可以减少正在使用的任何内置几何图形的 segmentCount s,因为这些细节将不可见.)

You can also use the SCNCylinder approach you mentioned (or any of the other built-in primitive shapes). Remember that geometries are defined in their own local (aka Model) coordinate space, which Scene Kit interprets relative to the coordinate space defined by a node. In other words, you can define a cylinder (or box or capsule or plane or whatever) that's 1.0 units wide in all dimensions, then use the rotation/scale/position or transform of the SCNNode containing that geometry to make it long, thin, and stretching between the two points you want. (Also note that since your line is going to be pretty thin, you can reduce the segmentCounts of whichever built-in geometry you're using, because that much detail won't be visible.)

另一个选项是 SCNShape 类,它可以让您从 2D 贝塞尔路径创建挤出的 3D 对象.计算出正确的变换来获得连接两个任意点的平面听起来像是一些有趣的数学运算,但是一旦你做到了,你就可以轻松地将你的点与你选择的任何形状的线连接起来.

Yet another option is the SCNShape class that lets you create an extruded 3D object from a 2D Bézier path. Working out the right transform to get a plane connecting two arbitrary points sounds like some fun math, but once you do it you could easily connect your points with any shape of line you choose.