diff options
Diffstat (limited to 'basicsuite/advancedcustommaterial/shaders')
10 files changed, 766 insertions, 0 deletions
diff --git a/basicsuite/advancedcustommaterial/shaders/es2/coordinatesystems.inc b/basicsuite/advancedcustommaterial/shaders/es2/coordinatesystems.inc new file mode 100644 index 0000000..5f6ee40 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/es2/coordinatesystems.inc @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#define FP highp + +FP mat3 transpose(const in FP mat3 inputMatrix) +{ + FP vec3 i0 = inputMatrix[0]; + FP vec3 i1 = inputMatrix[1]; + FP vec3 i2 = inputMatrix[2]; + + FP mat3 outputMatrix = mat3( + vec3(i0.x, i1.x, i2.x), + vec3(i0.y, i1.y, i2.y), + vec3(i0.z, i1.z, i2.z) + ); + + return outputMatrix; +} + +FP mat3 calcWorldSpaceToTangentSpaceMatrix(const in FP vec3 wNormal, const in FP vec4 wTangent) +{ + // Make the tangent truly orthogonal to the normal by using Gram-Schmidt. + // This allows to build the tangentMatrix below by simply transposing the + // tangent -> eyespace matrix (which would now be orthogonal) + FP vec3 wFixedTangent = normalize(wTangent.xyz - dot(wTangent.xyz, wNormal) * wNormal); + + // Calculate binormal vector. No "real" need to renormalize it, + // as built by crossing two normal vectors. + // To orient the binormal correctly, use the fourth coordinate of the tangent, + // which is +1 for a right hand system, and -1 for a left hand system. + FP vec3 wBinormal = cross(wNormal, wFixedTangent.xyz) * wTangent.w; + + // Construct matrix to transform from world space to tangent space + // This is the transpose of the tangentToWorld transformation matrix + FP mat3 tangentToWorldMatrix = mat3(wFixedTangent, wBinormal, wNormal); + FP mat3 worldToTangentMatrix = transpose(tangentToWorldMatrix); + return worldToTangentMatrix; +} + diff --git a/basicsuite/advancedcustommaterial/shaders/es2/light.inc.frag b/basicsuite/advancedcustommaterial/shaders/es2/light.inc.frag new file mode 100644 index 0000000..167ff30 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/es2/light.inc.frag @@ -0,0 +1,17 @@ +#define FP highp + +const int MAX_LIGHTS = 8; +const int TYPE_POINT = 0; +const int TYPE_DIRECTIONAL = 1; +const int TYPE_SPOT = 2; +struct Light { + int type; + FP vec3 position; + FP vec3 color; + FP float intensity; + FP vec3 direction; + FP vec3 attenuation; + FP float cutOffAngle; +}; +uniform Light lights[MAX_LIGHTS]; +uniform int lightCount; diff --git a/basicsuite/advancedcustommaterial/shaders/es2/phong.inc.frag b/basicsuite/advancedcustommaterial/shaders/es2/phong.inc.frag new file mode 100644 index 0000000..9d17b68 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/es2/phong.inc.frag @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#pragma include light.inc.frag + +void adsModel(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3 vview, const in FP float shininess, + out FP vec3 diffuseColor, out FP vec3 specularColor) +{ + diffuseColor = vec3(0.0); + specularColor = vec3(0.0); + + FP vec3 n = normalize( vnormal ); + + FP vec3 s; + Light light; + for (int i = 0; i < lightCount; ++i) { + if (i == 0) + light = lights[0]; + else if (i == 1) + light = lights[1]; + else if (i == 2) + light = lights[2]; + else if (i == 3) + light = lights[3]; + else if (i == 4) + light = lights[4]; + else if (i == 5) + light = lights[5]; + else if (i == 6) + light = lights[6]; + else if (i == 7) + light = lights[7]; + + FP float att = 1.0; + if ( light.type != TYPE_DIRECTIONAL ) { + s = light.position - vpos; + if (length( light.attenuation ) != 0.0) { + FP float dist = length(s); + att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist); + } + s = normalize( s ); + if ( light.type == TYPE_SPOT ) { + if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle) + att = 0.0; + } + } else { + s = normalize( -light.direction ); + } + + FP float diffuse = max( dot( s, n ), 0.0 ); + + FP float specular = 0.0; + if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) { + FP vec3 r = reflect( -s, n ); + FP float normFactor = ( shininess + 2.0 ) / 2.0; + specular = normFactor * pow( max( dot( r, vview ), 0.0 ), shininess ); + } + + diffuseColor += att * light.intensity * diffuse * light.color; + specularColor += att * light.intensity * specular * light.color; + } +} + +FP vec4 phongFunction(const in FP vec4 ambient, + const in FP vec4 diffuse, + const in FP vec4 specular, + const in FP float shininess, + const in FP vec3 worldPosition, + const in FP vec3 worldView, + const in FP vec3 worldNormal) +{ + // Calculate the lighting model, keeping the specular component separate + FP vec3 diffuseColor, specularColor; + adsModel(worldPosition, worldNormal, worldView, shininess, diffuseColor, specularColor); + + // Combine spec with ambient+diffuse for final fragment color + FP vec3 color = (ambient.rgb + diffuseColor) * diffuse.rgb + + specularColor * specular.rgb; + + return vec4(color, diffuse.a); +} diff --git a/basicsuite/advancedcustommaterial/shaders/es2/water.frag b/basicsuite/advancedcustommaterial/shaders/es2/water.frag new file mode 100644 index 0000000..f471504 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/es2/water.frag @@ -0,0 +1,76 @@ +#define FP highp + +varying FP vec3 worldPosition; +varying FP vec3 worldNormal; +varying FP vec4 worldTangent; +varying FP vec2 texCoord; +varying FP vec2 waveTexCoord; +varying FP vec2 movtexCoord; +varying FP vec2 multexCoord; +varying FP vec2 skyTexCoord; + +varying FP vec3 vpos; + +varying FP vec3 color; + +uniform FP sampler2D diffuseTexture; +uniform FP sampler2D specularTexture; +uniform FP sampler2D normalTexture; +uniform FP sampler2D waveTexture; +uniform FP sampler2D skyTexture; +uniform FP sampler2D foamTexture; + +uniform FP float offsetx; +uniform FP float offsety; +uniform FP float specularity; +uniform FP float waveStrenght; +uniform FP vec4 ka; +uniform FP float shininess; +uniform FP float normalAmount; +uniform FP vec3 eyePosition; + +#pragma include phong.inc.frag +#pragma include coordinatesystems.inc + +void main() +{ + // Move waveTexCoords + FP vec2 waveMovCoord = waveTexCoord; + waveMovCoord.x += offsetx; + waveMovCoord.y -= offsety; + FP vec4 wave = texture2D(waveTexture, waveMovCoord); + + //Wiggle the newCoord by r and b colors of waveTexture + FP 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"). + FP vec4 diffuseTextureColor = texture2D(diffuseTexture, texCoord); + // 2 Animated Layers of specularTexture mixed with the newCoord + FP vec4 specularTextureColor = texture2D( specularTexture, multexCoord+newCoord) + (texture2D( specularTexture, movtexCoord+newCoord )); + // 2 Animated Layers of normalTexture mixed with the newCoord + FP vec3 tNormal = normalAmount * texture2D( normalTexture, movtexCoord+newCoord ).rgb - vec3( 1.0 )+(normalAmount * texture2D( normalTexture, multexCoord+newCoord ).rgb - vec3( 1.0 )); + // Animated skyTexture layer + FP vec4 skycolor = texture2D(skyTexture, skyTexCoord); + skycolor = skycolor * 0.4; + //Animated foamTexture layer + FP vec4 foamTextureColor = texture2D(foamTexture, texCoord); + + FP mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent); + FP mat3 invertTangentMatrix = transpose(tangentMatrix); + + FP vec3 wNormal = normalize(invertTangentMatrix * tNormal); + FP vec3 worldView = normalize(eyePosition - worldPosition); + + FP vec4 diffuse = vec4(diffuseTextureColor.rgb, vpos.y); + FP vec4 specular = vec4(specularTextureColor.a*specularity); + FP vec4 outputColor = phongFunction(ka, diffuse, specular, shininess, worldPosition, worldView, wNormal); + + outputColor += vec4(skycolor.rgb, vpos.y); + outputColor += (foamTextureColor.rgba*vpos.y); + + gl_FragColor = vec4(outputColor.rgb,1.0); +} + diff --git a/basicsuite/advancedcustommaterial/shaders/es2/water.vert b/basicsuite/advancedcustommaterial/shaders/es2/water.vert new file mode 100644 index 0000000..76dd3f8 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/es2/water.vert @@ -0,0 +1,73 @@ +#define FP highp + +attribute FP vec3 vertexPosition; +attribute FP vec3 vertexNormal; +attribute FP vec2 vertexTexCoord; +attribute FP vec4 vertexTangent; + +varying FP vec3 worldPosition; +varying FP vec3 worldNormal; +varying FP vec4 worldTangent; +varying FP vec2 texCoord; +varying FP vec2 movtexCoord; +varying FP vec2 multexCoord; +varying FP vec2 waveTexCoord; +varying FP vec2 skyTexCoord; +varying FP vec3 vpos; + +uniform FP mat4 modelMatrix; +uniform FP mat3 modelNormalMatrix; +uniform FP mat4 mvp; + +uniform FP float offsetx; +uniform FP float offsety; +uniform FP float vertYpos; +uniform FP float texCoordScale; +uniform FP float waveheight; +uniform FP float waveRandom; + + +void main() +{ + // Scale texture coordinates for for fragment shader + texCoord = vertexTexCoord * texCoordScale; + movtexCoord = vertexTexCoord * texCoordScale; + multexCoord = vertexTexCoord * (texCoordScale*0.5); + waveTexCoord = vertexTexCoord * (texCoordScale * 6.0); + 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.0),texCoord.y-(offsety/2.0)); + + // 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 + + FP float sinPos = (vertexPosition.z)+(vertexPosition.x); + FP float sinPos2 = (vertexPosition.y/2.0)+(vertexPosition.z); + FP 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); + + FP 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); +} diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/coordinatesystems.inc b/basicsuite/advancedcustommaterial/shaders/gl3/coordinatesystems.inc new file mode 100644 index 0000000..ed3d2cb --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/coordinatesystems.inc @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +mat3 calcWorldSpaceToTangentSpaceMatrix(const in vec3 wNormal, const in vec4 wTangent) +{ + // Make the tangent truly orthogonal to the normal by using Gram-Schmidt. + // This allows to build the tangentMatrix below by simply transposing the + // tangent -> eyespace matrix (which would now be orthogonal) + vec3 wFixedTangent = normalize(wTangent.xyz - dot(wTangent.xyz, wNormal) * wNormal); + + // Calculate binormal vector. No "real" need to renormalize it, + // as built by crossing two normal vectors. + // To orient the binormal correctly, use the fourth coordinate of the tangent, + // which is +1 for a right hand system, and -1 for a left hand system. + vec3 wBinormal = cross(wNormal, wFixedTangent.xyz) * wTangent.w; + + // Construct matrix to transform from world space to tangent space + // This is the transpose of the tangentToWorld transformation matrix + mat3 tangentToWorldMatrix = mat3(wFixedTangent, wBinormal, wNormal); + mat3 worldToTangentMatrix = transpose(tangentToWorldMatrix); + return worldToTangentMatrix; +} + diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/light.inc.frag b/basicsuite/advancedcustommaterial/shaders/gl3/light.inc.frag new file mode 100644 index 0000000..0b64263 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/light.inc.frag @@ -0,0 +1,25 @@ +const int MAX_LIGHTS = 8; +const int TYPE_POINT = 0; +const int TYPE_DIRECTIONAL = 1; +const int TYPE_SPOT = 2; +struct Light { + int type; + vec3 position; + vec3 color; + float intensity; + vec3 direction; + float constantAttenuation; + float linearAttenuation; + float quadraticAttenuation; + float cutOffAngle; +}; +uniform Light lights[MAX_LIGHTS]; +uniform int lightCount; + +// Pre-convolved environment maps +struct EnvironmentLight { + samplerCube irradiance; // For diffuse contribution + samplerCube specular; // For specular contribution +}; +uniform EnvironmentLight envLight; +uniform int envLightCount = 0; diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/phong.inc.frag b/basicsuite/advancedcustommaterial/shaders/gl3/phong.inc.frag new file mode 100644 index 0000000..47a6ecd --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/phong.inc.frag @@ -0,0 +1,138 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#pragma include light.inc.frag + +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); +} diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/water.frag b/basicsuite/advancedcustommaterial/shaders/gl3/water.frag new file mode 100644 index 0000000..6e97223 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/water.frag @@ -0,0 +1,79 @@ +#version 150 core + +in vec3 worldPosition; +in vec3 worldNormal; +in vec4 worldTangent; +in vec2 texCoord; +in vec2 waveTexCoord; +in vec2 movtexCoord; +in vec2 multexCoord; +in vec2 skyTexCoord; + +in vec3 vpos; + +in vec3 color; + +uniform sampler2D diffuseTexture; +uniform sampler2D specularTexture; +uniform sampler2D normalTexture; +uniform sampler2D waveTexture; +uniform sampler2D skyTexture; +uniform sampler2D foamTexture; + +uniform float offsetx; +uniform float offsety; +uniform float specularity; +uniform float waveStrenght; +uniform vec4 ka; +uniform vec3 specularColor; +uniform float shininess; +uniform float normalAmount; +uniform vec3 eyePosition; + +out vec4 fragColor; + +#pragma include phong.inc.frag +#pragma include coordinatesystems.inc + +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/basicsuite/advancedcustommaterial/shaders/gl3/water.vert b/basicsuite/advancedcustommaterial/shaders/gl3/water.vert new file mode 100644 index 0000000..3af37e9 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/water.vert @@ -0,0 +1,73 @@ +#version 150 core + +in vec3 vertexPosition; +in vec3 vertexNormal; +in vec2 vertexTexCoord; +in vec4 vertexTangent; + +out vec3 worldPosition; +out vec3 worldNormal; +out vec4 worldTangent; +out vec2 texCoord; +out vec2 movtexCoord; +out vec2 multexCoord; +out vec2 waveTexCoord; +out vec2 skyTexCoord; +out vec3 vpos; + +uniform mat4 modelMatrix; +uniform mat3 modelNormalMatrix; +uniform mat4 mvp; + +uniform float offsetx; +uniform float offsety; +uniform float vertYpos; +uniform float texCoordScale; +uniform float waveheight; +uniform float waveRandom; + + +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); +} |