Libgdx 着色器优化

诺杰索

大家好,使用 libgdx!

由于着色器,我在游戏中有 40 fps。这个着色器绘制背景,我想减少它处理的像素数。

在 Create 方法中,我使用像素图生成纹理,其宽度和高度为实际屏幕尺寸的 1/10。

pixmap = new Pixmap( (int)(Math.ceil(WIDTH/10f)), (int)(Math.ceil(HEIGHT/10f)), Pixmap.Format.RGBA8888 );
backGround = new Texture(pixmap);

然后我将 camera.zoom 设置为 0.1,这样纹理现在是全屏的。

camera.position.set( WIDTH/2f*0.1f, HEIGHT/2f*0.1f, 0);
camera.zoom = 0.1f;
camera.update();
batch.setProjectionMatrix(camera.combined);

然后 Render 方法中的着色器绘制一个圆,但该着色器处理屏幕的像素而不是纹理的像素,我得到了像以前一样漂亮和平滑的图片。

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.draw(backGround,0,0);

片段着色器的代码:

vec3 yellow = vec3(0.93, 0.93, 0.32);

vec2 relativePosition = gl_FragCoord.xy / u_ScreenResolution;
relativePosition.x *= u_ScreenResolution.x / u_ScreenResolution.y;
float normDistance = pow(   (  pow(relativePosition.x - 0.3,2.0) + pow(relativePosition.y - 0.5,2.0)  ), 0.5   );
int distance = int(  round( normDistance + 0.4  )  );
gl_FragColor = vec4(vec3(0.0) + yellow*clamp(1.0-distance, 0.0, 1.0), 1.0);

不确定我是否正确表达了这个想法,所以这是一张图片:

我画的图

伦敦獾

"that shader processes screen's pixel instead of texture's ones" <- this

You have a texture which is backed by the pixmap (of a lower resolution), but you have not drawn to that lower resolution pixmap first. What happens is that what you see on screen is what is calculated by the shader on the basis of -screen coordinates-. i.e. your pixmap resolution is irrelevant because what you get is calculated by the shader and not derived from the pixmap.

The effect you want needs the lower resolution texture backed by pixmap to be drawn to first with your shader, and then that -low resolution rendered texture- be scaled up to the screen using the default shader which maps to the texture.

使用帧缓冲区,它实际上是一个屏幕,除了在内存中实际上不在屏幕上,允许 openGL 将您的低分辨率像素图视为屏幕,首先绘制到该屏幕,然后使用生成的块状纹理作为纹理缩放到屏幕。这是

FrameBuffer fbo = new FrameBuffer(Format.RGB888, screenwidth, screenheight, false);
fbo.begin();
//render here
fbo.end();
//grab and use as texture scaled to the full  screen

这里有一个例子

LibGDX 帧缓冲

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章