Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Glass shaders

Discussion in 'Shaders' started by Daniel_Brauer, Feb 26, 2013.

  1. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Unity doesn't have any built-in shaders suitable for glass, and I found it somewhat tricky to get acceptable results out of a surface shader. Here are some very simple, fully reflective glass shaders:

    http://wiki.unity3d.com/index.php/Glass_Shader

    As with most surface shaders, it would be easy to add normal and gloss/reflectivity maps to either shader.

    Unfortunately, I haven't been able to get shadows working the way I want: receiving but not casting. As far as I can tell, Unity assumes that objects in the Transparent queue don't cast or receive shadows. You can change the queue, but then your transparency sorting is broken, and shadows on other objects seem to disappear when viewed through the glass.

    The lack of precise control over blending in surface shaders makes it impossible to use premultiplied alpha, which I think would be the only way of adding opaque parts to a pane of glass. It's certainly possible in a custom vertex/fragment shader, but I'm reluctant to do that since it's a lot more complicated and difficult to keep up to date.

    Other than the above limitations, the only thing I've noticed is that the shader doesn't respond to vertex lights. I don't know why this is, and I'm so tired of trying to debug Unity's lighting setup that I'm not going to try and fix it.

    Comments, improvements and variants are of course welcome.
     
  2. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    as for Unity's Lighting setup,my test result is :unity_LightPosition[] is used in vertexlit pass,camera space,
    unity_LightPosX,Y,Z,0 is used in forward pass,world space,
    they both combined with unity_LightColor[],

    when your shader both have forwardbase and forwardadd ,unity_LightPosX,Y,Z only have vertex lights,dir+point
    if your shader just have forwardbase pass,then it have the 4 brightest point lights
    after all,dont use unity_LightPosX,Y,Z0 in forwardadd pass,it is just prepared for forwardbase pass.

    and the lights index in array is dicided by
    ( pixel lights,vertex light)*(direction lights,point lights)*(luminance(light))

    i have a full test course and papers ,but it is wrriten in chinese.

    maybe you will be confused by the data in light position array,unity doesnt always update them,but unity_LightColor[] is always updated.
     
    Last edited: Feb 26, 2013
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I'm pretty sure that the unity_4Light fields don't ever contain directional light information. I don't think they should ever contain spotlight information either, but for some reason they do and that causes spot lights to be rendered as point lights if they can't be rendered as pixel lights.

    In any case, after a bit more research I can say that vertex lights in the forward renderer only ever give a diffuse contribution–no specular. This is true of spherical harmonic (directional) lights as well, although in that case it's a limitation of the technique rather than the implementation. I can only assume that the reasoning is to reduce the complexity of the surface shader compiler, although the results can be very confusing when you use the forward renderer.
     
  4. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    Yeah, all they in unity_4LightPos_0 are point lights, i dont do any test for spot lights, i just assume them as a special point lights :eek: