iOS 10 SceneKit 新特性 - SceneKit 製做3D 場景框架

今天早上很開心,掘金的微博轉發我第一個blog 服務端Swift 篇(一),讓正在愁期末考試的我一下心花盛開。正好掘金賜給我一個內測功能(原創文章編寫),因而今天打算擼一篇,但願長輩指導。javascript

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

SceneKit 介紹,SceneKit相似於Unity是一個製做3D物件的框架,不少3D遊戲均可以基於這個框架進行開發,自幾年前和Swift一塊兒推出到如今廣受歡迎。好比今年推出的Swift Playground就有用到SceneKit開發一個3D遊戲幫助小盆友學習coding。
swift

Swift Playground

你能夠看這裏對SceneKit有點基礎的瞭解Objc-SceneKit介紹api

今年的SceneKit已經支持iWatch了,大東目前沒iWatch,不過以爲屏幕過小了,最多就是幾個3D的小部件吧。網絡

Physically based rendering

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

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框架

官方文檔談到的ide

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

相比以前,如今映射一個3D物體變得很容易,只要實現這3個變量 diffuse metalness roughness 便可。咱們作一個小Demo更加幫助理解。ui

首先下載本身喜歡的物品材質,所謂的材質能夠理解一個爲意見物品的「皮膚」,你能夠選擇喜歡的貼圖來實現不一樣的效果。下載網址

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")複製代碼

這裏就實現了物品材質的選擇,可是球的背景圖是怎麼實現的,怎麼會有倒攝的效果呢?

其實實現也很簡單,假設你把一個物體的LightingModel定位爲PBR,只要提供一張全景照片就可以將照片隱射到上面去

let env = UIImage(named: "interior_hdri_29_20150416_1169368110.jpg")
scene.lightingEnvironment.contents = env複製代碼

注意,這裏的全景照片指的不是手機拍的全景照片而是高質量,專業效果拍攝出來的圖片,若是照片不合格,就會出現錯誤,以下示例圖。

圖片源於網絡

固然還要有後面背景圖

let bg = UIImage(named: "interior_hdri_29_20150416_1169368110.jpg")
scene.background.contents = bg複製代碼

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

最後就會呈現以下的效果圖,我用Swift Playground可以使,真是太強大了。我還加入了Core Motion,能夠移動iPad來切換不一樣的視角,這同時也是VR View的製做方式呀,之後更新如何開發一個屬於iOS 的VR應用,並鏈接SceneKit,感受之後發展可觀。

固然你還能夠新建一個.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 的更新是新增一種燈源,就是能夠在網上找到一種文件格式.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上有展現過一個效果場景就是當一個忽然物體飛速移動時,周圍的場景會變有模糊效果,聯繫一下開車場景,這種效果讓遊戲開發變得更加逼真!

蘋果今年給SCNCamera添加了不少新的效果,能夠找官方的APi文檔查找。

今年的 WWDC Demo

蘋果今年開源了兩個適合你們入們SceneKit的Demo,並且都有用Swift 3編寫,看Session到那裏時很是激動,現場也不少人鼓掌歡呼(表示大東很想去WWDC)

寫在最後,大東想說,SceneKit很強大,我也相信蘋果若是之後要推出VR,SceneKit這種製做3D場景的框架必定會被廣受利用。最近我還在探索如何用Google 的VR-iOS SDK來實現一個SceneKit場景。也但願能和你們交流學習。

相關文章
相關標籤/搜索