Search Unity

Resolved Good way to input Direction into Material?

Discussion in 'Shaders' started by DimitriX89, Nov 4, 2022.

  1. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Let's say I have a shader which takes a directional vector as an input, to generate cel shading effect. I want this direction to be customisable in editor, so passing a vector from script isnt an option. Using color input or 3 floats is too inconvenient. Can a custom UI be created for shader? For example, a circle where you can drag with a mouse to set a vector?
    https://gyazo.com/6b50a7614bd83974e915a5f31b9f1eb3
     
  2. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Solved, more or less.
    https://gyazo.com/465f196431a8add5a13d5acb0db4c2a6
    I noticed that the central square of Unity color picker is perfect for my goal, as it sets SV of HSV color model from cursor XY. So I've written a function which translates SV values into direction inside "coordinate hemisphere", and also custom function for RGB to HSV conversion
    Code (CSharp):
    1.             float3 DirFromRGB(float3 dir){//uses color picker (HSV) UI for more intuitive input of directions
    2.                 float3 _LightDir = RGBtoHSV(dir);//directional input using color
    3.                 float2 _xy =float2(_LightDir.y - 0.5,_LightDir.z -0.5)*2;
    4.                 if (length(_xy) >= 1){
    5.                 _xy = normalize(_xy)*0.999;
    6.                 }
    7.                 float _z = -1*sqrt(1 - _xy.x*_xy.x - _xy.y*_xy.y);
    8.                 float3 LightDir = float3(_xy.x,_xy.y,_z);
    9.                 if (_LightDir.x < 180){
    10.                     LightDir.z *= 1;
    11.                 } else {
    12.                     LightDir.z *= -1;
    13.                 };//directional input using color
    14.                 return LightDir;
    15.             }
    Code (CSharp):
    1.             float3 RGBtoHSV (fixed3 _rgb){//convert RGB input to HSV; useful for inputing directions from shader UI
    2.                 fixed H = 0;
    3.  
    4.                 fixed M1 = max(_rgb.r,_rgb.g);//max
    5.                 fixed M2 = max(_rgb.g,_rgb.b);
    6.                 fixed M = 0;
    7.                 if (M1>M2){
    8.                     M = M1;
    9.                 } else {
    10.                     M = M2;
    11.                 }
    12.                 fixed m1 = min(_rgb.r,_rgb.g);//min
    13.                 fixed m2 = min(_rgb.g,_rgb.b);
    14.                 fixed m = 0;
    15.                 if (m1<m2){
    16.                     m = m1;
    17.                 } else {
    18.                     m = m2;
    19.                 }
    20.                 fixed delta = M-m;
    21.              
    22.                 if (delta == 0)
    23.                     H = 0;
    24.                 if (M == _rgb.r)
    25.                     H = 60*(_rgb.g-_rgb.b*delta%6);
    26.                 if (M == _rgb.g)
    27.                     H = 60*(_rgb.b-_rgb.r*delta + 2);
    28.                 if (M == _rgb.b)
    29.                     H = 60*(_rgb.r-_rgb.g*delta + 4);
    30.                 float S = (M-m)/M;
    31.                 if (M == 0)
    32.                     S = 0;
    33.  
    34.                 return fixed3(H,S,M);
    35.             }