Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make colored windows to throw colored shadows

Discussion in 'Shaders' started by Kusras, Feb 20, 2018.

  1. Kusras

    Kusras

    Joined:
    Jul 9, 2015
    Posts:
    134


    There is an example. In my problem I have church windows and it looks weird as they throw dark/white shadows. I tried to make projectors with custom shader, but it does not stop on first face...

    Any idea how to make it?
     
  2. RepoGames

    RepoGames

    Joined:
    Apr 15, 2016
    Posts:
    69
  3. Kusras

    Kusras

    Joined:
    Jul 9, 2015
    Posts:
    134
    When I make cookie from this, it becomes white texture in unity, so I am not sure it will work. It seems cookie is just a grayscale texture and is able just to shade light, not change its color. And yes I tried also
    custom shader for that projector. Unfortunately it penetrates mesh and appears on all objects in a way...
     
    Last edited: Feb 20, 2018
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    perhaps using command buffers?
     
  5. Kusras

    Kusras

    Joined:
    Jul 9, 2015
    Posts:
    134
    Here is that projector shader material code I wrote:
    I used unity shadow projector material and added color from texture and blending as a multiply in photoshop


    Code (CSharp):
    1. Shader "My/ColoredWinShade"
    2. {
    3.  
    4.     Properties {
    5.         _ShadowTex ("Cookie", 2D) = "gray" {}
    6.         _ColorTex ("Color", 2D) = "gray" {}
    7.  
    8.     }
    9.     Subshader {
    10.         Tags {"Queue"="Transparent"}
    11.  
    12.         GrabPass{
    13.         "_BackgroundTexture"
    14.         }
    15.  
    16.  
    17.         Pass {
    18.             ZWrite Off
    19.             ColorMask RGB
    20.             Blend OneMinusSrcAlpha SrcAlpha
    21.             Offset -1, -1
    22.  
    23.             CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #pragma multi_compile_fog
    27.             #include "UnityCG.cginc"
    28.          
    29.             struct v2f {
    30.                 float4 uvShadow : TEXCOORD0;
    31.                 float4 grabPos: TEXCOORD1;
    32.                 UNITY_FOG_COORDS(2)
    33.                 float4 pos : SV_POSITION;
    34.  
    35.             };
    36.          
    37.             float4x4 unity_Projector;
    38.             float4x4 unity_ProjectorClip;
    39.             float _Visibility;
    40.  
    41.             v2f vert (float4 vertex : POSITION)
    42.             {
    43.                 v2f o;
    44.                 o.pos = mul (UNITY_MATRIX_MVP, vertex);
    45.                 o.uvShadow = mul (unity_Projector, vertex);
    46.                 o.grabPos = ComputeGrabScreenPos(o.pos);
    47.                 UNITY_TRANSFER_FOG(o,o.pos);
    48.                 return o;
    49.             }
    50.          
    51.             sampler2D _ShadowTex;
    52.             sampler2D _ColorTex;
    53.             sampler2D _BackgroundTexture;
    54.  
    55.  
    56.             fixed4 frag (v2f i) : SV_Target
    57.             {
    58.                 fixed4 texS =tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
    59.  
    60.  
    61.                 UNITY_APPLY_FOG_COLOR(i.fogCoord, texS, fixed4(1,1,1,1));
    62.                 fixed4 Background = tex2Dproj (_BackgroundTexture, i.grabPos);
    63.                 texS.rgb=tex2Dproj (_ColorTex, UNITY_PROJ_COORD(i.uvShadow))+0.5;
    64.                 texS=Background*texS*texS;
    65.  
    66.              
    67.                 return texS;
    68.             }
    69.             ENDCG
    70.         }
    71.     }
    72. }
    73.  
     
  6. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    You could build on this and to "fake" the effect, you could have a surface material on the floor and effect its albedo and emmissive properties based on the projection on it, to "fake" the luminance
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Do you for sure need a real time light? If you're using baked lighting Unity already supports colored shadows, though it does require you write a custom shader to take advantage of this.

    See the section on Custom RGB transparency:
    https://docs.unity3d.com/Manual/MetaPass.html

    Note, this will work for baked lighting, but if you're using mixed lighting the real time lighting on dynamic objects will not be affected. For that you do still need to use projectors.
     
  8. Kusras

    Kusras

    Joined:
    Jul 9, 2015
    Posts:
    134
    Unfortunately I need - as every structure is placed by players in the game. so I have no clue how player place this object under which angle etc. Also I am unable to bake anything....
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    With how Unity is setup, using only existing Unity features, faking it with projectors is basically the only option.

    The alternative would be to convert over to the Alloy asset which supports full color cookies. But that only gets you so far.

    The "real" solution would be something like this:
    https://turanszkij.wordpress.com/2018/01/18/easy-transparent-shadow-maps/

    Fairly straightforward to implement with command buffers, but does require custom shaders for everything.
     
    Mehrdad995 likes this.
  10. Kusras

    Kusras

    Joined:
    Jul 9, 2015
    Posts:
    134
    wow cool thats because unity has just attenuation as float not as color