What's the default fragment shader Qt uses with QShaderPrograms inside the premade Materials in Qt3DExtras?
-
As the title says, I noticed all premade materials have parameters like ka, ks, shiness and so on, even thought there's no fragment shader code inside any of the QShaderPrograms they hold.
-
As the title says, I noticed all premade materials have parameters like ka, ks, shiness and so on, even thought there's no fragment shader code inside any of the QShaderPrograms they hold.
Hi,
check it yourself with
fragmentShaderCode()
It returns the current fragment shader. So if you don't set your own, you see what is used.Or check the Qt source code.
-
As the title says, I noticed all premade materials have parameters like ka, ks, shiness and so on, even thought there's no fragment shader code inside any of the QShaderPrograms they hold.
@AhmedAhmedEG Actually I did this, I even gone a step ahead and serialized the whole material as json, and there's no fragment shader code at all, vertex shader code only that exists.
{ "Parameters": {}, "Effect": { "Parameters": { "ka": "PySide6.QtGui.QColor.fromRgbF(0.050004, 0.050004, 0.050004, 1.000000)", "kd": "PySide6.QtGui.QColor.fromRgbF(0.700008, 0.700008, 0.700008, 1.000000)", "ks": "PySide6.QtGui.QColor.fromRgbF(0.009995, 0.009995, 0.009995, 1.000000)", "shininess": "150.0", "texCoordScale": "1.0" }, "Techniques": [ { "Parameters": {}, "Filter Keys": { "renderingStyle": "forward" }, "API": "Api.OpenGL", "Extensions": "[]", "Major Version": 3, "Minor Version": 1, "Profile": "OpenGLProfile.CoreProfile", "Vendor": "", "Render Passes": [ { "Parameters": {}, "Filter Keys": {}, "Render States": "[<PySide6.Qt3DRender.Qt3DRender.QNoDepthMask(0x21257ba0850) at 0x000002125823FF80>, <PySide6.Qt3DRender.Qt3DRender.QBlendEquationArguments(0x21257ba06b0) at 0x000002125823FFC0>, <PySide6.Qt3DRender.Qt3DRender.QBlendEquation(0x21257ba0770) at 0x0000021258264040>]", "Shader Program": { "Format": "GLSL", "Vertex": "// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).\n// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause\n\n#version 150 core\n\nin vec3 vertexPosition;\nin vec3 vertexNormal;\nin vec4 vertexTangent;\nin vec2 vertexTexCoord;\n\nout vec3 worldPosition;\nout vec3 worldNormal;\nout vec4 worldTangent;\nout vec2 texCoord;\n\nuniform mat4 modelMatrix;\nuniform mat3 modelNormalMatrix;\nuniform mat4 modelViewProjection;\n\nuniform float texCoordScale;\n\nvoid main()\n{\n // Pass through scaled texture coordinates\n texCoord = vertexTexCoord * texCoordScale;\n\n // Transform position, normal, and tangent to world space\n worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));\n worldNormal = normalize(modelNormalMatrix * vertexNormal);\n worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));\n worldTangent.w = vertexTangent.w;\n\n // Calculate vertex position in clip coordinates\n gl_Position = modelViewProjection * vec4(vertexPosition, 1.0);\n}\n", "Fragment": "", "TessellationControl": "", "TessellationEvaluation": "", "Geometry": "", "Compute": "" } } ] }, { "Parameters": {}, "Filter Keys": { "renderingStyle": "forward" }, "API": "Api.OpenGL", "Extensions": "[]", "Major Version": 2, "Minor Version": 0, "Profile": "OpenGLProfile.NoProfile", "Vendor": "", "Render Passes": [ { "Parameters": {}, "Filter Keys": {}, "Render States": "[<PySide6.Qt3DRender.Qt3DRender.QNoDepthMask(0x21257ba0850) at 0x0000021258264500>, <PySide6.Qt3DRender.Qt3DRender.QBlendEquationArguments(0x21257ba06b0) at 0x0000021258264540>, <PySide6.Qt3DRender.Qt3DRender.QBlendEquation(0x21257ba0770) at 0x0000021258264580>]", "Shader Program": { "Format": "GLSL", "Vertex": "// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).\n// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause\n\nattribute vec3 vertexPosition;\nattribute vec3 vertexNormal;\nattribute vec4 vertexTangent;\nattribute vec2 vertexTexCoord;\n\nvarying vec3 worldPosition;\nvarying vec3 worldNormal;\nvarying vec4 worldTangent;\nvarying vec2 texCoord;\n\nuniform mat4 modelMatrix;\nuniform mat3 modelNormalMatrix;\nuniform mat4 modelViewProjection;\n\nuniform float texCoordScale;\n\nvoid main()\n{\n // Pass through texture coordinates\n texCoord = vertexTexCoord * texCoordScale;\n\n // Transform position, normal, and tangent to world coords\n worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));\n worldNormal = normalize(modelNormalMatrix * vertexNormal);\n worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));\n worldTangent.w = vertexTangent.w;\n\n // Calculate vertex position in clip coordinates\n gl_Position = modelViewProjection * vec4(vertexPosition, 1.0);\n}\n", "Fragment": "", "TessellationControl": "", "TessellationEvaluation": "", "Geometry": "", "Compute": "" } } ] }, { "Parameters": {}, "Filter Keys": { "renderingStyle": "forward" }, "API": "Api.OpenGLES", "Extensions": "[]", "Major Version": 2, "Minor Version": 0, "Profile": "OpenGLProfile.NoProfile", "Vendor": "", "Render Passes": [ { "Parameters": {}, "Filter Keys": {}, "Render States": "[<PySide6.Qt3DRender.Qt3DRender.QNoDepthMask(0x21257ba0850) at 0x0000021258264A00>, <PySide6.Qt3DRender.Qt3DRender.QBlendEquationArguments(0x21257ba06b0) at 0x0000021258264A40>, <PySide6.Qt3DRender.Qt3DRender.QBlendEquation(0x21257ba0770) at 0x0000021258264A80>]", "Shader Program": { "Format": "GLSL", "Vertex": "// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).\n// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause\n\nattribute vec3 vertexPosition;\nattribute vec3 vertexNormal;\nattribute vec4 vertexTangent;\nattribute vec2 vertexTexCoord;\n\nvarying vec3 worldPosition;\nvarying vec3 worldNormal;\nvarying vec4 worldTangent;\nvarying vec2 texCoord;\n\nuniform mat4 modelMatrix;\nuniform mat3 modelNormalMatrix;\nuniform mat4 modelViewProjection;\n\nuniform float texCoordScale;\n\nvoid main()\n{\n // Pass through texture coordinates\n texCoord = vertexTexCoord * texCoordScale;\n\n // Transform position, normal, and tangent to world coords\n worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));\n worldNormal = normalize(modelNormalMatrix * vertexNormal);\n worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));\n worldTangent.w = vertexTangent.w;\n\n // Calculate vertex position in clip coordinates\n gl_Position = modelViewProjection * vec4(vertexPosition, 1.0);\n}\n", "Fragment": "", "TessellationControl": "", "TessellationEvaluation": "", "Geometry": "", "Compute": "" } } ] }, { "Parameters": {}, "Filter Keys": { "renderingStyle": "forward" }, "API": "Api.RHI", "Extensions": "[]", "Major Version": 1, "Minor Version": 0, "Profile": "OpenGLProfile.NoProfile", "Vendor": "", "Render Passes": [ { "Parameters": {}, "Filter Keys": {}, "Render States": "[<PySide6.Qt3DRender.Qt3DRender.QNoDepthMask(0x21257ba0850) at 0x0000021258264F00>, <PySide6.Qt3DRender.Qt3DRender.QBlendEquationArguments(0x21257ba06b0) at 0x0000021258264F40>, <PySide6.Qt3DRender.Qt3DRender.QBlendEquation(0x21257ba0770) at 0x0000021258264F80>]", "Shader Program": { "Format": "GLSL", "Vertex": "// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).\n// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause\n\n#version 450\n\nlayout(location = 0) in vec3 vertexPosition;\nlayout(location = 1) in vec3 vertexNormal;\n\nlayout(location = 0) out vec3 worldPosition;\nlayout(location = 1) out vec3 worldNormal;\n\nlayout(std140, binding = 0) uniform qt3d_render_view_uniforms {\n mat4 viewMatrix;\n mat4 projectionMatrix;\n mat4 uncorrectedProjectionMatrix;\n mat4 clipCorrectionMatrix;\n mat4 viewProjectionMatrix;\n mat4 inverseViewMatrix;\n mat4 inverseProjectionMatrix;\n mat4 inverseViewProjectionMatrix;\n mat4 viewportMatrix;\n mat4 inverseViewportMatrix;\n vec4 textureTransformMatrix;\n vec3 eyePosition;\n float aspectRatio;\n float gamma;\n float exposure;\n float time;\n};\n\nlayout(std140, binding = 1) uniform qt3d_command_uniforms {\n mat4 modelMatrix;\n mat4 inverseModelMatrix;\n mat4 modelViewMatrix;\n mat3 modelNormalMatrix;\n mat4 inverseModelViewMatrix;\n mat4 modelViewProjection;\n mat4 inverseModelViewProjectionMatrix;\n};\n\nvoid main()\n{\n // Transform position, normal, and tangent to world space\n worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));\n worldNormal = normalize(modelNormalMatrix * vertexNormal);\n\n // Calculate vertex position in clip coordinates\n gl_Position = modelViewProjection * vec4(vertexPosition, 1.0);\n}\n", "Fragment": "", "TessellationControl": "", "TessellationEvaluation": "", "Geometry": "", "Compute": "" } } ] } ] } }
-
Hi,
check it yourself with
fragmentShaderCode()
It returns the current fragment shader. So if you don't set your own, you see what is used.Or check the Qt source code.
@Pl45m4 That material I serialized is the QDiffuseSpecularMaterial, and as you can see, it only holds vertex shader code targeting different apis and versions, this same behavior happens with all the other materials, I couldn't find any fragment shader code in any of them.
-
@Pl45m4 That material I serialized is the QDiffuseSpecularMaterial, and as you can see, it only holds vertex shader code targeting different apis and versions, this same behavior happens with all the other materials, I couldn't find any fragment shader code in any of them.
Cant tell you more than what I've said before.
Here is the source code -
Cant tell you more than what I've said before.
Here is the source code@Pl45m4 Ok, thanks a lot for staying with me, I checked the source you send, it has nothing to do with the fragment shader, but I found out something else, I looked at the source for the QDiffuseSpecularMaterial, and found out this weird function.
The source if set to a file, but using a weird function call "setFragmentShaderGraph", I know "setFragmentShader", but the "Graph" one is very new.
Turns out it used a class called QShaderProgramBuilder, I have no idea what's it thought.
-
Cant tell you more than what I've said before.
Here is the source code@Pl45m4 By accessing the children of the material, I found those QShaderProgramBuilder instances and I loaded that resource file and yah it's totally something I never expected
{ "nodes": [ { "uuid": "{00000000-0000-0000-0000-000000000001}", "type": "input", "parameters": { "name": "worldPosition", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" }, "location": "0" } }, { "uuid": "{00000000-0000-0000-0000-000000000002}", "type": "eyePosition" }, { "uuid": "{00000000-0000-0000-0000-000000000003}", "type": "input", "parameters": { "name": "worldNormal", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" }, "location": "1" } }, { "uuid": "{00000000-0000-0000-0000-000000000004}", "type": "input", "layers": [ "normalTexture" ], "parameters": { "name": "worldTangent", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec4" }, "location": "2" } }, { "uuid": "{00000000-0000-0000-0000-000000000005}", "type": "input", "layers": [ "diffuseTexture", "specularTexture", "normalTexture" ], "parameters": { "name": "texCoord", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec2" }, "location": "3" } }, { "uuid": "{00000000-0000-0000-0000-000000000006}", "type": "uniformInput", "parameters": { "name": "ka", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec4" } } }, { "uuid": "{00000000-0000-0000-0000-000000000007}", "type": "uniformInput", "layers": [ "diffuse" ], "parameters": { "name": "kd", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec4" } } }, { "uuid": "{00000000-0000-0000-0000-000000000008}", "type": "sampleTexture", "layers": [ "diffuseTexture" ], "parameters": { "name": "diffuseTexture" } }, { "uuid": "{00000000-0000-0000-0000-000000000009}", "type": "uniformInput", "layers": [ "specular" ], "parameters": { "name": "ks", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec4" } } }, { "uuid": "{00000000-0000-0000-0000-000000000010}", "layers": [ "specularTexture" ], "type": "sampleTexture", "parameters": { "name": "specularTexture" } }, { "uuid": "{00000000-0000-0000-0000-000000000011}", "type": "uniformInput", "parameters": { "name": "shininess", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Float" } } }, { "uuid": "{00000000-0000-0000-0000-000000000012}", "type": "subtract", "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000013}", "type": "normalize", "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000014}", "type": "normalize", "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000015}", "type": "worldSpaceToTangentSpaceMatrix", "layers": [ "normalTexture" ] }, { "uuid": "{00000000-0000-0000-0000-000000000016}", "type": "transpose", "layers": [ "normalTexture" ], "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Mat3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000017}", "type": "sampleTexture", "layers": [ "normalTexture" ], "parameters": { "name": "normalTexture" } }, { "uuid": "{00000000-0000-0000-0000-000000000018}", "type": "swizzle", "layers": [ "normalTexture" ], "parameters": { "fields": "rgb", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000019}", "type": "constant", "layers": [ "normalTexture" ], "parameters": { "constant": "2.0", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Float" } } }, { "uuid": "{00000000-0000-0000-0000-000000000020}", "type": "multiply", "layers": [ "normalTexture" ], "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000021}", "type": "constant", "layers": [ "normalTexture" ], "parameters": { "constant": "1.0", "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000022}", "type": "subtract", "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000023}", "type": "multiply", "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3" } } }, { "uuid": "{00000000-0000-0000-0000-000000000024}", "type": "phongFunction" }, { "uuid": "{00000000-0000-0000-0000-000000000025}", "type": "fragColor" } ], "edges": [ { "sourceUuid": "{00000000-0000-0000-0000-000000000001}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "worldPosition" }, { "sourceUuid": "{00000000-0000-0000-0000-000000000001}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000012}", "targetPort": "subtrahend" }, { "sourceUuid": "{00000000-0000-0000-0000-000000000002}", "sourcePort": "eyePosition", "targetUuid": "{00000000-0000-0000-0000-000000000012}", "targetPort": "minuend" }, { "sourceUuid": "{00000000-0000-0000-0000-000000000012}", "sourcePort": "difference", "targetUuid": "{00000000-0000-0000-0000-000000000013}", "targetPort": "input" }, { "sourceUuid": "{00000000-0000-0000-0000-000000000013}", "sourcePort": "output", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "worldView" }, { "sourceUuid": "{00000000-0000-0000-0000-000000000003}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000014}", "targetPort": "input", "layers": [ "normal" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000003}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000015}", "targetPort": "worldNormal", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000004}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000015}", "targetPort": "worldTangent", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000015}", "sourcePort": "matrix", "targetUuid": "{00000000-0000-0000-0000-000000000016}", "targetPort": "input", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000016}", "sourcePort": "output", "targetUuid": "{00000000-0000-0000-0000-000000000023}", "targetPort": "first", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000005}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000017}", "targetPort": "coord", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000017}", "sourcePort": "color", "targetUuid": "{00000000-0000-0000-0000-000000000018}", "targetPort": "input", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000018}", "sourcePort": "output", "targetUuid": "{00000000-0000-0000-0000-000000000020}", "targetPort": "first", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000019}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000020}", "targetPort": "second", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000020}", "sourcePort": "product", "targetUuid": "{00000000-0000-0000-0000-000000000022}", "targetPort": "minuend", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000021}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000022}", "targetPort": "subtrahend", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000022}", "sourcePort": "difference", "targetUuid": "{00000000-0000-0000-0000-000000000023}", "targetPort": "second", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000023}", "sourcePort": "product", "targetUuid": "{00000000-0000-0000-0000-000000000014}", "targetPort": "input", "layers": [ "normalTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000014}", "sourcePort": "output", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "worldNormal" }, { "sourceUuid": "{00000000-0000-0000-0000-000000000006}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "ambient" }, { "sourceUuid": "{00000000-0000-0000-0000-000000000007}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "diffuse", "layers": [ "diffuse" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000005}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000008}", "targetPort": "coord", "layers": [ "diffuseTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000008}", "sourcePort": "color", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "diffuse", "layers": [ "diffuseTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000009}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "specular", "layers": [ "specular" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000005}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000010}", "targetPort": "coord", "layers": [ "specularTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000010}", "sourcePort": "color", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "specular", "layers": [ "specularTexture" ] }, { "sourceUuid": "{00000000-0000-0000-0000-000000000011}", "sourcePort": "value", "targetUuid": "{00000000-0000-0000-0000-000000000024}", "targetPort": "shininess" }, { "sourceUuid": "{00000000-0000-0000-0000-000000000024}", "sourcePort": "outputColor", "targetUuid": "{00000000-0000-0000-0000-000000000025}", "targetPort": "fragColor" } ] }
And I found this that I am about to read: https://www.kdab.com/wp-content/uploads/stories/qt3d_node_editor_and_shader_generator.pdf
-
And here's the generated fragment shader code from the cache directory.
#version 450 #define LAYER_diffuse #define LAYER_specular #define LAYER_normal layout(location = 0) in vec3 worldPosition; layout(location = 1) in vec3 worldNormal; // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause layout(std140, binding = 0) uniform qt3d_render_view_uniforms { mat4 viewMatrix; mat4 projectionMatrix; mat4 uncorrectedProjectionMatrix; mat4 clipCorrectionMatrix; mat4 viewProjectionMatrix; mat4 inverseViewMatrix; mat4 inverseProjectionMatrix; mat4 inverseViewProjectionMatrix; mat4 viewportMatrix; mat4 inverseViewportMatrix; vec4 textureTransformMatrix; vec3 eyePosition; float aspectRatio; float gamma; float exposure; float time; }; layout(std140, binding = 1) uniform qt3d_command_uniforms { mat4 modelMatrix; mat4 inverseModelMatrix; mat4 modelViewMatrix; mat3 modelNormalMatrix; mat4 inverseModelViewMatrix; mat4 mvp; mat4 inverseModelViewProjectionMatrix; }; layout(std140, binding = 2) uniform qt3d_extras_uniforms { float texCoordScale; }; #line 2 const int MAX_LIGHTS = 8; const int TYPE_POINT = 0; const int TYPE_DIRECTIONAL = 1; const int TYPE_SPOT = 2; struct Light { vec3 position; float intensity; vec3 color; float constantAttenuation; vec3 direction; float linearAttenuation; float quadraticAttenuation; float cutOffAngle; int type; }; layout(std140, binding = 3) uniform qt3d_light_uniforms { Light lights[MAX_LIGHTS]; int lightCount; int envLightCount; }; // Pre-convolved environment maps layout(binding = 4) uniform samplerCube envLight_irradiance; // For diffuse contribution layout(binding = 5) uniform samplerCube envLight_specular; // For specular contribution #line 5 void adsModel(const in vec3 worldPos, const in vec3 worldNormal, const in vec3 worldView, const in float shininess, out vec3 diffuseColor, out vec3 specularColor) { diffuseColor = vec3(0.0); specularColor = vec3(0.0); // We perform all work in world space vec3 n = normalize(worldNormal); vec3 s = vec3(0.0); for (int i = 0; i < lightCount; ++i) { float att = 1.0; float sDotN = 0.0; if (lights[i].type != TYPE_DIRECTIONAL) { // Point and Spot lights // Light position is already in world space vec3 sUnnormalized = lights[i].position - worldPos; s = normalize(sUnnormalized); // Light direction // Calculate the attenuation factor sDotN = dot(s, n); if (sDotN > 0.0) { if (lights[i].constantAttenuation != 0.0 || lights[i].linearAttenuation != 0.0 || lights[i].quadraticAttenuation != 0.0) { float dist = length(sUnnormalized); att = 1.0 / (lights[i].constantAttenuation + lights[i].linearAttenuation * dist + lights[i].quadraticAttenuation * dist * dist); } // The light direction is in world space already if (lights[i].type == TYPE_SPOT) { // Check if fragment is inside or outside of the spot light cone if (degrees(acos(dot(-s, lights[i].direction))) > lights[i].cutOffAngle) sDotN = 0.0; } } } else { // Directional lights // The light direction is in world space already s = normalize(-lights[i].direction); sDotN = dot(s, n); } // Calculate the diffuse factor float diffuse = max(sDotN, 0.0); // Calculate the specular factor float specular = 0.0; if (diffuse > 0.0 && shininess > 0.0) { float normFactor = (shininess + 2.0) / 2.0; vec3 r = reflect(-s, n); // Reflection direction in world space specular = normFactor * pow(max(dot(r, worldView), 0.0), shininess); } // Accumulate the diffuse and specular contributions diffuseColor += att * lights[i].intensity * diffuse * lights[i].color; specularColor += att * lights[i].intensity * specular * lights[i].color; } } vec4 phongFunction(const in vec4 ambient, const in vec4 diffuse, const in vec4 specular, const in float shininess, const in vec3 worldPosition, const in vec3 worldView, const in vec3 worldNormal) { // Calculate the lighting model, keeping the specular component separate vec3 diffuseColor, specularColor; adsModel(worldPosition, worldNormal, worldView, shininess, diffuseColor, specularColor); // Combine spec with ambient+diffuse for final fragment color vec3 color = (ambient.rgb + diffuseColor) * diffuse.rgb + specularColor * specular.rgb; return vec4(color, diffuse.a); } layout(location = 0) out vec4 fragColor; layout(std140, binding = 6) uniform qt3d_shadergraph_generated_uniforms { float shininess; vec4 ks; vec4 kd; vec4 ka; }; void main() { fragColor = (((((((phongFunction(ka, kd, ks, shininess, worldPosition, normalize(((eyePosition - worldPosition))), normalize(worldNormal))))))))); }