<?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.9</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.9</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">Qt 5.9.4 Reference Documentation</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-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 Scene3D to render a scene which will use the custom material. The scene contains a plane model, which uses the custom material.</p> <pre class="qml"> <span class="type"><a href="qml-qt3d-core-entity.html">Entity</a></span> { <span class="name">id</span>: <span class="name">root</span> <span class="name">components</span>: [<span class="name">transform</span>, <span class="name">mesh</span>, <span class="name">material</span>] <span class="type">SimpleMaterial</span> { <span class="name">id</span>: <span class="name">material</span> <span class="name">maincolor</span>: <span class="string">"red"</span> } <span class="type"><a href="qml-qt3d-core-transform.html">Transform</a></span> { <span class="name">id</span>: <span class="name">transform</span> <span class="name">rotationX</span>: <span class="number">45</span> } <span class="type"><a href="qml-qt3d-extras-planemesh.html">PlaneMesh</a></span> { <span class="name">id</span>: <span class="name">mesh</span> <span class="name">width</span>: <span class="number">1.0</span> <span class="name">height</span>: <span class="number">1.0</span> <span class="name">meshResolution</span>: <span class="name">Qt</span>.<span class="name">size</span>(<span class="number">2</span>, <span class="number">2</span>) } } </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 <span class="type">color</span> <span class="name">maincolor</span>: <span class="name">Qt</span>.<span class="name">rgba</span>(<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">1.0</span>) <span class="name">parameters</span>: [ <span class="type"><a href="qml-qt3d-render-parameter.html">Parameter</a></span> { <span class="name">name</span>: <span class="string">"maincolor"</span> <span class="name">value</span>: <span class="name">Qt</span>.<span class="name">vector3d</span>(<span class="name">root</span>.<span class="name">maincolor</span>.<span class="name">r</span>, <span class="name">root</span>.<span class="name">maincolor</span>.<span class="name">g</span>, <span class="name">root</span>.<span class="name">maincolor</span>.<span class="name">b</span>) } ] </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 <span class="type">string</span> <span class="name">vertex</span>: <span class="string">"qrc:/shaders/gl3/simpleColor.vert"</span> property <span class="type">string</span> <span class="name">fragment</span>: <span class="string">"qrc:/shaders/gl3/simpleColor.frag"</span> property <span class="type">string</span> <span class="name">vertexES</span>: <span class="string">"qrc:/shaders/es2/simpleColor.vert"</span> property <span class="type">string</span> <span class="name">fragmentES</span>: <span class="string">"qrc:/shaders/es2/simpleColor.frag"</span> </pre> <p>In the vertex shader we simply transform the position by the transformation matrices.</p> <pre class="qml"> 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="qml"> 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"> <span class="type"><a href="qml-qt3d-render-shaderprogram.html">ShaderProgram</a></span> { <span class="name">id</span>: <span class="name">gl3Shader</span> <span class="name">vertexShaderCode</span>: <span class="name">loadSource</span>(<span class="name">parent</span>.<span class="name">vertex</span>) <span class="name">fragmentShaderCode</span>: <span class="name">loadSource</span>(<span class="name">parent</span>.<span class="name">fragment</span>) } <span class="type"><a href="qml-qt3d-render-shaderprogram.html">ShaderProgram</a></span> { <span class="name">id</span>: <span class="name">es2Shader</span> <span class="name">vertexShaderCode</span>: <span class="name">loadSource</span>(<span class="name">parent</span>.<span class="name">vertexES</span>) <span class="name">fragmentShaderCode</span>: <span class="name">loadSource</span>(<span class="name">parent</span>.<span class="name">fragmentES</span>) } </pre> <p>Finally the shader programs are used in the Techniques corresponding to a specific Api profile.</p> <pre class="qml"> <span class="comment">// OpenGL 3.1</span> <span class="type"><a href="qml-qt3d-render-technique.html">Technique</a></span> { <span class="name">filterKeys</span>: [<span class="name">forward</span>] <span class="type">graphicsApiFilter</span> { <span class="name">api</span>: <span class="name">GraphicsApiFilter</span>.<span class="name">OpenGL</span> <span class="name">profile</span>: <span class="name">GraphicsApiFilter</span>.<span class="name">CoreProfile</span> <span class="name">majorVersion</span>: <span class="number">3</span> <span class="name">minorVersion</span>: <span class="number">1</span> } <span class="name">renderPasses</span>: <span class="name">RenderPass</span> { <span class="name">shaderProgram</span>: <span class="name">gl3Shader</span> } }, </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-qml.html">simplecustommaterial/main.qml</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-main-cpp.html">simplecustommaterial/main.cpp</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-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> 2017 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>