我有两个向量作为输入,我试图在它们之间创建一个圆柱体。 但是,当我的点的x和z坐标相同且y坐标不同时,这会失败
我有以下功能:
ubuntu 18.04
}
当两个点的x,z坐标相同但y坐标完全不同时,我仍然不确定它失败的原因。
如果有人能解释我,我会很高兴。
先谢谢。
答案 0 :(得分:1)
查看你的代码,看起来非常复杂,可以用很少的代码实现。
此代码可与SCNBox
或SCNCylinder
几何图形一起使用。
class MeasuringLineNode: SCNNode{
init(startingVector vectorA: GLKVector3, endingVector vectorB: GLKVector3) {
super.init()
//1. Create The Height Our Box Which Is The Distance Between Our 2 Vectors
let height = CGFloat(GLKVector3Distance(vectorA, vectorB))
//2. Set The Nodes Position At The Same Position As The Starting Vector
self.position = SCNVector3(vectorA.x, vectorA.y, vectorA.z)
//3. Create A Second Node Which Is Placed At The Ending Vectors Posirions
let nodeVectorTwo = SCNNode()
nodeVectorTwo.position = SCNVector3(vectorB.x, vectorB.y, vectorB.z)
//4. Create An SCNNode For Alignment Purposes At 90 Degrees
let nodeZAlign = SCNNode()
nodeZAlign.eulerAngles.x = Float.pi/2
//5. Create An SCNCyclinder Geometry To Act As Our Line
let cylinder = SCNCylinder(radius: 0.001, height: height)
let material = SCNMaterial()
material.diffuse.contents = UIColor.white
cylinder.materials = [material]
/*
If you want to use an SCNBox then use the following:
let box = SCNBox(width: 0.001, height: height, length: 0.001, chamferRadius: 0)
let material = SCNMaterial()
material.diffuse.contents = UIColor.white
box.materials = [material]
*/
//6. Create The LineNode Centering On The Alignment Node
let nodeLine = SCNNode(geometry: cylinder)
nodeLine.position.y = Float(-height/2)
nodeZAlign.addChildNode(nodeLine)
self.addChildNode(nodeZAlign)
//7. Force The Node To Look At Our End Vector
self.constraints = [SCNLookAtConstraint(target: nodeVectorTwo)]
}
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}
假设您有2个SCNNodes
(在我的示例中,我将简单地称为nodeA和nodeB),您可以创建一个连接线,如下所示:
//1. Convert The Nodes SCNVector3 to GLVector3
let nodeAVector3 = GLKVector3Make(nodeA.position.x, nodeA.position.y, nodeA.position.z)
let nodeBVector3 = GLKVector3Make(nodeB.position.x, nodeB.position.y, nodeB.position.z)
//2. Draw A Line Between The Nodes
let line = MeasuringLineNode(startingVector: nodeAVector3 , endingVector: nodeBVector3)
self.augmentedRealityView.scene.rootNode.addChildNode(line)
希望这会有所帮助......