Transparent glass with reflections in SceneKit

760 views Asked by At

Dear Scenekit Experts,

I'm trying to get a material resembling clear glass using PBR. It should be 99% transparent while also very reflective.

Here's the sample I'm using:

    lightingModel = .physicallyBased  
    metalness.contents = UIColor(white: 1.0, alpha: 1.0)  
    diffuse.contents = UIColor(white: 0.2, alpha: 1.0)  
    roughness.contents = UIColor(white: CGFloat(0.03), alpha: 1.0)  

    transparencyMode = .dualLayer  
    isDoubleSided = true  
    transparency = 0.2  

The problem is that the amount of reflections seem to be affected by the transparency:

  1. transparency = 0.01: the reflections of the scenes environment map are barely visible anymore.
  2. transparency = 0.2: I get some nice reflections, but unfortunately now my whole object is visible, i.e. it looks like a piece of tinted glass (depending on which color I set in diffuse).

How can I make it clear , i.e. see-through while still seeing the reflections ?

1

There are 1 answers

0
Bersaelor On BEST ANSWER

So, I think I figured it out myself now. The solution was to set all alpha material values to 1.0 (diffuse, transparency etc) and then set the alpha in the fragment shader based on the amount of specular lighting.

Fragment Shader:

#pragma transparent
#pragma body
uniform sampler2D emissionTexture;

vec3 light = _lightingContribution.specular;
float alpha = 0.5 * min(1.0, 0.33 * light.r + 0.33 * light.g + 0.33 * light.b);
_output.color.rgb *= min(1.0, 1.5 * alpha);
_output.color.a = 0.75 * alpha;

Basically this shader has

  • low alpha where there is no highlight
  • high alpha where there is a specular highlight.