Search Unity

2D pseudo edge light effect

Discussion in '2D' started by migueltuliompg, Aug 17, 2019.

  1. migueltuliompg

    migueltuliompg

    Joined:
    Feb 12, 2018
    Posts:
    41
    Ok guys, I'm working on a pseudo edge lighting effect and I need some help.

    I've got an arm sprite and a sprite that is just the edge light. I also have a stencil shader for the edge light.

    https://imgur.com/a/sIi5F4I

    The first question is, can you have a feathered sprite mask or stencil shader? Cause I as you can see in the picture, thee edge is too sharp to make a good lighting effect.

    The second is, is there a way make only the closest edges (of the edge light sprite) to the "light source" show up in the stencil?

    Thanks in advance!

    Edit: It could also work if I had a lit shader for the edge light and the opacity is dependent on the amount of light hitting it.
     
    Last edited: Aug 17, 2019
  2. migueltuliompg

    migueltuliompg

    Joined:
    Feb 12, 2018
    Posts:
    41
    Anyone know a solution to this?

    I found this shader and it works, but the edge is to sharp. Any way to make the alpha based on the amount of light?

    Code (CSharp):
    1. Shader "Custom/LightToAlpha" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _ALightThreshold("Light Threshold", float) = 0.0
    8.         _ALightContrast("Light Contrast", float) = 1.0
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    12.         LOD 200
    13.  
    14.         CGPROGRAM
    15.         // Physically based Standard lighting model, and enable shadows on all light types
    16.         #pragma surface surf Standard fullforwardshadows finalcolor:alphaLight alpha:fade
    17.  
    18.         // Use shader model 3.0 target, to get nicer looking lighting
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _MainTex;
    22.  
    23.         struct Input {
    24.             float2 uv_MainTex;
    25.         };
    26.  
    27.         half _Glossiness;
    28.         half _Metallic;
    29.         fixed4 _Color;
    30.         half _ALightThreshold;
    31.         half _ALightContrast;
    32.  
    33.         void surf (Input IN, inout SurfaceOutputStandard o) {
    34.             // Albedo comes from a texture tinted by color
    35.             fixed4 c = (fixed4)1.0f;
    36.             o.Albedo = c.rgb;
    37.             // Metallic and smoothness come from slider variables
    38.             o.Metallic = _Metallic;
    39.             o.Smoothness = _Glossiness;
    40.             o.Alpha = c.a;
    41.         }
    42.  
    43.         void alphaLight(Input IN, SurfaceOutputStandard o, inout fixed4 color)
    44.         {
    45.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    46.             color.a = 1.0f - saturate(c.a * saturate(dot(color.rgb, fixed3(1.0f, 1.0f, 1.0f) / 3.0f) - _ALightThreshold) * _ALightContrast);          
    47.             color.rgb = c.rgb;
    48.         }
    49.  
    50.         ENDCG
    51.     }
    52.     FallBack "Diffuse"
    53. }