您的纹理可以是 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 上下文丢失,纹理将无法自动重新加载。