[Interest] Drawing a dashed line with Qt Quick 3D

Laszlo Agocs laszlo.agocs at qt.io
Fri May 28 20:59:16 CEST 2021


The custom geometry lacks UV coordinates in your example application, and so it samples with texture coordinates (0, 0) for any fragment, hence getting a line with color0 instead of the expected  color0-color1-color0-...

Add two floats per vertex and register an attribute for TexCoord0:

  m_vertexData.resize(m_count * 5 * sizeof(float));
  *p++ = 0.0f; // U
  *p++ = 0.0f; // V
  *p++ = 1.0f; // U
  *p++ = 0.0f; // V
  setStride(5 * sizeof(float));
  addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0, Attribute::F32Type);
  addAttribute(QQuick3DGeometry::Attribute::TexCoord0Semantic, 3 * sizeof(float), Attribute::F32Type);

Best regards,

From: Interest <interest-bounces at qt-project.org> on behalf of joao morgado via Interest <interest at qt-project.org>
Sent: Friday, May 28, 2021 6:07 PM
To: interest at qt-project.org <interest at qt-project.org>
Subject: [Interest] Drawing a dashed line with Qt Quick 3D

I'm trying to make a dashed line in quick3d.
I made the line with QQuick3DGeometry, and exposed it to qml, all good.
Then I tried to apply a  custom texture with QQuick3DTextureData, using a texture made with 2 colors with size of 2x1 pixels, but I only get to see the first color.

I sucessfully did in the past a dashed line example with raw OpenGL see here:



Here is the small qt quick 3d example that is failling:



Any help is welcome.

The relevant code is:

// main.qml
View3D {
        id: view3D
        visible: true
        anchors.fill: parent

        importScene: sceneRoot

        Node {
            id: sceneRoot

            // solid line, all good
            Line {
                p0: Qt.vector3d(-55, 55, 0)
                p1: Qt.vector3d(55, -55, 0)
                baseColor: "red"
                //visible: false

            // dashed line, error: only the first color is show
            Line_dashed {
                p0: Qt.vector3d(-80, -40, 0)
                p1: Qt.vector3d(80, 80, 0)
                color1: "orange"
                color2: "yellow"
                //visible: false

        }//Node: sceneRoot


// Line_dashed.qml

import QtQuick 2.12
import QtQuick3D 1.15
import Entity_Line 1.0
import LineTexture 1.0

Node {

    property alias p0: line.p0
    property alias p1: line.p1
    property alias color1: lineTex.color1
    property alias color2: lineTex.color2

    Model {
         geometry: EntityLine {
             id: line
             p0: Qt.vector3d(-55, -55, 50)
             p1: Qt.vector3d(55, 55, 50)

         materials: DefaultMaterial {
             id: material
             lighting:  DefaultMaterial.NoLighting
             diffuseMap: Texture {
                 textureData: LineTexture {
                     id: lineTex
                     //color1: "blue"
                     //color2: "white"
                 magFilter: Texture.Nearest
                 minFilter: Texture.Nearest
                 mappingMode: Texture.UV
                 scaleU: 2
                 scaleV: 1

         pickable: true
         property bool isPicked: false

// custom texture

void LineTexture::generateTextureData()
   m_textureData.resize(2 * 4 * sizeof (float)); //QByteArray
    float *p = reinterpret_cast<float *>(m_textureData.data());

    *p++ = m_color1.redF();
    *p++ = m_color1.greenF();
    *p++ = m_color1.blueF();
    *p++ = m_color1.alphaF();

    *p++ = m_color2.redF();
    *p++ = m_color2.greenF();
    *p++ = m_color2.blueF();
    *p++ = m_color2.alphaF();



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210528/4f9acc53/attachment.html>

More information about the Interest mailing list