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

Bicolor shader

Discussion in 'Shaders' started by Thibault-Potier, Nov 7, 2018.

  1. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Hi :)

    I'm a total beginner with shader.

    What i want to achieve is a shader wich would color any surface hit by light in green, and the other in red.

    (Final goal would be to achieve this effect :


    So right now i have done that :

    Code (CSharp):
    1. Shader "Custom/TESTSHADER" {
    2.     Properties {
    3.         _FirstColor ("First Color", Color) = (1,1,1,1)
    4.         _SecondColor("Second Color", Color) = (0,0,0,0)
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         LOD 200
    9.  
    10.         CGPROGRAM
    11.         #pragma surface surf ViewShed
    12.  
    13.         float4 _FirstColor;
    14.         float4 _SecondColor;
    15.         float4 c;
    16.  
    17.         struct Input {
    18.             float2 uv_MainTex;
    19.         };
    20.  
    21.         inline float4 LightingViewShed(SurfaceOutput surface, float3 lightDirection, float attenuation) {
    22.             float delta = dot(surface.Normal, lightDirection);
    23.            
    24.             if (delta > 0) {
    25.                 c.rgb = _FirstColor.rgb;
    26.             }
    27.             else {
    28.                 c.rgb = _SecondColor.rgb;
    29.             }
    30.  
    31.             c.a = surface.Alpha;
    32.             return c;
    33.         }
    34.  
    35.         void surf (Input IN, inout SurfaceOutput surface) {
    36.             surface.Albedo = fixed3(1.0, 1.0, 1.0);
    37.         }
    38.         ENDCG
    39.     }
    40.     FallBack "Diffuse"
    41. }
    So basically it is a first try, but the result is not a total crap



    However, when using a spotLight instead of directionnal light, it doesn't behave has intended:



    There is also the fact that i want to be able see the shadows in red :O

    Any help or advices would be so appreciate ! I'm still very far from my final goal :0
     
  2. Quakeulf

    Quakeulf

    Joined:
    Mar 26, 2013
    Posts:
    40
    This is what you can use stencils and volumes for. I really wish I could show you how but I have no idea how to do it because I am still trying to figure out how to do the passes, Zwrites, Ztests, Ztests, and so on in a geometry shader that extrudes the edges of the shapes and colours the colliding gameobject accordingly...

    If you find a solution for this please post it here as I am dying to see it.