使用SceneKit在两点之间绘制一条线 [英] Drawing a line between two points using SceneKit

查看:348
本文介绍了使用SceneKit在两点之间绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类型为SCNVector3的点(我们称它们为pointA和pointB).我想在它们之间画一条线.似乎应该很容易,但是找不到解决方法.

I have two points (let's call them pointA and pointB) of type SCNVector3. I want to draw a line between them. Seems like it should be easy, but can't find a way to do it.

我看到两个选项,都有问题:

I see two options, both have issues:

使用半径非常小的SCNCylinder,其长度| pointA-pointB |然后定位/旋转它.

Use a SCNCylinder with a very small radius, with length |pointA-pointB| and then position it/rotate it.

使用自定义SCNGeometry,但不确定如何使用;可能必须定义两个三角形才能形成一个非常薄的矩形?

Use a custom SCNGeometry but not sure how; would have to define two triangles to form a very thin rectangle perhaps?

似乎应该有一种更简单的方法,但是我似乎找不到一个.

It seems like there should be an easier way of doing this, but I can't seem to find one.

使用三角形方法使我能够在(0,0,0)和(10,10,10)之间画一条线:

Using the triangle method gives me this for drawing a line between (0,0,0) and (10,10,10):

CGFloat delta = 0.1;
SCNVector3 positions[] = {  SCNVector3Make(0,0,0),
    SCNVector3Make(10, 10, 10),
    SCNVector3Make(0+delta, 0+delta, 0+delta),
    SCNVector3Make(10+delta, 10+delta, 10+delta)};
int indicies[] = {
    0,2,1,
    1,2,3
};

SCNGeometrySource *vertexSource = [SCNGeometrySource geometrySourceWithVertices:positions count:4];
NSData *indexData = [NSData dataWithBytes:indicies length:sizeof(indicies)];
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:indexData primitiveType:SCNGeometryPrimitiveTypeTriangles primitiveCount:2 bytesPerIndex:sizeof(int)];
SCNGeometry *line = [SCNGeometry geometryWithSources:@[vertexSource] elements:@[element]];

SCNNode *lineNode = [SCNNode nodeWithGeometry:line];
[root addChildNode:lineNode];

但是有问题:由于法线,您只能从一侧看到这条线!从另一面看不见它.另外,如果增量"太小,您根本看不到该行.实际上,它实际上是矩形,而不是我要使用的线,如果我要绘制多条连接的线,可能会导致图形上的小故障.

But there are problems: due to the normals, you can only see this line from one side! It's invisible from the other side. Also, if "delta" is too small you can't see the line at all. As it is, it's technically a rectangle, rather than the line I was going for, which might result in small graphical glitches if I want to draw multiple joined up lines.

推荐答案

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

There are lots of ways to do this.

如上所述,您的自定义几何方法有一些缺点.您应该能够通过为其材料提供

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,因为很多细节都不可见.)

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类,该类使您可以从2DBézier路径创建拉伸的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.

这篇关于使用SceneKit在两点之间绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆