Search Unity

Contrast and hue using CG in Unity

Discussion in 'Shaders' started by rimiki, Apr 18, 2015.

  1. rimiki

    rimiki

    Joined:
    Dec 30, 2014
    Posts:
    102
    Dears CG developer,

    I'm looking for how I could how to adjust contrast or hue of 3D object using CG ?

    Thx for any help :)
     
  2. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    181
    I did contrast image effect in cg before,
    in cg's pixel shader,
    adjust contrast:

    //custom data
    //you can control this in C#
    //SetFloat("contrastIntensity", contrastIntensity);
    uniform float contrastIntensity;

    float4 frag (v2f_img i) : COLOR
    {
    float4 temp = tex2D(_MainTex,i.uv);
    temp = (temp-0.5)*contrastIntensity + 0.5;

    return temp;
    }

    //when contrastIntensity = 1, the result is normal
    //when contrastIntensity < 1, the result is less contrast
    //when contrastIntensity > 1, the result is more contrast
     
  3. rimiki

    rimiki

    Joined:
    Dec 30, 2014
    Posts:
    102
    Thank you!
    Did u worked on the noise, distortion or chromatic aberrations of the texture? that would be much inetresting for me. Thank you again for your help : ))
     
  4. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    181
    chromatic aberrations =
    Code (CSharp):
    1. Shader "C4Cat/Image Effects/Chromatic Aberration" {
    2. Properties {
    3.     _MainTex ("Base (RGB)", RECT) = "white" {}
    4. }
    5. SubShader {
    6.     Pass {
    7.         ZTest Always Cull Off ZWrite Off
    8.         Fog { Mode off }
    9. CGPROGRAM
    10. #pragma vertex vert_img
    11. #pragma fragment frag
    12. #pragma fragmentoption ARB_precision_hint_fastest
    13. #include "UnityCG.cginc"
    14. // frag shaders data
    15. uniform sampler2D _MainTex;
    16.  
    17. //custom data
    18. uniform float chromaticAberrationIntensity;
    19. uniform float vignetteStrength;
    20. // frag shader
    21. float4 frag (v2f_img i) : COLOR
    22. {
    23.     float uvOffsetModify = (abs(i.uv.y-0.5) * abs(i.uv.x-0.5));
    24.     //float uvOffsetModify = abs(i.uv.y-0.5);          
    25.     float2 uvOffset = float2(chromaticAberrationIntensity,0);
    26.     uvOffset *= uvOffsetModify;
    27.    
    28.     //shift R to left a bit
    29.     float2 uvR = i.uv - uvOffset;
    30.     float4 temp = tex2D(_MainTex, uvR);
    31.     float R = temp.r;
    32.    
    33.     //keep B in original position
    34.     temp = tex2D(_MainTex, i.uv);
    35.     float B = temp.b;
    36.    
    37.     //shift G to right a bit
    38.     float2 uvG = i.uv + uvOffset;
    39.     temp = tex2D(_MainTex, uvG);
    40.     float G = temp.g;
    41.    
    42.     //combine R G B result
    43.     float4 result = float4(R,G,B,1);
    44.    
    45.     result *= 1 - uvOffsetModify * vignetteStrength;
    46.    
    47.    
    48.     return result;
    49. }
    50. ENDCG
    51.     }
    52. }
    53. Fallback off
    54. }
    sample R a bit left,
    sample G a bit right,
    sample B as usually,
    combine RGB for output.
     
  5. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    181
    distortion =
    sample pixel color with offset UV,
    how each pixel offset when sampling texture is base on your need.

    noise, what kind of noise you need?

    Are you doing something like a TV shader?
     
  6. rimiki

    rimiki

    Joined:
    Dec 30, 2014
    Posts:
    102
    In my case it's exactly to copy noise from a texture(I dunno how to do it) and put it my 3D object via the shader.
     
  7. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    181
    can a simple multiply works for you?
    mainTex * noise