Search Unity

lights color through objects

Discussion in 'Global Illumination' started by FreeFly90, Aug 14, 2016.

  1. FreeFly90

    FreeFly90

    Joined:
    May 28, 2016
    Posts:
    177
    Here is the situation: I am making some tests to see how different lights behave while passing through objects, but so far I didn't find the solution I was looking for. My final goal is to obtain a lampshade-like effect, where the light color is changed while passing through a semi-transparent mesh, but so far, I managed to have only a small portion of the light influenced by the object.

    I tried to set up a simpler environment, with a light pointing straight to a plane with a wall behind, but still, I had no success in influencing the light that gets through the mesh. (see the attached file).

    Of course I could use a script to change the light color to match the mesh, but that's not what I am trying to do. Can anyone tell me what am I doing wrong, or at least point me to the right direction?
     

    Attached Files:

    yuvsv47 likes this.
  2. thefranke

    thefranke

    Unity Technologies

    Joined:
    Jun 20, 2015
    Posts:
    153
    Hey FreeFly90,

    so in Unity the regular baking process will use transparency as a measure of partial coverage of the object (i.e., a surface has tiny holes through which light passes, rather than being transmissive where light passes through the material itself), which will result in gray shadows.

    To get the transmissive behavior that you are looking for, you can do the following: Add a shader to the object, and in the shader properties add a texture called _TransparencyLM (in your case a simple red image). This will use the texture as a filter for the color to get transmissive behavior.

    Cheers
     
    yuvsv47 and AcidArrow like this.
  3. FreeFly90

    FreeFly90

    Joined:
    May 28, 2016
    Posts:
    177
    Thank you so much for replying, I've never modified shaders before and I guess it will take me some time to make it good, but I'll post something as soon as I'm done! :)
     
  4. thefranke

    thefranke

    Unity Technologies

    Joined:
    Jun 20, 2015
    Posts:
    153
    You can use this one as a starting point (it'll do what you need):

    Code (CSharp):
    1. Shader "Transparent/Transmission"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    7.         _TransparencyLM("Transmission", 2D) = "white" {}
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    13.  
    14.         CGPROGRAM
    15.         #pragma surface surf Lambert alpha
    16.  
    17.         sampler2D _MainTex;
    18.         fixed4 _Color;
    19.  
    20.         struct Input
    21.         {
    22.             float2 uv_MainTex;
    23.         };
    24.  
    25.         void surf (Input IN, inout SurfaceOutput o)
    26.         {
    27.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    28.             o.Albedo = c.rgb;
    29.             o.Alpha = c.a;
    30.         }
    31.         ENDCG
    32.     }
    33.  
    34.     Fallback "Transparent/VertexLit"
    35. }
    36.  
    If you insert a texture in the Transmission you should get a "colored shadow".
     
    yuvsv47 and buttmatrix like this.
  5. FreeFly90

    FreeFly90

    Joined:
    May 28, 2016
    Posts:
    177
    Wow thank you so much! I really appreciate man! :)
     
    yuvsv47 and thefranke like this.
  6. FreeFly90

    FreeFly90

    Joined:
    May 28, 2016
    Posts:
    177
    Argh, I'm sorry to come back on this, but I've tried to use the shader you've posted, and I couldn't manage to make it work no matter what I tried. I've just used a simple setup with a spotlight, I've tried both baked and realtime lighting and nothing changed. What could I possibly be doing wrong?
     
    yuvsv47 likes this.
  7. amasinton

    amasinton

    Joined:
    Aug 12, 2006
    Posts:
    138
    Did you ever get this to work? The shader code above doesn't do anything with the Transparency material. It would be fantastic to get some transmissive materials/shadows colored by transparent texture!
     
    yuvsv47 likes this.
  8. thefranke

    thefranke

    Unity Technologies

    Joined:
    Jun 20, 2015
    Posts:
    153
    Hey FreeFly90 and amasinton,

    what you need to do is the following: Create a shader in your project and paste in the above code. Create a Material and select Transparent/Transmission in the shader dropbdown menu. It should look similar to the screenshot I've attached.

    Clipboard-1.png

    Both the Main Color and the Base (RGB) Trans (A) are used for the object appearance itself, while the texture Transmission is used to generate colored shadows. If you do not set it, nothing happens. Additionally, the object which you set up this material for needs to be static, because colored shadows only work for when baked (realtime GI does not work!).

    Hope that helps!

    Cheers
     
    yuvsv47, amasinton and KEngelstoft like this.
  9. amasinton

    amasinton

    Joined:
    Aug 12, 2006
    Posts:
    138
    Baking! That's the magic ingredient. I'll have to give this a shot. It may not be ideal for my current use-case, but it's good to know it's there in the future, so I can plan for and around this. I do a lot of projects with stained glass, so this is really important to me.

    Thanks!
     
    yuvsv47 and thefranke like this.
  10. thefranke

    thefranke

    Unity Technologies

    Joined:
    Jun 20, 2015
    Posts:
    153
    amasinton, I'm interested in your use-case. Are there scenarios where you need to use transmission through stained glass in real-time?
     
    yuvsv47 likes this.
  11. amasinton

    amasinton

    Joined:
    Aug 12, 2006
    Posts:
    138
    Yes. Many. I'm an archaeologist doing archviz of cathedrals as they looked several centuries ago. I'm literally a day away from finishing my first big project with Unity (with SEGI) as my primary renderer (and I absolutely love it - I never want to go back to traditional renderers). But these cathedrals are massive pains to UV map (I mostly do texture projection in C4D, my main modeller) and they're also heavy-poly (7-10 million polys) - so baking is kind of a no-go (hence using SEGI). Also, my clients love dynamic lighting.

    I would be overjoyed with a non-baked transmission lighting model - or even colored light cookies!

    So, cathedrals and stained glass. Lots and lots. Attached is a little screenshot of the sort of thing I'm talking about.

    Thanks for asking!
     

    Attached Files:

    yuvsv47 and thefranke like this.
  12. buttmatrix

    buttmatrix

    Joined:
    Mar 23, 2015
    Posts:
    609
    First, the cathedrals look excellent and I recall seeing these on the SEGI forum. Did you end up using the shader posted above to achieve a "transmissive" surface?
     
    yuvsv47 and amasinton like this.
  13. buttmatrix

    buttmatrix

    Joined:
    Mar 23, 2015
    Posts:
    609
    This shader is really saving me right now, so thank you very much. Right now the shader takes a tex2D to define the transmission color. Is it possible to multiply the transmission 2D by a color value via a color picker?

    Artistically, it is difficult to author dozens of color swatches when you could use a color picker to define TransparencyLM instead.
     
    Last edited: Feb 20, 2018
    PF-Sebastien and yuvsv47 like this.
  14. amasinton

    amasinton

    Joined:
    Aug 12, 2006
    Posts:
    138
    Cheers! The cathedrals are fun to do, but a little time-consuming!

    I did not, in the end. I just left the transmissive stuff out of the project. No one seemed to notice. I can't remember why that shader from @thefranke didn't do the trick for me, but it may have been due to the baking requirement, as my cathedrals are just too huge (in file size and geometric complexity) to realistically bake.

    @doppioslash and I explored some other possibilities, but I've gotten overwhelmed with work (sorry @doppioslash) to take this further.

    If anything that works does appear, I'll post back here.
     
    yuvsv47 and buttmatrix like this.
  15. Amir-P

    Amir-P

    Joined:
    Jul 2, 2018
    Posts:
    1
    Hi.
    Has anyone test this recently? because I followed every step but nothing happens!
     
  16. Maf

    Maf

    Joined:
    Jun 25, 2013
    Posts:
    7
    I'm trying to work out how to replicate this effect in the LWRP, but can't find any working examples. The example shader works perfectly, but I'm not sure how the magic "_TransparencyLM" line works, and as such how I'd replicate it in LWRP either in code or through ShaderGraph.

    Any guidance would be appreciated!
     
    Beauque likes this.
  17. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    As you probably already know, the above posted shader is only relevant for baked lighting. I don't think this effect is supported out of the box in Universal RP (formerly known as LWRP), but I guess it can be done with a custom shader (though not necessary a trivial one :p).

    I recommend that you ask the question in the Universal RP forum (the current forum is only about baking).
     
    yuvsv47 likes this.
  18. Maf

    Maf

    Joined:
    Jun 25, 2013
    Posts:
    7
    Hi Rasmusn, thanks for your reply.

    It's writing custom shaders that affect baked lighting that I'm struggling with. I'm not sure if it's more relevant here or under URP, but this thread was the closest I'd found thus far to what I was trying to achieve.

    It seems easy enough with the HDRP, but I'm currently targeting Oculus Quest, so I need to keep things lightweight.

    I stumbled over the Baked GI node in Shader Graph, but couldn't find any guidance on what it does or what I could use it for, so I don't know if it's of any use to me.
     
    Beauque likes this.
  19. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
  20. yuvsv47

    yuvsv47

    Joined:
    Nov 2, 2022
    Posts:
    16
    thefranke , thank you so much!
    This is exactly what I've been looking for a long time!
    I need exactly to bake those colored gaps!
    I hope this works in Unity2018?