如何在AR套件中旋转或缩放3D模型scn文件 [英] How to rotate or scale 3D model scn file in AR kit

查看:629
本文介绍了如何在AR套件中旋转或缩放3D模型scn文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此类也渲染了我的SCN文件.

this class render my SCN file as well.

import UIKit
import ARKit

class SimpleViewController: UIViewController {

    @IBOutlet var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()        
        sceneView.scene = SCNScene(named: "duck.scn", inDirectory: "models.scnassets/furniture")!
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        sceneView.session.run(ARWorldTrackingConfiguration())
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        sceneView.session.pause()
    }
}

但是我知道如何旋转或缩放对象(duck.scn)文件.我希望用户可以与我的对象进行交互.

but I know how can I rotate or scale my object (duck.scn) file. I would like to user can do interaction with my object.

推荐答案

如果要缩放SCNNode,可以执行以下操作:

If you want to scale an SCNNode you can do something like this:

/// Scales An SCNNode
///
/// - Parameter gesture: UIPinchGestureRecognizer
@objc func scaleObject(gesture: UIPinchGestureRecognizer) {

    guard let nodeToScale = currentNode else { return }
    if gesture.state == .changed {

        let pinchScaleX: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.x))
        let pinchScaleY: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.y))
        let pinchScaleZ: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.z))
        nodeToScale.scale = SCNVector3Make(Float(pinchScaleX), Float(pinchScaleY), Float(pinchScaleZ))
        gesture.scale = 1

    }
    if gesture.state == .ended { }

}

因此,当前节点是指SCNNode.

如果要移动SCNNode,可以执行以下操作:

If you want to move an SCNNode you can do something like this:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    //1. Get The Current Touch Point
    guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
        //2. Get The Next Feature Point Etc
        let hitTest = augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }

    //3. Convert To World Coordinates
    let worldTransform = hitTest.worldTransform

    //4. Set The New Position
    let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)

    //5. Apply To The Node
    currentNode.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)

}

如果要旋转SCNNode,则首先应创建一个变量来存储原始角度,例如:

If you would like to rotate an SCNNode you first should create a variable(s) to store the original angle e.g:

//Store The Rotation Of The CurrentNode
var currentAngleY: Float = 0.0

然后您可以执行以下操作:

Then you can do something like this:

/// Rotates An SCNNode Around It's YAxis
///
/// - Parameter gesture: UIRotationGestureRecognizer
@objc func rotateNode(_ gesture: UIRotationGestureRecognizer){

    //1. Get The Current Rotation From The Gesture
    let rotation = Float(gesture.rotation)

    //2. If The Gesture State Has Changed Set The Nodes EulerAngles.y
    if gesture.state == .changed{

        currentNode.eulerAngles.y = currentAngleY + rotation
    }

    //3. If The Gesture Has Ended Store The Last Angle Of The Cube
    if(gesture.state == .ended) {
        currentAngleY = currentNode.eulerAngles.y

    }
}

如果您想直接与SCNScene交互(尽管我相信不在ARKit中),则可以使用以下方法:

If you want to interact directly with your SCNScene (although I believe not in ARKit) you can use the following method:

var allowsCameraControl: Bool { get set }

特此:

如果将此属性设置为true,SceneKit将创建一个摄影机节点,然后 处理鼠标或触摸事件,以允许用户平移,缩放和 旋转他们对场景的看法. (启用用户相机控制不会 修改场景图或节点中已经存在的相机对象 包含它们.)

If you set this property to true, SceneKit creates a camera node and handles mouse or touch events to allow the user to pan, zoom, and rotate their view of the scene. (Enabling user camera control does not modify camera objects already existing in the scene graph or the nodes containing them.)

一个例子就是:

 sceneView.scene = SCNScene(named: "duck.scn", inDirectory: "models.scnassets/furniture")!
 sceneView.allowsCameraControl = true;

这篇关于如何在AR套件中旋转或缩放3D模型scn文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆