ShapeRenderer使用LibGDX生成像素化形状

use*_*001 2 java libgdx

当我使用ShapeRenderer时,它总是像素化。但是,如果我在Photoshop中以相同的尺寸绘制形状,则该形状非常平滑且干净。

我的方法如下:

package com.me.actors;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class bub_actors extends Actor {
    private ShapeRenderer shapes;
    private Texture text;
    private Sprite sprite;

    public bub_actors(){
        shapes = new ShapeRenderer();
        text = new Texture(Gdx.files.internal("data/circle.png"));
        sprite = new Sprite();
        sprite.setRegion(text);
    }
    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
            batch.draw(sprite, 200, 200, 64, 64);
            shapes.begin(ShapeType.FilledCircle);
            shapes.filledCircle(50, 50, 32);
            shapes.setColor(Color.BLACK);
            shapes.end();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是输出的图像:

在此处输入图片说明

有什么想法为什么会这样?是否可以使ShapeRenderer看起来像图像(所以我不必创建不同颜色的圆的SpriteBatch ...)。

P.T*_*.T. 7

区别在于,抗锯齿功能是将Photoshop应用于其生成的图像。如果放大两个圆的边缘,则会看到抗锯齿的ShapeRenderer圆的边缘周围有一些半黑色像素,其中生成的圆仅显示完全打开或关闭的像素。

抗锯齿图像

锯齿图像

Libgdx的ShapeRenderer设计是一种用于在屏幕上调试形状的快速简单的方法,它不支持抗锯齿。获得一致的抗锯齿渲染以使用纹理的最简单方法。(也可以使用OpenGL着色器。)

就是说,您不必为了渲染不同的彩色圆圈而创建不同的精灵。只需使用带有透明背景的白色圆圈,然后使用color进行渲染即可。(假设您需要各种纯色圆圈)。


小智 6

Here is really simple way to achieve smooth & well-looking shapes without using a texture and SpriteBatch.

All you have to do is to render couple of shapes with slightly larger size and lower alpha channel along with the first one.

The more passes the better result, but, of course, consider ppi of your screen.

...
float alphaMultiplier = 0.5f; //you may play with different coefficients
float radiusStep = radius/200;
int sampleRate = 3;
...

//do not forget to enable blending
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

shapeRenderer.begin(ShapeType.Filled);

//first rendering
shapeRenderer.setColor(r, g, b, a);
shapeRenderer.circle(x, y, radius);

//additional renderings
for(int i=0; i<sampleRate; i++) {
    a *= alphaMultiplier;
    radius += radiusStep;
    shapeRenderer.setColor(r, g, b, a);
    shapeRenderer.circle(x, y, radius);
}

shapeRenderer.end();
...
Run Code Online (Sandbox Code Playgroud)

Here is a screenshot of what can you achieve.