Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Depth-based Shader for dungeon lighting

Discussion in 'Shaders' started by Jiraboas, Nov 26, 2014.

Thread Status:
Not open for further replies.
  1. Jiraboas

    Jiraboas

    Joined:
    Nov 16, 2012
    Posts:
    13
    Hello,

    I want to achive this kind of lighting effect for a retro dungeon crawler:



    I allready searched half of the internet to get the idea, but i dont even know that this kind of effect or lighting or shading is called.

    I dont want a smooth lighting like its built-in in unity. Instead i want a pixelated lighting for my 3D dungeon so it better fit with the pixel/retro look of the textures and 2D sprites. In addition to that i want this kind of depth/fog effect like in the picture.

    Can someone tell me how to achive this kind of lighting/shading? anything would help. Thank you really much!

    kind regards,

    Jiraboas
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,415
    Hi,

    a simple solution would be to use a custom lighting function in your surface shader, that is used to create the banding effect.

    It can be done in various ways, here are two examples:
    1. As shown in the attached example project, just throwing away precision.
    2. Using an additional texture that describes the falloff. A smooth gradient from white to black would be a linear falloff, but if you only use 10 colors for that white/black gradient, you would have banding. This is a nice and flexible solution, because it gives the artist very precise control of this effect. You use the NdL (Normal dot Lightdirection) to sample this texture to shade a pixel. You can take a look at the Toon Ramp example which is using this technique for a different but similar manner.
    Fog can be done using simply Unity's fog system, found under Edit > Render Settings.

    Here is a video of the example I just threw together for you. It is just something to look at and maybe it proves useful, I don't know. It's definitely not a fully-fledged retro dungeon solution. The download link can be found at the bottom of my post.



    The idea is that the character controller moves a point light around, so you get that lighting around the player. The corridor fades to black using fog.

    Here is the shader code that implements a custom lighting function to implement the banding effect. It can be found in the example project at "Assets/NewShader.shader".
    Code (csharp):
    1. // this is the "Diffuse" shader example from
    2. // http://docs.unity3d.com/Manual/SL-SurfaceShaderLightingExamples.html
    3. // what has been added is the "Band" function which will cause the banding of the light
    4. Shader "Example/Diffuse Texture" {
    5. Properties {
    6.   _MainTex ("Texture", 2D) = "white" {}
    7.   _Banding ("Banding", float) = 11
    8. }
    9. SubShader {
    10. Tags { "RenderType" = "Opaque" }
    11. CGPROGRAM
    12.    #pragma surface surf SimpleLambert
    13.    float _Banding;
    14.  
    15.    float Band(float value)
    16.    {
    17.      return ((int)(value*(int)_Banding) / _Banding);
    18.    }
    19.  
    20.    half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
    21.     half NdotL = dot (s.Normal, lightDir);
    22.     half4 c;
    23.     c.rgb = s.Albedo * _LightColor0.rgb * Band(NdotL * atten * 2);
    24.     c.a = s.Alpha;
    25.     return c;
    26.    }
    27.  
    28.    struct Input {
    29.     float2 uv_MainTex;
    30.    };
    31.  
    32.    sampler2D _MainTex;
    33.  
    34.    void surf (Input IN, inout SurfaceOutput o) {
    35.     o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    36.    }
    37. ENDCG
    38. }
    39. Fallback "Diffuse"
    40. }
    Example project of video above (created in Unity 4.6 beta):
    http://www.console-dev.de/bin/UnityRetroDungeonShading.zip

    I hope it helps you get started. :)
     
    Last edited: Nov 27, 2014
    Jiraboas likes this.
  3. Jiraboas

    Jiraboas

    Joined:
    Nov 16, 2012
    Posts:
    13
    thank you very much for your amazing effort! :)

    This is what i needed to start with. Thanks :)

    kind regards,

    Jira
     
  4. brombs73

    brombs73

    Joined:
    Jul 21, 2012
    Posts:
    10
    Hi, is it possible to do such an effect with a shadergraph?
    best regards
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yes, just use a depth node and multiply the result. Feel free to ask in the shadergraph section. As the thread contains outdated information for a different pipeline Unity is deprecating, I'll lock this thread.
     
    brombs73 likes this.
Thread Status:
Not open for further replies.