libgdx - 3D网格生成

时间:2017-05-02 01:45:49

标签: java opengl 3d

嗨,所以我使用libgdx 1.9.6使用ModelBuilder创建一个3D网格。然而,我需要能够编辑网格空间中的每个顶点以使用OpenSimplex(Perlin噪声)。

我可以只创建一次网格(static grid)但我遇到的问题是,不断渲染它会将帧数降低到每秒1帧,因为有太多的事情发生了(low frame grid being constantly updated)。

以下是我用来获取此输出的代码:

private PerspectiveCamera camera;
private ModelBatch modelBatch;              
private Model gridModel;                    
private ModelInstance gridInstance;

public void create(){
    camera = new 
    PerspectiveCamera(80,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

    camera.position.set(10f, 10f, 10f);

    camera.lookAt(0f,0f,0f);                
    camera.near = 0.1f;                     
    camera.far = 1000f;                     

    modelBatch = new ModelBatch();

    gridLineColor = new Color(Color.LIGHT_GRAY);

    //createGrid();  //This was originally here but it needs to update
}
public void render(){
    camera.update();
    render(instances);
}
private void render(Array<ModelInstance> instances){
    modelBatch.begin(camera);
    createGrid();   //So it was moved here and being called 60 times per sec
    modelBatch.render(gridInstance);
    modelBatch.end();
}

private final float gridMin = -50f;
private final float gridMax = 50f;
private final float scale = 1f;

private void createGrid() {
    ModelBuilder modelBuilder = new ModelBuilder();

    modelBuilder.begin();

    Random ran = new Random();
    float temp = ran.nextFloat();

    MeshPartBuilder builder = modelBuilder.part("gridpart1", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
    builder.setColor(gridLineColor);

    for(float z = gridMin; z < gridMax; z += scale){
        for(float x = gridMin; x < gridMax; x += scale){
            builder.line(x, temp, z, x+scale, temp, z);
        }
    }
            // So we add another part with a different id
    builder = modelBuilder.part("gridpart2", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
    for(float z = gridMin; z < gridMax; z += scale){
        for(float x = gridMin; x < gridMax; x += scale){
            builder.line(x, temp, z, x, temp, z+scale);
        }
    }
    gridModel = modelBuilder.end();

    gridInstance = new ModelInstance(gridModel);
}

因此,我们将另一部分添加到标记为 gridpart2 的modelBuilder id中,因为仅使用一个会创建一个错误,指出太多顶点

现在我知道网格只是上下移动,但是当我完全实现perlin噪声时,网格的每个顶点都能够自由流动,因为我绘制了连接到网格上每个点的每条线。 for循环。我不确定我还能用什么方法来加速这个过程。也许不同的解决方案可能使用shaperender可以工作,但我没有太多的看法。

这就是 TheCodingTrain 使用perlin噪音生成3D地形的例子。

此外,我无法改变编码语言或环境以使事情变得更容易,我必须使用Java,因为它是我为自己上课的作业。

0 个答案:

没有答案