Search Unity

Looking to alter the StainedBumpDistort Shader

Discussion in 'Shaders' started by LMan, Sep 18, 2017.

  1. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    All I'd like to do is replace the texture tint option with a color picker option.

    This way I could alter the tint through C#, and adjust the transparency of the tint using the alpha.

    I'm comfortable with C# but shaderLab has not been as easy for me to pick up.

    The Standard Shader is as follows:

    Code (CSharp):
    1. // Per pixel bumped refraction.
    2. // Uses a normal map to distort the image behind, and
    3. // an additional texture to tint the color.
    4.  
    5. Shader "FX/Glass/Stained BumpDistort" {
    6. Properties {
    7.     _BumpAmt  ("Distortion", range (0,128)) = 10
    8.     _MainTex ("Tint Color (RGB)", 2D) = "white" {}
    9.     _BumpMap ("Normalmap", 2D) = "bump" {}
    10.  
    11. }
    12.  
    13. Category {
    14.  
    15.     // We must be transparent, so other objects are drawn before this one.
    16.     Tags { "Queue"="Transparent" "RenderType"="Opaque" }
    17.  
    18.  
    19.     SubShader {
    20.  
    21.         // This pass grabs the screen behind the object into a texture.
    22.         // We can access the result in the next pass as _GrabTexture
    23.         GrabPass {
    24.             Name "BASE"
    25.             Tags { "LightMode" = "Always" }
    26.         }
    27.      
    28.         // Main pass: Take the texture grabbed above and use the bumpmap to perturb it
    29.         // on to the screen
    30.         Pass {
    31.             Name "BASE"
    32.             Tags { "LightMode" = "Always" }
    33.          
    34. CGPROGRAM
    35. #pragma vertex vert
    36. #pragma fragment frag
    37. #pragma multi_compile_fog
    38. #include "UnityCG.cginc"
    39.  
    40. struct appdata_t {
    41.     float4 vertex : POSITION;
    42.     float2 texcoord: TEXCOORD0;
    43. };
    44.  
    45. struct v2f {
    46.     float4 vertex : SV_POSITION;
    47.     float4 uvgrab : TEXCOORD0;
    48.     float2 uvbump : TEXCOORD1;
    49.     float2 uvmain : TEXCOORD2;
    50.     UNITY_FOG_COORDS(3)
    51. };
    52.  
    53. float _BumpAmt;
    54. float4 _BumpMap_ST;
    55. float4 _MainTex_ST;
    56.  
    57. v2f vert (appdata_t v)
    58. {
    59.     v2f o;
    60.     o.vertex = UnityObjectToClipPos(v.vertex);
    61.     o.uvgrab = ComputeGrabScreenPos(o.vertex);
    62.     o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
    63.     o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
    64.     UNITY_TRANSFER_FOG(o,o.vertex);
    65.     return o;
    66. }
    67.  
    68. sampler2D _GrabTexture;
    69. float4 _GrabTexture_TexelSize;
    70. sampler2D _BumpMap;
    71. sampler2D _MainTex;
    72.  
    73. half4 frag (v2f i) : SV_Target
    74. {
    75.     #if UNITY_SINGLE_PASS_STEREO
    76.     i.uvgrab.xy = TransformStereoScreenSpaceTex(i.uvgrab.xy, i.uvgrab.w);
    77.     #endif
    78.  
    79.     // calculate perturbed coordinates
    80.     half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
    81.     float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
    82.     #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE //to handle recent standard asset package on older version of unity (before 5.5)
    83.         i.uvgrab.xy = offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(i.uvgrab.z) + i.uvgrab.xy;
    84.     #else
    85.         i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
    86.     #endif
    87.  
    88.     half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    89.     half4 tint = tex2D(_MainTex, i.uvmain);
    90.     col *= tint;
    91.     UNITY_APPLY_FOG(i.fogCoord, col);
    92.     return col;
    93. }
    94. ENDCG
    95.         }
    96.     }
    97.  
    98.     // ------------------------------------------------------------------
    99.     // Fallback for older cards and Unity non-Pro
    100.  
    101.     SubShader {
    102.         Blend DstColor Zero
    103.         Pass {
    104.             Name "BASE"
    105.             SetTexture [_MainTex] {    combine texture }
    106.         }
    107.     }
    108. }
    109.  
    110. }
    111.  
    I know I'd need to add a color property, but after the initial declaration I'm entirely befuddled by the terminology- I cant tell what bit does which.

    I appreciate your time and assistance.
     
  2. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    204
    I can maybe take a look at this in the next couple of days if nobody else stops in with an answer, but I actually do something that I believe is similar to what you want to do.

    I have two materials applied to a gelatinous cube mob. I have the Stained BumpDistort to give it a distorted appearance (this has only the normal map, no _MainTex), and a second material using the standard shader (in my case with transparent rendering mode) and I set both the main textures here and can also programmatically change the color with a picker.
     
    Last edited: Sep 20, 2017
    LMan likes this.
  3. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    That gives the flexibility I was looking for! I'd still love to see the color implementation on the single shader, if only so that I can get a toehold on some shader code- I'm probably going to need it at some point or another. I imagine that it's cheaper to run a single material instead of two as well.
     
  4. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    204
    It is definitely cheaper, though the BumpDistort itself seems not so great when a color texture is applied (in my opinion and for my project, YMMV). The normal map alone really is what it does best. If you're targeting mobile, obviously every texture you don't have to add is a plus, but for other platform, using Bump with a single normal doesn't take much in the way of resources, even drawing both materials.

    Still, I get what you're saying. I'm not great with shader code by any means, but when I have a chance I also want to look into this to see what I can get working. Unfortunately this week is insanity for me so it may not be until closer to the weekend.

    If it helps you get started, you might want to add something like:

    Code (HLSL):
    1. _MainColor ("Main color", Color) = (f, f, f, 1)
    in the properties section. Then maybe try replacing _MainTex with MainColor. You may have to programmatically create a Tex2D from the color (so it's just a solid colored texture) before the replacement.
     
    LMan likes this.
  5. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    I don't understand what you mean.

    Same here, so I can't test right now.

    @LMan:
    This is all from memory, but I think you'll need to do something along those lines:

    -Replace line 8 with a color property like you can copy and paste from any other shader that has one. Call it _TintColor

    -Replace line 71 with with:
    float4 _TintColor;

    -Comment out line 89

    -Replace line 90 with:
    col *= _TintColor;


    My apologies if any of that was wrong, I normally trial & error my way through shaderproblems or use Shaderforge.
     
    Xepherys and LMan like this.
  6. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    Many thanks @Martin_H, I'll give it a shot!
    I missed shaderForge last time it was on sale, and now I'm trying to throw together quick and dirty Sci fi effects and really wishing I had it.
     
  7. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    I'm sure it'll go on sale again, maybe for christmas? Add it to your wishlist and set the notification interval to daily (iirc).

    I forgot to mention that you would fade that kind of tint out by lerping towards white, not by adjusting the alpha, since the tinting is just a multiplication in this shader. I don't know what exactly happens when you change the alpha here.
     
    Xepherys and LMan like this.