Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SceneKit - Add Fresnel effect to material transparency

Tags:

ios

scenekit

I have a Geometry Node that pictures a human body. The material for the object is transparent

let node = newScene.rootNode.childNode(withName: "man_mesh", recursively: true)
let nodeMaterial = node?.geometry?.firstMaterial
nodeMaterial?.emission.contents = UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0)
nodeMaterial?.transparencyMode = .rgbZero
nodeMaterial?.transparent.contents = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

The effect I'm trying to get is for the object to be fully transparent in areas facing the camera, but become more opaque as the polygons face sideways... this creates a sort of a shiny blue rim. something like this:

Rim Effect

Any idea how to create this effect?

Thanks

EDIT: Another interesting point: it appears the SceneKit does support the fresnel effect. The effect affects the Reflectance map - as should be, however, once you apply transparency to your object, it also affects the reflectance (full transparency will make reflectance disappear as well)- I believe this is a conceptual mistake by Apple - a material can be transparent and still reflect light!

like image 572
T-Rex Avatar asked May 08 '17 08:05

T-Rex


1 Answers


I've done some research and found an answer. To achieve this effect, you can use following code.

func ghostEffect(scene: SCNScene) {

    let sphere = SCNSphere(radius: 3)

    let material = SCNMaterial()
    material.diffuse.contents = UIColor.black
    material.reflective.contents = UIColor(red: 0, green: 0.764, blue: 1, alpha: 1)
    material.reflective.intensity = 3
    material.transparent.contents = UIColor.black.withAlphaComponent(0.3)
    material.transparencyMode = .default
    material.fresnelExponent = 4

    sphere.materials = [material]

    let node = SCNNode(geometry: sphere)

    scene.rootNode.addChildNode(node)

}

Explanation from: SCNMaterial.

transparent - An object that determines the opacity of each point in a material

reflective - An object that defines the reflected color for each point on a surface

fresnselExponent - A factor affecting the material’s reflectivity

This approach can be applied to any 3D object.

Result

P.S. Hope it will be useful.

like image 160
Eugene Avatar answered Oct 21 '22 16:10

Eugene