在我的应用程序中,我需要生成各种几何对象。 例如,我可以使用坐标中的BoxShapeBuilder创建一个框 {{1,1,1},{1,-1,1},{-1,-1,1},{-1,1,1}, {1,1,-1},{1,-1,-1},{-1,-1,-1},{-1,1,-1}}。 然后,它可以缩放,旋转它,它没关系。 但是,如果我想将对象的形状转换为 {{2,2,1},{1,-1,1},{-1,-1,1},{-1,1,1}, {1,1,-1},{1,-1,-2},{-1,-1,-1},{-1,1,-1}}或类似的东西。是否可以不重新创建模型? 现在,我需要创建一个新模型,这对性能非常不利。 我是3D编程的新手,也许只是不知道,搜索什么。我假设必须有一些简单的技巧来处理这个问题。 以下是我想要做的事情的示例代码:
private class MyScreen implements Screen {
ModelBatch modelBatch;
PerspectiveCamera camera3d;
ModelBuilder modelBuilder;
Material boxMaterial;
Environment environment;
Model boxModel = null;
float mult = 0f;
Vector3[] boxShape1 = {
new Vector3(2f, 2f, 2f),
new Vector3(-2f, 2f, 2f),
new Vector3(2f, -2f, 2f),
new Vector3(-2f, -2f, 2f),
new Vector3(2f, 2f, -2f),
new Vector3(-2f, 2f, -2f),
new Vector3(2f, -2f, -2f),
new Vector3(-2f, -2f, -2f)
};
Vector3[] boxShape2 = {
new Vector3(5f, 5f, 5f),
new Vector3(-6f, 6f, 8f),
new Vector3(5f, -5f, 4f),
new Vector3(-5f, -5f, 5f),
new Vector3(2f, 2f, -2f),
new Vector3(-2f, 2f, -2f),
new Vector3(2f, -2f, -2f),
new Vector3(-2f, -2f, -2f)
};
Vector3[] boxShape = new Vector3[8];
final float moveTimeSeconds = 3f;
float totalDelta = 0f;
public MyScreen() {
for(int i = 0; i < 8; i++){
boxShape[i] = new Vector3(0f, 0f, 0f);
}
Color lightColor = new Color(0.8f, 0.8f, 0.8f, 1.0f);
boxMaterial = new Material(ColorAttribute.createDiffuse(1f, 0f, 0f, 1f));
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
//environment.add(new PointLight().set(lightColor, cameraPos, 90.0f));
environment.add(new DirectionalLight().set(lightColor, -3f, +3.8f, +3.2f));
modelBatch = new ModelBatch();
modelBuilder = new ModelBuilder();
camera3d = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Vector3 cameraPos = new Vector3( 5f, -19f, 5f);
camera3d.position.set(cameraPos);
camera3d.up.set(0f,0f,1f);
camera3d.lookAt(cameraPos.x,0,cameraPos.z);
camera3d.near = 1f;
camera3d.far = 55f;
camera3d.update();
}
@Override
public void show() {
}
@Override
public void render(float delta) {
if(boxModel != null){
boxModel.dispose();
}
totalDelta += delta;
if (totalDelta > moveTimeSeconds) {
totalDelta = moveTimeSeconds;
}
mult = totalDelta / moveTimeSeconds;
for(int i = 0; i < 8; i++){
boxShape[i].x = boxShape1[i].x + (boxShape2[i].x - boxShape1[i].x) * mult;
boxShape[i].y = boxShape1[i].y + (boxShape2[i].y - boxShape1[i].y) * mult;
boxShape[i].z = boxShape1[i].z + (boxShape2[i].z - boxShape1[i].z) * mult;
}
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
modelBatch.begin(camera3d);
//here, I must to recreate box model each time to get shape needed
modelBuilder.begin();
MeshPartBuilder mpb = modelBuilder.part("box", GL30.GL_TRIANGLES,
VertexAttributes.Usage.Normal | VertexAttributes.Usage.Position, boxMaterial);
BoxShapeBuilder.build(mpb,
boxShape[0], boxShape[1], boxShape[2], boxShape[3],
boxShape[4], boxShape[5], boxShape[6], boxShape[7]);
boxModel = modelBuilder.end();
ModelInstance instance = new ModelInstance(boxModel);
modelBatch.render(instance, environment);
modelBatch.end();
}
@Override
public void dispose () {
boxModel.dispose();
modelBatch.dispose();
}
}
我只需要创建一次模型,然后改变它顶点的位置并改变形状。