There are three fundamental Kits in iOS development framework named "OpenGL ES", "Metal", "SceneKit" and an extended kit named "RealityKit" for 3d development. The "ARkit" is functional for both 3d and camera display that render a 3d scene in the camera environment.

So how to add a 3d scene to an iOS project by a .scn file? Actualy we can add the 3d scene by other format like "usdz", ".dae", ".abc". see at AppleDeveloper

1. Create a scene assets folder

If we build a mixed app with UIKit and SceneKit, the prefered way is to create a specific assets folder to manage our 3d resources. Then put the meterial files, or we can create a new scene (.scn file) in it.

It is easy to create a simple scene in Xcode using File > New File, and it will automatically create an scn file.

2. Import SceneKit on your viewController File ( view file of SwiftUI )

Whether you are using Storyboard or SwiftUI, import the SceneKit directly.

// ViewController
import UIKit
import SceneKit

// SwiftUI
import SwiftUI
import SceneKit

3. Create Scene view with some basic arguments

A scene object is an object that holds all 3d objects' properties, and the sceneView object render the scene on the screen. We should add the 3d file to a sceneView as the scene, in code: Remember that, SCNScene(named:String) need the full path of the scn file.

// ViewController
    let scene = SCNScene(named: "art.scnassets/bull.scn")!
    let sceneView = SCNView(frame: self.view.bounds)
        sceneView.scene = scene
    self.addSubView( sceneView )

// SwiftUI
struct ContentView: View {
    var body: some View {

        SceneView(scene: SCNScene(named: "art.scnassets/bull.scn"))
            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

    }
}

4. Add interactions to scene or subobject in the scene



override func viewDidLoad() {
    super.viewDidLoad()

    // ...
    let moveGesture = UIPanGestureRecognizer(target: self, action: #selector(drag(_:)))

    sceneView.addGestureRecognizer(moveGesture)
}

@objc
func dragCups(_ gestureRecognize: UIGestureRecognizer) {
// do something
}

5. Run the app in the simulator

More

Listen to Rendering events
PhysicsWorld
AudioPlayer

文章地址:




标签: ios, 3d, sceneKit

仅有一条评论

  1. 学习一下~~~~支持顶起

添加新评论