SceneKit to show 3D content in Swift 5

— In this history, I going to show how easy was show 3D content with SceneKit framework using high-level scene descriptions.

 

This means that we can use SceneKit for easy things (normal development), and Metal or OpenGL to make high-level customization e.g.: Games or for a high level of images manipulation.

In addition, there is little documentation to use the metal and OpenGL, adding its complexity of use.

fter putting ourselves in context and getting the theory, it’s time for the code 💻...

 

I will show how to open .obj files, although SceneKit it is able to open more kinds of files like a .dae.blend, .scn…

So, open Xcode… Create new project… Select Single View App…

⌨ TIME FOR CODING…

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import UIKit
import SceneKit
 
class ViewController: UIViewController {
     
    @IBOutlet weak var sceneView: SCNView!
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        // 1: Load .obj file
        let scene = SCNScene(named: "converse_obj.obj")
         
        // 2: Add camera node
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        // 3: Place camera
        cameraNode.position = SCNVector3(x: 0, y: 10, z: 35)
        // 4: Set camera on scene
        scene?.rootNode.addChildNode(cameraNode)
         
        // 5: Adding light to scene
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .omni
        lightNode.position = SCNVector3(x: 0, y: 10, z: 35)
        scene?.rootNode.addChildNode(lightNode)
         
        // 6: Creating and adding ambien light to scene
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light?.type = .ambient
        ambientLightNode.light?.color = UIColor.darkGray
        scene?.rootNode.addChildNode(ambientLightNode)
         
        // If you don't want to fix manually the lights
//        sceneView.autoenablesDefaultLighting = true
         
        // Allow user to manipulate camera
        sceneView.allowsCameraControl = true
         
        // Show FPS logs and timming
        // sceneView.showsStatistics = true
         
        // Set background color
        sceneView.backgroundColor = UIColor.white
         
        // Allow user translate image
        sceneView.cameraControlConfiguration.allowsTranslation = false
         
        // Set scene settings
        sceneView.scene = scene
         
    }
     
     
}

Step by step explanation

  

 

👁 The basic UI

CONCLUSION

posted on   youhui  阅读(214)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示