var vertexShaderSource3D =
"attribute vec3 Vertex;" +
"attribute vec3 Normal;" +
"attribute vec4 aColor;" +
"attribute vec2 aTexture;" +
"varying vec2 vTexture;" +
"uniform vec4 color;" +
"uniform bool usingMat;" +
"uniform vec3 specular;" +
"uniform vec3 mat_emissive;" +
"uniform vec3 mat_ambient;" +
"uniform vec3 mat_specular;" +
"uniform float shininess;" +
"uniform mat4 model;" +
"uniform mat4 view;" +
"uniform mat4 projection;" +
"uniform mat4 normalTransform;" +
"uniform int lightCount;" +
"uniform vec3 falloff;" +
"struct Light {" +
" bool dummy;" +
" int type;" +
" vec3 color;" +
" vec3 position;" +
" vec3 direction;" +
" float angle;" +
" vec3 halfVector;" +
" float concentration;" +
"};" +
"uniform Light lights[8];" +
"void AmbientLight( inout vec3 totalAmbient, in vec3 ecPos, in Light light ) {" +
// Get the vector from the light to the vertex
// Get the distance from the current vector to the light position
" float d = length( light.position - ecPos );" +
" float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ));" + " totalAmbient += light.color * attenuation;" +
"}" +
"void DirectionalLight( inout vec3 col, in vec3 ecPos, inout vec3 spec, in vec3 vertNormal, in Light light ) {" +
" float powerfactor = 0.0;" +
" float nDotVP = max(0.0, dot( vertNormal, light.position ));" +
" float nDotVH = max(0.0, dot( vertNormal, normalize( light.position-ecPos )));" +
" if( nDotVP != 0.0 ){" +
" powerfactor = pow( nDotVH, shininess );" +
" }" +
" col += light.color * nDotVP;" +
" spec += specular * powerfactor;" +
"}" +
"void PointLight( inout vec3 col, inout vec3 spec, in vec3 vertNormal, in vec3 ecPos, in vec3 eye, in Light light ) {" +
" float powerfactor;" +
// Get the vector from the light to the vertex
" vec3 VP = light.position - ecPos;" +
// Get the distance from the current vector to the light position
" float d = length( VP ); " +
// Normalize the light ray so it can be used in the dot product operation.
" VP = normalize( VP );" +
" float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ));" +
" float nDotVP = max( 0.0, dot( vertNormal, VP ));" +
" vec3 halfVector = normalize( VP + eye );" +
" float nDotHV = max( 0.0, dot( vertNormal, halfVector ));" +
" if( nDotVP == 0.0) {" +
" powerfactor = 0.0;" +
" }" +
" else{" +
" powerfactor = pow( nDotHV, shininess );" +
" }" +
" spec += specular * powerfactor * attenuation;" +
" col += light.color * nDotVP * attenuation;" +
"}" +
/*
*/
"void SpotLight( inout vec3 col, inout vec3 spec, in vec3 vertNormal, in vec3 ecPos, in vec3 eye, in Light light ) {" +
" float spotAttenuation;" +
" float powerfactor;" +
// calculate the vector from the current vertex to the light.
" vec3 VP = light.position - ecPos; " +
" vec3 ldir = normalize( light.direction );" +
// get the distance from the spotlight and the vertex
" float d = length( VP );" +
" VP = normalize( VP );" +
" float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ) );" +
// dot product of the vector from vertex to light and light direction.
" float spotDot = dot( VP, ldir );" +
// if the vertex falls inside the cone
" if( spotDot < cos( light.angle ) ) {" +
" spotAttenuation = pow( spotDot, light.concentration );" +
" }" +
" else{" +
" spotAttenuation = 1.0;" +
" }" +
" attenuation *= spotAttenuation;" +
" float nDotVP = max( 0.0, dot( vertNormal, VP ));" +
" vec3 halfVector = normalize( VP + eye );" +
" float nDotHV = max( 0.0, dot( vertNormal, halfVector ));" +
" if( nDotVP == 0.0 ) {" +
" powerfactor = 0.0;" +
" }" +
" else {" +
" powerfactor = pow( nDotHV, shininess );" +
" }" +
" spec += specular * powerfactor * attenuation;" +
" col += light.color * nDotVP * attenuation;" +
"}" +
"void main(void) {" +
" vec3 finalAmbient = vec3( 0.0, 0.0, 0.0 );" +
" vec3 finalDiffuse = vec3( 0.0, 0.0, 0.0 );" +
" vec3 finalSpecular = vec3( 0.0, 0.0, 0.0 );" +
" vec4 col = color;" +
" if(color[0] == -1.0){" +
" col = aColor;" +
" }" +
" vec3 norm = vec3( normalTransform * vec4( Normal, 0.0 ) );" +
" vec4 ecPos4 = view * model * vec4(Vertex,1.0);" +
" vec3 ecPos = (vec3(ecPos4))/ecPos4.w;" +
" vec3 eye = vec3( 0.0, 0.0, 1.0 );" +
// If there were no lights this draw call, just use the
// assigned fill color of the shape and the specular value
" if( lightCount == 0 ) {" +
" gl_FrontColor = col + vec4(mat_specular,1.0);" +
" }" +
" else {" +
" for( int i = 0; i < lightCount; i++ ) {" +
" if( lights[i].type == 0 ) {" +
" AmbientLight( finalAmbient, ecPos, lights[i] );" +
" }" +
" else if( lights[i].type == 1 ) {" +
" DirectionalLight( finalDiffuse,ecPos, finalSpecular, norm, lights[i] );" +
" }" +
" else if( lights[i].type == 2 ) {" +
" PointLight( finalDiffuse, finalSpecular, norm, ecPos, eye, lights[i] );" +
" }" +
" else if( lights[i].type == 3 ) {" +
" SpotLight( finalDiffuse, finalSpecular, norm, ecPos, eye, lights[i] );" +
" }" +
" }" +
" if( usingMat == false ) {" +
" gl_FrontColor = vec4( " +
" vec3(col) * finalAmbient +" +
" vec3(col) * finalDiffuse +" +
" vec3(col) * finalSpecular," +
" col[3] );" +
" }" +
" else{" +
" gl_FrontColor = vec4( " +
" mat_emissive + " +
" (vec3(col) * mat_ambient * finalAmbient) + " +
" (vec3(col) * finalDiffuse) + " +
" (mat_specular * finalSpecular), " +
" col[3] );" +
" }" +
" }" +
" vTexture.xy = aTexture.xy;" +
" gl_Position = projection * view * model * vec4( Vertex, 1.0 );" +
"}";
var fragmentShaderSource3D =
"uniform sampler2D sampler;" +
"uniform bool usingTexture;" +
"varying vec2 vTexture;" +
// In Processing, when a texture is used, the fill color is ignored
"void main(void){" +
" if(usingTexture){" +
" gl_FragColor = vec4(texture2D(sampler, vTexture.xy));" +
" }"+
" else{" +
" gl_FragColor = vec4(gl_Color);" +
" }" +
"}";