Qt 3D: 간단한 커스텀 머티리얼 QML 예제
Qt 3D 에서 사용자 지정 머티리얼을 만드는 방법을 보여줍니다.
이 예제에서는 간단한 사용자 지정 머티리얼을 만드는 방법을 보여줍니다.
예제 실행하기
에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행을 참조하십시오.
씬 지정하기
이 예제에서는 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) } }
머티리얼 지정하기
머티리얼은 Material 유형을 사용하여 simplecustommaterial/SimpleMaterial.qml
에서 지정됩니다. 먼저 머티리얼은 매개변수를 지정하고, 이는 셰이더의 해당 유니폼에 매핑되어 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 } },
© 2025 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.