如何解决我试图实现透明度,但出了点问题
我想在我的游戏引擎中添加“透明度”,而我以前并不了解。我没有找到关于如何实现它的直接答案,所以我最终做了一些关于 Alpha Blending 的研究,如果我了解的话,这是显示不透明性的一种方法。没错。
我到处搜索,试图找到一个显示像素阵列时如何实现此功能的资源。我不知道怎么做,但除了关于YouTube的教程之外,我什么也没找到。他们没有解释为什么像他们那样做,并且由于这个原因,我仍然不知道如何实现它或如何工作。我尝试按照该教程进行操作,但是他们使用的代码根本不起作用,因此我对其进行了一些更改(显然不起作用)。
下面的代码是我的 setPixel()函数,该函数在指定位置设置像素。从功能开始,它只是检查是否需要放置一个像素。此功能用于从像素数据中绘制每个单独的像素。屏幕的像素数据存储在变量 pixels 中。图片数据以值存储。但是,值只是一个整数,而像素是一个数组。
public void setPixel(int x,int y,int value,Color invis) {
int alpha = (value>>24);
if(invis != null && value == invis.getRGB() || alpha == 0x00) {
return;
}
if(!isOutSideScreen(x,y)) {
if(alpha == 255) {
pixels[x + y * pWidth] = value;
}
else {
int pixelColor = value;
int newRed = ((pixelColor >> 16) & 0xff) + (int)((((pixelColor >> 16) & 0xff) - ((pixels[x + y * pWidth] >> 16) & 0xff)) * (alpha/255f));
int newGreen = ((pixelColor >> 8) & 0xff) + (int)((((pixelColor >> 8) & 0xff) - ((pixels[x + y * pWidth] >> 8) & 0xff)) * (alpha/255f));
int newBlue = (pixelColor & 0xff) + (int)(((pixelColor & 0xff) - (pixels[x + y * pWidth] & 0xff)) * (alpha/255f));
pixels[x+y * pWidth] = ((255 << 24) | (newRed << 16) | (newGreen << 8) | newBlue);
}
}
}
我对这段代码不了解的是所有按位代码,以及为什么要计算这样的颜色。
int newRed = ((pixelColor >> 16) & 0xff) + (int)((((pixelColor >> 16) & 0xff) - ((pixels[x + y * pWidth] >> 16) & 0xff)) * (alpha/255f));
int newGreen = ((pixelColor >> 8) & 0xff) + (int)((((pixelColor >> 8) & 0xff) - ((pixels[x + y * pWidth] >> 8) & 0xff)) * (alpha/255f));
int newBlue = (pixelColor & 0xff) + (int)(((pixelColor & 0xff) - (pixels[x + y * pWidth] & 0xff)) * (alpha/255f));
pixels[x+y * pWidth] = ((255 << 24) | (newRed << 16) | (newGreen << 8) | newBlue);
如果有人可以解释为什么此代码不起作用以及它实际上如何起作用,我将永远感激不已!
在此先感谢您的无知!
在乔尼回答后编辑
这是我现在使用的代码:
int pixelColor = pixels[x+y * pWidth];
int newRed = (int)((1 - (alpha / 255f)) * ((pixelColor>>16) & 0xff) + (alpha / 255f) * ((value >> 16) & 0xff));
int newGreen = (int)((1 - (alpha / 255f)) * ((pixelColor>>8) & 0xff) + (alpha / 255f) * ((value >> 8) & 0xff));
int newBlue = (int)((1 - (alpha / 255f)) * (pixelColor & 0xff) + (alpha / 255f) * (value & 0xff));
pixels[x+y * pWidth] = ((255 << 24) | (newRed << 16) | (newGreen << 8) | newBlue);
我使用了公式:outColor = (1 - alpha) * backgroundColor + alpha * newColor
解决方法
在“ backgroundColor”顶部的alpha blending“ newColor”的公式为:
outColor = (1 - alpha) * backgroundColor + alpha * newColor
要了解其工作原理,请尝试不同的alpha值。当alpha = 0时,您将获得背景色。使用alpha = 1,您将获得newColor。
您编写的公式是
outColor = backgroundColor + alpha * (backgroundColor - newColor)
Alpha = 1给您outColor = 2*backgroundColor-newColor
,这是不正确的。要解决此问题,您需要左右交换pixelColor
和pixels[x+y*pWidth]
-例如蓝色通道:
int newBlue = (pixelColor & 0xff) + (int)(((pixels[x + y * pWidth] & 0xff) - (pixelColor & 0xff)) * (alpha/255f));
我对这段代码不了解的是所有按位代码,以及为什么要计算这样的颜色。
此代码假定一个颜色模型将四个8位整数打包为一个int
。最高的8位组成alpha分量,其后分别是红色,绿色和蓝色的8位。从int
中提取组件的方式是按位运算符。例如,color&0xff
是最低的8位,因此它是蓝色分量。 (color>>8)&0xff
为您提供倒数第二低的8位,即绿色部分。
屏幕的像素数据存储在可变像素中。
不知道这是否有帮助,但是您可以使用以下内容从像素数组创建BufferedImage:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class ImageFromArray3 extends JFrame
{
int width = 50;
int height = 50;
int imageSize = width * height;
public ImageFromArray3()
{
JPanel panel = new JPanel();
getContentPane().add( panel );
int[] pixels = new int[imageSize];
// Create Red Image
for (int i = 0; i < imageSize; i++)
{
//pixels[i] = 255 << 16; // no alpha
pixels[i] = (64 << 24 ) + (255 << 16);
}
panel.add( createImageLabel(pixels) );
// Create Green Image
for (int i = 0; i < imageSize; i++)
{
//pixels[i] = 255 << 8;
pixels[i] = (128 << 24 ) + (255 << 8);
}
panel.add( createImageLabel(pixels) );
// Create Blue Image
for (int i = 0; i < imageSize; i++)
{
//pixels[i] = 255;
pixels[i] = (192 << 24 ) + (255);
}
panel.add( createImageLabel(pixels) );
// Create Cyan Image
for (int i = 0; i < imageSize; i++)
{
//pixels[i] = (255 << 8) + 255;
pixels[i] = (255 << 24 ) + (255 << 8) + (255);
}
panel.add( createImageLabel(pixels) );
}
private JLabel createImageLabel(int[] pixels)
{
//BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RRGB);
BufferedImage image = new BufferedImage(width,BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = image.getRaster();
raster.setDataElements(0,width,pixels);
JLabel label = new JLabel( new ImageIcon(image) );
return label;
}
public static void main(String args[])
{
JFrame frame = new ImageFromArray3();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
每种颜色使用不同的alpha值。
现在您有了BufferedImage,因此您应该可以使用AlphaComposite。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。