pythongraphicscollada3d-modelling3d-texture

Texturing on 3d Blocks using pycollada


I am developing a python script which will be able to generate .DAE (COLLADA) files along with the associated KML files for developing 3D models of buildings. I have the street images of the buildings. By street images, I mean the front face image of each building. I need to put these images as a texture over their respective building models. I am unable to find suitable method by which I can do this using python. Till now, I have succeeded in generating blank cubes or cuboids which can be positioned over the map representing the buildings. I need to put the image as a texture on the front plane of these models taking the image as an input. Kindly help.


Solution

  • To be honest, I am pretty surprised that no one could give an answer to my question. But cutting the crap and coming to the point.

    For putting an Image on a surface you need to have good knowledge of Collada. First make an object of [CImage]: (http://pycollada.github.io/reference/generated/collada.material.CImage.html?highlight=cimage#collada.material.CImage) , Include that in Surface object, Include this in Sampler2d object. Include this Sampler2d object in Map object. Include this map in Effects which is further included in Materials.

    Now include this material in scene.MaterialNode class' object which is further included in scene.GeometryNode class' object which is at last included in scene.Node object. I know this is bit tuff to understand

    For understanding below is the code in python using pycollad which adds photos as textures on the two faces of the cuboids of any length, breadth, and height.

    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")
    

    Any doubts are welcomed..!! :)