我正在开发一个 python 脚本,它将能够生成 .DAE (COLLADA) 文件以及相关的 KML 文件,用于开发建筑物的 3D 模型。我有建筑物的街道图像。我所说的街道图像是指每座建筑物的正面图像。我需要将这些图像作为纹理放在它们各自的建筑模型上。我找不到合适的方法来使用 python 做到这一点。到现在为止,我已经成功地生成了可以放置在代表建筑物的地图上的空白立方体或长方体。我需要将图像作为纹理放在这些模型的前平面上,并将图像作为输入。请帮忙。
2 回答
老实说,我很惊讶没有人能回答我的问题。但是废话少说,切入正题。
要将图像放在表面上,您需要对 Collada 有很好的了解。首先制作一个 [CImage] 的对象:( http://pycollada.github.io/reference/generated/collada.material.CImage.html?highlight=cimage#collada.material.CImage ) ,将其包含在 Surface 对象中,包含这在 Sampler2d 对象中。将此 Sampler2d 对象包含在 Map 对象中。将此地图包含在效果中,该效果进一步包含在材质中。
现在将此材质包含在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")
欢迎任何疑问..!! :)
不是 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”部分,我相信这是定义纹理的地方。