以与 Unity 相同的方式为 Texture2D 着色

Tint a Texture2D in the same way Unity does

我正在尝试以与 sprite 渲染器相同的方式在 Unity3D 中使用 Color 为 Texture2D 着色。 执行此操作的最佳方法是什么?

编辑: 这是我试过的代码:

  private Color Tint(Color source, Color tint) {
    Color tinted = source;
    //tinted.r = Math.Min(Math.Max(0f, (source.r + (255f - source.r) * tint.r)), 255f);
    //tinted.g = Math.Min(Math.Max(0f, (source.g + (255f - source.g) * tint.g)), 255f);
    //tinted.b = Math.Min(Math.Max(0f, (source.b + (255f - source.b) * tint.b)), 255f);

    //tinted.r = (float)Math.Floor(source.r * tint.r / 255f);
    //tinted.g = (float)Math.Floor(source.g * tint.g / 255f);
    //tinted.b = (float)Math.Floor(source.b * tint.b / 255f);

    Color.RGBToHSV(source, out float sourceH, out float sourceS, out float sourceV);
    Color.RGBToHSV(tint, out float tintH, out float tintS, out float tintV);
    tinted = Color.HSVToRGB(tintH, tintS, sourceV);
    tinted.a = source.a;
    return tinted;
  }

Afaik Renderer 的色调通常是通过乘法完成的(黑色保持黑色)并且可以简单地使用 * operator

来实现
private Color Tint(Color source, Color tint) 
{
    return source * tint;
}

更灵活的方法是使用 lerp

Color original = Color.red;
Color tint = Color.cyan;
float tintPercentage = 0.3f;

// This will give you original color tinted by tintPercentage
UnityEngine.Color.Lerp(original, tint, tintPercentage);

// this will give you original color
UnityEngine.Color.Lerp(original, tint, 0f);

// this will give you full tint color
UnityEngine.Color.Lerp(original, tint, 1f);