Search Unity

Question Change CubeMap surface color at runtime

Discussion in 'Shaders' started by TEEBQNE, Sep 7, 2021.

  1. TEEBQNE

    TEEBQNE

    Joined:
    Jan 25, 2017
    Posts:
    88
    I have a surface shader for 3D and 2D objects that changes its color by hue shifting it. Below is the code I would use in one of these surface shaders to shift the color.

    Code (CSharp):
    1. float3 hsv_to_rgb(float3 HSV)
    2. {
    3.     half3 resultHsv = half3(HSV.xyz);
    4.     half cosHsv = _HsvBright * _HsvSaturation * cos(_HsvShift * 3.14159265 / 180);
    5.     half sinHsv = _HsvBright * _HsvSaturation * sin(_HsvShift * 3.14159265 / 180);
    6.     resultHsv.x = (.299 * _HsvBright + .701 * cosHsv + .168 * sinHsv) * HSV.x
    7.         + (.587 * _HsvBright - .587 * cosHsv + .330 * sinHsv) * HSV.y
    8.         + (.114 * _HsvBright - .114 * cosHsv - .497 * sinHsv) * HSV.z;
    9.     resultHsv.y = (.299 * _HsvBright - .299 * cosHsv - .328 * sinHsv) *HSV.x
    10.         + (.587 * _HsvBright + .413 * cosHsv + .035 * sinHsv) * HSV.y
    11.         + (.114 * _HsvBright - .114 * cosHsv + .292 * sinHsv) * HSV.z;
    12.     resultHsv.z = (.299 * _HsvBright - .3 * cosHsv + 1.25 * sinHsv) * HSV.x
    13.         + (.587 * _HsvBright - .588 * cosHsv - 1.05 * sinHsv) * HSV.y
    14.         + (.114 * _HsvBright + .886 * cosHsv - .203 * sinHsv) * HSV.z;
    15.     return resultHsv;
    16. }
    I am able to edit the CubeMap's Tint color by the shader keyword from code but would like to change the color across the texture it is using on a pixel basis similar to how I am with other textures. I was able to find a StackOverflow post that goes over dynamically creating a CubeMap from a source Texture2D. I use the linked code and transcribed the above shader code to c# with semi-successful results, just generating a new CubeMap took a few minutes. Is there a way to utilize the above snippet on a CubeMap to alter not just the Tint, but the color on a pixel-by-pixel basis to achieve a similar effect I have elsewhere using a surface shader?
     
  2. TEEBQNE

    TEEBQNE

    Joined:
    Jan 25, 2017
    Posts:
    88