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. Dismiss Notice

combine two color in a single shader

Discussion in 'Shaders' started by GCMSA, Jul 9, 2018.

  1. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    Hello everyone,

    I'm trying to write a shader that allow to render a cube with two colors. I already succeded to write the shader (see the code bellow); however, I want the blue color (see attached picture) to be Fade and completely transparent. How can I modify the code to obtain such an
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3.  
    4. Shader "Volumetric Clouds/Volumetric Clouds Example"
    5. {
    6.     Properties
    7.     {
    8.         // How many iterations we should step through space
    9.         _Iterations("Iterations", Range(0, 200)) = 100
    10.         // How long through space we should step
    11.         _ViewDistance("View Distance", Range(0, 5)) = 2
    12.         // Essentially the background color
    13.         _SkyColor("Sky Color", Color) = (0.176, 0.478, 0.871, 1)
    14.         // Cloud color
    15.         _CloudColor("Cloud Color", Color) = (1, 1, 1, 1)
    16.         // How dense our clouds should be
    17.         _CloudDensity("Cloud Density", Range(0, 1)) = 0.5
    18.  
    19.         // Note that the longer your view distance is, the more steps are required. And the smaller your clouds are, the bigger a render target is needed.
    20.     }
    21.         SubShader
    22.     {
    23.         Pass
    24.     {
    25.         // Ignore the destination color and print the new one.
    26.         Blend SrcAlpha Zero
    27.  
    28.         CGPROGRAM
    29. #pragma vertex vert
    30. #pragma fragment frag
    31. #include "UnityCG.cginc"
    32.  
    33.         // Global properties
    34.         sampler2D _NoiseOffsets;
    35.     float3 _CamPos;
    36.     float3 _CamRight;
    37.     float3 _CamUp;
    38.     float3 _CamForward;
    39.     float _AspectRatio;
    40.     float _FieldOfView;
    41.  
    42.     // Local properties
    43.     int _Iterations;
    44.     float3 _SkyColor;
    45.     float4 _CloudColor;
    46.     float _ViewDistance;
    47.     float _CloudDensity;
    48.  
    49.     struct v2f
    50.     {
    51.         float2 uv : TEXCOORD0;
    52.         float4 vertex : SV_POSITION;
    53.     };
    54.  
    55.     v2f vert(appdata_base  v)
    56.     {
    57.         v2f o;
    58.         o.vertex = UnityObjectToClipPos(v.vertex);
    59.         o.uv = v.texcoord;
    60.         return o;
    61.     }
    62.  
    63.     // Noise function by Inigo Quilez - https://www.shadertoy.com/view/4sfGzS
    64.     float noise(float3 x) { x *= 4.0; float3 p = floor(x); float3 f = frac(x); f = f*f*(3.0 - 2.0*f); float2 uv = (p.xy + float2(37.0, 17.0)*p.z) + f.xy; float2 rg = tex2D(_NoiseOffsets, (uv + 0.5) / 256.0).yx; return lerp(rg.x, rg.y, f.z); }
    65.  
    66.     // This function is the actual noise function we are going to be using.
    67.     // The more octaves you give it, the more details we'll get in our noise.
    68.     float fbm(float3 pos, int octaves) { float f = 0.; for (int i = 0; i < octaves; i++) { f += noise(pos) / pow(2, i + 1); pos *= 2.01; } f /= 1 - 1 / pow(2, octaves + 1); return f; }
    69.  
    70.     fixed4 frag(v2f i) : SV_Target
    71.     {
    72.         float2 uv = (i.uv - 0.5) * _FieldOfView;
    73.         uv.x *= _AspectRatio;
    74.  
    75.         float3 ray = _CamUp * uv.y + _CamRight * uv.x + _CamForward;
    76.         float3 pos = _CamPos * 0.4;
    77.  
    78.         // So now we have a position, and a ray defined for our current fragment, and we know from earlier in this article that it matches the field of view and aspect ratio of the camera. And we can now start iterating and creating our clouds.
    79.         // We will not be ray-marching twoards any distance field in this example. So the following code should be much easier to understand.
    80.         // pos is our original position, and p is our current position which we are going to be using later on.
    81.         float3 p = pos;
    82.         // For each iteration, we read from our noise function the density of our current position, and adds it to this density variable.
    83.         float density = 0;
    84.  
    85.         for (float i = 0; i < _Iterations; i++)
    86.         {
    87.             // f gives a number between 0 and 1.
    88.             // We use that to fade our clouds in and out depending on how far and close from our camera we are.
    89.             float f = i / _Iterations;
    90.             // And here we do just that:
    91.             float alpha = smoothstep(0, _Iterations * 0.2, i) * (1 - f) * (1 - f);
    92.             // Note that smoothstep here doesn't do the same as Mathf.SmoothStep() in Unity C# - which is frustrating btw. Get a grip Unity!
    93.             // Smoothstep in shader languages interpolates between two values, given t, and returns a value between 0 and 1.
    94.             // To get a bit of variety in our clouds we collect two different samples for each iteration.
    95.             float denseClouds = smoothstep(_CloudDensity, 0.75, fbm(p, 5));
    96.             float lightClouds = (smoothstep(-0.2, 1.2, fbm(p * 2, 2)) - 0.5) * 0.5;
    97.             // Note that I smoothstep again to tell which range of the noise we should consider clouds.
    98.             // Here we add our result to our density variable
    99.             density += (lightClouds + denseClouds) * alpha;
    100.             // And then we move one step further away from the camera.
    101.             p = pos + ray * f * _ViewDistance;
    102.         }
    103.         // And here i just melted all our variables together with random numbers until I had something that looked good.
    104.         // You can try playing around with them too.
    105.         float3 color = _SkyColor + (_CloudColor.rgb - 0.5) * (density / _Iterations) * 20 * _CloudColor.a;
    106.  
    107.         return fixed4(color, 1);
    108.     }
    109.         ENDCG
    110.     }
    111.     }
    112. }
    effect?

    Thanks in advance
     
  2. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    sorry the picture is attached here
     

    Attached Files:

  3. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    Could anyone help on this issue please?

    Thanks
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    Sounds like you want alpha blending?
    Blend SrcAlpha OneMinusSrcAlpha

    https://docs.unity3d.com/Manual/SL-Blend.html

    The short version is don't output the shader with fixed4(color, 1), but instead set the last component of the fixed4 to how transparent you want the shader. Also note that like in the above documentation you'll want to change the Queue, and may want to disable Zwrite.