第一节是我在网站偶尔看到一个模型,如果没有模型,我想实现一个魔方怎么办?魔方
本节目标:
- 创建一个块,每个面都不一样
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"))));
}
网友评论