java - 如何绘制像素化形状(LIBGDX 概念)
问题描述
我想以像素化风格绘制形状。我目前使用 shaperenderer 绘制的圆圈以真实像素呈现。
但我喜欢它更像素化。
我玩弄了相机和视口来获得效果,但没有成功。我更喜欢使用 FillViewport。
我们是否需要创建自己的 drawLine 函数并沿着一条线绘制小方块?或者这可以通过保持纵横比的视口来完成吗?
是否有任何小型示例项目显示如何创建像素化的简单形状。
我知道我可以自己编码,但我想避免在使用框架时。
在 skretch 中,您可以画一条线,只需指定像素大小,非常简单。
有什么提示吗?
public class SimpleCirleActor extends Actor {
ShapeRenderer renderer;
float radius;
public SimpleCirleActor(int x , int y, float radius) {
setBounds(0,0, Constants.pixelsPerSector(),Constants.pixelsPerSector());
this.radius = radius;
setX(x);setY(y);
renderer = new ShapeRenderer();
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
renderer.setProjectionMatrix(getStage().getCamera().combined);
renderer.begin(ShapeRenderer.ShapeType.Line);
renderer.setColor(Color.CYAN);
float x = getX();
renderer.circle(x, getY(), radius);
renderer.end();
}
@Override
public void act(float delta) {
super.act(delta);
}
}
解决方案
好吧,无聊了。干得好。
笔记:
绿色框只是为了显示帧缓冲区纹理被绘制到的位置,只需在 fbo.begin() 语句之后将填充颜色更改为零 alpha 以使其清晰。
BINDING_DEPTH_FRAMEBUFFER,BINDING_DEFAULT 变量来自我的代码。你可能不需要担心这些,除非你在切换纹理和使用定制着色器方面搞砸了。
绘制 fbo,batch.draw(fbo.getColorBufferTexture(), 20, 20, 200, 200, 0, 0, 1, 1); 希望这对你来说没问题。如果你想旋转 fbo,你应该能够从你的相机创建一个旋转的投影矩阵。如果您想使用定制着色器,您只需小心将 fbo 作为纹理链接到着色器。
package com.funferret.pixelshapes;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.ScreenUtils;
public class MyGdxGame extends ApplicationAdapter {
ShapeRenderer shapeBatch;
SpriteBatch batch;
Texture img;
FrameBuffer fbo;
public OrthographicCamera fboCamera;
int fboWidth=40;
int fboHeight=40;
int BINDING_DEPTH_FRAMEBUFFER=3;
int BINDING_DEFAULT=0;
void createFrameBuffer()
{
if (fbo!=null)
{
fbo.dispose();
}
fbo = new FrameBuffer(Pixmap.Format.RGBA8888, fboWidth, fboHeight, false);
fbo.getColorBufferTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
fbo.getColorBufferTexture().bind(BINDING_DEPTH_FRAMEBUFFER);
Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE0 + BINDING_DEFAULT); //make sure this is set to 0 for batch draws
fboCamera=new OrthographicCamera();
fboCamera.setToOrtho(false, fboWidth, fboHeight);
fboCamera.update();
}
@Override
public void create () {
batch = new SpriteBatch();
shapeBatch=new ShapeRenderer();
img = new Texture("badlogic.jpg");
}
@Override
public void render ()
{
//I had some initialisation problems with the fbo when trying to create a framebuffer. Need to be absolutely certain
//size is correct and opengl context is set up before creating the framebuffer (as far as I remember). This if statement and the
//createFrameBuffer function works for me and will allow you to change the size of fboWidth/fboHeight on the fly (though will cause stutters if you do)
if ((fbo==null) || (fbo.getWidth()!=fboWidth) || (fbo.getHeight()!=fboHeight))
{
createFrameBuffer();
}
//start drawing to a small framebuffer, that we will then scale up to create a pixelated effect
fbo.begin();
ScreenUtils.clear(0, 1, 0, 0.5f);
shapeBatch.begin(ShapeRenderer.ShapeType.Line);
shapeBatch.setProjectionMatrix(fboCamera.combined);
Gdx.gl.glDisable(GL20.GL_BLEND);
shapeBatch.setColor(1f, 0f, 1f, 1);
shapeBatch.circle(20,20,15,10);
shapeBatch.end();
fbo.end();
ScreenUtils.clear(1, 0, 0, 1);
batch.begin();
batch.draw(img, 0, 0);
batch.draw(fbo.getColorBufferTexture(), 20, 20, 200, 200, 0, 0, 1, 1);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
shapeBatch.dispose();
fbo.dispose();
}
}
推荐阅读
- windows - InnoSetup - unisXXX.exe 文件的设置图标
- python - 无法使用 Flask Python 下载 CSV 文件
- python - 从python中的图中获取特定数据
- azure-active-directory - 创建应用程序网关并在网关上应用 Web 应用程序防火墙策略所需的 Azure IAM 角色
- php - DOMPDF - PHP 页面到 PDF
- spring-boot - Map的Bean之间的区别
与地图 - python - nltk 停用词的 for 循环进入无穷大
- javascript - Next.js 页面尺寸比视口大得多
- google-bigquery - BigQuery 包含语句
- reactjs - setState 没有发生。如何解决这个问题?