Libgdx meshbuilder手动创建3d对象

Jam*_*mmo 5 java 3d mesh libgdx

我正在使用 Libgdx (Java) 并尝试构建 3D 模型。目前,我可以使用已定义的方法(例如modelBuilder.createBox()或 )创建模型modelBuilder.createSphere()。但是,我想使用自定义网格(根据一些基本数学计算顶点)构建自己的球体,然后细分以增加面数。例如,从二十面体开始,然后进一步细分以增加面数。我必须手动执行此操作,因为似乎没有任何更简单的方法可以执行此操作。我想这样做是因为以后想分别针对这些人脸来为所说的人脸应用独特的颜色/纹理。

在此处输入图片说明

我想如果有人帮我制作一个自定义/手动立方体,我可能会解决其余的问题。

我的环境已经设置好了,我目前可以使用预定义的方法构建一个球体:

public class ManagerSphere {

    //Settings
    public String ASSET_TEXTURE_BOX = "data/libgdx.png";
    public float SPHERE_RADIUS = 2f;

    //init
    private GameScreen _gameRef;
    private ModelBuilder modelBuilder = new ModelBuilder();
    private Texture tex;
    private Material mat;
    private ModelInstance boxInstance;
    public AssetManager assets;

    public ManagerSphere(GameScreen gameRef) {
        this._gameRef = gameRef;
        createSphere();
    }


    public void createSphere() {
        this.tex = new Texture("spheretexture02.jpg"); 
        this.mat = new Material(TextureAttribute.createDiffuse(this.tex));

        Model tempSphere;
        tempSphere = modelBuilder.createSphere(SPHERE_RADIUS*2, SPHERE_RADIUS*2, SPHERE_RADIUS*2,
                50,50,
                new Material(),
                VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates
        );

        boxInstance = new ModelInstance(tempSphere,0,0,0); //x,y,z (+ve z = away, -ve z = behind)
        boxInstance.nodes.get(0).parts.get(0).material.set(this.mat);

    }

    public void render(float delta) {

        this._gameRef.modelBatch.render(boxInstance, this._gameRef.environment);

    }

}
Run Code Online (Sandbox Code Playgroud)

总之:

  • 我需要一种基本的方法来在 3d 空间中手动构建一个对象。理想情况下作为二十面体,但会很高兴有一个立方体让我开始
  • 我需要能够编辑这个网格并细分以创建更多的面/三角形
  • 我希望能够定位这些面中的每一个并更改它们的颜色/纹理,最有可能使用“meshpart”及其自己的网格 ID。

提前谢谢了。J

编辑:我现在使用meshbuilder创建了二十面体对象。我使用了一些基本数学来计算 12 个顶点的初始位置,将其放入一个数组中,然后创建一个包含 20 个面的数组(其中每个面包含对三个顶点的引用)。这是我的新代码:

public void createSphere() {

    createFaces();

    int attr = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates;
    Texture myTexture = new Texture("badlogic.jpg");

    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();
    modelBuilder.manage(myTexture); //make modelbuilder responsible for disposing the texture resource

    Random r = new Random();
    for(Face face : faces) {

        modelBuilder.part("face"+face.id, GL20.GL_TRIANGLES, attr,
                //new Material(TextureAttribute.createDiffuse(myTexture)))
                new Material(ColorAttribute.createDiffuse(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1)))
                .triangle(face.p1.toVector3(), face.p2.toVector3(), face.p3.toVector3());

    }


    boxInstance = new ModelInstance(modelBuilder.end(), 0, 0, 0);

}
Run Code Online (Sandbox Code Playgroud)

然而,因为我使用modelBuilder.part(...).triangle()而不是modelBuilder.part(...).box()我似乎无法将纹理应用于这个三角形的脸,我只能分配一个随机的漫反射颜色(见图片)

在此处输入图片说明

所以我仍然需要

  • 找出如何细分每个面并通过细分将二十面体的形状更改为更圆
  • 将纹理(从某个点的地图集)应用于单个三角形面。也许所有的脸都可以有相同的纹理,但也有随它扩散的可变颜色。

编辑2:

稍后进行一些数学运算,我已经研究出如何编辑网格并对每个面应用 N 个细分,加上从原点向外挤出该点以保持球形。请参阅下图,其中包含我的细分方法的 0、1 和 2 次迭代。

在此处输入图片说明

我现在需要的只是能够将纹理应用于每个人的脸,而不是随机颜色。有什么帮助吗?谢谢。