diff options
author | Antti Määttä <antti.maatta@qt.io> | 2017-02-06 13:41:56 +0200 |
---|---|---|
committer | Antti Määttä <antti.maatta@qt.io> | 2017-02-28 07:05:25 +0000 |
commit | 63d417d4ee7611d6453289db62b82da9ac0a39a5 (patch) | |
tree | 170f06273325f6e04091534a0d5cf077009b3cb8 /examples/qt3d/advancedcustommaterial/shaders | |
parent | f4601e86d20a39c9d41500bd8378e990f4739506 (diff) |
Add advanced custom material example
Change-Id: I6c5ae0e29960aaab2d0babfe325a9f6483729600
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'examples/qt3d/advancedcustommaterial/shaders')
4 files changed, 322 insertions, 0 deletions
diff --git a/examples/qt3d/advancedcustommaterial/shaders/es2/water.frag b/examples/qt3d/advancedcustommaterial/shaders/es2/water.frag new file mode 100644 index 000000000..ea54a87f1 --- /dev/null +++ b/examples/qt3d/advancedcustommaterial/shaders/es2/water.frag @@ -0,0 +1,72 @@ +#define FP highp + +varying FP vec3 worldPosition; +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 mat3 tangentMatrix; +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 vec3 ka; +uniform FP vec3 specularColor; +uniform FP float shininess; +uniform FP float normalAmount; +uniform FP vec3 eyePosition; + +#pragma include light.inc.frag + +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 normal = 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); + + // Calculate the lighting model, keeping the specular component separate + FP vec3 diffuseColor, specularColor; + adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor); + + // Combine final fragment color + FP vec4 outputColor = vec4(((skycolor.rgb + ka + diffuseTextureColor.rgb * (diffuseColor))+(specularColor * specularTextureColor.a*specularity)), vpos.y ); + + + outputColor += (foamTextureColor.rgba*vpos.y); + + gl_FragColor = vec4(outputColor.rgb,1.0); +} + diff --git a/examples/qt3d/advancedcustommaterial/shaders/es2/water.vert b/examples/qt3d/advancedcustommaterial/shaders/es2/water.vert new file mode 100644 index 000000000..1a7d46a46 --- /dev/null +++ b/examples/qt3d/advancedcustommaterial/shaders/es2/water.vert @@ -0,0 +1,88 @@ +#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 vec2 texCoord; +varying FP vec2 movtexCoord; +varying FP vec2 multexCoord; +varying FP vec2 waveTexCoord; +varying FP vec2 skyTexCoord; +varying FP mat3 tangentMatrix; +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)); + FP vec3 normal = normalize(modelNormalMatrix * vertexNormal); + FP vec3 tangent = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0))); + + // Make the tangent truly orthogonal to the normal by using Gram-Schmidt. + // This allows to build the tangentMatrix below by simply transposing the + // tangent -> world space matrix (which would now be orthogonal) + tangent = normalize(tangent - dot(tangent, normal) * normal); + + // 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 binormal = cross(normal, tangent) * vertexTangent.w; + + // Construct matrix to transform from eye coords to tangent space + tangentMatrix = mat3( + tangent.x, binormal.x, normal.x, + tangent.y, binormal.y, normal.y, + tangent.z, binormal.z, normal.z); + + // 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/examples/qt3d/advancedcustommaterial/shaders/gl3/water.frag b/examples/qt3d/advancedcustommaterial/shaders/gl3/water.frag new file mode 100644 index 000000000..ae40bdf37 --- /dev/null +++ b/examples/qt3d/advancedcustommaterial/shaders/gl3/water.frag @@ -0,0 +1,74 @@ +#version 150 core + +in vec3 worldPosition; +in vec2 texCoord; +in vec2 waveTexCoord; +in vec2 movtexCoord; +in vec2 multexCoord; +in vec2 skyTexCoord; + +in vec3 vpos; + +in mat3 tangentMatrix; +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 vec3 ka; +uniform vec3 specularColor; +uniform float shininess; +uniform float normalAmount; +uniform vec3 eyePosition; + +out vec4 fragColor; + +#pragma include light.inc.frag + +void main() +{ + // Move waveTexCoords + vec2 waveMovCoord = waveTexCoord; + waveMovCoord.x += offsetx; + waveMovCoord.y -= offsety; + vec4 wave = texture2D(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 normal = 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); + + // Calculate the lighting model, keeping the specular component separate + vec3 diffuseColor, specularColor; + adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor); + + // Combine final fragment color + vec4 outputColor = vec4(((skycolor.rgb + ka + diffuseTextureColor.rgb * (diffuseColor))+(specularColor * specularTextureColor.a*specularity)), vpos.y ); + + + outputColor += (foamTextureColor.rgba*vpos.y); + + fragColor = vec4(outputColor.rgb,1.0); +} + diff --git a/examples/qt3d/advancedcustommaterial/shaders/gl3/water.vert b/examples/qt3d/advancedcustommaterial/shaders/gl3/water.vert new file mode 100644 index 000000000..a4ddcf62b --- /dev/null +++ b/examples/qt3d/advancedcustommaterial/shaders/gl3/water.vert @@ -0,0 +1,88 @@ +#version 150 core + +in vec3 vertexPosition; +in vec3 vertexNormal; +in vec2 vertexTexCoord; +in vec4 vertexTangent; + +out vec3 worldPosition; +out vec2 texCoord; +out vec2 movtexCoord; +out vec2 multexCoord; +out vec2 waveTexCoord; +out vec2 skyTexCoord; +out mat3 tangentMatrix; +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)); + vec3 normal = normalize(modelNormalMatrix * vertexNormal); + vec3 tangent = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0))); + + // Make the tangent truly orthogonal to the normal by using Gram-Schmidt. + // This allows to build the tangentMatrix below by simply transposing the + // tangent -> world space matrix (which would now be orthogonal) + tangent = normalize(tangent - dot(tangent, normal) * normal); + + // 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 binormal = cross(normal, tangent) * vertexTangent.w; + + // Construct matrix to transform from eye coords to tangent space + tangentMatrix = mat3( + tangent.x, binormal.x, normal.x, + tangent.y, binormal.y, normal.y, + tangent.z, binormal.z, normal.z); + + // 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); +} |