我正在制作一款具有篝火对象的游戏。我想要做的是让每个篝火周围的所有像素变亮。但是,遍历每个像素并更改半径范围内的像素并不是那么有效,并且使游戏运行速度达到7 fps。关于如何使这个过程高效或以不同方式模拟光的想法?Java中的2D动态照明
我没有写对火灾的代码,但这个是基本的循环检查每个像素/更改基于它的一些亮度:
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;
}
我道歉,如果我的代码是不是干净,我自我教导。
这个问题似乎是题外话,因为它更适合于http://codereview.stackexchange.com/ – jonrsharpe
不过,我会建议你考虑对称性:如果在像素( x,y)在以(0,0)为中心的圆内,因此是(-x,y),(x,-y)和(-x,-y)。 – jonrsharpe
不一定jonrsharpe,我认为替代方法在这里更重要,而不仅仅是代码打高尔夫他的实现。 – SpacePrez