【发布时间】:2019-08-18 12:12:35
【问题描述】:
所以我尝试使用函数 GetPixels 基本上根据第一个(或源)纹理的像素颜色来更改第二个纹理的颜色。
我没有尝试太多,我一直在寻找方法来解决这个问题或获取一些文档,但 Unity Scripting API 相当混乱。
到目前为止,我有一个根本不工作的伪代码,如果你们中的任何人可以帮助让它工作,那将是非常好的。
var secondTexture = new Texture2D(width, height);
Texture2D source = sourceTexture;
var pixels = new Color[width * height];
for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
{
Color pixels2 = source.GetPixels(x, y);
if(pixels2 == Color.white)
{
//Paint the pixels in secondTexture that match X and Y of sourceTexture with blue per example
pixels[x + y * width] = Color.blue;
}
else
{
//Paint the rest of the pixels with black
pixels[x + y * width] = Color.black;
}
}
}
secondTexture.SetPixels(pixels);
secondTexture.wrapMode = TextureWrapMode.Clamp;
secondTexture.Apply();
return secondTexture;
所以基本上,应该发生的是,每当 sourceTexture 的像素是白色时,第二个纹理中的像素就会变为蓝色,并且当 sourceTexture 中的像素不是白色时,第二个纹理上的像素是黑色的。这或多或少是个想法,但是我不断得到的东西并不重要,sourceTexture 颜色是什么,我在第二个纹理中的所有像素都是黑色的。知道为什么吗?
【问题讨论】:
标签: unity3d textures pixel texture2d