var world, camera, gamePaused, waterSurface, shady, shadowDrawn, lodka, lodka2, seagulls, speed, turnss, sharks, sharksNum, seagullsNum var pause = function() { gamePaused = !gamePaused } window.setup = function() { setGL("agl"); start = Date.now(); gamePaused = false seagulls = [] sharks = [] var path = "/media/Sea/" var path2 = "/media/Sea/" // var path = "textures/" // var path2 = "objs/" var agl = new aexolGL(); world = new Scene(); camera = new Camera(0.1, 300.0, 45.0) shadowDrawn = false sharksNum = 6 seagullsNum = 8 customBasicShader = function(options) { var settings = { useBump: false, useDiffuse: false, useAtlas: false, useSpecular: false, useLights: false, useTiling: false, useReflection: false, useSky: false } if (options) { for (var o in options) { settings[o] = options[o] } } var bShader = new Shader("", "", 1) bShader.addVarying("vec2", "vTex") bShader.addVarying("float", "nrmY") bShader.addVarying("vec3", "dZ") bShader.addVarying("vec4", "vPosition") bShader.addVarying("vec3", "vNormal") bShader.addVarying("vec3", "normalEye") bShader.addVertexSource('\ void main(void) {\ normalEye = normalize(Normal*NormalMatrix);\ vNormal = Normal;\ vPosition = gl_ModelViewMatrix * vec4(Vertex, 1.0);\ dZ = (gl_ProjectionMatrix * vPosition).xyz;\ nrmY = abs(Normal.y);\ vTex = TexCoord;\ gl_Position = gl_ProjectionMatrix * vPosition;\ }') bShader.addStruct("Material") bShader.addToStruct("Material", "vec3", "color") if (settings.useLights) { bShader.addStruct("Light") bShader.addToStruct("Light", "vec3", "lightPosition") bShader.addToStruct("Light", "vec3", "color") bShader.addToStruct("Light", "float", "attenuation") bShader.addToStruct("Light", "float", "intensity") bShader.addToStruct("Light", "float", "lightType") bShader.addUniform("float", "numlights") bShader.addUniform("Light", "lights[32]") bShader.addToStruct("Light", "bool", "shadow") if (settings.useShadow) { bShader.addUniform("samplerCube", "shadows") } } bShader.addToStruct("Material", "float", "shininess") bShader.addToStruct("Material", "float", "mappingType") bShader.addToStruct("Material", "float", "alpha") bShader.addToStruct("Material", "float", "specularWeight") bShader.addUniform("Material", "material") bShader.addUniform("float", "cameraNear") bShader.addUniform("float", "cameraFar") if (settings.useAtlas) { bShader.addUniform("vec4", "atlas") } if (settings.useTiling) { bShader.addUniform("vec2", "tiling") } if (settings.useRain) { bShader.addToStruct("Material", "vec4", "fogColor") bShader.addToStruct("Material", "float", "fogDensity") bShader.addToStruct("Material", "bool", "fogY") } if (settings.useDiffuse) { bShader.addUniform("sampler2D", "diffuse") } if (settings.useSpecular) { bShader.addUniform("sampler2D", "specular") } if (settings.useBump) { bShader.addUniform("sampler2D", "bump") bShader.addToStruct("Material", "float", "bumpWeight") } if (settings.useReflection || settings.useSky) { bShader.addUniform("samplerCube", "cube") bShader.addToStruct("Material", "float", "reflectionWeight") } var fragSource = '\ float reducer = 1.0;\ float rand(vec2 co){\ return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\ }' + (settings.useShadow ? 'float shadowFac(vec3 ld){\ vec3 ld2 = vec3(-ld.x,ld.y,ld.z);\ float sd = textureCube(shadows,ld2).r;\ float eps = (cameraFar)/10000.0;\ float distance = length(ld)/cameraFar;\ if(distance<=(sd+eps)){\ return 1.0;\ }\ else{\ return 0.5;\ }\ }' : '') + (settings.useLights ? 'vec3 lightPow(Light li,vec2 til){\ vec3 vpos = vPosition.xyz;\ vec3 lp = li.lightPosition;\ vec3 lightSub = lp-vpos;\ float distance = length(lightSub);\ float att = max(li.attenuation-distance,0.0)/(li.attenuation/0.8);\ vec3 lightDirection = normalize(lightSub);\ vec3 eyeDirection = normalize(-vpos.xyz);\ float dW = max(0.0,dot(normalEye,lightDirection));\ vec3 reflectionDirection = reflect(-lightDirection, normalEye);\ float shininess = material.shininess;\ ' + (settings.useBump ? '\ vec3 bmpp = texture2D(bump, til).xyz;\ bmpp = (bmpp -0.5) * 2.0;\ dW = dW*bmpp.x*(material.bumpWeight)+dW*(1.0-material.bumpWeight);' : '') + '\ float specularT = material.specularWeight;' + (settings.useSpecular ? '\ specularT = texture2D(specular, til).r * material.specularWeight;' : '') + '\ float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), shininess);\ \ ' + (settings.useShadow ? '\ if(li.shadow){\ dW = dW*shadowFac(lightSub);\ }' : '') + 'vec3 returnedLight = li.color*dW + specularLightWeighting*material.specularWeight;\ returnedLight *= att;\ returnedLight *= li.intensity;\ return returnedLight;\ }' : '') + 'void main(void) {\ vec2 tiler;' + (settings.useAtlas ? "\ float aU = atlas.y-atlas.x;\ float aV = atlas.w-atlas.z;\ tiler = vec2(atlas.x+vTex.x*aU,atlas.z+vTex.y*aV);\ " : "\ tiler = vTex;") + (settings.useTiling ? 'tiler = vec2(vTex.s*tiling.x,vTex.t*tiling.y);\ ' : '') + (settings.boxMapping ? '\ if(abs(vNormal.z)>0.5){\ tiler = vec2(vPosition.x*tiling.x,vPosition.y*tiling.y);\ }else{\ if(abs(vNormal.y)>0.5){\ tiler = vec2(vPosition.z*tiling.x,vPosition.x*tiling.x);\ }\ else{\ tiler = vec2(vPosition.z*tiling.x,vPosition.y*tiling.y);\ }\ }' : '') + '\ vec3 dWei = vec3(0.0,0.0,0.0);\ vec4 clr = vec4(material.color,1.0);\ ' + (settings.useDiffuse ? 'clr = texture2D(diffuse, tiler);\ clr = vec4(clr.rgb*material.color,clr.a);' : '') + (settings.useReflection ? '\ vec3 thh = reflect(dZ,normalEye);\ vec4 txc = textureCube(cube,thh);\ clr = vec4(clr.rgb*(1.0-material.reflectionWeight)+txc.rgb*material.reflectionWeight,clr.a);\ ' : '') + (settings.useRain ? '\ float density = 0.011;\ float log2 = 1.442695;\ if (material.fogY){\ density = (200.0-vPosition.y)/33000.0;\ }\ else{ density = 0.011; }\ float zz = gl_FragCoord.z/gl_FragCoord.w;\ float fogFactor3= exp2(-density*density*zz*zz*log2);\ fogFactor3 = clamp(fogFactor3, 0.0,0.8);\ \ clr = mix(material.fogColor, clr, fogFactor3);\ \ ;' : '') + (settings.darken ? '\ float darkensity = (vPosition.y);\ clr = mix(vec4(0.2,60.0/255.0,20.0/255.0,1.0), clr, darkensity*0.14);\ \ \ ;' : '') + (settings.useLights ? '\ for(int i = 0;i<32;i++){\ if( i >= int(numlights)){\ break;\ };\ Light li = lights[i];\ if(li.lightType == 1.0){\ dWei += lightPow(li,tiler);\ }else{\ dWei += li.color*li.intensity;\ }\ };' : 'dWei = vec3(1.0,1.0,1.0);') + (settings.useSky ? '\ vec3 thh = vPosition.xyz;\ vec4 txc = textureCube(cube,thh);\ clr = vec4(clr.rgb*(1.0-material.reflectionWeight)+txc.rgb*material.reflectionWeight,clr.a);\ ' : '') + 'clr = vec4(clr.rgb*dWei,clr.a);' + '\ gl_FragColor = vec4(clr.rgb,clr.a*material.alpha);\ }'; bShader.addFragmentSource(fragSource) bShader._build(); return bShader } customShader = function(options) { var settings = { useBump: false, useDiffuse: false, useAtlas: false, useSpecular: false, useLights: false, useTiling: false, useReflection: false, useSky: false } if (options) { for (var o in options) { settings[o] = options[o] } } var bShader = new Shader("", "", 1) bShader.addVarying("vec2", "vTex") bShader.addVarying("float", "nrmY") bShader.addVarying("vec3", "dZ") bShader.addVarying("vec4", "vPosition") bShader.addVarying("vec3", "vNormal") bShader.addVarying("vec3", "normalEye") bShader.addVarying("vec4", "refrCoords") bShader.addVarying("vec4", "normCoords") bShader.addVarying("vec4", "viewCoords") bShader.addVarying("vec4", "viewTangetSpace") bShader.addVarying("vec4", "lightTangetSpace") bShader.addUniform("vec4", "lightPos") bShader.addUniform("vec4", "cameraPos") bShader.addUniform("float", "time") bShader.addVertexSource('\ void main(void) {\ normalEye = normalize(Normal*NormalMatrix);\ vNormal = Normal;\ vPosition = gl_ModelViewMatrix * vec4(Vertex+vec3(0.0,sin(Vertex.x+0.0008*time),0.0), 1.0);\ dZ = (gl_ProjectionMatrix * vPosition).xyz;\ nrmY = abs(Normal.y);\ vTex = TexCoord;\ vec4 tangent = vec4(1.0, 0.0, 0.0, 0.0);\ vec4 normal = vec4(0.0, 1.0, 0.0, 0.0);\ vec4 biTangent = vec4(0.0, 0.0, 1.0, 0.0);\ vec4 viewDir = vec4(0.1, 22.59, -16.40,0.0)-(gl_ModelViewMatrix*vec4(Vertex, 1.0)) ;\ viewTangetSpace.x = dot(viewDir, tangent);\ viewTangetSpace.y = dot(viewDir, biTangent);\ viewTangetSpace.z = dot(viewDir, normal);\ viewTangetSpace.w = 1.0;\ vec4 lp = vec4(-12.0,20.0,0.0, -5.0);\ vec4 lightDir = (lp + vec4(Vertex ,1.0)) ;\ lightTangetSpace.x = dot(lightDir, tangent);\ lightTangetSpace.y = dot(lightDir, biTangent);\ lightTangetSpace.z = dot(lightDir, normal);\ lightTangetSpace.w = 1.0;\ refrCoords = gl_ProjectionMatrix * vPosition*gl_ModelViewProjectionMatrix * vec4(Vertex, 1.0);\ normCoords = gl_ModelViewProjectionMatrix*vec4(vNormal, 0.0);\ viewCoords = gl_ProjectionMatrix * vPosition;\ gl_Position = gl_ProjectionMatrix * vPosition;\ }') bShader.addStruct("Material") bShader.addToStruct("Material", "vec3", "color") if (settings.useLights) { bShader.addStruct("Light") bShader.addToStruct("Light", "vec3", "lightPosition") bShader.addToStruct("Light", "vec3", "color") bShader.addToStruct("Light", "float", "attenuation") bShader.addToStruct("Light", "float", "intensity") bShader.addToStruct("Light", "float", "lightType") bShader.addUniform("float", "numlights") bShader.addUniform("Light", "lights[32]") bShader.addToStruct("Light", "bool", "shadow") if (settings.useShadow) { bShader.addUniform("samplerCube", "shadows") } } bShader.addToStruct("Material", "float", "shininess") bShader.addToStruct("Material", "float", "mappingType") bShader.addToStruct("Material", "float", "alpha") bShader.addToStruct("Material", "float", "specularWeight") bShader.addUniform("Material", "material") bShader.addUniform("float", "cameraNear") bShader.addUniform("float", "cameraFar") if (settings.useAtlas) { bShader.addUniform("vec4", "atlas") } if (settings.useTiling) { bShader.addUniform("vec2", "tiling") } if (settings.useWater) { bShader.addUniform("vec4", "waterColor") bShader.addUniform("sampler2D", "normalMap") bShader.addUniform("sampler2D", "dudvMap") bShader.addUniform("sampler2D", "shineMap") bShader.addToStruct("Material", "vec3", "waterColor") bShader.addToStruct("Material", "float", "time") } if (settings.useRain) { bShader.addUniform("vec2", "resolution") bShader.addUniform("float", "k") } if (settings.useDiffuse) { bShader.addUniform("sampler2D", "diffuse") } if (settings.useSpecular) { bShader.addUniform("sampler2D", "specular") } if (settings.useBump) { bShader.addUniform("sampler2D", "bump") bShader.addToStruct("Material", "float", "bumpWeight") } if (settings.useReflection || settings.useSky) { bShader.addUniform("samplerCube", "cube") bShader.addToStruct("Material", "float", "reflectionWeight") } if (settings.useFog) { bShader.addStruct("Fog") bShader.addToStruct("Fog", "vec2", "zMinMax") bShader.addToStruct("Fog", "vec3", "color") bShader.addToStruct("Fog", "float", "intensity") bShader.addUniform("Fog", "fog") } var fragSource = '\ float reducer = 1.0;\ float rand(vec2 co){\ return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\ }' + (settings.useShadow ? 'float shadowFac(vec3 ld){\ vec3 ld2 = vec3(-ld.x,ld.y,ld.z);\ float sd = textureCube(shadows,ld2).r;\ float eps = (cameraFar)/10000.0;\ float distance = length(ld)/cameraFar;\ if(distance<=(sd+eps)){\ return 1.0;\ }\ else{\ return 0.5;\ }\ }' : '') + (settings.useLights ? 'vec3 lightPow(Light li,vec2 til){\ vec3 vpos = vPosition.xyz;\ vec3 lp = li.lightPosition;\ vec3 lightSub = lp-vpos;\ float distance = length(lightSub);\ float att = max(li.attenuation-distance,0.0)/li.attenuation;\ vec3 lightDirection = normalize(lightSub);\ vec3 eyeDirection = normalize(-vpos.xyz);\ float dW = max(0.0,dot(normalEye,lightDirection));\ vec3 reflectionDirection = reflect(-lightDirection, normalEye);\ float shininess = material.shininess;\ ' + (settings.useBump ? '\ vec3 bmpp = texture2D(bump, til).xyz;\ bmpp = (bmpp -0.5) * 2.0;\ dW = dW*bmpp.x*(material.bumpWeight)+dW*(1.0-material.bumpWeight);' : '') + '\ float specularT = material.specularWeight;' + (settings.useSpecular ? '\ specularT = texture2D(specular, til).r * material.specularWeight;' : '') + '\ float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), shininess*reducer);\ \ ' + (settings.useShadow ? '\ if(li.shadow){\ dW = dW*shadowFac(lightSub);\ }' : '') + 'vec3 returnedLight = li.color*dW + specularLightWeighting*material.specularWeight;\ returnedLight *= att;\ return returnedLight;\ }' : '') + 'void main(void) {\ vec2 tiler;' + (settings.useAtlas ? "\ float aU = atlas.y-atlas.x;\ float aV = atlas.w-atlas.z;\ tiler = vec2(atlas.x+vTex.x*aU,atlas.z+vTex.y*aV);\ " : "\ tiler = vTex;") + (settings.useTiling ? 'tiler = vec2(vTex.s*tiling.x,vTex.t*tiling.y);\ ' : '') + (settings.boxMapping ? '\ if(abs(vNormal.z)>0.5){\ tiler = vec2(vPosition.x*tiling.x,vPosition.y*tiling.y);\ }else{\ if(abs(vNormal.y)>0.5){\ tiler = vec2(vPosition.z*tiling.x,vPosition.x*tiling.x);\ }\ else{\ tiler = vec2(vPosition.z*tiling.x,vPosition.y*tiling.y);\ }\ }' : '') + '\ vec3 dWei = vec3(0.0,0.0,0.0);\ vec4 clr = vec4(material.color,1.0);\ ' + (settings.useDiffuse ? 'clr = texture2D(diffuse, tiler);\ clr = vec4(clr.rgb*material.color,clr.a);' : '') + (settings.useReflection ? '\ vec3 thh = reflect(dZ,normalEye);\ vec4 txc = textureCube(cube,thh);\ clr = vec4(clr.rgb*(1.0-material.reflectionWeight)+txc.rgb*material.reflectionWeight,clr.a);\ ' : '') + (settings.useLights ? '\ for(int i = 0;i<32;i++){\ if( i >= int(numlights)){\ break;\ };\ Light li = lights[i];\ if(li.lightType == 1.0){\ dWei += lightPow(li,tiler);\ }else{\ dWei += vec3(0.1, 0.13, 0.16)*1.9;\ }\ };' : 'dWei = vec3(1.0,1.0,1.0);') + (settings.useWater ? '\ const float kShine = 128.0;\ const float kDistortion = 0.015;\ const float kRefraction = 0.009;\ vec2 posi = vec2(vTex.s*10.5,vTex.t*20.5);\ vec2 posi2 = vec2(vTex.s*20.5,vTex.t*28.5);\ vec2 til = posi;\ posi.y-=sin(0.0001*time);\ posi2.y+=sin(0.00014*time);\ vec2 norm = normalize(vTex);\ vec4 distOffset = texture2D(dudvMap, posi*norm) * kDistortion;\ vec4 dudvColor = texture2D(normalMap, vec2(refrCoords+ distOffset));\ dudvColor = normalize(dudvColor * 2.0 - 1.0) * kRefraction;\ vec4 distOffset2 = texture2D(normalMap, posi2*norm) * kDistortion;\ vec4 dudvColor2 = texture2D(normalMap, vec2(refrCoords+ distOffset2));\ dudvColor2 = normalize(dudvColor2 * 2.0 - 1.0) * kRefraction;\ \ vec4 normalVector = texture2D(normalMap, vec2(refrCoords+distOffset));\ normalVector = normalVector * 2.0 - 1.0;\ normalVector.a = 0.0;\ vec4 normalVector2 = texture2D(normalMap, vec2(refrCoords+distOffset2));\ normalVector2 = normalVector2 * 2.0 - 1.0;\ normalVector2.a = 0.0;\ normalVector = (normalVector+normalVector2)/2.2;\ vec4 lightReflection = normalize(reflect(-lightTangetSpace, normalVector) );\ vec4 invertedFresnel = vec4( dot(normalVector, lightReflection ) );\ vec4 fresnelTerm = 1.0 - invertedFresnel;\ \ vec4 projCoord = vec4(posi, 1.0,1.0); \ projCoord = (projCoord + 1.0) * 0.5;\ projCoord -= dudvColor;\ projCoord = clamp(projCoord, 0.001, 0.999);\ \ vec4 reflectionColor = texture2D(shineMap, projCoord.xy);\ vec4 refractionColor = texture2D(dudvMap, projCoord.xy) ;\ vec4 depthValue = refractionColor;\ vec4 invDepth = 1.0 - depthValue;\ refractionColor *= invertedFresnel * invDepth;\ refractionColor += waterColor+ invDepth *invertedFresnel ;\ reflectionColor *= fresnelTerm;\ vec4 localView = normalize(viewTangetSpace);\ float intensity = max(0.0, dot(lightReflection, localView));\ vec4 specular = vec4(pow(intensity, kShine));\ clr =vec4((reflectionColor+specular+refractionColor*0.7).rgb,0.92);\ clr = clr - vec4(0.1,0.1,0.0,0.2);\ \ \ ' : '') + (settings.useRain ? '\ float log2 = 1.442695;\ float fogCoord = (gl_FragCoord.z/gl_FragCoord.w)*0.88;\ float density = 0.0097 ;\ float ff = exp2(-density *density * fogCoord *fogCoord *log2);\ ff = clamp(ff, 0.0,1.0);\ vec4 fogColor = vec4(2.98,2.37,1.97,1.0)*1.2;\ clr = mix(fogColor, clr, ff);\ \ ;' : '') + (settings.useSky ? '\ vec3 thh = vPosition.xyz;\ vec4 txc = textureCube(cube,thh);\ clr = vec4(clr.rgb*(1.0-material.reflectionWeight)+txc.rgb*material.reflectionWeight,clr.a);\ ' : '') + 'clr = vec4(clr.rgb*dWei,clr.a);' + (settings.useFog ? '\ float depth = vPosition.z/fog.zMinMax.y;\ clr = vec4(clr.rgb+fog.color*depth*fog.intensity,clr.a);\ ' : '') + '\ gl_FragColor = vec4(clr.rgb,clr.a*material.alpha);\ }'; bShader.addFragmentSource(fragSource) bShader._build(); return bShader } sharkShader = function(options) { var settings = { useBump: false, useDiffuse: false, useAtlas: false, useSpecular: false, useLights: false, useTiling: false, useReflection: false, useSky: false, mewa:false } if (options) { for (var o in options) { settings[o] = options[o] } } var bShader = new Shader("", "", 1) bShader.addVarying("vec2", "vTex") bShader.addVarying("float", "nrmY") bShader.addVarying("vec3", "dZ") bShader.addVarying("vec4", "vPosition") bShader.addVarying("vec3", "vNormal") bShader.addVarying("vec3", "normalEye") bShader.addUniform("float", "time") if (settings.mewa){ bShader.addVertexSource('\ void main(void) {\ normalEye = normalize(Normal*NormalMatrix);\ vNormal = Normal;\ vPosition = vec4(Vertex + (vec3(0.0, 0.3, 0.0) * sin(time*0.24 +abs(Vertex.x))) *min(max(abs(Vertex.x), -1.5), 1.5), 1.0);\ vPosition = gl_ModelViewMatrix * vPosition;\ dZ = (gl_ProjectionMatrix * vPosition).xyz;\ nrmY = abs(Normal.y);\ vTex = TexCoord;\ gl_Position = gl_ProjectionMatrix * vPosition;\ }') } else { bShader.addVertexSource('\ void main(void) {\ normalEye = normalize(Normal*NormalMatrix);\ vNormal = Normal;\ vPosition = vec4(Vertex + (vec3(0.2, 0.0, 0.0) * sin(time*0.008 +Vertex.z-abs(Vertex.z)) * min(max(Vertex.z, -1.0), 1.0)), 1.0);\ vPosition = gl_ModelViewMatrix * vPosition;\ dZ = (gl_ProjectionMatrix * vPosition).xyz;\ nrmY = abs(Normal.y);\ vTex = TexCoord;\ gl_Position = gl_ProjectionMatrix * vPosition;\ }') } bShader.addStruct("Material") bShader.addToStruct("Material", "vec3", "color") if (settings.useLights) { bShader.addStruct("Light") bShader.addToStruct("Light", "vec3", "lightPosition") bShader.addToStruct("Light", "vec3", "color") bShader.addToStruct("Light", "float", "attenuation") bShader.addToStruct("Light", "float", "intensity") bShader.addToStruct("Light", "float", "lightType") bShader.addUniform("float", "numlights") bShader.addUniform("Light", "lights[32]") bShader.addToStruct("Light", "bool", "shadow") if (settings.useShadow) { bShader.addUniform("samplerCube", "shadows") } } bShader.addToStruct("Material", "float", "shininess") bShader.addToStruct("Material", "float", "mappingType") bShader.addToStruct("Material", "float", "alpha") bShader.addToStruct("Material", "float", "specularWeight") bShader.addUniform("Material", "material") bShader.addUniform("float", "cameraNear") bShader.addUniform("float", "cameraFar") if (settings.useAtlas) { bShader.addUniform("vec4", "atlas") } if (settings.useTiling) { bShader.addUniform("vec2", "tiling") } if (settings.useRain) { bShader.addUniform("vec2", "resolution") bShader.addUniform("float", "k") } if (settings.useDiffuse) { bShader.addUniform("sampler2D", "diffuse") } if (settings.useSpecular) { bShader.addUniform("sampler2D", "specular") } if (settings.useBump) { bShader.addUniform("sampler2D", "bump") bShader.addToStruct("Material", "float", "bumpWeight") } if (settings.useReflection || settings.useSky) { bShader.addUniform("samplerCube", "cube") bShader.addToStruct("Material", "float", "reflectionWeight") } if (settings.useFog) { bShader.addStruct("Fog") bShader.addToStruct("Fog", "vec2", "zMinMax") bShader.addToStruct("Fog", "vec3", "color") bShader.addToStruct("Fog", "float", "intensity") bShader.addUniform("Fog", "fog") } var fragSource = '\ float reducer = 1.0;\ float rand(vec2 co){\ return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\ }' + (settings.useShadow ? 'float shadowFac(vec3 ld){\ vec3 ld2 = vec3(-ld.x,ld.y,ld.z);\ float sd = textureCube(shadows,ld2).r;\ float eps = (cameraFar)/10000.0;\ float distance = length(ld)/cameraFar;\ if(distance<=(sd+eps)){\ return 1.0;\ }\ else{\ return 0.5;\ }\ }' : '') + (settings.useLights ? 'vec3 lightPow(Light li,vec2 til){\ vec3 vpos = vPosition.xyz;\ vec3 lp = li.lightPosition;\ vec3 lightSub = lp-vpos;\ float distance = length(lightSub);\ float att = max(li.attenuation-distance,0.0)/li.attenuation;\ vec3 lightDirection = normalize(lightSub);\ vec3 eyeDirection = normalize(-vpos.xyz);\ float dW = max(0.0,dot(normalEye,lightDirection));\ vec3 reflectionDirection = reflect(-lightDirection, normalEye);\ float shininess = material.shininess;\ ' + (settings.useBump ? '\ vec3 bmpp = texture2D(bump, til).xyz;\ bmpp = (bmpp -0.5) * 2.0;\ dW = dW*bmpp.x*(material.bumpWeight)+dW*(1.0-material.bumpWeight);' : '') + '\ float specularT = material.specularWeight;' + (settings.useSpecular ? '\ specularT = texture2D(specular, til).r * material.specularWeight;' : '') + '\ float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), shininess*reducer);\ \ ' + (settings.useShadow ? '\ if(li.shadow){\ dW = dW*shadowFac(lightSub);\ }' : '') + 'vec3 returnedLight = li.color*dW + specularLightWeighting*material.specularWeight;\ returnedLight *= att;\ return returnedLight;\ }' : '') + 'void main(void) {\ vec2 tiler;' + (settings.useAtlas ? "\ float aU = atlas.y-atlas.x;\ float aV = atlas.w-atlas.z;\ tiler = vec2(atlas.x+vTex.x*aU,atlas.z+vTex.y*aV);\ " : "\ tiler = vTex;") + (settings.useTiling ? 'tiler = vec2(vTex.s*tiling.x,vTex.t*tiling.y);\ ' : '') + '\ vec3 dWei = vec3(0.0,0.0,0.0);\ vec4 clr = vec4(material.color,1.0);\ ' + (settings.useDiffuse ? 'clr = texture2D(diffuse, tiler);\ clr = vec4(clr.rgb*material.color,clr.a);' : '') + (settings.useReflection ? '\ vec3 thh = reflect(dZ,normalEye);\ vec4 txc = textureCube(cube,thh);\ clr = vec4(clr.rgb*(1.0-material.reflectionWeight)+txc.rgb*material.reflectionWeight,clr.a);\ ' : '') + (settings.useLights ? '\ for(int i = 0;i<32;i++){\ if( i >= int(numlights)){\ break;\ };\ Light li = lights[i];\ if(li.lightType == 1.0){\ dWei += lightPow(li,tiler);\ }else{\ dWei += li.color*1.9;\ }\ };' : 'dWei = vec3(1.0,1.0,1.0);') + (settings.useRain ? '\ float log2 = 1.442695;\ float fogCoord = (gl_FragCoord.z/gl_FragCoord.w)*1.0;\ float density = 0.0097 ;\ float ff = exp2(-density *density * fogCoord *fogCoord *log2);\ ff = clamp(ff, 0.0,1.0);\ vec4 fogColor = vec4(0.1,0.1,0.3,0.4);\ clr = mix(fogColor, clr, ff);\ \ ;' : '') + '\ gl_FragColor = vec4(clr.rgb,clr.a*material.alpha);\ }'; bShader.addFragmentSource(fragSource) bShader._build(); return bShader } kamienTex = Texture.fromImage(path + "/kamien.jpg", { wrap: gl.REPEAT }) drewnotex = Texture.fromImage(path + "/oldwood.jpg", { wrap: gl.REPEAT }) lodkaTex1 = Texture.fromImage(path + "/planks.jpg", { wrap: gl.REPEAT }) lodkaTex2 = Texture.fromImage(path + "/wood.jpg", { wrap: gl.REPEAT }) seagulTex = Texture.fromImage(path + "/seagull.jpg", { wrap: gl.REPEAT }) skyTex = Texture.fromImage(path + "/sky_photo.jpg", { wrap: gl.REPEAT }) sharkTex = Texture.fromImage(path + "/shark.jpg", { wrap: gl.REPEAT }) drewnoMat = new Material({ color: [1, 1, 1], fogColor: [0.7, 0.8, 1.1, 0.85], specularWeight: 1.5, shininess: 15.0, diffuse: drewnotex, bump: drewnotex, bumpWeight: 0.2 }) kamienMat = new Material({ color: [1, 1, 1], fogColor: [1.4, 1.4, 1.4, 0.95], specularWeight: 0.3, shininess: 93.0, diffuse: kamienTex, bump: kamienTex, bumpWeight: 0.4 }) kamienMat2 = new Material({ color: [1, 1, 1], fogColor: [1.2, 1.2, 1.2, 0.95], specularWeight: 0.3, shininess: 100.0, diffuse: kamienTex, bump: kamienTex, bumpWeight: 0.4 }) skyMat = new Material({ color: [1, 1, 1], fogColor: [0.8, 0.8, 0.8, 1.0], fogY: true, specularWeight: 1.5, shininess: 4.0, diffuse: skyTex }) light = new Light([{ intensity: 1.0, lightType: 2.0, color: [1.0, 1.0, 1.0] }, ]) lights = new Light([ { lightPosition: new Vector(20.0, 30.0, -40.0), attenuation: 2000.0, intensity: 1.5, color: [0.9, 0.82, 0.7], shadow: true, }, { intensity: 0.2, lightType: 2.0, color: [0.1, 0.13, 0.16] } ]) shady = new Shadow(world, new Vector(20.0, 30.0, -40.0), 0.1, 300.0) lights.setShadow(shady) Mesh.obj(path2 + "/ter.obj", function(e) { gm = new GameObject(world, { shader: customBasicShader({ useRain: true, useDiffuse: true, useTiling: true, useLights: true }), light: lights, material: new Material({ color: [1.0, 1.0, 1.0], diffuse: Texture.fromImage(path + "/9.jpg", { wrap: gl.CLAMP }) }), mesh: e.scale(30.0, 15.0, 30.0).move(0, -25, 0) }) }) Mesh.obj(path2 + "/castle.obj", function(e) { for (var i in e) { var newMesh = e[i] var shaderAdd = customBasicShader({ useDiffuse: true, useTiling: true, useLights: true, useBump: true, boxMapping: true, useRain: true, useShadow: true }) var materialAdd = new Material() if (i == "initialShadingGroup") { materialAdd = new Material({ color: [1, 1, 1], fogColor: [1.1, 1.0, 1.0, 0.8], specularWeight: 0.1, shininess: 0.1, diffuse: drewnotex, bump: drewnotex, bumpWeight: 0.2 }) q = new GameObject(world, { shader: shaderAdd, material: materialAdd, light: lights, mesh: newMesh.scale(2.2, 2.2, 2.2).move(0, 1, 0) }) q.move(13.5, -2.5, 4.7) q.uniforms["tiling"] = [0.1, 0.1] } else if (i == "pasted__lambert3SG") { materialAdd = new Material({ color: [1, 1, 1], fogColor: [1.1, 1.0, 1.0, 0.8], specularWeight: 0.1, shininess: 0.1, diffuse: lodkaTex1, bump: lodkaTex1, bumpWeight: 0.2 }) q = new GameObject(world, { shader: customBasicShader({ useDiffuse: true, useTiling: true, useLights: true, useBump: true, useRain: true, useShadow: true }), material: materialAdd, light: lights, mesh: newMesh.scale(2.2, 2.2, 2.2).move(0, 1, 0) }) q.move(13.5, -2.5, 4.7) q.uniforms["tiling"] = [2.0, 2.0] } else if (i == "lambert3SG") { materialAdd = new Material({ color: [1, 1, 1], fogColor: [1.1, 1.0, 1.0, 0.8], specularWeight: 0.1, shininess: 0.1, diffuse: drewnotex, bump: drewnotex, bumpWeight: 0.2 }) q = new GameObject(world, { shader: shaderAdd, material: materialAdd, light: lights, mesh: newMesh.scale(2.2, 2.2, 2.2).move(0, 1, 0) }) q.move(13.5, -2.5, 4.7) q.uniforms["tiling"] = [0.1, 0.1] } else { } } }) Mesh.obj(path2 + "/rocks2.obj", function(e) { gm = new GameObject(world, { shader: customBasicShader({ useRain: true, useDiffuse: true, useTiling: true, useLights: true, useBump: true, boxMapping: true, useShadow: true, darken: true }), light: lights, material: kamienMat, mesh: e.scale(200.0, 200.0, 200.0).move(0, 0, 0) }) gm.uniforms["tiling"] = [0.11, 0.09] }) Mesh.obj(path2 + "/stone_3.obj", function(e) { gm = new GameObject(world, { shader: customBasicShader({ useRain: true, useDiffuse: true, useTiling: true, useLights: true, useBump: true, boxMapping: true, useShadow: true, darken: true }), light: lights, material: kamienMat2, mesh: e.scale(0.3, 0.2, 0.3).rotate(-90, 0, 0).move(35, -4, 50) }) gm.uniforms["tiling"] = [0.11, 0.09] }) coords = [30, 30, -25, -25, 50, 50, -45, -45, 65, 65] speed = [0.1, 0.1, 0.12, 0.12, 0.18, 0.18, 0.2, 0.2, 0.15, 0.15] var seagullMesh = [] Mesh.obj(path2 + "/seagull.obj", function(e) {seagullMesh = e}) function randomSpherePoint(x0, y0, z0, radius) { var u = Math.random(); var v = Math.random(); var theta = 2 * Math.PI * u; var phi = Math.acos(2 * v - 1); var x = x0 + (radius * Math.sin(phi) * Math.cos(theta)); var y = y0 + (radius/4.0 * Math.sin(phi) * Math.sin(theta)); var z = z0 + (radius * Math.cos(phi)); return [x, y, z]; } function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } Seagull = function(id) { if (Math.random(1,3)<= 2){ this.dir = 1 } else { this.dir = -1 } this.speed = randomInt(2,4)/100 var seagullShader = sharkShader({ mewa:true, useDiffuse: true, useTiling: true, useLights: true, useBump: true, }) var seagullMat = new Material({ color: [0.6,0.6,0.6], fogColor: [0,0,0], specularWeight: 1.0, shininess: 1.0, diffuse: seagulTex, bump: seagulTex, bumpWeight: 0.5}) for (var i in seagullMesh) { var t = randomSpherePoint(0,20,0,20) var newMesh = seagullMesh[i] if (i == "Seagull1:Seagull_TN") { this.obj1 = new GameObject(world, { shader: seagullShader, material: seagullMat, light: lights, mesh: newMesh.scale(0.95,1.0,1.0) }) this.obj1.rotY = randomInt(1, 360) this.obj1.move(t[0]*this.dir, t[1], t[2]*this.dir) } else if (i == "Seagull1:Seagull_TN_NONE") { this.obj2 = new GameObject(world, { shader: seagullShader, material: new Material({ color: [0.3,0.3,0.3], fogColor: [0,0,0], specularWeight: 1.0, shininess: 15.0, diffuse: seagulTex, bump: seagulTex, bumpWeight: 0.5}), light: lights, mesh: newMesh.scale(1.0,1.0,1.0) }) this.obj2.rotY = this.obj1.rotY this.obj2.x = this.obj1.x this.obj2.y = this.obj1.y this.obj2.z = this.obj1.z } else {} } } Seagull.prototype.fly = function() { this.obj1.uniforms.time =(Date.now() - start)*this.speed this.obj2.uniforms.time = Date.now() - start this.obj1.x += this.speed * Math.sin(Math.degToRad * (this.obj1.rotY+180)); this.obj1.z += this.speed * Math.cos(Math.degToRad * (this.obj1.rotY+180)); if (this.obj1.x < -100 || this.obj1.x > 100) { this.obj1.rotY += randomInt(180, 360) } if (this.obj1.z < -100 || this.obj1.z > 100) { this.obj1.rotY += randomInt(180, 360) } this.obj2.x = this.obj1.x this.obj2.z = this.obj1.z this.obj1.rotY += this.dir * randomInt(1, 10)/100 this.obj2.rotY = this.obj1.rotY } Mesh.obj(path2 + "/shaaark.obj", function(e) { myTab = e; }) Shark = function(id) { this.obj = new GameObject(world, { shader: sharkShader({ useDiffuse: true, useTiling: true, useLights: true, useBump: true, useRain: true }), light: lights, material: new Material({ color: [138.0 / 255.0, 138.0 / 255.0, 138.0 / 255.0], diffuse: sharkTex, bump: sharkTex, bumpWeight: 0.1 }), mesh: myTab }) this.obj.scale(0.2, 0.2, 0.2) this.obj.rotY = randomInt(1, 180) this.obj.move(10 + Math.random(1, 4) * 10, 0, randomInt(1, 4) * 10) this.speed = randomInt(2, 4) / 50 this.obj.y = -1.2 var ani, ani22 ani22 = new Animation(this.obj, 5000, { y: -1.6, rotX: 5.0, onComplete: function(e) { ani2.run() } }) // ani22.run() ani2 = new Animation(this.obj, 1000, { y: -1.2, rotX: -5.0, onComplete: function(e) { ani22.run() } }) ani2.run() } Shark.prototype.mySwim = function() { this.obj.uniforms.time = Date.now() - start this.obj.x += this.speed * Math.sin(Math.degToRad * this.obj.rotY); this.obj.z += this.speed * Math.cos(Math.degToRad * this.obj.rotY); if (this.obj.x < -200 || this.obj.x > 200) { this.obj.rotY += 180 } if (this.obj.z < -200 || this.obj.z > 200) { this.obj.rotY += 180 + randomInt(1, 4) } } Mesh.obj(path2 + "/cala_lodka4.obj", function(e) { for (var i in e) { var newMesh = e[i] var lodkaShader = customBasicShader({ useDiffuse: true, useTiling: true, useLights: true, useBump: true, specularWeight: 1.0, shininess: 15.0, useRain: true }) if (i == "lambert4SG") { lodka = new GameObject(world, { shader: lodkaShader, material: new Material({ color: [1, 1, 1], fogColor: [0.5, 0.55, 0.65, 0.95], diffuse: lodkaTex2, bump: lodkaTex2, bumpWeight: 0.1 }), light: lights, mesh: newMesh.scale(3.2, 3.2, 3.2) }) lodka.move(20, 1.0, 50) } else if (i == "lambert3SG") { lodka2 = new GameObject(world, { shader: lodkaShader, material: new Material({ color: [1, 1, 1], fogColor: [0.5, 0.55, 0.65, 0.95], diffuse: lodkaTex1, bump: lodkaTex1, bumpWeight: 0.2 }), light: lights, mesh: newMesh.scale(3.2, 3.2, 3.2) }) lodka2.move(20, 1.0, 50) } else {} } }) Mesh.obj(path2 + "/kopula.obj", function(e) { gm4 = new GameObject(world, { shader: customBasicShader({ useSky: true, useRain: true, useDiffuse: true, useLights: true }), light: light, material: skyMat, mesh: e.scale(3.5, 3.5, 3.5).rotate(0, -30, 0).move(0, -3, 0) }) }) pressedButton = 0 Camera.prototype.customMovement = function(factor) { var t = this; t.md = 0; t.eC = 0; t.sensitivity = 0.5; t.factor = factor || t.factor; var fac = t.factor; var sen = t.sensitivity; t.boundCloser = 0 t.boundFurther = 0 t.releaseButton = false t.rad = 44 t.boundLX =0 t.boundRX = 0 document.onkeypress = pressButton; function pressButton(e) { var ev = e || window.event; t.myF = 1 t.releaseButton = false if(ev.keyCode == 87 || ev.charCode == 119) { if (t.boundCloser<3){ pressedButton = 119 t.boundCloser += t.factor t.boundFurther +=t.factor } else{ pressedButton = 0 } } if(ev.keyCode == 83 || ev.charCode ==115) { if (t.boundFurther> -3){ pressedButton = 115 t.boundFurther -= t.factor t.boundCloser-=t.factor} else{ pressedButton = 0 } } if(ev.keyCode == 81 || ev.charCode == 113) { if (t.position.y < 35){ pressedButton = 113 } else{ pressedButton = 0 } } if(ev.keyCode == 69 || ev.charCode == 101) { if (t.position.y> 4){ pressedButton = 101 } else{ pressedButton = 0 } } } document.onkeyup= function(e) { camera.releaseButton = true } mouseDown = function(e) { var x = e.clientX ? e.clientX : e.x; var y = e.clientY ? e.clientY : e.y; t.tempY = y; t.mD = e.button+1; } mouseMove = function(ev) { if( t.mD != 0) { if( t.tempY == null) { t.tempY = ev.clientY; } var curY = ev.clientY; var deltaY = (t.tempY - curY) * t.sensitivity; t.tempY = curY; if( t.mD ==1 ){ if (t.rotation.x < -15){ if (deltaY >0 ){ deltaY = 0 } } else if(t.rotation.x > 40){ if (deltaY < 0 ){ deltaY = 0 } } t.pitchStep = -deltaY; t.pitch(t.pitchStep*0.3) } } } mouseUp = function(e) { t.mD = 0; } mouseWheel = function(e){ if (e.wheelDelta> 0){ if (t.rad > 30 ){ t.rad -= 1 } } else if(e.wheelDelta <0){ if (t.rad <55 ){ t.rad += 1 } } } // gl.canvas.addEventListener('mousewheel', mouseWheel, false); gl.canvas.addEventListener('mousedown', mouseDown, false); gl.canvas.addEventListener('mousemove', mouseMove, false); gl.canvas.addEventListener('mouseup', mouseUp, false); } waterNorm = Texture.fromImage(path + "/13.png", { wrap: gl.REPEAT }) dudv1 = Texture.fromImage(path + "/12.png", { wrap: gl.REPEAT }) wShader = customShader({ useWater: true, useRain: true, useShadow: true, useLights: true, useTiling: true, lightPos: new Vector(-20, 40, -200.0), cameraPos: new Vector(3.45, 22.59, -16.40, 1.0) }) waterTimer = function() { for (var i = 0; i < sharksNum; i++) { sharks[i] = new Shark(i) }; for (var i = 0; i < seagullsNum; i++) { seagulls[i] = new Seagull(i) }; waterSurface = new GameObject(world, { shader: wShader, light: lights, material: new Material({ color: [1.0, 1.0, 1.0], dudvMap: dudv1, specularWeight: 0.1, shininess: 15.0, cameraPos: new Vector(3.45, 22.59, -16.40, 1.0), shineMap: skyTex, normalMap: waterNorm, time: .00025 * (Date.now() - start) }), mesh: Mesh.plane(10, 10).scale(250.0, 250.0, 250.0).rotate(90, 0, 0).move(-40, 0.5, -50), }) waterSurface.uniforms.cameraPos = [3.45, 22.59, -16.40, 1.0] waterSurface.uniforms.waterColor = [0.3, 0.5, 0.6, 1.0] } gl.angle = 60 camera.position = new Vector(3.456853334133533, 22.599672949188665, -16.405737770944057); camera.rotation = new Vector(10.0, 0.0, 0); camera.customMovement(0.2) agl.init(); } var q = 0.0 var rot = 2.5 var turnss = 0.0 var rrr = 180 window.logic = function() { if (!gamePaused) { if (pressedButton== 119 && camera.releaseButton == false){ camera.rad -=camera.factor } else if (pressedButton== 115 && camera.releaseButton == false){ camera.rad +=camera.factor } else if (pressedButton== 113 && camera.releaseButton == false){ camera.position.y += camera.factor } else if (pressedButton== 101 && camera.releaseButton == false){ camera.position.y -= camera.factor } else if(camera.releaseButton == true){ if (camera.myF>0){ camera.myF -=camera.factor/3 if (pressedButton== 119){ camera.rad -= camera.factor * camera.myF} else if (pressedButton== 115){ camera.rad += camera.factor * camera.myF } else if (pressedButton== 113){ camera.position.y += camera.factor * camera.myF } else if (pressedButton== 101){ camera.position.y -= camera.factor * camera.myF } } else if(camera.myF < 0 ){ pressedButton = 0 } } turnss += 0.05 rrr -=0.05 camera.position.z = Math.cos(turnss%360 * Math.degToRad)*camera.rad ; camera.position.x = Math.sin(turnss%360 * Math.degToRad)*camera.rad ; camera.rotation.y = rrr camera.setModelView() if(Resource.isLoading == false){ if(!waterSurface){ waterTimer() } } if (waterSurface) { if (!shadowDrawn) { shadowDrawn = true shady.draw() } for (var i = 0; i < sharksNum; i++) { sharks[i].mySwim() }; for (var i = 0; i < seagullsNum; i++) { seagulls[i].fly() }; waterSurface.uniforms.time = Date.now() - start var myY = Math.sin(lodka.x - 0.62 + Math.PI + 0.0008 * (Date.now() - start)) lodka.y = myY + 0.4 if (myY < 0.0 && myY >= -0.5) { q = -0.01 } else if (myY <= -0.5) { q = 0.05 } else if (myY > 0 && myY <= 0.5) { q = 0.01 } else { q = -0.05 } rot += q lodka.rotate(-rot, 120 + rot, 0) lodka2.rotate(-rot, 120 + rot, 0) lodka2.y = lodka.y } } } window.draw = function() { if (!gamePaused) { world.draw(camera); } } glStart(window.setup);
RUN