Search Unity

Having a mesh acting as a light

Discussion in 'Shaders' started by PierreCAMILLI, Mar 1, 2017.

  1. PierreCAMILLI

    PierreCAMILLI

    Joined:
    Mar 1, 2017
    Posts:
    2
    Hi!

    First, I'm french so my english can be pretty weak, I'm sorry for this.

    My problem is kinda specific. I'm new to shaders and I'm developping a 2D dark platformer game using sprites and light effects.

    In order to manage dynamic lights and shadows effects on 2D sprites, I'm doing 2D Spotlight effects with a mesh I generate every frames using this tutorial :
    .
    I used stencil masks as recommanded in the third part to display what the player can see but the render was ugly. So I came to think of using the mesh as pure light and sprites as simples diffuse sprites.

    Combining the mesh with a shader from an asset in the store (https://www.assetstore.unity3d.com/en/#!/content/24083) and little modifications, I got this result :

    (don't mind the circle, the new light effect isn't implemented on it yet)

    The smoothing effect is good but now I would like to display what's behind the mesh as if it was enlightened in a dark scene. Here is the shader I'm using :
    Code (CSharp):
    1. Shader "Gradient/No Texture/Radial/Single-Color/To Transparent/Regular UV/Alpha Blend" {
    2.  
    3.     //Set up the shader to receive external inputs from Unity
    4.     Properties {
    5.         _Color ("Color", Color) = (1,1,1,1)                //Receive input from a fixed Color
    6.         _UVXOffset ("UV X Offset", float) = 0            //Receive input from UV coordinate X offset
    7.         _UVYOffset ("UV Y Offset", float) = 0            //Receive input from UV coordinate Y offset
    8.         _UVXScale ("UV X Scale", float) = 1.0            //Receive input from UV X scale
    9.         _UVYScale ("UV Y Scale", float) = 1.0            //Receive input from UV Y scale
    10.         _Offset ("Offset", float) = 0                    //Receive input from a float
    11.     }
    12.  
    13.     //Define a shader
    14.     SubShader {
    15.  
    16.         //Define what queue/order to render this shader in
    17.         Tags {"Queue" = "Transparent" "RenderType" = "Opaque" "LightMode" = "ForwardBase" }        //Background | Geometry | AlphaTest | Transparent | Overlay
    18.  
    19.         //Define a pass
    20.         Pass {
    21.  
    22.             Tags{ "LightMode" = "ForwardAdd" }
    23.  
    24.             //Set up blending and other operations
    25.             Cull Off            // Back | Front | Off - Do not cull any triangle faces
    26.             ZTest LEqual        //Less | Greater | LEqual | GEqual | Equal | NotEqual | Always - Z-Buffer/Depth testing is off
    27.             ZWrite Off            //On | Off - Z coordinates from pixel positions will not be written to the Z/Depth buffer
    28.             AlphaTest Off        //0.0    //Less | Greater | LEqual | GEqual | Equal | NotEqual | Always   (also 0.0 (float value) | [_AlphaTestThreshold]) - All pixels will continue through the graphics pipeline because alpha testing is Off
    29.             Lighting On            //On | Off - Lighting will not be calculated or applied
    30.             ColorMask RGBA        //RGBA | RGB | A | 0 | any combination of R, G, B, A - Color channels allowed to be modified in the backbuffer are: RGBA
    31.             //BlendOp    //Add    // Min | Max | Sub | RevSub - BlendOp is not being used and will default to an Add operation when combining the source and destination parts of the blend mode
    32.             Blend SrcAlpha OneMinusSrcAlpha            //SrcFactor DstFactor (also:, SrcFactorA DstFactorA) = One | Zero | SrcColor | SrcAlpha | DstColor | DstAlpha | OneMinusSrcColor | OneMinusSrcAlpha | OneMinusDstColor | OneMinusDstAlpha - Blending between shader output and the backbuffer will use blend mode 'Alpha Blend'
    33.                                 //Blend SrcAlpha OneMinusSrcAlpha     = Alpha blending
    34.                                 //Blend One One                       = Additive
    35.                                 //Blend OneMinusDstColor One          = Soft Additive
    36.                                 //Blend DstColor Zero                 = Multiplicative
    37.                                 //Blend DstColor SrcColor             = 2x Multiplicative
    38.  
    39.             CGPROGRAM                        //Start a program in the CG language
    40.             #pragma target 2.0                //Run this shader on at least Shader Model 2.0 hardware (e.g. Direct3D 9)
    41.             #pragma fragment frag            //The fragment shader is named 'frag'
    42.             #pragma vertex vert                //The vertex shader is named 'vert'
    43.             #include "UnityCG.cginc"        //Include Unity's predefined inputs and macros
    44.  
    45.             //Unity variables to be made accessible to Vertex and/or Fragment shader
    46.             uniform sampler2D _MainTex;                    //Define _MainTex from Texture Unit 0 to be sampled in 2D
    47.             //uniform float4 _MainTex_ST;                    //Use the Float _MainTex_ST to pass the Offset and Tiling for the texture(s)
    48.             uniform fixed4 _Color;                            //Use the Color _Color provided by Unity
    49.             uniform float _UVXOffset;
    50.             uniform float _UVYOffset;
    51.             uniform float _UVXScale;
    52.             uniform float _UVYScale;
    53.             uniform float _Offset;
    54.  
    55.             //Data structure communication from Unity to the vertex shader
    56.             //Defines what inputs the vertex shader accepts
    57.             struct AppData {
    58.                 float4 vertex : POSITION;                    //Receive vertex position
    59.                 half2 texcoord : TEXCOORD0;                    //Receive texture coordinates
    60.                             //half2 texcoord1 : TEXCOORD1;                //Receive texture coordinates
    61.                             //fixed4 color : COLOR;                        //Receive vertex colors
    62.             };
    63.  
    64.             //Data structure for communication from vertex shader to fragment shader
    65.             //Defines what inputs the fragment shader accepts
    66.             struct VertexToFragment {
    67.                 float4 pos : POSITION;                        //Send fragment position to fragment shader
    68.                 half2 uv : TEXCOORD0;                        //Send interpolated texture coordinate to fragment shader
    69.                             //half2 uv2 : TEXCOORD1;                    //Send interpolated texture coordinate to fragment shader
    70.                             //fixed4 color : COLOR;                        //Send interpolated gouraud-shaded vertex color to fragment shader
    71.             };
    72.  
    73.             struct vertexOutput {
    74.                 float4 pos : SV_POSITION;
    75.                 float4 posWorld : TEXCOORD0;
    76.                 // position of the vertex (and fragment) in world space
    77.                 float4 posLight : TEXCOORD1;
    78.                 // position of the vertex (and fragment) in light space
    79.                 float3 normalDir : TEXCOORD2;
    80.                 // surface normal vector in world space
    81.             };
    82.  
    83.             //Vertex shader
    84.             VertexToFragment vert(AppData v) {
    85.                 VertexToFragment o;                            //Create a data structure to pass to fragment shader
    86.                 o.pos = mul(UNITY_MATRIX_MVP,v.vertex);        //Include influence of Modelview + Projection matrices
    87.                 //o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);//Send texture coords from unit 0 to fragment shader
    88.                 //o.uv = v.texcoord.xy;
    89.                 o.uv = half2((v.texcoord.x+_UVXOffset)*_UVXScale,(v.texcoord.y+_UVYOffset)*_UVYScale);    //Scale and position
    90.                             //o.uv2 = v.texcoord1.xy;                    //Send texture coords from unit 1 to fragment shader
    91.                             //o.color = v.color;                        //Send interpolated vertex color to fragment shader
    92.                             //o.color = _Color;                            //Send solid color to fragment shader
    93.                 return o;                                    //Transmit data to the fragment shader
    94.             }
    95.  
    96.             //Fragment shader
    97.             fixed4 frag(VertexToFragment i) : COLOR {
    98.                 return fixed4(lerp(_Color,fixed4(_Color.rgb,0),sqrt( (i.uv.x*i.uv.x)+(i.uv.y*i.uv.y) )+_Offset ));    //Output radial gradient
    99.                 // return fixed4(lerp(_Color, fixed4(0,0,0,0), sqrt((i.uv.x*i.uv.x) + (i.uv.y*i.uv.y)) + _Offset).rgb, 1.0);    //Output radial gradient
    100.             }
    101.  
    102.             ENDCG                            //End of CG program
    103.         }
    104.     }
    105.     Fallback "Specular"
    106. }
    107.  
    108. //Copyright (c) 2013 Paul West/Venus12 LLC
    Can you help me to "transform" this mesh into a pure light?

    Thank you,
     
  2. PierreCAMILLI

    PierreCAMILLI

    Joined:
    Mar 1, 2017
    Posts:
    2
    I succeeded to get pixels behind the mesh with a GrabPass, but now, I would like to get those pixels unaffected by light and shadowings.

    Is there some way to do it?

    Thank you,