我想改变我在团结场景中上传的图像的亮度。这是我目前的剧本:
public void AdjustBrightness(string brightness)
{
int brightnessInt = Convert.ToInt32(brightness);
int mappedBrightness = (51 * brightnessInt) / 10 - 255;
//Make an empty Texture the same same as the original
Texture2D bitmapImage = new Texture2D(imgTexture.width, imgTexture.height);
if (mappedBrightness < -255) mappedBrightness = -255;
if (mappedBrightness > 255) mappedBrightness = 255;
Color color;
for (int i = 0; i < bitmapImage.width; i++)
{
for (int j = 0; j < bitmapImage.height; j++)
{
color = bitmapImage.GetPixel(i, j);
int cR = (int)color.r + mappedBrightness;
int cG = (int)color.g + mappedBrightness;
int cB = (int)color.b + mappedBrightness;
if (cR < 0) cR = 0;
if (cR > 255) cR = 255;
if (cG < 0) cG = 0;
if (cG > 255) cG = 255;
if (cB < 0) cB = 0;
if (cB > 255) cB = 255;
bitmapImage.SetPixel(i, j,
new Color((float)cR, (float)cG, (float)cB));
}
}
//Apply all SetPixel changes
bitmapImage.Apply();
//Connect texture to material of GameObject this script is attached to
Image.renderer.material.mainTexture = bitmapImage as Texture;
}
出于某种原因,此脚本将我的图像完全更改为黑色。然后,如果我将值增加到某个点,图像将变为白色。
我在这里正确计算亮度吗?
顺便说一下,我在开始时将亮度值从当前输入值范围映射到0%到100%到-255到255之间,以使用此脚本。这样好吗?也就是说,有一个输入字段可以输入0到100的亮度值,并且使用这个公式,我必须将50%的值映射到-255到255的范围/范围内的50%意味着什么?
答案 0 :(得分:1)
这里我稍微修改了你的脚本,尝试30到60%之间它仍然与你的脚本相同的比例只是颜色在0和1之间浮动,你如何做数学它是0黑色或1白色试试这个当你应该得到imgTexture的像素时,你得到你的图像的像素你得到像素的imgTexture我也把alpha放入它只是因为你想使用带有alpha的图片因为它不应该随着亮度而改变
我认为你应该保留Texture2D的第三个副本,这样你每次都可以恢复它
public void AdjustBrightness(string brightness)
{
int brightnessInt = Convert.ToInt32(brightness);
float mappedBrightness = (51 * brightnessInt) / 10 - 255;
//Make an empty Texture the same same as the original
Texture2D bitmapImage = new Texture2D(imgTexture.width, imgTexture.height);
if (mappedBrightness < -255) mappedBrightness = -255;
if (mappedBrightness > 255) mappedBrightness = 255;
Color color;
for (int i = 0; i < bitmapImage.width; i++)
{
for (int j = 0; j < bitmapImage.height; j++)
{
color = imgTexture.GetPixel(i, j);
float cR;
float cG;
float cB;
float cA;
if(color.a == 1f){
cR = color.r + (mappedBrightness/255);
cG = color.g + (mappedBrightness/255);
cB = color.b + (mappedBrightness/255);
cA = color.a;
if (cR < 0) cR = 0;
if (cR > 255) cR = 255;
if (cG < 0) cG = 0;
if (cG > 255) cG = 255;
if (cB < 0) cB = 0;
if (cB > 255) cB = 255;
}else{
cR = color.r;
cG = color.g;
cB = color.b;
cA = color.a;
}
bitmapImage.SetPixel(i, j,
new Color(cR, cG, cB,cA));
}
}
//Apply all SetPixel changes
bitmapImage.Apply();
//Connect texture to material of GameObject this script is attached to
imgTexture = bitmapImage;
}
答案 1 :(得分:0)
我假设您正在使用this Color
class。它的成员既不是float
也不是int
,而是byte
从0到255。
我认为当您改变亮度时,您应该缩放 r
,g
和b
相同的因素,而不是向他们添加相同的项目。试试这段代码:
public void AdjustBrightness(string brightnessString)
{
float brightnessInput = Convert.ToFloat(brightnessString);
if (brightnessInput < 0) brightnessInput = 0;
if (brightnessInput > 100) brightnessInput = 100;
float brightnessFactor = (brightnessInput/50.)*(brightnessInput/50.);
//Make an empty Texture the same same as the original
Texture2D bitmapImage = new Texture2D(imgTexture.width, imgTexture.height);
Color color;
for (int i = 0; i < bitmapImage.width; i++)
{
for (int j = 0; j < bitmapImage.height; j++)
{
color = bitmapImage.GetPixel(i, j);
int cR = int(color.r * brightnessFactor);
int cG = int(color.g * brightnessFactor);
int cB = int(color.b * brightnessFactor);
if (cR > 255) cR = 255;
if (cG > 255) cG = 255;
if (cB > 255) cB = 255;
bitmapImage.SetPixel(i, j, Color.FromArgb(cR, cG, cB));
}
}
//Apply all SetPixel changes
bitmapImage.Apply();
//Connect texture to material of GameObject this script is attached to
Image.renderer.material.mainTexture = bitmapImage as Texture;
}
使用FromArgb()
保存必须进行投射。
此外,使用.SetPixel()
非常慢。有关更快速推送像素的方法,请参阅http://www.codeproject.com/Articles/617613/Fast-pixel-operations-in-NET-with-and-without-unsa。