前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >use SceneKit to display 3d objects in an iOS project

use SceneKit to display 3d objects in an iOS project

作者头像
雪碧君
发布2023-02-15 15:31:04
3490
发布2023-02-15 15:31:04
举报

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.

代码语言:javascript
复制
// 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.

代码语言:javascript
复制
// 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
代码语言:javascript
复制
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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-02-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Create a scene assets folder
  • 2. Import SceneKit on your viewController File ( view file of SwiftUI )
  • 3. Create Scene view with some basic arguments
  • 4. Add interactions to scene or subobject in the scene
  • 5. Run the app in the simulator
  • More
    • Listen to Rendering events
      • PhysicsWorld
        • AudioPlayer
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档