<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- simplecustommaterial.qdoc --> <title>Qt 3D: Simple Custom Material QML Example | Qt 3D 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qt3d-index.html">Qt 3D</a></td><td >Qt 3D: Simple Custom Material QML Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qt3d-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#running-the-example">Running the Example</a></li> <li class="level1"><a href="#specifying-the-scene">Specifying the Scene</a></li> <li class="level1"><a href="#specifying-the-material">Specifying the Material</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Qt 3D: Simple Custom Material QML Example</h1> <span class="subtitle"></span> <!-- $$$simplecustommaterial-brief --> <p>Demonstrates creating a custom material in Qt 3D.</p> <!-- @@@simplecustommaterial --> <!-- $$$simplecustommaterial-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/simple-custom-material.jpg" alt="" /></p><p><i>This</i> example demonstrates creating a simple custom material.</p> <a name="running-the-example"></a> <h2 id="running-the-example">Running the Example</h2> <p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p> <a name="specifying-the-scene"></a> <h2 id="specifying-the-scene">Specifying the Scene</h2> <p>The example uses <a href="qml-qtquick-scene3d-scene3d.html">Scene3D</a> to render a scene which will use the custom material. The scene contains a plane model, which uses the custom material.</p> <pre class="cpp"> 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) } } </pre> <a name="specifying-the-material"></a> <h2 id="specifying-the-material">Specifying the Material</h2> <p>The material is specified in <a href="qt3d-simplecustommaterial-simplematerial-qml.html">SimpleMaterial.qml</a> using <a href="qml-qt3d-render-material.html">Material</a> type. First the material specifies parameters, which are mapped to the corresponding uniforms in the shaders so that they can be changed from the qml.</p> <pre class="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) } ] </pre> <p>Next we specify which shaders are loaded. Separate versions of the shaders are provided for OpenGL ES 2 and OpenGL renderers.</p> <pre class="qml"> property string vertex: "qrc:/shaders/gl3/simpleColor.vert" property string fragment: "qrc:/shaders/gl3/simpleColor.frag" property string vertexES: "qrc:/shaders/es2/simpleColor.vert" property string fragmentES: "qrc:/shaders/es2/simpleColor.frag" </pre> <p>In the vertex shader we simply transform the position by the transformation matrices.</p> <pre class="cpp"> 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); } </pre> <p>In the fragment shader we simply set the fragment color to be the maincolor specified in the material.</p> <pre class="cpp"> uniform vec3 maincolor; void main() { //output color from material fragColor = vec4(maincolor,1.0); } </pre> <p>Next, we create <a href="qml-qt3d-render-shaderprogram.html">ShaderPrograms</a> from the shaders.</p> <pre class="qml"> ShaderProgram { id: gl3Shader vertexShaderCode: loadSource(parent.vertex) fragmentShaderCode: loadSource(parent.fragment) } ShaderProgram { id: es2Shader vertexShaderCode: loadSource(parent.vertexES) fragmentShaderCode: loadSource(parent.fragmentES) } </pre> <p>Finally the shader programs are used in the Techniques corresponding to a specific Api profile.</p> <pre class="qml"> // OpenGL 3.1 Technique { filterKeys: [forward] graphicsApiFilter { api: GraphicsApiFilter.OpenGL profile: GraphicsApiFilter.CoreProfile majorVersion: 3 minorVersion: 1 } renderPasses: RenderPass { shaderProgram: gl3Shader } }, </pre> <p>Files:</p> <ul> <li><a href="qt3d-simplecustommaterial-planemodel-qml.html">simplecustommaterial/PlaneModel.qml</a></li> <li><a href="qt3d-simplecustommaterial-sceneroot-qml.html">simplecustommaterial/SceneRoot.qml</a></li> <li><a href="qt3d-simplecustommaterial-simplematerial-qml.html">simplecustommaterial/SimpleMaterial.qml</a></li> <li><a href="qt3d-simplecustommaterial-main-cpp.html">simplecustommaterial/main.cpp</a></li> <li><a href="qt3d-simplecustommaterial-main-qml.html">simplecustommaterial/main.qml</a></li> <li><a href="qt3d-simplecustommaterial-models-qrc.html">simplecustommaterial/models.qrc</a></li> <li><a href="qt3d-simplecustommaterial-qml-qrc.html">simplecustommaterial/qml.qrc</a></li> <li><a href="qt3d-simplecustommaterial-shaders-qrc.html">simplecustommaterial/shaders.qrc</a></li> <li><a href="qt3d-simplecustommaterial-shaders-es2-simplecolor-vert.html">simplecustommaterial/shaders/es2/simpleColor.vert</a></li> <li><a href="qt3d-simplecustommaterial-shaders-gl3-simplecolor-vert.html">simplecustommaterial/shaders/gl3/simpleColor.vert</a></li> <li><a href="qt3d-simplecustommaterial-simplecustommaterial-pro.html">simplecustommaterial/simplecustommaterial.pro</a></li> <li><a href="qt3d-simplecustommaterial-textures-qrc.html">simplecustommaterial/textures.qrc</a></li> </ul> </div> <!-- @@@simplecustommaterial --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> 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. </p> </div> </body> </html>