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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bicolor viewshed shader

Discussion in 'Scripting' 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. Smaika

    Smaika

    Joined:
    Apr 28, 2014
    Posts:
    14
    I am trying to achieve the same behavior have you figured it out?
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    550
    You could get the same effect with two directional lights pointing in opposite directions and with high intensity.