use SceneKit to display 3d objects in an iOS project
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
相关文章
- 在 Go 里用 CGO?这 7 个问题你要关注!
- 9款优秀的去中心化通讯软件 Matrix 的客户端
- 求职数据分析,项目经验该怎么写
- 在OKR中,我看到了数据驱动业务的未来
- 火山引擎云原生大数据在金融行业的实践
- OpenHarmony富设备移植指南(二)—从postmarketOS获取移植资源
- 《数据成熟度指数》报告:64%的企业领袖认为大多数员工“不懂数据”
- OpenHarmony 小型系统兼容性测试指南
- 肯睿中国(Cloudera):2023年企业数字战略三大趋势预测
- 适用于 Linux 的十大命令行游戏
- GNOME 截图工具的新旧截图方式
- System76 即将推出的 COSMIC 桌面正在酝酿大变化
- 2GB 内存 8GB 存储即可流畅运行,Windows 11 极致精简版系统 Tiny11 发布
- 迎接 ecode:一个即将推出的具有全新图形用户界面框架的现代、轻量级代码编辑器
- loongarch架构介绍(三)—地址翻译
- Go 语言怎么解决编译器错误“err is shadowed during return”?
- 敏捷:可能被开发人员遗忘的部分
- Denodo预测2023年数据管理和分析的未来
- 利用数据推动可持续发展
- 在 Vue3 中实现 React 原生 Hooks(useState、useEffect),深入理解 React Hooks 的