42

« **on:** August 11, 2015, 01:03:44 pm »
**The first shaders:**

vertex shader-

`uniform mat4 modelViewMatrix;`

uniform mat4 modelViewProjectionMatrix;

uniform mat4 textureMatrix;

uniform vec4 ambientColor;

uniform vec4 additionalColor;

uniform vec3 myLightPosition;

uniform vec3 myLightDiffuseColor;

uniform float myAttenuation;

uniform mat4 skinPalette[17];

attribute vec4 position;

attribute vec3 normal;

attribute vec2 texture0;

attribute vec4 skinWeights;

attribute vec4 skinJointIndices;

varying vec2 texCoord[2];

varying vec4 vertexColor;

const vec4 WHITE = vec4(1,1,1,1);

void main() {

texCoord[0] = texture0;

texCoord[1] = (textureMatrix * vec4(texture0, 0, 1)).xy;

vertexColor = ambientColor + additionalColor;

vec4 myPosition = vec4(0,0,0,0);

vec3 myNormal = vec3(0,0,0);

vec4 vertexTemp;

vec3 normalTemp;

mat4 mat;

mat = skinPalette[int(skinJointIndices[0])];

vertexTemp = mat * position;

vertexTemp *= skinWeights.x;

myPosition += vertexTemp;

normalTemp = mat3(mat) * normal;

normalTemp *= skinWeights.x;

myNormal += normalTemp;

mat = skinPalette[int(skinJointIndices[1])];

vertexTemp = mat * position;

vertexTemp *= skinWeights.y;

myPosition += vertexTemp;

normalTemp = mat3(mat) * normal;

normalTemp *= skinWeights.y;

myNormal += normalTemp;

myPosition.yz *= -1.0;

myNormal.yz *= -1.0;

vec4 vertexPos = modelViewMatrix * myPosition;

vec3 normalEye = normalize(modelViewMatrix * vec4(myNormal, 0.0)).xyz;

float angle = dot(normalEye, normalize(myLightPosition - vertexPos.xyz));

//if (angle > 0.0) {

vertexColor += vec4((myLightDiffuseColor * angle)*(1.0/(1.0+length(myLightPosition - vertexPos.xyz)*myAttenuation)), 1);

//}

gl_Position = modelViewProjectionMatrix * myPosition;

}

fragment shader-

`precision mediump float;`

uniform sampler2D textureUnit0;

uniform sampler2D textureUnit1;

varying vec2 texCoord[2];

varying vec4 vertexColor;

const vec4 WHITE = vec4(1,1,1,1);

void main() {

vec4 col = texture2D(textureUnit0, texCoord[0]) * vertexColor;

vec4 blend = texture2D(textureUnit1, texCoord[1]) * 0.7;

col = min(col / (1.0 - blend), 1.0);

gl_FragColor = col;

}

**The second shaders:**

vertex shader-

`uniform mat4 modelViewMatrix;`

uniform mat4 modelViewProjectionMatrix;

uniform mat4 textureMatrix;

uniform vec4 ambientColor;

uniform vec3 myLightPositions;

uniform mat4 skinPalette[17];

uniform vec3 lightPositions[8];

uniform vec3 diffuseColors[8];

uniform vec3 specularColors[8];

uniform float attenuation[8];

attribute vec4 position;

attribute vec3 normal;

attribute vec4 tangent;

attribute vec2 texture0;

attribute vec2 texture1;

attribute vec4 skinWeights;

attribute vec4 skinJointIndices;

varying vec2 texCoord[3];

varying vec3 lightVec[2];

varying vec3 eyeVec;

const vec4 WHITE = vec4(1,1,1,1);

void main() {

texCoord[0] = texture0;

texCoord[2] = (textureMatrix * vec4(texture0, 0, 1)).xy;

vec4 myPosition = vec4(0,0,0,0);

vec3 myNormal = vec3(0,0,0);

vec4 myTangent = vec4(0,0,0,0);

vec4 vertexTemp;

vec3 normalTemp;

vec4 tangentTemp;

mat4 mat;

mat = skinPalette[int(skinJointIndices[0])];

vertexTemp = mat * position;

vertexTemp *= skinWeights.x;

myPosition += vertexTemp;

normalTemp = mat3(mat) * normal;

normalTemp *= skinWeights.x;

myNormal += normalTemp;

tangentTemp = mat * tangent;

tangentTemp *= skinWeights.x;

myTangent += tangentTemp;

mat = skinPalette[int(skinJointIndices[1])];

vertexTemp = mat * position;

vertexTemp *= skinWeights.y;

myPosition += vertexTemp;

normalTemp = mat3(mat) * normal;

normalTemp *= skinWeights.y;

myNormal += normalTemp;

tangentTemp = mat * tangent;

tangentTemp *= skinWeights.y;

myTangent += tangentTemp;

myPosition.yz *= -1.0;

myNormal.yz *= -1.0;

myTangent.yz *= -1.0;

vec3 vertexPos = vec3(modelViewMatrix * myPosition);

vec3 n = normalize(modelViewMatrix * vec4(myNormal, 0.0)).xyz;

vec3 t = normalize(modelViewMatrix * vec4(myTangent.xyz, 0.0)).xyz;

vec3 b = myTangent.w*cross(n, t);

vec3 tmpVec = myLightPositions.xyz - vertexPos;

vec3 lv;

vec3 ev;

lv.x = dot(tmpVec, t);

lv.y = dot(tmpVec, b);

lv.z = dot(tmpVec, n);

lightVec[0]=lv;

tmpVec = vertexPos*-1.0;

eyeVec.x = dot(tmpVec, t);

eyeVec.y = dot(tmpVec, b);

eyeVec.z = dot(tmpVec, n);

gl_Position = modelViewProjectionMatrix * myPosition;

}

fragment shader (with bump map)-

`precision mediump float;`

uniform sampler2D textureUnit0;

uniform sampler2D textureUnit1;

uniform sampler2D textureUnit2;

uniform float shinyStrength;

uniform vec4 additionalColor;

uniform vec4 ambientColor;

uniform vec3 diffuseColors[8];

uniform vec3 specularColors[8];

varying vec2 texCoord[3];

varying vec3 lightVec[2];

varying vec3 eyeVec;

const vec4 WHITE = vec4(1,1,1,1);

void main() {

vec4 vAmbient = ambientColor;

vec3 vVec = normalize(eyeVec);

//base == col

vec4 base = texture2D(textureUnit0, texCoord[0]);

vec3 bump = normalize(texture2D(textureUnit1, texCoord[0]).xyz * 2.0 - 1.0);

float invRadius = 0.0003;

float distSqr = dot(lightVec[0], lightVec[0]);

float att = clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0);

vec3 lVec = lightVec[0] * inversesqrt(distSqr);

float diffuse = max(dot(lVec, bump), 0.0);

vec4 vDiffuse = vec4(diffuseColors[0],0) * diffuse;

float specular = pow(clamp(dot(reflect(-lVec, bump), vVec), 0.0, 1.0), 0.85);

vec4 vSpecular = vec4(specularColors[0],0) * specular * 0.1;

vec4 col = (vAmbient*base + vDiffuse*base + vSpecular ) * att*2.0;

vec4 blend = texture2D(textureUnit2, texCoord[2]);

col = min(col / (1.0 - blend), 1.0);

//if (additionalColor.x > 0.0)

// col = WHITE;

gl_FragColor = col;

}

The vertex shader is implemented with hardware skinning we talked about with raft in last few months.