使用ARKit的动画3D模型。迅速

时间:2017-12-19 13:45:56

标签: swift xcode arkit collada 3d-model

我添加了购买的3D僵尸模型,我想多次添加它,所以有一群僵尸在追逐玩家(他的相机)。 DAE文件(僵尸)我有几个动画(攻击,步行,运行等)。我添加了几个这些僵尸模型,它们都出现在我希望它们出现的位置上(先前确定的坐标存储在僵尸数组中) 当它们出现在屏幕上时,它们会执行DAE文件中显示的所有动画。

现在我的问题是:是否可以添加此DAE僵尸模型但是在指定的框架中显示它?例如2-122?到目前为止我的代码(我删除了不必要的内容)

import UIKit
import CoreLocation
import SceneKit
import ARKit

@available(iOS 11.0, *)
class VRViewController: UIViewController, CLLocationManagerDelegate
{
    /* VARIABLES */

var number:Int = 10
var speed:Int = 4

var zombies = [[AnyObject]]()

var timer = Timer()

var nodes = [SCNNode]()
let configuration = ARWorldTrackingConfiguration()
@IBOutlet weak var sceneView: ARSCNView!

override func viewWillAppear(_ animated: Bool)
{
    super.viewWillAppear(animated)

    // Run the view's session
    sceneView.session.run(configuration)
}

override func viewDidLoad()
{
    super.viewDidLoad()

    // Set the view's delegate
    sceneView.delegate = self as? ARSCNViewDelegate
    // Create a new scene
    let scene = SCNScene()
    // Set the scene to the view
    sceneView.scene = scene
    sceneView.autoenablesDefaultLighting = true
    /* sceneView.debugOptions  = [.showConstraints, .showLightExtents, ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin] */
}

override func viewDidAppear(_ animated: Bool)
{
    setZombies()
    timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(VRViewController.restartSession)), userInfo: nil, repeats: true)
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}

/* MAIN FUNCTIONS */

func setZombies()
{
    for i in 0..<number
    {
        let zombiePlane = zombies[i][0] as! CGPoint
        let thetaPlane = zombies[i][1] as! Double
        let xPlane = abs(zombiePlane.x - center.x)
        let yPlane = abs(zombiePlane.y - center.y)
        let distance = sqrt((xPlane*xPlane) + (yPlane*yPlane))

        var theta3D:Double = thetaPlane * (180/Double.pi) - diff2D - 90 /* degrees */
        theta3D = theta3D * (Double.pi/180) /* radians */

        let x3D = Float(distance) * Float(cos(theta3D))
        let z3D = Float(distance) * Float(sin(theta3D))

        addZombies(i:i, x: x3D, y: -1.5, z: z3D)
    }
}

func addZombies(i:Int, x: Float, y: Float, z: Float) {
    guard let zombieScene = SCNScene(named: "art.scnassets/zombie/StrongZombie.DAE") else { return }
    let zombieNode = SCNNode()
    let zombieSceneChildNodes = zombieScene.rootNode.childNodes

    for childNode in zombieSceneChildNodes {
        zombieNode.addChildNode(childNode)
    }

    zombieNode.position = SCNVector3(x, y, z)
    zombieNode.scale = SCNVector3(0.1, 0.1, 0.1)
    sceneView.scene.rootNode.addChildNode(zombieNode)

    nodes.append(zombieNode)
}

func restartSession()
{
    /* self.sceneView.session.run(configuration, options: [.resetTracking]) */

    for i in 0..<number
    {
        let theta3D = atan2(nodes[i].position.z, nodes[i].position.x)

        let movement = (Float(speed)/5)
        print(speed)
        let distance = sqrt((nodes[i].position.x)*(nodes[i].position.x) + (nodes[i].position.z)*(nodes[i].position.z)) - movement
        let x3D = Float(distance) * Float(cos(theta3D))
        let z3D = Float(distance) * Float(sin(theta3D))

        nodes[i].position = SCNVector3(x:x3D, y:-10, z:z3D)
    }
}

/* SESSION FUNCTIONS */

func session(_ session: ARSession, didFailWithError error: Error)
{
    // Present an error message to the user
}

func sessionWasInterrupted(_ session: ARSession)
{
    // Inform the user that the session has been interrupted, for example, by presenting an overlay
}

func sessionInterruptionEnded(_ session: ARSession)
{
    // Reset tracking and/or remove existing anchors if consistent tracking is required
}
}

0 个答案:

没有答案