ios-SceneKit/ARKit/OpenGL/Unity3DiOS Developer

SceneKit 材质(Material)

2017-12-13  本文已影响749人  牛奈奈
前言

光照与材质,是决定模型怎么样被渲染的关键参数,不同的设置带来的直观效果不一致,所以熟悉光照与材质,能够快速调试到理想的效果。
SCNMaterialProperty.content决定了材质长什么样子,可以是纯色也可以是renderer(渲染)的,而renderer的数据源主要有以下四种:

visual properties
-(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"
}

运行效果如图:

c88d60ba-8a07-4647-8c1b-dc342afd426b.png
可以看出物体太过贴近屏幕,我们可以自己加个摄像头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];

此时的效果是:


自定义摄像头.gif

此时再添加一个环境光,在省略处接着添加上以下代码

    SCNNode *ambientlightNode = [SCNNode node];
    ambientlightNode.light =[SCNLight light];
    ambientlightNode.light.type = SCNLightTypeDirectional;
    ambientlightNode.light.color = [UIColor whiteColor];
    [scnView.scene.rootNode addChildNode:ambientlightNode];

关于SCNLightType的多种类型,有兴趣的可以分别试下效果,此时再运行

环境光.gif
sphere.firstMaterial.normal.contents = @"earth-bump.png";
之后.png

*reflective指定了材质对周围环境的反射,物体表面会有周围环境的倒影。例如如果是在树林里材质的表面应该会泛绿,在沙漠里则会泛黄

 sphere.firstMaterial.reflective.contents = @"earth-reflective.jpg"
  sphere.firstMaterial.fresnelExponent = 0
反射.png
sphere.firstMaterial.emission.contents = @"earth-emissive.jpg"
散发.png
//指定纹理或者颜色,当此属性设置的时候emission属性将会被忽略
 sphere.firstMaterial.selfIllumination.contents = [UIColor blueColor];
纹理.png
   sphere.firstMaterial.transparent.contents = @"cloudsTransparency.png";
    sphere.firstMaterial.transparencyMode = SCNTransparencyModeRGBZero;
    sphere.firstMaterial.transparency = 0.5;
透明度.png
*multiply的内容,会在材质被渲染之后,叠加在材质上,multiply默认是无效的
sphere.firstMaterial.multiply.contents = [UIColor yellowColor];
黄色.png
上一篇 下一篇

猜你喜欢

热点阅读