ios - 缩放节点的问题(.simdPivot 与 simdScale 和 .scale 属性)

标签 ios swift scenekit augmented-reality arkit

我不明白节点上的节点缩放是如何工作的。

我正在尝试了解 Apple 的 Creating Face Based AR Experiences 上的代码示例项目工作。具体来说,我试图了解 TransformVisualization.swift 文件和应用于其节点的转换。

addEyeTransformNodes() 方法被调用,左眼节点和右眼节点都使用 simdScale 属性进行缩放。这是我感到困惑的部分。

我尝试使用 .scale 和 .simdScale 属性缩放同一个节点,但它们都没有执行任何操作。

此外,更令人困惑的是,即使 .simdPivot 的值大于 1,节点也会按比例缩小。我预计节点会扩大。

为什么我们需要设置 .simdPivot 来缩放节点而不是 .scale 和 .simdScale 属性?

这是我正在谈论的功能。

func addEyeTransformNodes() {
  guard #available(iOS 12.0, *), let anchorNode = contentNode else { return }

  // Scale down the coordinate axis visualizations for eyes.
  rightEyeNode.simdPivot = float4x4(diagonal: float4(3, 3, 3, 1))
  leftEyeNode.simdPivot = float4x4(diagonal: float4(3, 3, 3, 1))

  anchorNode.addChildNode(rightEyeNode)
  anchorNode.addChildNode(leftEyeNode)
}

这是我尝试过的:

func addEyeTransformNodes() {
  guard #available(iOS 12.0, *), let anchorNode = contentNode else { return }

  // Does nothing
  rightEyeNode.simdScale = float3(3, 3, 3)
  // Does nothing
  leftEyeNode.scale = SCNVector3(x: 3, y: 3, z: 3)

  anchorNode.addChildNode(rightEyeNode)
  anchorNode.addChildNode(leftEyeNode)
}

我希望按照我的方式扩展节点,但什么也没发生。

期待您的回答和帮助。

最佳答案

如果您需要偏移轴心点(在应用旋转和/或缩放之前)点使用 simdPivot实例属性。

使用我的测试代码来了解它是如何工作的:

let sphereNode1 = SCNNode(geometry: SCNSphere(radius: 1))
sphereNode1.geometry?.firstMaterial?.diffuse.contents = UIColor.red
sphereNode1.position = SCNVector3(-5, 0, 0)
scene.rootNode.addChildNode(sphereNode1)

let sphereNode2 = SCNNode(geometry: SCNSphere(radius: 1))
sphereNode2.geometry?.firstMaterial?.diffuse.contents = UIColor.green
sphereNode2.simdPivot.columns.3.x = -1

sphereNode2.scale = SCNVector3(2, 2, 2)           // WORKS FINE
//sphereNode2.simdScale = float3(2, 2, 2)         // WORKS FINE

scene.rootNode.addChildNode(sphereNode2)

let sphereNode3 = SCNNode(geometry: SCNSphere(radius: 1))
sphereNode3.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
sphereNode3.position = SCNVector3(5, 0, 0)
scene.rootNode.addChildNode(sphereNode3)

枢轴偏移量是x: -1:

enter image description here

枢轴偏移量是x: 0:

enter image description here

Using init(diagonal:) initializer helps you creating a new 4x4 Matrix with the specified vector on the main diagonal. This method has an issue: it scales down objects when you're assigning diagonal values greater than 1, and vice versa. So, if you want to scale up character's eyes use the following approach as a workaround:

rightEyeNode.simdPivot = float4x4(diagonal: float4(1/3, 1/3, 1/3, 1))
leftEyeNode.simdPivot = float4x4(diagonal: float4(1/3, 1/3, 1/3, 1))

我认为 Apple 工程师将来会解决这个问题。

希望这对您有所帮助。

关于ios - 缩放节点的问题(.simdPivot 与 simdScale 和 .scale 属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56028858/

相关文章:

ios - 如何使用 Scene Kit 每帧运行一段代码?

ios - 物理对象在 SceneKit 中落入无穷大

ios - GAIDictionaryBuilder的NSNumber值失败

ios - managedObject 类和我自己的模型类之间的关系?

ios - ACAccountStore accountsWithAccountType 返回空列表但访问请求被授予

ios - API.AI 的 JSON 响应

swift - 通过初始化器注入(inject)依赖项(使用 Storyboard)? [ swift ]

ios - 如何填充 SCNBox 的内部

ios - MKPolygon 中的邮政编码

swift - 旋转 UICollectionView 会导致错误