【发布时间】:2014-01-20 11:12:26
【问题描述】:
我正在制作一个包含篝火对象的游戏。我想要做的是照亮每个篝火周围圆圈中的所有像素。但是,循环遍历每个像素并更改半径内的像素并不是那么有效,并且会使游戏以约 7 fps 的速度运行。关于如何提高此过程效率或以不同方式模拟光线的想法?
我还没有为火灾编写代码,但这是检查每个像素/根据数字更改其亮度的基本循环:
public static BufferedImage updateLightLevels(BufferedImage img, float light)
{
BufferedImage brightnessBuffer = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
brightnessBuffer.getGraphics().drawImage(img, 0, 0, null);
for(int i = 0; i < brightnessBuffer.getWidth(); i++)
{
for(int a = 0; a < brightnessBuffer.getHeight(); a++)
{
//get the color at the pixel
int rgb = brightnessBuffer.getRGB(i, a);
//check to see if it is transparent
int alpha = (rgb >> 24) & 0x000000FF;
if(alpha != 0)
{
//make a new color
Color rgbColor = new Color(rgb);
//turn it into an hsb color
float[] hsbCol = Color.RGBtoHSB(rgbColor.getRed(), rgbColor.getGreen(), rgbColor.getBlue(), null);
//lower it by the certain amount
//if the pixel is already darker then push it all the way to black
if(hsbCol[2] <= light)
hsbCol[2] -= (hsbCol[2]) - .01f;
else
hsbCol[2] -= light;
//turn the hsb color into a rgb color
int rgbNew = Color.HSBtoRGB(hsbCol[0], hsbCol[1], hsbCol[2]);
//set the pixel to the new color
brightnessBuffer.setRGB(i, a, rgbNew);
}
}
}
return brightnessBuffer;
}
如果我的代码不干净,我很抱歉,我是自学的。
【问题讨论】:
-
这个问题似乎离题了,因为它更适合codereview.stackexchange.com
-
但是,我建议您考虑对称性:如果 (x, y) 处的像素在以 (0, 0) 为中心的圆内,那么 (-x, y), (x , -y) 和 (-x, -y)。
-
不一定是 jonrsharpe ,我认为这里的替代方法比代码打他的实现更重要。