開頭語(yǔ):
今天早上很開心窍蓝,掘金的微博轉(zhuǎn)發(fā)我第一個(gè)blog 服務(wù)端Swift - Vapor (一),讓正在愁期末考試的我一下心花怒放务热。正好掘金賜給我一個(gè)內(nèi)測(cè)功能(原創(chuàng)文章編寫)忆嗜,于是今天打算擼一篇,希望長(zhǎng)輩指導(dǎo)崎岂。
今天的主題是探索iOS10 SceneKit的新功能捆毫,你可以觀看今年WWDC的視頻Advances in SceneKit Rendering
SceneKit 介紹,SceneKit類似于Unity是一個(gè)制作3D物件的框架冲甘,很多3D游戲都可以基于這個(gè)框架進(jìn)行開發(fā)冻璃,自幾年前和Swift一起推出到現(xiàn)在廣受歡迎。比如今年推出的Swift Playground就有用到SceneKit開發(fā)一個(gè)3D游戲幫助小盆友學(xué)習(xí)coding损合。
你可以看這里對(duì)SceneKit有點(diǎn)基礎(chǔ)的了解Objc-SceneKit介紹
今年的SceneKit已經(jīng)支持iWatch了,大東目前沒iWatch娘纷,不過覺得屏幕太小了嫁审,最多就是幾個(gè)3D的小部件吧。
Physically based rendering
今年最大的更新是提供了Physically based rendering(PBR)赖晶,不懂可以上網(wǎng)查律适,簡(jiǎn)單的說就是讓3D的物品更加3D了,我后面重點(diǎn)就是談?wù)勥@個(gè)新的功能遏插。
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)在映射一個(gè)3D物體變得很容易,只要實(shí)現(xiàn)這3個(gè)變量 diffuse metalness roughness 即可胳嘲。我們做一個(gè)小Demo更加幫助理解厂僧。
首先下載自己喜歡的物品材質(zhì),所謂的材質(zhì)可以理解一個(gè)為意見物品的“皮膚”了牛,你可以選擇喜歡的貼圖來實(shí)現(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")
這里就實(shí)現(xiàn)了物品材質(zhì)的選擇,但是球的背景圖是怎么實(shí)現(xiàn)的鹰祸,怎么會(huì)有倒攝的效果呢甫窟?
其實(shí)實(shí)現(xiàn)也很簡(jiǎn)單,假設(shè)你把一個(gè)物體的LightingModel定位為PBR蛙婴,只要提供一張全景照片就能夠?qū)⒄掌[射到上面去
let env = UIImage(named: "interior_hdri_29_20150416_1169368110.jpg")
scene.lightingEnvironment.contents = env
注意粗井,這里的全景照片指的不是手機(jī)拍的全景照片而是高質(zhì)量,專業(yè)效果拍攝出來的圖片,如果照片不合格浇衬,就會(huì)出現(xiàn)錯(cuò)誤懒构,如下示例圖。
當(dāng)然還要有后面背景圖
let bg = UIImage(named: "interior_hdri_29_20150416_1169368110.jpg")
scene.background.contents = bg
注意觀察到它們是同一張圖
最后就會(huì)呈現(xiàn)如下的效果圖径玖,我用Swift Playground可使痴脾,真是太強(qiáng)大了。我還加入了Core Motion梳星,可以移動(dòng)iPad來切換不同的視角赞赖,這同時(shí)也是VR View的制作方式呀,以后更新如何開發(fā)一個(gè)屬于iOS 的VR應(yīng)用冤灾,并連接SceneKit前域,感覺以后發(fā)展可觀。
當(dāng)然你還可以新建一個(gè).scn文件來制作
-
先拖一個(gè)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.
今年對(duì)scnLight 的更新是新增一種燈源匿垄,就是可以在網(wǎng)上找到一種文件格式.ies,就可以導(dǎo)入不同格式的燈源归粉。這對(duì)設(shè)計(jì)師來說應(yīng)該是一個(gè)福利
//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.
我也做了一個(gè)Demo來嘗試新的api效果椿疗,大東表示非常贊??,有一些屬性是支持Animate的糠悼,這在官方的Demo上有展示過一個(gè)效果場(chǎng)景就是當(dāng)一個(gè)突然物體飛速移動(dòng)時(shí)届榄,周圍的場(chǎng)景會(huì)變有模糊效果,聯(lián)系一下開車場(chǎng)景倔喂,這種效果讓游戲開發(fā)變得更加逼真!
蘋果今年給SCNCamera添加了很多新的效果铝条,可以找官方的APi文檔查找。
今年的 WWDC Demo
蘋果今年開源了兩個(gè)適合大家入們SceneKit的Demo席噩,而且都有用Swift 3編寫班缰,看Session到那里時(shí)非常激動(dòng),現(xiàn)場(chǎng)也很多人鼓掌歡呼(表示大東很想去WWDC)
寫在最后悼枢,大東想說埠忘,SceneKit很強(qiáng)大,我也相信蘋果如果以后要推出VR馒索,SceneKit這種制作3D場(chǎng)景的框架一定會(huì)被廣受利用给梅。最近我還在探索如何用Google 的VR-iOS SDK來實(shí)現(xiàn)一個(gè)SceneKit場(chǎng)景。也希望能和大家交流學(xué)習(xí)双揪。