美文网首页使用Unity 3D学习LibGdx 3D
Libgdx 3D完成一个魔方绘制

Libgdx 3D完成一个魔方绘制

作者: 大旺旺的弟弟小旺旺 | 来源:发表于2022-12-24 15:45 被阅读0次

第一节是我在网站偶尔看到一个模型,如果没有模型,我想实现一个魔方怎么办?魔方
本节目标:

  • 创建一个块,每个面都不一样
         modelBuilder.begin();
        builder.begin(VertexAttributes.Usage.Position |
                VertexAttributes.Usage.TextureCoordinates | VertexAttributes.Usage.ColorPacked | VertexAttributes.Usage.Normal, GL20.GL_TRIANGLES);

        //上
        Texture cubeletTexture = new Texture(Gdx.files.internal("cubelet.png"));
        cubeMaterial = new Material(ColorAttribute.createSpecular(Color.WHITE),
                TextureAttribute.createDiffuse(cubeletTexture));
        cubeMaterial.id = "upMater";
        builder.setColor(color[0]);
        builder.setUVRange(0, 0, width, height);
        builder.rect(x, y + depth, z + depth,
                x + depth, y + depth, z + depth,
                x + depth, y + depth, z,
                x, y + depth, z,
                0, 1, 0);

//        //下
        builder.setColor(color[1]);
        builder.setUVRange(0, 0, width, height);
        builder.rect(x, y, z,
                x + depth, y, z,
                x + depth, y, z + depth,
                x, y, z + depth,
                0, -1, 0);
       
        //后
        builder.setColor(color[2]);
        builder.setUVRange(0, 0, width, height);
        builder.rect(x, y + depth, z,
                x + depth, y + depth, z,
                x + depth, y, z,
                x, y, z,
                0, 0, -1);
        //qian
        builder.setColor(color[3]);
        builder.setUVRange(0, 0, width, height);
        builder.rect(x, y, z + depth,
                x + depth, y, z + depth,
                x + depth, y + depth, z + depth,
                x, y + depth, z + depth,
                0, 0, 1);

        //left
        builder.setColor(color[4]);
        builder.setUVRange(0, 0, width, height);
        builder.rect(x, y, z + depth,
                x, y + depth, z + depth,
                x, y + depth, z,
                x, y, z,
                -1, 0, 0);
        cubeletTexture = new Texture(Gdx.files.internal("cubelet.png"));
        cubeMaterial = new Material(ColorAttribute.createSpecular(Color.WHITE),
                TextureAttribute.createDiffuse(cubeletTexture));

        //you
        builder.setColor(color[5]);
        builder.setUVRange(0, 0, width, height);
        builder.rect(x + depth, y, z,
                x + depth, y + depth, z,
                x + depth, y + depth, z + depth,
                x + depth, y, z + depth,
                1, 0, 0);
        mesh = builder.end();
        modelBuilder.part("cube6", mesh, GL20.GL_TRIANGLES, cubeMaterial);
        model = modelBuilder.end();
        modelInstance = new ModelInstance(model);
    
image.png

创建出来的位置都在(0,0,0)位置,然后每个面先这只颜色 ,在设置顶点。

for (int x = 0; x< 3; x++) {
          for (int y = 0; y < 3; y++) {
                for (int z = 0; z < 3; z++) {
                    CubeActor actor = new CubeActor();
                    actor.setX(x-1.5f);
                    actor.setY(y-1.5f);
                    actor.setZ(z-1.5f);
                    actor.setWidth(1);
                    actor.setHeight(1);
                    actor.setDepth(1);
//                    actor.init();
                    root.addActor3d(actor);
                    cubeActors[x][y][z] = actor;
                }
            }
        }

这里创建了3x3x3个,但是它的位置都是在(0,0,0),只是绘制的位置不同。所以错觉认为在不同的位置


image.png

使用过程中替换材质

创建了模型,使用过程中有时候需要更换一个材质。从模型中获取材质,设置进模型中

Array<Material> materials = objActor.getModelInstance().materials;
for (Material material : materials) {
      material.set(TextureAttribute.createDiffuse(
            new Texture(Gdx.files.internal("cubelet.png"))));
}

相关文章

网友评论

    本文标题:Libgdx 3D完成一个魔方绘制

    本文链接:https://www.haomeiwen.com/subject/vxxwqdtx.html