编程语言
首页 > 编程语言> > java-3D对象相遇的边缘和毛刺不均匀

java-3D对象相遇的边缘和毛刺不均匀

作者:互联网

我正尝试将基于图块的游戏转换为3D游戏,从而直接进入我的第一个3D游戏.我正在使用libGDX的新3D类来完成此操作.

在这种情况下,我将每个图块渲染为一个盒子,将图块图中的每个块都转换为3D对象.它工作得相当好,但是当框共享一个边或角时,我收到了图形错误.

这似乎在桌面项目上更好地工作(似乎有时会发生,但不太严重):

但不是在android设备上:

此小故障取决于在块的屏幕上绘制位置.这在几种不同的android设备上会发生,但在屏幕分辨率更高的设备上会更加明显.

这是我用来在主libGDX项目中呈现的代码:

    @Override
public void create() {      
    modelBatch = new ModelBatch();
    instances = new ArrayList<ModelInstance>();

    //Setup camera
    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(0, 0, 150f);
    cam.lookAt(0, 0,0);
    cam.near = 0.1f;
    cam.far = 500f;
    cam.update();

    //Setup Lighting
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

    // Add boxes
    addBox(new Vector3(-50,0,0));
    addBox(new Vector3(-50,16,0));
    addBox(new Vector3(-50,32,0));
    addBox(new Vector3(-50,48,0));

    addBox(new Vector3(50,48,0));
    addBox(new Vector3(66,48,0));
    addBox(new Vector3(82,48,0));
    addBox(new Vector3(98,48,0));

}

private void addBox(Vector3 position){
    ModelBuilder modelBuilder = new ModelBuilder();
    Model model = modelBuilder.createBox(16f, 16f, 16f, 
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);
    ModelInstance mis = new ModelInstance(model);
    mis.transform.setTranslation(position);
    instances.add(mis);
}


@Override
public void render() {      
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

//          The next two lines seem to improve the problem. Problem is almost totally gone
//          I tweaked the values in glPolygonOffset to make it work better
            Gdx.gl20.glEnable(Gdx.gl20.GL_POLYGON_OFFSET_FILL);
            Gdx.gl20.glPolygonOffset(0.1f,0.1f);

//          These make it worse!
//              Gdx.gl.glEnable(Gdx.gl20.GL_DEPTH_TEST);
//              Gdx.gl20.glDepthFunc(Gdx.gl20.GL_LEQUAL); // GL_LESS is default


    modelBatch.begin(cam);

    for(ModelInstance mi : instances)
        modelBatch.render(mi,environment);

    modelBatch.end();
}

有任何想法吗?

编辑:

更改render()方法后,在手机上渲染(放大,减少不良边缘):

解决方法:

当两个对象在完全相同的深度处重叠时出现的问题称为“拼接”.

您可以通过使用polygon depth offset避免它.它基本上为绘制的多边形增加了一些额外的深度.您可能需要为要渲染的每个对象使用一个不同的值,该值可能会重叠.

Gdx.gl.glEnable(Gdx.gl10.GL_POLYGON_OFFSET_FILL);
Gdx.gl10.glPolygonOffset(1.0f, 1.0f);

有时调整深度功能也可能会有所帮助.我们希望对其进行调整,以在深度等于之前已渲染的深度时也渲染多边形.

Gdx.gl.glEnable(Gdx.gl10.GL_DEPTH_TEST);
Gdx.gl10.glDepthFunc(GL10.GL_LEQUAL); // GL_LESS is default

标签:3d,opengl-es,libgdx,java,android
来源: https://codeday.me/bug/20191122/2060026.html