Java libgdx 1.20版本纹理错误 [英] Java libgdx 1.20 version texture bug

查看:76
本文介绍了Java libgdx 1.20版本纹理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2天内,我一直在尝试修复游戏中闪烁和变形的纹理的错误.我当时在网上闲逛,尝试了一些解决方案,例如使用scene2d,但没有用.我该怎么办?

For 2 days I've been trying to fix bug with fickering and distored textures in my game. I was serching on the internet and i tried a few solutions like using scene2d, but it didn't work. What should i do ?

此屏幕快照显示了问题:随着角色移动,一只眼睛有时比另一只眼睛大:

This screenshot shows the problem: as the character moves, one eye is sometimes bigger than the other:

当我使用sprite.setPosition((int)sprite.getX(),(int)sprite.getY())时,我仍然有宽眼问题.每次我渲染角色之前.

I still got the problem widthdistored eye when i use sprite.setPosition((int) sprite.getX(), (int) sprite.getY()); every time before i render my character.

当我从答案中使用自定义视口时,我在游戏窗口上什么都看不到,我做错了什么?

When i use custom viewport from the answer i see nothing on the game window what i do wrong?

package com.mygdx.redHoodie;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.StretchViewport;

public class GameScreen implements Screen {
    public static final int GAME_WIDTH = 800;
    public static final int GAME_HEIGHT= 480 ;

    SpriteBatch batch;
    Background background;
    public Hoodie hoodie;

    public PixelMultipleViewport viewport;
       OrthographicCamera camera;
    public int gameMode; // 0 normalna gra, 1 level up, 2 end game

    public GameScreen(){
         camera= new OrthographicCamera(GAME_WIDTH,GAME_HEIGHT);


         viewport = new PixelMultipleViewport(GAME_WIDTH, GAME_HEIGHT, camera);
         viewport.update();
         camera.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT);
        batch = new SpriteBatch();

        //klasy wyswietlane
        background= new Background(this);
        hoodie = new Hoodie(this);

        startNewGame();
    }

    @Override
    public void render(float delta) {
        // TODO Auto-generated method stu

         Gdx.gl.glClearColor(1, 1, 0, 1);

         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
         batch.setProjectionMatrix(camera.projection);
            batch.setTransformMatrix(camera.view);

         camera.update();
         //batch.setProjectionMatrix(camera.combined);
         this.update(delta);
         this.batch.begin();
            toRender(delta);
         this.batch.end();



    }

    public void update(float delta){
        hoodie.update(delta);
    }

    public void toRender(float delta){
        background.render();
        hoodie.render();

    }

    public void startNewGame(){

    }
    public void startNevLevel(){

    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
        viewport.update(width, height,false);
    }

    @Override
    public void show() {
        // TODO Auto-generated method stub

    }

    @Override
    public void hide() {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

}

推荐答案

加载纹理时,请使用线性过滤和mip映射.默认过滤器是最近/最近",这将导致您看到的问题.

When loading your texture, use linear filtering and mip-mapping. The default filter is Nearest/Nearest, which will cause the issue you're seeing.

Texture myTexture = new Texture("textureFilename", true); //must enable mip-mapping in constructor
myTexture.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Linear);

我现在看到您的屏幕截图,您正在一个更大的窗口中进行像素化图形处理.为此,是的,您需要保留最近/最近"过滤,而不是我的建议.

I realize now, looking at your screenshot, that you are doing pixelated graphics in a larger window. In order to do this, yes you need to keep the Nearest/Nearest filtering, instead of what I suggested.

为避免某些像素的大小发生变化,必须将角色移动和相机移动四舍五入到最近的世界单位.当角色位于像素之间时,精灵像素的大小会发生变化,因为它们与屏幕像素不对齐.

To avoid having the some of the pixels vary in size, you must round off character movement and camera movement to the nearest world unit. When your character is partway between pixels, the size of the sprite pixels varies because they don't line up with the screen pixels.

您已缩放世界,因此一个单位等于大像素之一.因此,无论何时绘制任何东西,都需要首先将其位置四舍五入为x和y中最接近的整数,以及摄像机的位置.因此,在移动摄像机或精灵后,您必须执行以下操作:

You have your world scaled so one unit equals one of your large pixels. So whenever you draw anything, you need to first round its position to the nearest integer in the x and the y, as well as the camera position. So after you move the camera or the sprites, you must do something like this:

sprite.position.set((int)sprite.position.x,(int)sprite.position.y,sprite.position.z);

就您的视口而言,如果您不想要任何黑条,则可能需要一个自定义的视口类,该类试图尽可能地匹配所需的分辨率,然后将其向外扩展以避免扭曲. ExtendViewport的功能类似,但是与像素化图形的不同之处在于,您需要将世界分辨率设置为屏幕分辨率的整数倍,以便像素的边缘看起来清晰而不是模糊.

As far as your Viewport goes, if you don't want any black bars, you will probably need a custom Viewport class that tries to match your desired resolution as closely as possible and then extends it outwards to avoid distortion. ExtendViewport does something similar, but the difference with pixellated graphics is that you need the world resolution to be an integer multiple of the screen's resolution so the edges of pixels look crisp rather than fuzzy.

我认为这可以满足您的需求.它采用所需的屏幕分辨率,并将其缩小以适合屏幕像素中每个像素的大小为整数的位置.然后,它将视图扩展到超出所需的分辨率,以避免失真和黑条.该类假设所有屏幕尺寸始终都是4的倍数.我认为是正确的.如果想花哨的话,可以使用OpenGL剪刀将视口大小四舍五入到最接近的4的倍数,以确保安全.最多您会有2个像素的黑条,我认为这不会引起注意.

I think this will do what you want. It takes your desired screen resolution and shrinks it to fit where the size of each of your pixels in screen pixels is an integer. Then it extends the view beyond your desired resolution to avoid distortion and black bars. This class makes the assumption that all screen dimensions are always a multiple of 4. I think that's true. If you want to get fancy, you could use OpenGL scissoring to round down the viewport size to the nearest multiples of 4, to be safe. At most you would be having 2 pixels of black bar, which I don't think would be noticeable.

public class PixelMultipleViewport extends Viewport {

    private int minWorldWidth, minWorldHeight;

    public PixelMultipleViewport (int minWorldWidth, int minWorldHeight, Camera camera) {
        this.minWorldHeight = minWorldHeight;
        this.minWorldWidth = minWorldWidth;
        this.camera = camera;
    }

    @Override
    public void update (int screenWidth, int screenHeight, boolean centerCamera) {

        viewportWidth = screenWidth;
        viewportHeight = screenHeight;

        int maxHorizontalMultiple = screenWidth / minWorldWidth;
        int maxVerticalMultiple = screenHeight / minWorldHeight;

        int pixelSize = Math.min(maxHorizontalMultiple, maxVerticalMultiple);

        worldWidth = (float)screenWidth/(float)pixelSize;
        worldHeight = (float)screenHeight/(float)pixelSize;

        super.update(screenWidth, screenHeight, centerCamera);
    }
}

这篇关于Java libgdx 1.20版本纹理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆