我想获取用于设置IK约束的骨骼位置。我尝试将每个骨骼位置从其父坐标转换为场景坐标,但是结果不正确。
下面是转换方法
func convertPositionToParentCoordinate(position: SCNVector3, until ancestor: SCNNode) -> SCNVector3 {
var convertedPos = position
if let selfParent = self.parent {
convertedPos = self.convertPosition(position, to: selfParent)
if selfParent != ancestor {
convertedPos = selfParent.convertPositionToParentCoordinate(position: convertedPos, until: ancestor)
}
}
return convertedPos
}
显示骨骼
func showBones(isShown: Bool, bone: SCNNode,scale: Float){
let xScale = CGFloat(bone.scale.x * scale)
if isShown == true {
if bone.geometry == nil {
bone.geometry = SCNBox(width: xScale*2, height: xScale, length: xScale, chamferRadius: 0)
var pos = bone.position
// draw reference objects in scene (self) coordinate.
pos = bone.convertPositionToParentCoordinate(position: pos, until: self)
pos = SCNVector3(pos.x - 1, pos.y, pos.z)
let refnode = SCNNode()
refnode.geometry = SCNBox(width: xScale*2, height: xScale, length: xScale, chamferRadius: 0)
refnode.position = pos
self.addChildNode(refnode)
if bone.name == "joint1" {
let material = SCNMaterial()
material.diffuse.contents = UIColor.red
bone.geometry?.materials = [material]
refnode.geometry?.materials = [material]
}
if bone.name == "joint2" {
let material = SCNMaterial()
material.diffuse.contents = UIColor.yellow
bone.geometry?.materials = [material]
refnode.geometry?.materials = [material]
}
if bone.name == "joint3" {
let material = SCNMaterial()
material.diffuse.contents = UIColor.green
bone.geometry?.materials = [material]
refnode.geometry?.materials = [material]
}
if bone.name == "joint4" {
let material = SCNMaterial()
material.diffuse.contents = UIColor.cyan
bone.geometry?.materials = [material]
refnode.geometry?.materials = [material]
}
if bone.name == "joint5" {
let material = SCNMaterial()
material.diffuse.contents = UIColor.blue
bone.geometry?.materials = [material]
refnode.geometry?.materials = [material]
}
if bone.name == "joint6" {
let material = SCNMaterial()
material.diffuse.contents = UIColor.purple
bone.geometry?.materials = [material]
refnode.geometry?.materials = [material]
}
}
}else {
let isBox = bone.geometry?.isKind(of: SCNBox.self)
if isBox == true {
bone.geometry = nil
}
}
}
这样的结果 can't convert position correctly
我还尝试使用骨骼的wordPosition属性,或使用transform进行转换,要么不起作用。如何正确将骨骼的位置从其局部坐标转换为世界坐标?
预先感谢