iOS 10 SceneKit 新特性 - SceneKit 制作 3D 場景框架

開頭語:
今天早上很開心,掘金的微博轉發(fā)我第一個blog 服務端Swift - Vapor (一),讓正在愁期末考試的我一下心花怒放。正好掘金賜給我一個內(nèi)測功能(原創(chuàng)文章編寫),于是今天打算擼一篇,希望長輩指導。

今天的主題是探索iOS10 SceneKit的新功能,你可以觀看今年WWDC的視頻Advances in SceneKit Rendering

SceneKit 介紹,SceneKit類似于Unity是一個制作3D物件的框架,很多3D游戲都可以基于這個框架進行開發(fā),自幾年前和Swift一起推出到現(xiàn)在廣受歡迎。比如今年推出的Swift Playground就有用到SceneKit開發(fā)一個3D游戲幫助小盆友學習coding。

Swift Playground

你可以看這里對SceneKit有點基礎的了解Objc-SceneKit介紹

今年的SceneKit已經(jīng)支持iWatch了,大東目前沒iWatch,不過覺得屏幕太小了,最多就是幾個3D的小部件吧。

Physically based rendering

今年最大的更新是提供了Physically based rendering(PBR),不懂可以上網(wǎng)查,簡單的說就是讓3D的物品更加3D了,我后面重點就是談談這個新的功能。

What's new in iOS 10 SceneKit: A new Physically Based Rendering (PBR) system allows you to leverage the latest in 3D graphics research to create more realistic results with simpler asset authoring

官方文檔談到的

PBR materials require only three fundamental properties— diffuse, metalness, and roughness—to produce a wide range of realistic shading effects.

相比之前,現(xiàn)在映射一個3D物體變得很容易,只要實現(xiàn)這3個變量 diffuse metalness roughness 即可。我們做一個小Demo更加幫助理解。

首先下載自己喜歡的物品材質,所謂的材質可以理解一個為意見物品的“皮膚”,你可以選擇喜歡的貼圖來實現(xiàn)不同的效果。下載網(wǎng)址

let materialPrefixes : [String] = ["Copper-scuffed"]
let material = sphereNode.geometry?.firstMaterial

// Declare that you intend to work in PBR shading mode
// Note that this requires iOS 10 and up
material?.lightingModel = SCNMaterial.LightingModel.physicallyBased
// Setup the material maps for your object
let materialFilePrefix = materialPrefixes[0]
material?.diffuse.contents = UIImage(named: "\(materialFilePrefix)-albedo.png")
material?.roughness.contents = UIImage(named: "\(materialFilePrefix)-roughness.png")
material?.metalness.contents = UIImage(named: "\(materialFilePrefix)-metal.png")

這里就實現(xiàn)了物品材質的選擇,但是球的背景圖是怎么實現(xiàn)的,怎么會有倒攝的效果呢?

其實實現(xiàn)也很簡單,假設你把一個物體的LightingModel定位為PBR,只要提供一張全景照片就能夠將照片隱射到上面去

let env = UIImage(named: "interior_hdri_29_20150416_1169368110.jpg")
scene.lightingEnvironment.contents = env

注意,這里的全景照片指的不是手機拍的全景照片而是高質量,專業(yè)效果拍攝出來的圖片,如果照片不合格,就會出現(xiàn)錯誤,如下示例圖。


圖片源于網(wǎng)絡

當然還要有后面背景圖

let bg = UIImage(named: "interior_hdri_29_20150416_1169368110.jpg")
scene.background.contents = bg

注意觀察到它們是同一張圖

最后就會呈現(xiàn)如下的效果圖,我用Swift Playground可使,真是太強大了。我還加入了Core Motion,可以移動iPad來切換不同的視角,這同時也是VR View的制作方式呀,以后更新如何開發(fā)一個屬于iOS 的VR應用,并連接SceneKit,感覺以后發(fā)展可觀。


當然你還可以新建一個.scn文件來制作

  • 先拖一個SphereNode


  • 更改Material的屬性


  • 更改Lighting Environment 和 Background 屬性


可以自己嘗試不同的3D物件,在Object-library上拖拽到.scn文件中即可

Physically Based Lights

Authors of PBR scene content often prefer working in physically based terms, so you can now define lighting using intensity (in lumens) and color temperature (in degrees Kelvin), and import specifications for real-world light fixtures using the IESProfileURL property.


今年對scnLight 的更新是新增一種燈源,就是可以在網(wǎng)上找到一種文件格式.ies,就可以導入不同格式的燈源。這對設計師來說應該是一個福利

//add a liteNode
let liteNode = SCNNode()
liteNode.light = SCNLight()
liteNode.light?.iesProfileURL = URL(fileReferenceLiteralResourceName: "LF6N_1_42TRT_F6LS73.ies")
liteNode.light?.type = .IES

HDR Camera

Add even more realism with the new HDR features and effects in the SCNCamera class. With HDR rendering, SceneKit captures a much wider range of brightness and contrast in a scene, then allows you to customize the tone mapping that adapts that scene for the narrower range of a device’s display. Enable exposure adaptation to create automatic effects when, for example, the player in your game moves from a darkened area into sunlight. Or use vignetting, color fringing, and color grading to add a filmic look to your game.


我也做了一個Demo來嘗試新的api效果,大東表示非常贊??,有一些屬性是支持Animate的,這在官方的Demo上有展示過一個效果場景就是當一個突然物體飛速移動時,周圍的場景會變有模糊效果,聯(lián)系一下開車場景,這種效果讓游戲開發(fā)變得更加逼真!

蘋果今年給SCNCamera添加了很多新的效果,可以找官方的APi文檔查找。

今年的 WWDC Demo

蘋果今年開源了兩個適合大家入們SceneKit的Demo,而且都有用Swift 3編寫,看Session到那里時非常激動,現(xiàn)場也很多人鼓掌歡呼(表示大東很想去WWDC)

寫在最后,大東想說,SceneKit很強大,我也相信蘋果如果以后要推出VR,SceneKit這種制作3D場景的框架一定會被廣受利用。最近我還在探索如何用Google 的VR-iOS SDK來實現(xiàn)一個SceneKit場景。也希望能和大家交流學習。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 上大學的第一天開始,吳立杰就在琢磨,怎樣把自己設計的服裝草圖賣出去呢?整個暑假,他都在做同一件事,就是頂著烈日,背...
    a_f59a閱讀 268評論 0 1
  • 今天跟同事閑聊,說到他家以前養(yǎng)了一條好玩的小狗,但是到后面因為有了小孩只好送人。初為父母的我立馬反應過來其中的原因...
    頭發(fā)很亂2閱讀 142評論 0 0
  • 他醒著時我困著他睡著了我醒著幾夜里來醒著困著腦殼里的小零件松了轉起來咔啦咔啦連說話都變溫柔
    鉛筆羊閱讀 186評論 0 1