我有SCNVector3类型的两个点(我们称之为pointA和pointB)。我想在它们之间画一条线。似乎应该很容易,但找不到办法。
我看到两个选项,都有问题:
使用半径非常小的SCNCylinder,长度为| pointA-pointB |然后定位/旋转它。
使用自定义SCNGeometry但不确定如何;必须定义两个三角形才能形成一个非常薄的矩形吗?
似乎应该有一种更简单的方法,但我似乎找不到一个。
编辑:使用三角形方法可以在(0,0,0)和(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];
但有问题:由于法线,你只能从一边看到这条线!它从另一边看不见。此外,如果“delta”太小,则根本看不到该线。实际上,它在技术上是一个矩形,而不是我想要的线,如果我想绘制多条连接线,可能会导致小的图形故障。
答案 0 :(得分:16)
这是Swift中的一个简单扩展:
extension SCNGeometry {
class func lineFrom(vector vector1: SCNVector3, toVector vector2: SCNVector3) -> SCNGeometry {
let indices: [Int32] = [0, 1]
let source = SCNGeometrySource(vertices: [vector1, vector2])
let element = SCNGeometryElement(indices: indices, primitiveType: .Line)
return SCNGeometry(sources: [source], elements: [element])
}
}
答案 1 :(得分:14)
有很多方法可以做到这一点。
如上所述,您的自定义几何方法有一些缺点。您应该能够通过为其材质提供doubleSided
属性来纠正它从一侧隐藏的问题。不过,你仍然可能会遇到二维问题。
您还可以修改自定义几何体以包含更多三角形,这样您就可以获得具有三个或更多边的管形状而不是扁平矩形。或者在几何体源中只有两个点,并使用SCNGeometryPrimitiveTypeLine
几何元素类型让Scene Kit在它们之间绘制线段。 (尽管使用阴影多边形渲染样式时,您将无法获得足够的灵活性。)
您还可以使用您提到的SCNCylinder
方法(或任何其他内置基元形状)。请记住,几何是在它们自己的局部(也称为Model)坐标空间中定义的,Scene Kit相对于节点定义的坐标空间进行解释。换句话说,您可以在所有维度中定义宽度为1.0个单位的圆柱体(或框或圆柱体或平面或其他),然后使用包含该几何体的SCNNode
的旋转/缩放/位置或变换来制作它长,细,并在你想要的两点之间伸展。 (另请注意,由于您的线条非常薄,因此您可以减少正在使用的内置几何体的segmentCount
,因为很多细节都不可见。)
另一个选项是SCNShape
类,它允许您从2DBézier路径创建拉伸的3D对象。通过计算正确的变换来获得连接两个任意点的平面听起来像一些有趣的数学,但是一旦你这样做,你就可以轻松地将你的点与你选择的任何形状的线连接起来。
答案 2 :(得分:9)
以下(0,0,0)到(10,10,10)的行的新代码。 我不确定它是否可以进一步改进。
SCNVector3 positions[] = {
SCNVector3Make(0.0, 0.0, 0.0),
SCNVector3Make(10.0, 10.0, 10.0)
};
int indices[] = {0, 1};
SCNGeometrySource *vertexSource = [SCNGeometrySource geometrySourceWithVertices:positions
count:2];
NSData *indexData = [NSData dataWithBytes:indices
length:sizeof(indices)];
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:indexData
primitiveType:SCNGeometryPrimitiveTypeLine
primitiveCount:1
bytesPerIndex:sizeof(int)];
SCNGeometry *line = [SCNGeometry geometryWithSources:@[vertexSource]
elements:@[element]];
SCNNode *lineNode = [SCNNode nodeWithGeometry:line];
[root addChildNode:lineNode];
答案 3 :(得分:9)
这是一个解决方案
class func lineBetweenNodeA(nodeA: SCNNode, nodeB: SCNNode) -> SCNNode {
let positions: [Float32] = [nodeA.position.x, nodeA.position.y, nodeA.position.z, nodeB.position.x, nodeB.position.y, nodeB.position.z]
let positionData = NSData(bytes: positions, length: MemoryLayout<Float32>.size*positions.count)
let indices: [Int32] = [0, 1]
let indexData = NSData(bytes: indices, length: MemoryLayout<Int32>.size * indices.count)
let source = SCNGeometrySource(data: positionData as Data, semantic: SCNGeometrySource.Semantic.vertex, vectorCount: indices.count, usesFloatComponents: true, componentsPerVector: 3, bytesPerComponent: MemoryLayout<Float32>.size, dataOffset: 0, dataStride: MemoryLayout<Float32>.size * 3)
let element = SCNGeometryElement(data: indexData as Data, primitiveType: SCNGeometryPrimitiveType.line, primitiveCount: indices.count, bytesPerIndex: MemoryLayout<Int32>.size)
let line = SCNGeometry(sources: [source], elements: [element])
return SCNNode(geometry: line)
}
如果您想更新线宽或与修改绘制线的属性相关的任何内容,您将要在SceneKit的渲染回调中使用其中一个openGL调用:
func renderer(aRenderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: NSTimeInterval) {
//Makes the lines thicker
glLineWidth(20)
}
答案 4 :(得分:2)
因此,在你的ViewController.cs中定义你的矢量点并调用一个Draw函数,然后在最后一行 - 它只是旋转它来看b点。
var a = someVector3;
var b = someOtherVector3;
nfloat cLength = (nfloat)Vector3Helper.DistanceBetweenPoints(a, b);
var cyclinderLine = CreateGeometry.DrawCylinderBetweenPoints(a, b, cLength, 0.05f, 10);
ARView.Scene.RootNode.Add(cyclinderLine);
cyclinderLine.Look(b, ARView.Scene.RootNode.WorldUp, cyclinderLine.WorldUp);
创建一个静态CreateGeomery类并将此静态方法放在那里
public static SCNNode DrawCylinderBetweenPoints(SCNVector3 a,SCNVector3 b, nfloat length, nfloat radius, int radialSegments){
SCNNode cylinderNode;
SCNCylinder cylinder = new SCNCylinder();
cylinder.Radius = radius;
cylinder.Height = length;
cylinder.RadialSegmentCount = radialSegments;
cylinderNode = SCNNode.FromGeometry(cylinder);
cylinderNode.Position = Vector3Helper.GetMidpoint(a,b);
return cylinderNode;
}
您可能还希望在静态助手类
中使用这些实用程序方法 public static double DistanceBetweenPoints(SCNVector3 a, SCNVector3 b)
{
SCNVector3 vector = new SCNVector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
return Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z);
}
public static SCNVector3 GetMidpoint(SCNVector3 a, SCNVector3 b){
float x = (a.X + b.X) / 2;
float y = (a.Y + b.Y) / 2;
float z = (a.Z + b.Z) / 2;
return new SCNVector3(x, y, z);
}
对于我所有的Xamarin c#homies。
答案 5 :(得分:2)
这是swift5版本:
func lineBetweenNodes(positionA: SCNVector3, positionB: SCNVector3, inScene: SCNScene) -> SCNNode {
let vector = SCNVector3(positionA.x - positionB.x, positionA.y - positionB.y, positionA.z - positionB.z)
let distance = sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z)
let midPosition = SCNVector3 (x:(positionA.x + positionB.x) / 2, y:(positionA.y + positionB.y) / 2, z:(positionA.z + positionB.z) / 2)
let lineGeometry = SCNCylinder()
lineGeometry.radius = 0.05
lineGeometry.height = distance
lineGeometry.radialSegmentCount = 5
lineGeometry.firstMaterial!.diffuse.contents = GREEN
let lineNode = SCNNode(geometry: lineGeometry)
lineNode.position = midPosition
lineNode.look (at: positionB, up: inScene.rootNode.worldUp, localFront: lineNode.worldUp)
return lineNode
}
答案 6 :(得分:0)
这是使用三角形的解决方案,其作用与线的方向无关。 它是使用叉积构造的,以获取垂直于直线的点。因此,您需要一个小的SCNVector3扩展名,但在其他情况下也可能会派上用场。
private func makeRect(startPoint: SCNVector3, endPoint: SCNVector3, width: Float ) -> SCNGeometry {
let dir = (endPoint - startPoint).normalized()
let perp = dir.cross(SCNNode.localUp) * width / 2
let firstPoint = startPoint + perp
let secondPoint = startPoint - perp
let thirdPoint = endPoint + perp
let fourthPoint = endPoint - perp
let points = [firstPoint, secondPoint, thirdPoint, fourthPoint]
let indices: [UInt16] = [
1,0,2,
1,2,3
]
let geoSource = SCNGeometrySource(vertices: points)
let geoElement = SCNGeometryElement(indices: indices, primitiveType: .triangles)
let geo = SCNGeometry(sources: [geoSource], elements: [geoElement])
geo.firstMaterial?.diffuse.contents = UIColor.blue.cgColor
return geo
}
SCNVector3扩展名:
import Foundation
import SceneKit
extension SCNVector3
{
/**
* Returns the length (magnitude) of the vector described by the SCNVector3
*/
func length() -> Float {
return sqrtf(x*x + y*y + z*z)
}
/**
* Normalizes the vector described by the SCNVector3 to length 1.0 and returns
* the result as a new SCNVector3.
*/
func normalized() -> SCNVector3 {
return self / length()
}
/**
* Calculates the cross product between two SCNVector3.
*/
func cross(_ vector: SCNVector3) -> SCNVector3 {
return SCNVector3(y * vector.z - z * vector.y, z * vector.x - x * vector.z, x * vector.y - y * vector.x)
}
}