如何在libgdx
创建圆环? Modelbuilder
不支持它。
我必须在代码中创建一个环面,并且无法加载任何对象。
答案 0 :(得分:1)
使用libGDX创建自定义Model
,您可以使用MeshBuilder
。
通过MeshBuilder.vertex(...)
方法,您可以逐个添加包含必要信息的顶点。基本上你需要两个嵌套循环并查找环面here所需的公式。
您必须将其打包在MeshBuilder.begin(...)
和MeshBuilder.end()
。
MeshBuilder.end()
会返回Mesh
,然后您可以转到ModelBuilder.fromMesh(mesh)
以获得您需要的Model
。
答案 1 :(得分:1)
查找解决方案,如何在torus
中创建libgdx
。可能会有所帮助。
private void createTorus (int glMaterial, float X, float Y, float Z, float widthR,
float height, int divisionsU, int divisionsV, float r, float g, float b, float a) {
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.begin();
MeshPartBuilder builder = modelBuilder.part("torus", glMaterial, Usage.Position |
Usage.Normal, new Material(ColorAttribute.createDiffuse(r, g, b, a)));
builder.setColor(Color.LIGHT_GRAY);
VertexInfo curr1 = vertTmp3.set(null, null, null, null);
curr1.hasUV = curr1.hasPosition = curr1.hasNormal = true;
VertexInfo curr2 = vertTmp4.set(null, null, null, null);
curr2.hasUV = curr2.hasPosition = curr2.hasNormal = true;
short i1, i2, i3 = 0, i4 = 0;
int i, j, k;
double s, t, twopi;
twopi = 2 * Math.PI;
for (i = 0; i < divisionsV; i++) {
for (j = 0; j <= divisionsU; j++) {
for (k = 1; k >= 0; k--) {
s = (i + k) % divisionsV + 0.5;
t = j % divisionsU;
curr1.position.set(
(float) ((widthR+height*Math.cos(s * twopi / divisionsV))*Math.cos(t * twopi / divisionsU)),
(float) ((widthR+height*Math.cos(s*twopi/divisionsV))*Math.sin(t * twopi / divisionsU)),
(float) (height * Math.sin(s * twopi / divisionsV)));
curr1.normal.set(curr1.position).nor();
k--;
s = (i + k) % divisionsV + 0.5;
curr2.position.set(
(float) ((widthR+height*Math.cos(s * twopi / divisionsV))*Math.cos(t * twopi / divisionsU)),
(float) ((widthR+height*Math.cos(s*twopi/divisionsV))*Math.sin(t * twopi / divisionsU)),
(float) (height * Math.sin(s * twopi / divisionsV)));
curr2.normal.set(curr1.normal);
//curr2.uv.set((float) s, 0);
i1 = builder.vertex(curr1);
i2 = builder.vertex(curr2);
builder.rect(i4, i2, i1, i3);
i4 = i2;
i3 = i1;
}
}
}
torus_Model = modelBuilder.end();
torus_Instances = new ModelInstance(torus_Model);
}