gpt4 book ai didi

python - 使用 pycollada 在 3d block 上制作纹理

转载 作者:太空宇宙 更新时间:2023-11-03 14:28:00 28 4
gpt4 key购买 nike

我正在开发一个 python 脚本,它将能够生成 .DAE (COLLADA) 文件以及相关的 KML 文件,用于开发建筑物的 3D 模型。我有建筑物的街道图像。街道图像是指每座建筑物的正面图像。我需要将这些图像作为纹理放在它们各自的建筑模型上。我无法找到合适的方法来使用 python 执行此操作。到目前为止,我已经成功地生成了可以放置在代表建筑物的 map 上的空白立方体或长方体。我需要将图像作为纹理放在这些模型的前平面上,将图像作为输入。请帮忙。

最佳答案

老实说,我很惊讶没有人能回答我的问题。废话少说,进入正题。

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

现在将此 Material 包含在 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")

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

关于python - 使用 pycollada 在 3d block 上制作纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16955533/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com