Search Unity

Greyscale to color at specified locations

Discussion in 'Shaders' started by Kog93, Nov 12, 2020.

  1. Kog93

    Kog93

    Joined:
    Jun 23, 2016
    Posts:
    1
    Hey everyone,

    I am hoping I may be able to get some help with some hlsl shader code. We are trying to achieve greyscale across a level and when an item is placed in a location, that area turns to color. This script works in a single location but we are trying to achieve this in multiple locations within the level. I have tried adding an array that stores the locations but unfortunately it only passes the first location from the C# script Array in which I use Shader.SetGlobalVectorArray();.

    I have minimal knowledge when it comes to hlsl and shaders, so any help and/or guidance would be great. Also if there is an easier way to achieve this effect, I would love to be pointed in the right direction. Thanks in advance.

    This is the hlsl script that I have been using to achieve the effect.

    Code (CSharp):
    1. Shader "Custom/SphericalMask"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _ColorStrength("Color Strength", Range(1,4)) = 1
    8.         _EmissionColor("Emission Color", Color) = (1,1,1,1)
    9.         _EmissionTex("Emission (RGB)", 2D) = "white" {}
    10.         _EmissionStrength("Emission Strength", Range(1,4)) = 1
    11.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    12.         _Metallic ("Metallic", Range(0,1)) = 0.0
    13.  
    14.     }
    15.     SubShader
    16.     {
    17.         Tags { "RenderType"="Opaque" }
    18.         LOD 200
    19.  
    20.         CGPROGRAM
    21.         // Physically based Standard lighting model, and enable shadows on all light types
    22.         #pragma surface surf Standard fullforwardshadows
    23.  
    24.         // Use shader model 3.0 target, to get nicer looking lighting
    25.         #pragma target 3.0
    26.  
    27.         sampler2D _MainTex;
    28.  
    29.         struct Input
    30.         {
    31.             float2 uv_MainTex;
    32.             float3 worldPos;
    33.         };
    34.  
    35.         half _Glossiness;
    36.         half _Metallic;
    37.         half _ColorStrength;
    38.  
    39.         //Spherical Mask
    40.         uniform half GLOBALmask_Radius;
    41.         uniform half GLOBALmask_Softness;
    42.         fixed4 GLOBALmask_Position[10];
    43.  
    44.         // #pragma instancing_options assumeuniformscaling
    45.         UNITY_INSTANCING_BUFFER_START(Props)
    46.             // put more per-instance properties here
    47.         UNITY_INSTANCING_BUFFER_END(Props)
    48.  
    49.         void surf (Input IN, inout SurfaceOutputStandard o)
    50.         {
    51.             // Albedo comes from a texture tinted by color
    52.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    53.             //Greyscale
    54.             half grayscale = (c.r + c.g + c.b) * 0.333;
    55.             fixed3 c_g = fixed3(grayscale,grayscale,grayscale);
    56.  
    57.             for (int i = 0; i < 10; i++)
    58.             {
    59.                 half d = distance(GLOBALmask_Position[1], IN.worldPos);
    60.                 half sum = saturate((d - GLOBALmask_Radius) / -GLOBALmask_Softness);
    61.                 fixed4 lerpColor = lerp(fixed4(c_g, 1), c * _ColorStrength, sum);
    62.                 o.Albedo = lerpColor.rgb;
    63.             }
    64.             // Metallic and smoothness come from slider variables
    65.             o.Metallic = _Metallic;
    66.             o.Smoothness = _Glossiness;
    67.             o.Alpha = c.a;
    68.         }
    69.         ENDCG
    70.     }
    71.     FallBack "Diffuse"
    72. }
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    If you cant solve the issue some other way, i do have a package in the assetstore called TOZ Color Zones that does the exact thing you want.