如何从对象面的顶点信息创建 collada 文件?
How to create a collada file from an objects faces' vertices information?
我有一个项目需要我以 .dae Collada 格式导出模型。我正在努力弄清楚如何做到这一点。我一直在尝试使用 pyCollada 来这样做,但我在 3D 建模方面的经验有限,而且文件结构让我感到困惑。为什么一定要指定一个 "normal_floats" 的数组来构建多边形。顶点本身还不够吗?
我有对象每个面的所有顶点,需要将数据导出为 collada 格式吗?因为每张脸都是二维的,有没有一种简单的方法可以做到这一点?是否有一种算法可以简单地将顶点输入以生成对象的适当面?任何帮助将不胜感激。
此外,我目前有一个在 openGL 中绘制对象的算法。有什么方法可以在生成导出文件时重用代码吗?
更新这是我试图遵循的创建对象的教程:http://pycollada.github.io/creating.html
#Allows the laminate to get exported as a DAE.
def toDAE(self):
"""
Exports the current lamiante to a DAE file format
"""
import collada
mesh = collada.Collada()
layerdef = self.layerdef
nodes = [] # Each node of the mesh scene. Typically one per layer.
for layer in layerdef.layers:
layer_thickness = layer.thickness
shapes = self.geoms[layer]
zvalue = layerdef.z_values[layer]
height = float(zvalue) #* 100 #*
if (len(shapes) == 0) : #In case there are no shapes.
continue
for s in shapes:
geom = self.createDAEFromShape(s, height, mesh, layer_thickness)
mesh.geometries.append(geom)
effect = collada.material.Effect("effect", [], "phone", diffuse=(1,0,0), specular=(0,1,0))
mat = collada.material.Material("material", "mymaterial" + str(s.id), effect)
matnode = collada.scene.MaterialNode("materialref" + str(s.id), mat, inputs=[])
mesh.effects.append(effect)
mesh.materials.append(mat)
geomnode = collada.scene.GeometryNode(geom, [matnode])
node = collada.scene.Node("node" + str(s.id), children=[geomnode])
nodes.append(node)
myscene = collada.scene.Scene("myscene", nodes)
mesh.scenes.append(myscene)
mesh.scene = myscene
filename = popupcad.exportdir + os.path.sep + str(self.id) + '.dae' #
mesh.write(filename)
def createDAEFromShape(self, s, layer_num, mesh, thickness): #TODO Move this method into the shape class.
import collada
vertices = s.extrudeVertices(thickness, z0=layer_num)
#This scales the verticies properly. So that they are in millimeters.
vert_floats = [float(x)/(popupcad.SI_length_scaling) for x in vertices]
vert_src_name = str(self.id) + '|' + str(s.id) + "-array"
vert_src = collada.source.FloatSource(vert_src_name, numpy.array(vert_floats), ('X', 'Y', 'Z'))
geom = collada.geometry.Geometry(mesh, "geometry-" + str(s.id), str(self.id), [vert_src])
input_list = collada.source.InputList()
input_list.addInput(0, 'VERTEX', "#" + vert_src_name)
indices = numpy.array(range(0,(len(vertices) // 3)));
triset = geom.createTriangleSet(indices, input_list, "materialref" + str(s.id))
triset.generateNormals()
geom.primitives.append(triset)
return geom
显然你实际上可以在没有法线的情况下计算它,但文档并不清楚。
我有一个项目需要我以 .dae Collada 格式导出模型。我正在努力弄清楚如何做到这一点。我一直在尝试使用 pyCollada 来这样做,但我在 3D 建模方面的经验有限,而且文件结构让我感到困惑。为什么一定要指定一个 "normal_floats" 的数组来构建多边形。顶点本身还不够吗?
我有对象每个面的所有顶点,需要将数据导出为 collada 格式吗?因为每张脸都是二维的,有没有一种简单的方法可以做到这一点?是否有一种算法可以简单地将顶点输入以生成对象的适当面?任何帮助将不胜感激。
此外,我目前有一个在 openGL 中绘制对象的算法。有什么方法可以在生成导出文件时重用代码吗?
更新这是我试图遵循的创建对象的教程:http://pycollada.github.io/creating.html
#Allows the laminate to get exported as a DAE.
def toDAE(self):
"""
Exports the current lamiante to a DAE file format
"""
import collada
mesh = collada.Collada()
layerdef = self.layerdef
nodes = [] # Each node of the mesh scene. Typically one per layer.
for layer in layerdef.layers:
layer_thickness = layer.thickness
shapes = self.geoms[layer]
zvalue = layerdef.z_values[layer]
height = float(zvalue) #* 100 #*
if (len(shapes) == 0) : #In case there are no shapes.
continue
for s in shapes:
geom = self.createDAEFromShape(s, height, mesh, layer_thickness)
mesh.geometries.append(geom)
effect = collada.material.Effect("effect", [], "phone", diffuse=(1,0,0), specular=(0,1,0))
mat = collada.material.Material("material", "mymaterial" + str(s.id), effect)
matnode = collada.scene.MaterialNode("materialref" + str(s.id), mat, inputs=[])
mesh.effects.append(effect)
mesh.materials.append(mat)
geomnode = collada.scene.GeometryNode(geom, [matnode])
node = collada.scene.Node("node" + str(s.id), children=[geomnode])
nodes.append(node)
myscene = collada.scene.Scene("myscene", nodes)
mesh.scenes.append(myscene)
mesh.scene = myscene
filename = popupcad.exportdir + os.path.sep + str(self.id) + '.dae' #
mesh.write(filename)
def createDAEFromShape(self, s, layer_num, mesh, thickness): #TODO Move this method into the shape class.
import collada
vertices = s.extrudeVertices(thickness, z0=layer_num)
#This scales the verticies properly. So that they are in millimeters.
vert_floats = [float(x)/(popupcad.SI_length_scaling) for x in vertices]
vert_src_name = str(self.id) + '|' + str(s.id) + "-array"
vert_src = collada.source.FloatSource(vert_src_name, numpy.array(vert_floats), ('X', 'Y', 'Z'))
geom = collada.geometry.Geometry(mesh, "geometry-" + str(s.id), str(self.id), [vert_src])
input_list = collada.source.InputList()
input_list.addInput(0, 'VERTEX', "#" + vert_src_name)
indices = numpy.array(range(0,(len(vertices) // 3)));
triset = geom.createTriangleSet(indices, input_list, "materialref" + str(s.id))
triset.generateNormals()
geom.primitives.append(triset)
return geom
显然你实际上可以在没有法线的情况下计算它,但文档并不清楚。