如何在 SceneKit 中显示金属和透明纹理? [英] How to show metallic and transparent textures in SceneKit?

查看:22
本文介绍了如何在 SceneKit 中显示金属和透明纹理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 SceneKit 中显示一些纹理,这是我想使用的模型:

Sketchfab 中的模型:

I have trouble showing some textures in SceneKit, this is the model I would like to use:

Model in Sketchfab : https://skfb.ly/6QVTQ

The model should appear in these colors and textures in the AR environment using Scene Kit. But the golden tips appear black and the transparent lenses do not appear at all. Are there any suggestions to solve this problem?

The model is .scn format. here is the the model materials properties: https://drive.google.com/file/d/1HIHEsyONLXyL95dcSy9xWMGX89udMPND/view?usp=sharing

https://drive.google.com/file/d/1ndrZfcjqIQ4d2OfG6ZNvwyfDXnihJbnH/view?usp=sharing

If you need any additional information please let me know. Thank you in advance.

解决方案

There's no photorealistic glass with true index of refraction (IoR) in SceneKit but you can easily create a fake one using phong shader. Phong shader also has three important properties of a glass – specularity, reflectivity, and fresnelExponent.

For metallic material use physicallyBased shading model.

Here's a code:

import SceneKit

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let sceneView = self.view as! SCNView
        sceneView.scene = SCNScene(named: "glasses.scn")!
        sceneView.allowsCameraControl = true
        sceneView.pointOfView?.position.z = 20

        let glassesFrame = sceneView.scene?.rootNode.childNode(withName: "glassesFrame", 
                                                            recursively: true)

        glassesFrame?.geometry?.firstMaterial?.lightingModel = .physicallyBased
        glassesFrame?.geometry?.firstMaterial?.metalness.intensity = 1
        glassesFrame?.geometry?.firstMaterial?.diffuse.contents = NSColor.systemBrown

        let lens1 = sceneView.scene?.rootNode.childNode(withName: "rightLens", 
                                                     recursively: true)

        let lens2 = sceneView.scene?.rootNode.childNode(withName: "leftLens", 
                                                     recursively: true)

        let material = SCNMaterial()
        material.lightingModel = .phong
        material.diffuse.contents = NSColor(white: 0.2,
                                            alpha: 1)
        material.diffuse.intensity = 0.9
        material.specular.contents = NSColor(white: 1,
                                             alpha: 1)
        material.specular.intensity = 1.0
        material.reflective.contents = NSImage.Name("art.scnassets/texture.png")
        material.reflective.intensity = 2.0
        material.transparencyMode = .dualLayer
        material.fresnelExponent = 2.2
        material.isDoubleSided = true
        material.blendMode = .alpha
        material.shininess = 100
        material.transparency.native = 0.7
        material.cullMode = .back

        lens1?.geometry?.firstMaterial = material
        lens2?.geometry?.firstMaterial = material
    }
}

这篇关于如何在 SceneKit 中显示金属和透明纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆