0

我有一个扩展名为 .scn 的 3D 模型。如何在没有来自 iOS 示例放置对象应用程序的虚拟对象文件的情况下使用捏合手势缩放它。

如果从 .obj 文件转换为 .scn,则捏合手势可以很好地工作。但它不适用于 .dae 模型。

    func addPinchGestureToSceneView() {

        pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(scale))
        pinchGesture.scale = 1.0;
        pinchGesture.delegate = self
        self.sceneView.addGestureRecognizer(pinchGesture)
    }
  private func node(at position: CGPoint) -> SCNNode? {
        var hitTestOptions = [SCNHitTestOption: Any]()
        hitTestOptions[SCNHitTestOption.boundingBoxOnly] = true
        return sceneView.hitTest(position, options: hitTestOptions)
            .first(where: { self.getOnlyModelName(name: $0.node.name ?? "") == self.currentmodel.modelname})?
            .node

    }

 @objc func scale(_ gesture: UIPinchGestureRecognizer) {

        if self.currentmodel.isZoomEnabled == false{
            return
        }

        let location = gesture.location(in: sceneView)

        guard let node = node(at: location)else{return}

    //    guard let node = node(at: location) else  { return }

        switch gesture.state {
        case .began:
            originalScale = node.scale
            gesture.scale = CGFloat(node.scale.x)
            print("Begin:: \(originalScale)")
        case .changed:
            guard var originalScale = originalScale else { return }
            if gesture.scale > 2.0{
                return
            }
            originalScale.x = Float(gesture.scale)
            originalScale.y = Float(gesture.scale)
            originalScale.z = Float(gesture.scale)
            node.scale = originalScale
        case .ended:

            guard var originalScale = originalScale else { return }
            if gesture.scale > 2.0{
                return
            }
            originalScale.x = Float(gesture.scale)
            originalScale.y = Float(gesture.scale)
            originalScale.z = Float(gesture.scale)
            node.scale = originalScale
            gesture.scale = CGFloat(node.scale.x)

        default:
            gesture.scale = 1.0
            originalScale = nil
        }
4

1 回答 1

0

当它是一个 dae 时,您可能需要尝试抓取在命中测试中捕获的节点的父节点。我有一个与 dae 类似的问题,通过让孩子的父母,甚至有时是孩子的祖父母来解决。

于 2018-02-13T13:22:46.343 回答