【问题标题】:SwiftUI Path: upside down and wrong Pivot in ScenekitSwiftUI 路径:Scenekit 中的颠倒和错误的 Pivot
【发布时间】:2020-04-09 19:34:54
【问题描述】:

问题: 当我解析到 Scenekit 的 SwiftUI 路径时,它是颠倒的,并且枢轴位于边界框之外。

我试过了: 将 SwiftUI .anchorPreference 设置为中心。没有任何区别。

目标: 如何使枢轴成为边界框的中心底部?

重要提示:我需要正确的 SwiftUI 路径 -> SCN Pivot 平移,而不是手动偏移枢轴,因为这会影响场景上的所有对象枢轴

import SwiftUI

struct PatternScene: View {
    let idx: Int

    let pattern = Path { path in
        path.move(to: CGPoint(x: 50, y: 50))
        path.addLine(to: CGPoint(x: 250, y: 50))
        path.addLine(to: CGPoint(x: 250, y: 250))
        path.addLine(to: CGPoint(x: 50, y: 250))
        path.addLine(to: CGPoint(x: 100, y: 100))
    }

    var body: some View {
        self.pattern
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            .anchorPreference(key: MyTextPreferenceKey.self, value: .bounds, transform: { [MyTextPreferenceData(viewIdx: self.idx, bounds: $0)] })
    }
}

struct PatternScene_Previews: PreviewProvider {
    static var previews: some View {
        PatternScene(idx: 0)
    }
  }


 //Connect PatternScene with Scenekit
        let scnpath = PatternScene(idx: 0).pattern.cgPath
        let shape = SCNShape(path: UIBezierPath(cgPath: scnpath), extrusionDepth: 1)
        let material = SCNMaterial()
        //material.diffuse.contents = UIColor.red
        material.isDoubleSided = true
        // shape.firstMaterial = material
         let shapeNode = SCNNode(geometry: shape)
        scene.rootNode.addChildNode(shapeNode)
        shapeNode.position = SCNVector3(x: 0, y: 0, z: 0)
        shapeNode.scale = SCNVector3(x: 1, y: 1, z: 1)
        shapeNode.pivot = SCNMatrix4MakeTranslation(0, 0, 0)

【问题讨论】:

    标签: swiftui scenekit


    【解决方案1】:

    UIKit 和 SwiftUI 使用翻转几何,当您从画布的顶部移动到底部时,y 值会增加。 Scenekit 不使用翻转几何体。所以你必须“翻转” y 值......

    在图层上你甚至可以设置这个值...https://developer.apple.com/documentation/quartzcore/calayer/1410960-geometryflipped ...但我认为scenekit没有这样的值...

    【讨论】:

    • 感谢 Chris,我找到了如何在 SceneKit 中翻转枢轴,但我想在 swiftUI Anchor 和 SceneKit 枢轴之间建立正确的关联。 shapeNode.pivot = SCNMatrix4MakeTranslation(0, 0, 0)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    相关资源
    最近更新 更多