Search Unity

2D Lighting effects

Discussion in 'Shaders' started by recon, Nov 20, 2011.

  1. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    I wanted to know if anyone might have a clue how to achieve a similar look to this, or specifically the rays of light that you see when he moves the cursor through the building.
    He mentions different layers and ambient lights but Im stumped how to do something like this in Unity because I have close none shader experience so far, so if anyone could offer any guidance I would greatly appreciate that :)

    Thanks!
     
  2. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
  3. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,438
    Shafts:
    Mobile/Particles/Additive
    Start with a dark texture and work your way up with the brightness. Scroll the texture.

    As for the shadow (its not being lit, its being shaded) for the moving lights, he is doing a basic light visibility routine, similar to this: http://www.gamedev.net/page/resourc...ming-and-theory/dynamic-2d-soft-shadows-r2032

    The job becomes significantly easier if you have unity pro, as you simply fake it with 3D in ortho view and use unity's shadows.
     
    Last edited: Nov 20, 2011
  5. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    Thanks a lot for the quick response!
    Luckily I do own a copy of Unity Pro so I'll be messing around with fake shadows this week then.
    And the link was really helpful to, so thanks again :)
     
  6. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    Just discovered a limitation I ran into, so I got a simple scene setup with a plane as background and a couple of cubes as shadow casters, now It all works fine when the camera is in perspective mode but not when I switch to orthographic.

    I found a comment from the Unity guys here that says "in Unity 3.2 forward renderer can support point/spot light shadows again. The built-in shaders do not support them, but you can write custom shaders (with "fullforwardshadows" directive) that do.".

    Is there by any chance any shaders available anywhere that supports this type of scenario? Or is it a simple command that I could just add to existing shaders? Would love to get this rolling for a prototype of mine!
     
  7. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    Never mind, just found this
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,438
    Simulate orth with a perspective view but with camera fov set low as possible, if that helps? for the shaders just add fullforwardshadows where the pragma are ie

    #pragma surface surfaceFunction lightModel fullforwardshadows

    More information here: http://unity3d.com/support/documentation/Components/SL-SurfaceShaders.html

    The source code for all unity shaders are here: http://unity3d.com/download_unity/builtin_shaders.zip - should be able to add to them with your own, remember to rename them in the shader source code ie: Shader "Mobile/Diffuse" { becomes Shader "Mobile/Diffuse_Shadows" {
    and save under a new name in assets folder.

    You then create a new unity material and pick the new shader you made for it from the drop down menu and assign as normal.
     
  9. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    No problem, it all works now and using a ortho camera! Very happy about the result :) Here's a follow up question though, since the wall meshes that casts the shadows will never be visible, is there a way to not render shadow casters? I mean, they will have to have at least the mesh renderer component to be able to cast shadows, but is there like, a shaders that doesn't draw anything to save some performance?
    Thanks again!
     
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,438
    There's always giving them alpha channel of 0 with alpha cutout for speed, but I'm sure there must be an elegant way to solve it, not sure. Hopefully someone will chip in :)

    Personally I'd use a simple mobile shader with a transparent texture pragma for now, while waiting for a more definitive answer. Perhaps you can stick it on another layer which isn't rendered by the main camera - could this work? not sure.

    I don't think culling front and back in the shader would work, it might though.
     
    Last edited: Nov 20, 2011
  11. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,679
    What is shown in the first video you posted can be achieved in 2 ways. 1st is easy, 2nd is very complicated but gives best volumetric results.
    1. with a godrays, lightshafts image post processing shader.
    2a. put a camera on your light source looking at the direction of the (spotlight, or to your main camera if its a point light)
    2b. render the depth of this camera (selectively the objects which will cause the lightshafts effect
    2c. put 30-50 (spheres if spotlight, planes if point light) infront of your light source in the same appropriate direction mentioned above, these spheres should cover the spotlights view frustum..etc) and they should recieve shadows but not cast them.
    2d. And finally, project the decoded depth texture on all these spheres. (at this point you need a custom transparent shader to be used on these spheres which will clip the depth texture's unshadowed parts to make them transparent)

    A variety of the Image effect version is added to my shaders pack as seen in the following photo:


    Second version should look like somethin like the below video (its done with gamestudio) (I didnt do it)
    http://youtu.be/_pcovt3Jheo
     
  12. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    Hey thanks Aubergine! I'll keep that in mind for later if I want to try and implement the godrays :) Although since it would only be in 2D, I think the method hippocoder suggested might be easier with scrolling additive textures layered on top :p

    Now for an other question, could anyone help me turn the diffuse shader into a solid color shader? I figured it's the easiest option for me to use on the shadow casting meshes.

    This is the code from the modified built-in diffuse shader that supports shadows in ortho mode, now I just need get rid of the uvs and main texture!

    Code (csharp):
    1.  
    2. Shader "SolidColorCastShadows"
    3. {
    4.  
    5.     Properties
    6.     {
    7.         _Color ("Main Color", Color) = (1,1,1,1)
    8.         _MainTex ("Base (RGB)", 2D) = "white" {}
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.    
    15. CGPROGRAM
    16. #pragma surface surf Lambert fullforwardshadows
    17.        
    18.         sampler2D _MainTex;
    19.         fixed4 _Color;
    20.        
    21.         struct Input {
    22.             float2 uv_MainTex;
    23.         };
    24.        
    25.         void surf (Input IN, inout SurfaceOutput o) {
    26.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    27.             o.Albedo = c.rgb;
    28.             o.Alpha = c.a;
    29.         }
    30. ENDCG
    31.     }
    32.    
    33.     Fallback "Diffuse"
    34. }
    35.  

    Actually, as i wrote this post I tried removing just the

    Code (csharp):
    1. _MainTex ("Base (RGB)", 2D) = "white" {}
    and that worked. It doesn't complain about missing texture coordinates anymore, however I feel like the CG code could be more optimized since it still contains uv coords so any tips how to strip that part would help me a lot.

    Thanks again guys!
     
  13. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    Uhm, scratch that, It did not actually use the shader I thought it did - it was using the fallback one (it seems). But Im really confused because the shadows still gets rendered even using the built-in Diffusive shader now... Im not going to complain about that, whatever works is great, but still trying to figure out if there's a version of the diffuse shader that doesn't use uv coordinates.
     
unityunity