显示弹出窗口时将灰色应用于非活动屏幕
Apply the grey colour to inactive screen when showing popup
我试图在基于 LibGdx 的 Android 游戏中显示弹出屏幕。这样做时,我必须模糊应用灰色到显示弹出窗口时处于非活动状态的背景屏幕。
截至目前为了实现这一点,我创建了一个黑色背景和 alpha 70% 的纹理。此纹理绘制在主背景之上,因此实现了。
但是上述方法需要一个和主背景一样大的Texture。它也增加了应用程序和渲染方法的重量。
请帮我一个合适的方法。
我的需求示例图片如下。这是我在 LibGdx 中想要的效果,而不是特定于 android
您可以将背景设置为半透明黑色,如#50000000
您的纹理可以是 1x1 的白色图像,您可以将其拉伸以填满屏幕。 (白色所以它更通用。)如果你不想计算如何相对于相机所在的位置拉伸它,你可以这样做:
private static final Matrix4 IDT = new Matrix4();
batch.setProjectionMatrix(camera.combined);
batch.begin();
//draw stuff in background
batch.setProjectionMatrix(IDT);
batch.setColor(0, 0, 0, 0.7f);
batch.draw(singlePixelTexture, -1, -1, 2, 2); //coordinates and size of the screen when identity projection matrix is used
batch.setProjectionMatrix(camera.combined);
//draw your front window
batch.end();
但是,您也可以单独保留投影矩阵,而是计算背景应该在何处以及有多大以使用现有矩阵填充屏幕:
float camX = camera.position.x;
float camY = camera.position.y;
float camWidth = camera.viewportWidth;
float camHeight = camera.viewportHeight;
batch.setProjectionMatrix(camera.combined);
batch.begin();
//draw stuff in background
batch.setColor(0, 0, 0, 0.7f);
batch.draw(singlePixelTexture, camX - camWidth/2, camY - camHeight/2, camWidth, camHeight);
//draw your front window
batch.end();
仅供参考,虽然大纹理确实使用更多内存和加载时间,但它不会减慢渲染速度,除非它在没有 mip 映射的情况下被缩小绘制。
编辑:如何在运行时生成单像素纹理:
singlePixelPixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
singlePixelPixmap.setColor(1, 1, 1, 1);
singlePixelPixmap.fill();
PixmapTextureData textureData = new PixmapTextureData(singlePixelPixmap, Pixmap.Format.RGBA8888, false, false, true);
singlePixelTexture = new Texture(textureData);
singlePixelTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
请注意,像素图必须在 dispose
方法中处理,但您不希望在此之前执行此操作,否则如果出现 GL 上下文丢失,纹理将无法自动重新加载。
我试图在基于 LibGdx 的 Android 游戏中显示弹出屏幕。这样做时,我必须模糊应用灰色到显示弹出窗口时处于非活动状态的背景屏幕。
截至目前为了实现这一点,我创建了一个黑色背景和 alpha 70% 的纹理。此纹理绘制在主背景之上,因此实现了。
但是上述方法需要一个和主背景一样大的Texture。它也增加了应用程序和渲染方法的重量。
请帮我一个合适的方法。
我的需求示例图片如下。这是我在 LibGdx 中想要的效果,而不是特定于 android
您可以将背景设置为半透明黑色,如#50000000
您的纹理可以是 1x1 的白色图像,您可以将其拉伸以填满屏幕。 (白色所以它更通用。)如果你不想计算如何相对于相机所在的位置拉伸它,你可以这样做:
private static final Matrix4 IDT = new Matrix4();
batch.setProjectionMatrix(camera.combined);
batch.begin();
//draw stuff in background
batch.setProjectionMatrix(IDT);
batch.setColor(0, 0, 0, 0.7f);
batch.draw(singlePixelTexture, -1, -1, 2, 2); //coordinates and size of the screen when identity projection matrix is used
batch.setProjectionMatrix(camera.combined);
//draw your front window
batch.end();
但是,您也可以单独保留投影矩阵,而是计算背景应该在何处以及有多大以使用现有矩阵填充屏幕:
float camX = camera.position.x;
float camY = camera.position.y;
float camWidth = camera.viewportWidth;
float camHeight = camera.viewportHeight;
batch.setProjectionMatrix(camera.combined);
batch.begin();
//draw stuff in background
batch.setColor(0, 0, 0, 0.7f);
batch.draw(singlePixelTexture, camX - camWidth/2, camY - camHeight/2, camWidth, camHeight);
//draw your front window
batch.end();
仅供参考,虽然大纹理确实使用更多内存和加载时间,但它不会减慢渲染速度,除非它在没有 mip 映射的情况下被缩小绘制。
编辑:如何在运行时生成单像素纹理:
singlePixelPixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
singlePixelPixmap.setColor(1, 1, 1, 1);
singlePixelPixmap.fill();
PixmapTextureData textureData = new PixmapTextureData(singlePixelPixmap, Pixmap.Format.RGBA8888, false, false, true);
singlePixelTexture = new Texture(textureData);
singlePixelTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
请注意,像素图必须在 dispose
方法中处理,但您不希望在此之前执行此操作,否则如果出现 GL 上下文丢失,纹理将无法自动重新加载。