以与Unity相同的方式对Texture2D进行着色

问题描述 投票:0回答:3

我正在尝试使用 Unity3D 中的颜色对 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;
  }
unity-game-engine colors sprite texture2d tint
3个回答
0
投票

据我所知,

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

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

0
投票

更灵活的方法是使用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);

0
投票

我知道我在这里参加聚会迟到了,但是对于任何在 2023 年(或以后)寻找简单方法来做到这一点的人来说......这里有一个函数,它将返回新着色的纹理:

public Texture2D TintTexture(Texture2D texture2D, Color tint)
{
    // cache variables
    int width = texture2D.width;
    int height = texture2D.height;
    Color[] pixels = new Color[width * height];
    int pixelIndex = 0;

    // iterate through each pixel
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            pixels[pixelIndex] = texture2D.GetPixel(j, i) * tint;
            pixelIndex++;
        }
    }

    // build a new texture from the pixels
    Texture2D result = new Texture2D(width, height);
    result.SetPixels(pixels);
    result.Apply();
    
    return result;
}
© www.soinside.com 2019 - 2024. All rights reserved.