纹理在3d块使用pycollada

时间:2013-06-06 06:52:14

标签: python graphics collada 3d-modelling 3d-texture

我正在开发一个python脚本,它能够生成.DAE(COLLADA)文件以及相关的KML文件,用于开发建筑物的3D模型。我有建筑物的街道图像。通过街道图像,我的意思是每个建筑物的正面图像。我需要将这些图像作为纹理放在各自的建筑模型上。我无法找到合适的方法来使用python来实现这一点。直到现在,我已成功生成空白立方体或长方体,可以定位在代表建筑物的地图上。我需要将图像作为纹理放在这些模型的前平面上,将图像作为输入。请帮助。

2 个答案:

答案 0 :(得分:5)

说实话,我很惊讶没有人能回答我的问题。但是切断废话并且达到了目的。

要将图像放在表面上,您需要对Collada有很好的了解。 首先创建[CImage]的对象:(http://pycollada.github.io/reference/generated/collada.material.CImage.html?highlight=cimage#collada.material.CImage),在Surface对象中包含它,将其包含在Sampler2d对象中。在Map对象中包含此Sampler2d对象。在效果中包含此地图,这将进一步包含在材料中。

现在将此材质包含在scene.MaterialNode类'对象中,该对象进一步包含在scene.GeometryNode类'对象中,该对象最后包含在scene.Node对象中。我知道这有点了解

为了理解下面是使用pycollad的python中的代码,它在任意长度,宽度和高度的长方体的两个面上添加照片作为纹理。

import numpy as np
from collada import *

mesh = Collada()
axis = asset.UP_AXIS.Z_UP
mesh.assetInfo.upaxis = axis

image = material.CImage("material_0_1_0-image", "DSC_5195.jpg")
surface = material.Surface("material_0_1_0-image-surface", image)
sampler2d = material.Sampler2D("material_0_1_0-image-sampler", surface)
map1 = material.Map(sampler2d, "UVSET0")

image2 = material.CImage("material_0_1_1-image", "Untitled.png")
surface2 = material.Surface("material_0_1_1-image-surface", image2)
sampler2d_2 = material.Sampler2D("material_0_1_1-image-sampler", surface2)
map2 = material.Map(sampler2d_2, "UVSET0")

effect1 = material.Effect("material_0_0-effect", [], "lambert", emission=(0.0, 0.0, 0.0, 1),\
                     ambient=(0.0, 0.0, 0.0, 1), diffuse=(0.890196, 0.882353, 0.870588, 1),\
                     transparent=(1, 1, 1, 1), transparency=1.0, double_sided=True)
effect2 = material.Effect("material_0_1_0-effect", [surface, sampler2d], "lambert", emission=(0.0, 0.0, 0.0, 1),\
                     ambient=(0.0, 0.0, 0.0, 1),  diffuse=map1, transparent=map1, transparency=0.0, double_sided=True)

effect3 = material.Effect("material_0_1_1-effect", [surface2, sampler2d_2], "lambert", emission=(0.0, 0.0, 0.0, 1),\
                     ambient=(0.0, 0.0, 0.0, 1),  diffuse=map2, transparent=map2, transparency=0.0, double_sided=True)

mat1 = material.Material("material_0_0ID", "material_0_0", effect1)
mat2 = material.Material("material_0_1_0ID", "material_0_1_0", effect2)
mat3 = material.Material("material_0_1_1ID", "material_0_1_1", effect3)

mesh.effects.append(effect1)
mesh.effects.append(effect2)
mesh.effects.append(effect3)

mesh.materials.append(mat1)
mesh.materials.append(mat2)
mesh.materials.append(mat3)

mesh.images.append(image)
mesh.images.append(image2)

#red x-axis
#green z-axis
#blue y-axis

h = 7.0
b = 7.0
w = 10.0

m1position = [0, 0, 0, 0, b, 0, w, b, 0, w, 0, 0, 0, 0, h, 0, b, h, w, b, h, w, 0, h]
m1normal = [1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1]
m1uv = [1, 1, 0, 0, 1, 0, 0, 1]

m1position_src = source.FloatSource("mesh1-geometry-position", np.array(m1position), ('X', 'Y', 'Z'))
m1normal_src = source.FloatSource("mesh1-geometry-normal", np.array(m1normal), ('X', 'Y', 'Z'))
m1uv_src = source.FloatSource("mesh1-geometry-uv", np.array(m1uv), ('S', 'T'))

geom = geometry.Geometry(mesh, "mesh1-geometry", "mesh1-geometry", [m1position_src, m1normal_src, m1uv_src])
geom1 = geometry.Geometry(mesh,"mesh1-geometry1","mesh1-geometry1",[m1position_src, m1normal_src, m1uv_src])
geom2 = geometry.Geometry(mesh,"mesh1-geometry2","mesh1-geometry2",[m1position_src,m1normal_src, m1uv_src])

input_list = source.InputList()
input_list.addInput(0, 'VERTEX', "#mesh1-geometry-position")

input_list1 = source.InputList()
input_list1.addInput(0, 'VERTEX', "#mesh1-geometry-position")
input_list1.addInput(1, 'TEXCOORD', "#mesh1-geometry-uv", set="0")

input_list2 = source.InputList()
input_list2.addInput(0, 'VERTEX', "#mesh1-geometry-position")
input_list2.addInput(1, 'TEXCOORD', "#mesh1-geometry-uv", set="0")

indices1 = np.array([0, 1, 2, 2, 3, 0, 0, 1, 5, 5, 0, 4, 7, 2, 6, 2, 7, 3, 4, 3, 7, 3, 4, 0]) 
indices2 = np.array([2, 1, 1, 2, 5, 0, 5, 0, 6, 3, 2, 1])
indices3 = np.array([6,1,5,2,4,0,4,0,7,3,6,1])

triset1 = geom.createTriangleSet(indices1, input_list, "material_0_0")
triset2 = geom1.createTriangleSet(indices2, input_list1, "material_0_1_0")
triset3 = geom2.createTriangleSet(indices3, input_list2, "material_0_1_1")

geom.primitives.append(triset1)
geom1.primitives.append(triset2)
geom2.primitives.append(triset3)

mesh.geometries.append(geom)
mesh.geometries.append(geom1)
mesh.geometries.append(geom2)

matnode1 = scene.MaterialNode("material_0_0", mat1, inputs=[])
matnode2 = scene.MaterialNode("material_0_1_0", mat2, inputs=[])
matnode3 = scene.MaterialNode("material_0_1_1", mat3, inputs=[])

geomnode = scene.GeometryNode(geom, [matnode1])
geomnode1 = scene.GeometryNode(geom1, [matnode2])
geomnode2 = scene.GeometryNode(geom2, [matnode3])

node = scene.Node("Model", children=[geomnode, geomnode1, geomnode2])
myscene = scene.Scene("SketchUpScene", [node])
mesh.scenes.append(myscene)
mesh.scene = myscene

mesh.write("untitled.dae")

欢迎任何疑惑.. !! :)

答案 1 :(得分:0)

不是100%肯定,但也许这些api资源中的一个可能有所帮助:

http://pycollada.github.io/reference/generated/collada.material.Surface.html

cimage对象可能就是你所追求的:

http://pycollada.github.io/reference/generated/collada.material.CImage.html#collada.material.CImage

完整的API描述就在这里:

http://pycollada.github.io/reference/index.html

如果您之前没有检查过这个问题,请查看“.material”部分,我相信,这部分将定义纹理。