Ios 无法为某些情况创建圆柱体-Arkit
我有两个向量作为输入,我试图在它们之间创建一个圆柱体。 但当我的点的x和z坐标相同而y坐标不同时,这就失败了 我有以下职能:Ios 无法为某些情况创建圆柱体-Arkit,ios,swift,arkit,Ios,Swift,Arkit,我有两个向量作为输入,我试图在它们之间创建一个圆柱体。 但当我的点的x和z坐标相同而y坐标不同时,这就失败了 我有以下职能: func buildLineInTwoPointsWithRotation(from startPoint: SCNVector3, to endPoint: SCNVector3, radius: CGF
func buildLineInTwoPointsWithRotation(from startPoint: SCNVector3,
to endPoint: SCNVector3,
radius: CGFloat,
color: UIColor) -> SCNNode {
let w = SCNVector3(x: endPoint.x-startPoint.x,
y: endPoint.y-startPoint.y,
z: endPoint.z-startPoint.z)
let l = CGFloat(sqrt(w.x * w.x + w.y * w.y + w.z * w.z))
if l == 0.0 {
// two points together.
let sphere = SCNSphere(radius: radius)
sphere.firstMaterial?.diffuse.contents = color
self.geometry = sphere
self.position = startPoint
return self
}
let cyl = SCNCylinder(radius: radius, height: l)
cyl.firstMaterial?.diffuse.contents = color
self.geometry = cyl
//original vector of cylinder above 0,0,0
let ov = SCNVector3(0, l/2.0,0)
//target vector, in new coordination
let nv = SCNVector3((endPoint.x - startPoint.x)/2.0, (endPoint.y - startPoint.y)/2.0,
(endPoint.z-startPoint.z)/2.0)
// axis between two vector
let av = SCNVector3( (ov.x + nv.x)/2.0, (ov.y+nv.y)/2.0, (ov.z+nv.z)/2.0)
//normalized axis vector
let av_normalized = normalizeVector(av)
let q0 = Float(0.0) //cos(angel/2), angle is always 180 or M_PI
let q1 = Float(av_normalized.x) // x' * sin(angle/2)
let q2 = Float(av_normalized.y) // y' * sin(angle/2)
let q3 = Float(av_normalized.z) // z' * sin(angle/2)
let r_m11 = q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3
let r_m12 = 2 * q1 * q2 + 2 * q0 * q3
let r_m13 = 2 * q1 * q3 - 2 * q0 * q2
let r_m21 = 2 * q1 * q2 - 2 * q0 * q3
let r_m22 = q0 * q0 - q1 * q1 + q2 * q2 - q3 * q3
let r_m23 = 2 * q2 * q3 + 2 * q0 * q1
let r_m31 = 2 * q1 * q3 + 2 * q0 * q2
let r_m32 = 2 * q2 * q3 - 2 * q0 * q1
let r_m33 = q0 * q0 - q1 * q1 - q2 * q2 + q3 * q3
self.transform.m11 = r_m11
self.transform.m12 = r_m12
self.transform.m13 = r_m13
self.transform.m14 = 0.0
self.transform.m21 = r_m21
self.transform.m22 = r_m22
self.transform.m23 = r_m23
self.transform.m24 = 0.0
self.transform.m31 = r_m31
self.transform.m32 = r_m32
self.transform.m33 = r_m33
self.transform.m34 = 0.0
self.transform.m41 = (startPoint.x + endPoint.x) / 2.0
self.transform.m42 = (startPoint.y + endPoint.y) / 2.0
self.transform.m43 = (startPoint.z + endPoint.z) / 2.0
self.transform.m44 = 1.0
return self
}
func normalizeVector(_ iv: SCNVector3) -> SCNVector3 {
let length = sqrt(iv.x * iv.x + iv.y * iv.y + iv.z * iv.z)
if length == 0 {
return SCNVector3(0.0, 0.0, 0.0)
}
return SCNVector3( iv.x / length, iv.y / length, iv.z / length)
}
当两个点的x,z坐标相同,但y坐标完全不同时,我仍然不确定它为什么失败
如果有人能给我解释一下,我会非常高兴的
提前感谢。看看你的代码,它看起来非常复杂,因为它可以用更少的代码实现 此代码可与
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)
希望这有帮助……好的,谢谢你,伙计!我将尝试此更新并检查我的更新。我已经为一个圆柱体和SCNBox做了此操作:)伙计,我在调用中遇到一个错误:“额外参数”“endingVector”“。如何解决此问题?您确定已正确复制类和初始化器吗?这里没有问题:)