【发布时间】:2018-09-25 19:07:02
【问题描述】:
我的想法是我想在选定的位置放置 2 个球体节点。从那时起,我基本上想绘制一个矩形,该矩形将使用滑块调整高度。基本上这意味着 2 个球体将在开始时代表所有 4 个角。但是在测试代码时,我使用 1 米高度作为测试。
我的问题是我似乎无法将矩形放置在正确的位置,如下图所示:
图像中的矩形具有比这些点更高的 y 点,它略微旋转并且其中心点在节点 2 上方,而不是在节点之间。我不想使用node.rotation,因为它不会动态工作。
这是我用来放置 2 个节点并绘制 + 添加矩形的代码。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let location = touches.first?.location(in: sceneView) else {return}
let hitTest = sceneView.hitTest(location, types: [ARHitTestResult.ResultType.featurePoint])
guard let result = hitTest.last else {return}
// Converts the matrix_float4x4 to an SCNMatrix4 to be used with SceneKit
let transform = SCNMatrix4.init(result.worldTransform)
// Creates an SCNVector3 with certain indexes in the matrix
let vector = SCNVector3Make(transform.m41, transform.m42, transform.m43)
let node = addSphere(withPosition: vector)
nodeArray.append(node)
sceneView.scene.rootNode.addChildNode(node)
if nodeArray.count == 2 {
let node1 = sceneView.scene.rootNode.childNodes[0]
let node2 = sceneView.scene.rootNode.childNodes[1]
let bezeierPath = UIBezierPath()
bezeierPath.lineWidth = 0.01
bezeierPath.move(to: CGPoint(x: CGFloat(node1.position.x), y: CGFloat(node1.position.y)))
bezeierPath.addLine(to: CGPoint(x: CGFloat(node2.position.x), y: CGFloat(node2.position.y)))
bezeierPath.addLine(to: CGPoint(x: CGFloat(node2.position.x), y: CGFloat(node2.position.y+1.0)))
bezeierPath.addLine(to: CGPoint(x: CGFloat(node1.position.x), y: CGFloat(node1.position.y+1.0)))
bezeierPath.close()
bezeierPath.fill()
let shape = SCNShape(path: bezeierPath, extrusionDepth: 0.02)
shape.firstMaterial?.diffuse.contents = UIColor.red.withAlphaComponent(0.8)
let node = SCNNode.init(geometry: shape)
node.position = SCNVector3(CGFloat(abs(node1.position.x-node2.position.x)/2), CGFloat(abs((node1.position.y)-(node2.position.y))/2), CGFloat(node1.position.z))
sceneView.scene.rootNode.addChildNode(node)
}
}
另请注意,这不是我的最终代码。一旦一切正常,它将被重构:)。
【问题讨论】:
-
你有没有找到解决这个问题的方法?我对这种实现也有类似的问题。
-
我发布了我当前的解决方案。尽管我已经重构了很多代码。希望它会有所帮助