Search Unity

Color Bleed Effect

Discussion in 'Shaders' started by ZerkyWerky, Jul 28, 2019.

  1. ZerkyWerky

    ZerkyWerky

    Joined:
    Apr 2, 2009
    Posts:
    113
    Hey everyone, I've been looking everywhere for how to do an effect I need for my game. What I want is as the player is moving through a black and white world, I want to bleed colors that turns the world into color as the bleeding expands leaving a trail of colored world.

    The closest I have come to finding this effect is this:
    .

    But this in only a global effect. It doesn't help me as far as leaving a trail behind.

    Does anybody have any advice on how I can accomplish this?
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    277
    Use render texture to store mask, and use that mask for blend colored and grayscale zonese.
    A mask can be drawn, like this for example.
    Code (CSharp):
    1. Shader "Unlit/UvTest"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _BrushColor("Brush color", Color) = (1,.5,.5,1)
    7.         _BrushSize("Brush size", Float) = 1
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" }
    12.         Cull Off
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 float4 vertex : SV_POSITION;
    32.                 float4 worldPos : TEXCOORD1;
    33.             };
    34.  
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.             float4 _BrushPos;
    38.             fixed4 _BrushColor;
    39.             float _BrushSize;
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = float4(v.uv.x-0.5, -v.uv.y+0.5, 0, 0.5);
    45.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 return o;
    48.             }
    49.  
    50.             fixed4 frag (v2f i) : SV_Target
    51.             {
    52.                 // sample the texture
    53.                 fixed4 col = tex2D(_MainTex, i.uv);
    54.                 float dist = distance(i.worldPos, float4(_BrushPos.xyz, 1));
    55.                 fixed t = smoothstep(0,_BrushSize,dist);
    56.                 col = lerp(_BrushColor, col, t);
    57.                 return col;
    58.                 // return t.xxxx;
    59.             }
    60.             ENDCG
    61.         }
    62.     }
    63. }
    64.  
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class BrushPositionSetter : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         Shader.SetGlobalVector("_BrushPos", transform.position);
    9.     }
    10. }
    The same render texture is used for both input and output.
    It's works but I do not know how to get rid of the black edging on the borders of the UV islands.
     
    Last edited: Jul 29, 2019
  3. ZerkyWerky

    ZerkyWerky

    Joined:
    Apr 2, 2009
    Posts:
    113
    Thanks! That's a lot closer to what I'm looking for. Where does the material go and I assume the setter going on the orb you paint with?
     
  4. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    277
    Here package with current setup, just for demonstrate conception, I think is better idea use camera controlled from script, or even something like drawMeshNow.
     

    Attached Files:

    Last edited: Jul 29, 2019
    bgolus likes this.
  5. ZerkyWerky

    ZerkyWerky

    Joined:
    Apr 2, 2009
    Posts:
    113
    Thanks again. Your project works only I'm not getting the same result as in your video. It's working more like a spotlight or a projector. Not actually leaving any trail behind.
     
  6. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    277
    I don't know what is happened, may by something was corrupted due version mismatch. Here I iterate thru all things to show settings.