前言
光照與材質料祠,是決定模型怎么樣被渲染的關鍵參數(shù)骆捧,不同的設置帶來的直觀效果不一致澎羞,所以熟悉光照與材質髓绽,能夠快速調試到理想的效果。
SCNMaterialProperty.content
決定了材質長什么樣子妆绞,可以是純色也可以是renderer
(渲染)的顺呕,而renderer
的數(shù)據(jù)源主要有以下四種:
UIImage
- 全景格式的圖像,如
cube images
CALayer
SpriteKit scene
-
SKTexture
等
visual properties
-
diffuse
:指定了材質對光照的漫反射括饶,人眼看到的物體實際上就是人眼接收到物體的漫反射株茶,所以diffuse
就是物理的基本樣貌(ps:ambient
材質對環(huán)境光的漫反射,而diffuse
已經包含了環(huán)境光和方向光的漫反射,當locksAmbientWithDiffuse
為NO
時,ambient
才生效图焰,一般情況下使用diffuse
已經足矣)启盛。
-(void)viewDidLoad {
SCNView *scnView = [[SCNView alloc]initWithFrame:self.view.bounds];
scnView.backgroundColor = [UIColor blackColor];
scnView.scene = [SCNScene scene];
scnView.allowsCameraControl = TRUE;
[self.view addSubview:scnView];
省略處......
SCNSphere *sphere = [SCNSphere sphereWithRadius:2];
SCNNode *sphereNode = [SCNNode nodeWithGeometry:sphere];
[scnView.scene.rootNode addChildNode:sphereNode];
sphere.firstMaterial.diffuse.contents =@"earth-diffuse.jpg"
}
運行效果如圖:
可以看出物體太過貼近屏幕鳖粟,我們可以自己加個攝像頭
SCNCamera
标沪,在省略處加上
SCNCamera *camera = [SCNCamera camera];
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = camera;
camera.automaticallyAdjustsZRange = TRUE;
//表示在z軸上距當前屏幕10個單位
cameraNode.position = SCNVector3Make(0, 0, 10);
[scnView.scene.rootNode addChildNode:cameraNode];
此時的效果是:
此時再添加一個環(huán)境光漠畜,在省略處接著添加上以下代碼
SCNNode *ambientlightNode = [SCNNode node];
ambientlightNode.light =[SCNLight light];
ambientlightNode.light.type = SCNLightTypeDirectional;
ambientlightNode.light.color = [UIColor whiteColor];
[scnView.scene.rootNode addChildNode:ambientlightNode];
關于SCNLightType
的多種類型瘾敢,有興趣的可以分別試下效果晃财,此時再運行
-
normal
指定了材質表面每個點的法線方向,在處理光照時,會根據(jù)normal
計算陰影,它提供了一種假的幾何凸起。
sphere.firstMaterial.normal.contents = @"earth-bump.png";
*reflective
指定了材質對周圍環(huán)境的反射剪芥,物體表面會有周圍環(huán)境的倒影榜田。例如如果是在樹林里材質的表面應該會泛綠,在沙漠里則會泛黃
sphere.firstMaterial.reflective.contents = @"earth-reflective.jpg"
sphere.firstMaterial.fresnelExponent = 0
-
emission
:指定了材質便面發(fā)光(亮度較高)的位置具钥,emission
并不能讓材質發(fā)光桃漾,只不過在計算光照時敦迄,emission
紋理中較亮的點不會參與到光照計算中,使這些點在陰暗的環(huán)境下顯得更亮一些猛拴。
sphere.firstMaterial.emission.contents = @"earth-emissive.jpg"
//指定紋理或者顏色芳室,當此屬性設置的時候emission屬性將會被忽略
sphere.firstMaterial.selfIllumination.contents = [UIColor blueColor];
-
transparent
指定了材質上面每個點的透明度,不同于transparency
控制的是整個材質的透明度雹拄,transparent
精準的控制每個點。
sphere.firstMaterial.transparent.contents = @"cloudsTransparency.png";
sphere.firstMaterial.transparencyMode = SCNTransparencyModeRGBZero;
sphere.firstMaterial.transparency = 0.5;
*
multiply
的內容念祭,會在材質被渲染之后粱坤,疊加在材質上站玄,multiply
默認是無效的
sphere.firstMaterial.multiply.contents = [UIColor yellowColor];