이 페이지에서

Qt 3D: 간단한 사용자 정의 머티리얼 QML 예제

Qt 3D 에서 사용자 정의 머티리얼을 생성하는 방법을 보여줍니다.

빨간 사각형의 삽화

이 예제는 간단한 사용자 정의 머티리얼을 만드는 방법을 보여줍니다.

예제 실행

다음에서 예제를 실행할 수 있습니다:

씬 지정

이 예제에서는 ` Scene3D `를 사용하여 사용자 정의 머티리얼을 적용할 장면을 렌더링합니다. 이 장면에는 사용자 정의 머티리얼을 사용하는 평면 모델이 포함되어 있습니다.

Entity {
    id: root

    components: [transform, mesh, material]

    SimpleMaterial {
        id: material
        maincolor: "red"
    }

    Transform {
        id: transform
        rotationX: 45
    }

    PlaneMesh {
        id: mesh
        width: 1.0
        height: 1.0
        meshResolution: Qt.size(2, 2)
    }
}

머티리얼 지정

머티리얼은 simplecustommaterial/SimpleMaterial.qml 에서 Material 유형을 사용하여 지정됩니다. 먼저 머티리얼에서 매개변수를 지정하면, 이 매개변수들은 셰이더 내의 해당 유니폼에 매핑되어 QML에서 변경할 수 있게 됩니다.

property color maincolor: Qt.rgba(0.0, 0.0, 0.0, 1.0)

parameters: [
    Parameter {
        name: "maincolor"
        value: Qt.vector3d(root.maincolor.r, root.maincolor.g, root.maincolor.b)
    }
]

다음으로 어떤 셰이더를 로드할지 지정합니다. OpenGL ES 2 및 OpenGL 렌더러용으로 각각 별도의 셰이더 버전이 제공됩니다.

property string vertex: "qrc:/shaders/gl3/simpleColor.vert"
property string fragment: "qrc:/shaders/gl3/simpleColor.frag"
property string vertexRHI: "qrc:/shaders/gl45/simpleColor.vert"
property string fragmentRHI: "qrc:/shaders/gl45/simpleColor.frag"
property string vertexES: "qrc:/shaders/es2/simpleColor.vert"
property string fragmentES: "qrc:/shaders/es2/simpleColor.frag"

버텍스 셰이더에서는 변환 행렬을 사용하여 위치를 변환합니다.

void main()
{
    // Transform position, normal, and tangent to world coords
    worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));

    // Calculate vertex position in clip coordinates
    gl_Position = mvp * vec4(worldPosition, 1.0);
}

프래그먼트 셰이더에서는 프래그먼트 색상을 머티리얼에 지정된 메인 컬러로 설정합니다.

uniform vec3 maincolor;
void main()
{
    //output color from material
    fragColor = vec4(maincolor,1.0);
}

다음으로, 셰이더를 사용하여 ShaderPrograms 를 생성합니다.

ShaderProgram {
    id: gl3Shader
    vertexShaderCode: loadSource(parent.vertex)
    fragmentShaderCode: loadSource(parent.fragment)
}
ShaderProgram {
    id: es2Shader
    vertexShaderCode: loadSource(parent.vertexES)
    fragmentShaderCode: loadSource(parent.fragmentES)
}
ShaderProgram {
    id: rhiShader
    vertexShaderCode: loadSource(parent.vertexRHI)
    fragmentShaderCode: loadSource(parent.fragmentRHI)
}

마지막으로, 셰이더 프로그램은 특정 API 프로필에 해당하는 테크닉에서 사용됩니다.

// OpenGL 3.1
Technique {
    filterKeys: [forward]
    graphicsApiFilter {
        api: GraphicsApiFilter.OpenGL
        profile: GraphicsApiFilter.CoreProfile
        majorVersion: 3
        minorVersion: 1
    }
    renderPasses: RenderPass {
        shaderProgram: gl3Shader
    }
},

예제 프로젝트 @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.