Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Define material indexes for triangles in QQuick3DGeometry



  • I'm trying to load a model to a Quick 3D scene using QQuick3DGeometry
    in the scene I can define (See Sub-mesh Example):

    Model {
        source: "qrc:/meshes/distortedcube.mesh"
        //geometry: MeshData
        
        PrincipledMaterial {
            id: frontTop_material
            baseColor: "red"
            cullMode: Material.NoCulling
            lighting: PrincipledMaterial.NoLighting
        }
    
        PrincipledMaterial {
            id: frontBottom_material
            baseColor: "green"
            cullMode: Material.NoCulling
            lighting: PrincipledMaterial.NoLighting
        }
    
        materials: [
            frontTop_material,
            frontBottom_material
        ]
    }
    

    One part of the model is painted with frontTop_material and for the other frontBottom_material is used.
    I created similar scene where mesh data is generated by QQuick3DGeometry and it is painted on the screen, but I can't see a way how to assing different materials to different triangles. How can I do that?
    There is a set of attributes, which i can set via QQuick3DGeometry::addAttribute to every vertex, but none of them is for defining texture id.



  • I'm not sure if you want to apply diferente colors or different textures.
    If you're trying to use textures, for a simple model like a sphere, a texture would be applied like this:

    Model {
                    id: planetEarth
                    source: "#Sphere"
                    materials: [
                        PrincipledMaterial {
                            baseColorMap: Texture {
                                source: "qrc:/images/earth.png"
                                scaleU: 1.0
                                scaleV: 1.0
                            }
                        }
                    ]
                }
    

    The example you posted distortedcube, AFAIK the model is generated with the balsam tool, (from a obj file, or other similiar 3d format file), hence you can define diferente materials for each sub mesh, be either a texture or a color. See https://doc.qt.io/qt-5/qtquick3d-tool-balsam.html

    Note that I don´t think you can have submeshs with QQuick3DGeometry, so you can have only one texture for one model (but I maybe wrong). With QQuick3DGeometry, you should use QQuick3DGeometry::addAttribute with TexCoordSemantic to define your texture positions in your model. So if you want to load several textures I think you have to break your model a part.
    If you have one model and want to apply different colors to different triangles, I would try to use Quick3DGeometry::addAttribute with ColorSemantic.

    See also the custom geometry example https://doc-snapshots.qt.io/qt6-dev/qtquick3d-customgeometry-example.html


Log in to reply