我正在製作一款具有篝火對象的遊戲。我想要做的是讓每個篝火周圍的所有像素變亮。但是,遍歷每個像素並更改半徑範圍內的像素並不是那麼有效,並且使遊戲運行速度達到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