diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2020-11-26 09:50:59 +0100 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2020-11-26 14:49:22 +0100 |
commit | 6531a4e6c5d28c4eb18fffb7611fdc97d42f97b8 (patch) | |
tree | 88b3e8e58acf6aa1a2efc3a342ac028f2a1aca22 /examples | |
parent | 4ad2efac3e734f72c0c8d0ff9739106728fe6c98 (diff) |
advancedcustommaterial example: add RHI support
Pick-to: 6.0 6.0.0
Change-Id: I2e356cb893710c759a4d73eaf850ce39d46e5935
Reviewed-by: Mike Krus <mike.krus@kdab.com>
Diffstat (limited to 'examples')
5 files changed, 185 insertions, 0 deletions
diff --git a/examples/qt3d/advancedcustommaterial/CMakeLists.txt b/examples/qt3d/advancedcustommaterial/CMakeLists.txt index 19dde30f3..848991f2d 100644 --- a/examples/qt3d/advancedcustommaterial/CMakeLists.txt +++ b/examples/qt3d/advancedcustommaterial/CMakeLists.txt @@ -79,6 +79,8 @@ set(shaders_resource_files "shaders/es2/water.vert" "shaders/gl3/water.frag" "shaders/gl3/water.vert" + "shaders/rhi/water.frag" + "shaders/rhi/water.vert" ) qt6_add_resources(advancedcustommaterial "shaders" diff --git a/examples/qt3d/advancedcustommaterial/WaterMaterial.qml b/examples/qt3d/advancedcustommaterial/WaterMaterial.qml index b67e8322c..0627f6519 100644 --- a/examples/qt3d/advancedcustommaterial/WaterMaterial.qml +++ b/examples/qt3d/advancedcustommaterial/WaterMaterial.qml @@ -189,6 +189,8 @@ Material { property string fragment: "qrc:/shaders/gl3/water.frag" property string vertexES: "qrc:/shaders/es2/water.vert" property string fragmentES: "qrc:/shaders/es2/water.frag" + property string vertexRHI: "qrc:/shaders/rhi/water.vert" + property string fragmentRHI: "qrc:/shaders/rhi/water.frag" FilterKey { id: forward @@ -205,6 +207,11 @@ Material { vertexShaderCode: loadSource(parent.vertexES) fragmentShaderCode: loadSource(parent.fragmentES) } + ShaderProgram { + id: rhiShader + vertexShaderCode: loadSource(parent.vertexRHI) + fragmentShaderCode: loadSource(parent.fragmentRHI) + } AlphaCoverage { id: alphaCoverage } @@ -255,6 +262,21 @@ Material { shaderProgram: esShader renderStates: [ alphaCoverage ] } + }, + + // RHI + Technique { + filterKeys: [ forward ] + graphicsApiFilter { + api: GraphicsApiFilter.RHI + profile: GraphicsApiFilter.NoProfile + majorVersion: 1 + minorVersion: 0 + } + renderPasses: RenderPass { + shaderProgram: rhiShader + renderStates: [ alphaCoverage ] + } } ] } diff --git a/examples/qt3d/advancedcustommaterial/shaders.qrc b/examples/qt3d/advancedcustommaterial/shaders.qrc index 1fcce7ae3..18dea881a 100644 --- a/examples/qt3d/advancedcustommaterial/shaders.qrc +++ b/examples/qt3d/advancedcustommaterial/shaders.qrc @@ -4,5 +4,7 @@ <file>shaders/gl3/water.vert</file> <file>shaders/es2/water.frag</file> <file>shaders/es2/water.vert</file> + <file>shaders/rhi/water.frag</file> + <file>shaders/rhi/water.vert</file> </qresource> </RCC> diff --git a/examples/qt3d/advancedcustommaterial/shaders/rhi/water.frag b/examples/qt3d/advancedcustommaterial/shaders/rhi/water.frag new file mode 100644 index 000000000..5ba519c76 --- /dev/null +++ b/examples/qt3d/advancedcustommaterial/shaders/rhi/water.frag @@ -0,0 +1,80 @@ +#version 450 core + +// These includes will include the default Qt3D uniform buffers +#pragma include phong.inc.frag +#pragma include coordinatesystems.inc + +layout(location = 0) in vec3 worldPosition; +layout(location = 1) in vec3 worldNormal; +layout(location = 2) in vec4 worldTangent; +layout(location = 3) in vec2 texCoord; +layout(location = 4) in vec2 waveTexCoord; +layout(location = 5) in vec2 movtexCoord; +layout(location = 6) in vec2 multexCoord; +layout(location = 7)in vec2 skyTexCoord; +layout(location = 8) in vec3 vpos; + +layout(std140, binding = auto) uniform wave_uniforms { + float offsetx; + float offsety; + float vertYpos; + float waveheight; + float waveRandom; + float specularity; + float waveStrenght; + float normalAmount; + vec3 specularColor; + float shininess; + vec4 ka; +}; + +layout(binding = auto) uniform sampler2D diffuseTexture; +layout(binding = auto) uniform sampler2D specularTexture; +layout(binding = auto) uniform sampler2D normalTexture; +layout(binding = auto) uniform sampler2D waveTexture; +layout(binding = auto) uniform sampler2D skyTexture; +layout(binding = auto) uniform sampler2D foamTexture; + +layout(location = 0) out vec4 fragColor; + +void main() +{ + // Move waveTexCoords + vec2 waveMovCoord = waveTexCoord; + waveMovCoord.x += offsetx; + waveMovCoord.y -= offsety; + vec4 wave = texture(waveTexture, waveMovCoord); + + //Wiggle the newCoord by r and b colors of waveTexture + vec2 newCoord = texCoord; + newCoord.x += wave.r * waveStrenght; + newCoord.y -= wave.b * waveStrenght; + + // Sample the textures at the interpolated texCoords + // Use default texCoord for diffuse (it does not move on x or y, so it can be used as "ground under the water"). + vec4 diffuseTextureColor = texture(diffuseTexture, texCoord); + // 2 Animated Layers of specularTexture mixed with the newCoord + vec4 specularTextureColor = texture( specularTexture, multexCoord+newCoord) + (texture( specularTexture, movtexCoord+newCoord )); + // 2 Animated Layers of normalTexture mixed with the newCoord + vec3 tNormal = normalAmount * texture( normalTexture, movtexCoord+newCoord ).rgb - vec3( 1.0 )+(normalAmount * texture( normalTexture, multexCoord+newCoord ).rgb - vec3( 1.0 )); + // Animated skyTexture layer + vec4 skycolor = texture(skyTexture, skyTexCoord); + skycolor = skycolor * 0.4; + //Animated foamTexture layer + vec4 foamTextureColor = texture(foamTexture, texCoord); + + mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent); + mat3 invertTangentMatrix = transpose(tangentMatrix); + + vec3 wNormal = normalize(invertTangentMatrix * tNormal); + vec3 worldView = normalize(eyePosition - worldPosition); + + vec4 diffuse = vec4(diffuseTextureColor.rgb, vpos.y); + vec4 specular = vec4(specularTextureColor.a*specularity); + vec4 outputColor = phongFunction(ka, diffuse, specular, shininess, worldPosition, worldView, wNormal); + + outputColor += vec4(skycolor.rgb, vpos.y); + outputColor += (foamTextureColor.rgba*vpos.y); + + fragColor = vec4(outputColor.rgb,1.0); +} diff --git a/examples/qt3d/advancedcustommaterial/shaders/rhi/water.vert b/examples/qt3d/advancedcustommaterial/shaders/rhi/water.vert new file mode 100644 index 000000000..ffa08f6fe --- /dev/null +++ b/examples/qt3d/advancedcustommaterial/shaders/rhi/water.vert @@ -0,0 +1,79 @@ +#version 450 core + +// These includes will include the default Qt3D uniform buffers +#pragma include phong.inc.frag +#pragma include coordinatesystems.inc + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 vertexNormal; +layout(location = 2) in vec2 vertexTexCoord; +layout(location = 3) in vec4 vertexTangent; + +layout(location = 0) out vec3 worldPosition; +layout(location = 1) out vec3 worldNormal; +layout(location = 2) out vec4 worldTangent; +layout(location = 3) out vec2 texCoord; +layout(location = 4) out vec2 movtexCoord; +layout(location = 5) out vec2 multexCoord; +layout(location = 6) out vec2 waveTexCoord; +layout(location = 7) out vec2 skyTexCoord; +layout(location = 8) out vec3 vpos; + +layout(std140, binding = auto) uniform wave_uniforms { + float offsetx; + float offsety; + float vertYpos; + float waveheight; + float waveRandom; + float specularity; + float waveStrenght; + float normalAmount; + vec3 specularColor; + float shininess; + vec4 ka; +}; + +void main() +{ + // Scale texture coordinates for for fragment shader + texCoord = vertexTexCoord * texCoordScale; + movtexCoord = vertexTexCoord * texCoordScale; + multexCoord = vertexTexCoord * (texCoordScale*0.5); + waveTexCoord = vertexTexCoord * (texCoordScale * 6); + skyTexCoord = vertexTexCoord * (texCoordScale * 0.2); + + // Add Animated x and y Offset to SKY, MOV and MUL texCoords + movtexCoord = vec2(texCoord.x+offsetx,texCoord.y+offsety); + multexCoord = vec2(texCoord.x-offsetx,texCoord.y+offsety); + skyTexCoord = vec2(texCoord.x-(offsetx/2),texCoord.y-(offsety/2)); + + // Transform position, normal, and tangent to world coords + worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0)); + worldNormal = normalize(modelNormalMatrix * vertexNormal); + worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0))); + worldTangent.w = vertexTangent.w; + + // Calculate animated vertex positions + + float sinPos = (vertexPosition.z)+(vertexPosition.x); + float sinPos2 = (vertexPosition.y/2)+(vertexPosition.z); + vec3 vertMod = vec3(vertexPosition.x,vertexPosition.y,vertexPosition.z); + + vertMod = vec3(vertMod.x+=sin(vertYpos*2.2-sinPos2)*waveheight, + vertMod.y=sin(vertYpos*2.2+sinPos)*waveheight, + vertMod.z-=sin(vertYpos*2.2-cos(sinPos2))*waveheight); + + vec3 vertModCom = vec3(vertMod.x+=cos(vertYpos*2.2-cos(sinPos2))*waveheight, + vertMod.y=sin(vertYpos*2.2+cos(sinPos))*waveheight, + vertMod.z-=cos(vertYpos*2.2-cos(sinPos))*waveheight); + + + // Add wave animation only to vertices above world pos.y zero + if(vertexPosition.y < 0.0){vertModCom = vertexPosition;} + else{vertModCom = vertModCom;} + + vpos = vertModCom; + + // Calculate vertex position in clip coordinates + gl_Position = mvp * vec4(vertModCom, 1.0); +} |