Unity 2D:区域色相偏移效果

时间:2019-02-21 12:26:46

标签: unity3d unityscript

我正在尝试在Unity中创建一个透明的GameObejct,如果激活,则将其后面的任何颜色更改180度。如果我的解释不够好,可以在Photoshop中轻松看到效果。

我猜想我将需要一个着色器,但是到目前为止,我的着色器制作知识还很有限,因此可以提供任何帮助。

谢谢!

在photoshop中取得所需效果的示例: enter image description here

1 个答案:

答案 0 :(得分:0)

您可以从this post中获取着色器,并进行以下调整:

1)在此处添加GrabPass

 SubShader 
  {
    GrabPass {"_GrabTexture"} // <--

2)然后在此处为抓取纹理添加一个变量:

  float _HueShift;
  float _SaturationShift;
  sampler2D _GrabTexture; // <--

3)将HueShift属性更改为一个范围(为了易于使用):

 Properties 
  {
      _HueShift("HueShift", Range(0.0, 1.0)) = 0 // <--
      _SaturationShift("SaturationShift", Float) = 1.0
  }

4)将Input结构更改为包含screenPos:

 struct Input {
        float4 screenPos; // <--
    };

5)然后,最后更新片段着色器以从GrabTexture中读取:

void surf (Input IN, inout SurfaceOutput o) {
        float2 uv = IN.screenPos.xy / IN.screenPos.w; // <--
        half4 c = tex2D (_GrabTexture, uv);// <--

        o.Albedo = c.rgb;// <--

        float3 hsv = rgb_to_hsv_no_clip(o.Albedo.xyz);
        hsv.x+=_HueShift;
        hsv.y = _SaturationShift;

        if ( hsv.x > 1.0 ) { hsv.x -= 1.0; }
        o.Albedo = half3(hsv_to_rgb(hsv));
        o.Alpha = c.a;// <--
  }

最终结果如下:

enter image description here

这里是最终的着色器供参考,以防万一我忘记提及其中一项更改:

Shader "Custom/HueRotation" 
{
  Properties 
  {
      _HueShift("HueShift", Range(0.0, 1.0)) = 0
      _SaturationShift("SaturationShift", Float) = 1.0
  }

  SubShader 
  {
    GrabPass {"_GrabTexture"}
    Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType" = "Transparent" }
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    Cull Off
    CGPROGRAM
    #pragma surface surf Lambert alpha
    #pragma target 3.0
    #include "UnityCG.cginc"

    struct Input {
        float4 screenPos;
    };

    float3 rgb_to_hsv_no_clip(float3 RGB)
    {
            float3 HSV;

        float minChannel, maxChannel;
        if (RGB.x > RGB.y) {
        maxChannel = RGB.x;
        minChannel = RGB.y;
        }
        else {
        maxChannel = RGB.y;
        minChannel = RGB.x;
        }

        if (RGB.z > maxChannel) maxChannel = RGB.z;
        if (RGB.z < minChannel) minChannel = RGB.z;

            HSV.xy = 0;
            HSV.z = maxChannel;
            float delta = maxChannel - minChannel;             //Delta RGB value
            if (delta != 0) {                    // If gray, leave H  S at zero
                HSV.y = delta / HSV.z;
                float3 delRGB;
                delRGB = (HSV.zzz - RGB + 3*delta) / (6.0*delta);
                if      ( RGB.x == HSV.z ) HSV.x = delRGB.z - delRGB.y;
                else if ( RGB.y == HSV.z ) HSV.x = ( 1.0/3.0) + delRGB.x - delRGB.z;
                else if ( RGB.z == HSV.z ) HSV.x = ( 2.0/3.0) + delRGB.y - delRGB.x;
            }
            return (HSV);
    }

    float3 hsv_to_rgb(float3 HSV)
    {
            float3 RGB = HSV.z;

                float var_h = HSV.x * 6;
                float var_i = floor(var_h);   // Or ... var_i = floor( var_h )
                float var_1 = HSV.z * (1.0 - HSV.y);
                float var_2 = HSV.z * (1.0 - HSV.y * (var_h-var_i));
                float var_3 = HSV.z * (1.0 - HSV.y * (1-(var_h-var_i)));
                if      (var_i == 0) { RGB = float3(HSV.z, var_3, var_1); }
                else if (var_i == 1) { RGB = float3(var_2, HSV.z, var_1); }
                else if (var_i == 2) { RGB = float3(var_1, HSV.z, var_3); }
                else if (var_i == 3) { RGB = float3(var_1, var_2, HSV.z); }
                else if (var_i == 4) { RGB = float3(var_3, var_1, HSV.z); }
                else                 { RGB = float3(HSV.z, var_1, var_2); }

        return (RGB);
    }

      float _HueShift;
      float _SaturationShift;
      sampler2D _GrabTexture;

      void surf (Input IN, inout SurfaceOutput o) {
        float2 uv = IN.screenPos.xy / IN.screenPos.w;
        half4 c = tex2D (_GrabTexture, uv);

        o.Albedo = c.rgb;

        float3 hsv = rgb_to_hsv_no_clip(o.Albedo.xyz);
        hsv.x+=_HueShift;
        hsv.y = _SaturationShift;

        if ( hsv.x > 1.0 ) { hsv.x -= 1.0; }
        o.Albedo = half3(hsv_to_rgb(hsv));
        o.Alpha = c.a;
      }

      ENDCG
    }
    Fallback "Diffuse"
  }

获得Beeky的原始代码。