From 8f06a4f37dc500104616ffd457c87b22a3445f2c Mon Sep 17 00:00:00 2001 From: alteredq Date: Tue, 30 Nov 2010 17:40:40 +0100 Subject: [PATCH] Refactored default uniforms out of Fresnel shader and ubershader into buildProgram. Also yet another fix for car demo load progress. --- build/Three.js | 19 ++++++------ build/ThreeDebug.js | 19 ++++++------ build/ThreeExtras.js | 21 +++++++------ examples/materials_cars.html | 2 +- src/extras/ShaderUtils.js | 12 ++------ src/materials/MeshShaderMaterial.js | 2 +- src/renderers/WebGLRenderer.js | 46 ++++++++++++++++------------- 7 files changed, 58 insertions(+), 63 deletions(-) diff --git a/build/Three.js b/build/Three.js index e7fe0fb436..b9faee8eab 100644 --- a/build/Three.js +++ b/build/Three.js @@ -123,13 +123,12 @@ x.setAttribute("y1",V.positionScreen.y);x.setAttribute("x2",ba.positionScreen.x) -H;F.positionScreen.x*=E;F.positionScreen.y*=-H;P.positionScreen.x*=E;P.positionScreen.y*=-H;s.addPoint(w.positionScreen.x,w.positionScreen.y);s.addPoint(F.positionScreen.x,F.positionScreen.y);s.addPoint(P.positionScreen.x,P.positionScreen.y);if(O.instersects(s)){C=0;for(A=K.meshMaterial.length;C= 0.0 )":"",f?"dirSpecularWeight = pow( dirDotNormalHalf, mShininess );":"",f?"dirDiffuse += mColor * dirDiffuseWeight;":"",f?"dirSpecular += mSpecular * dirSpecularWeight;":"",f?"}":"","vec4 totalLight = mAmbient;",f?"totalLight += dirDiffuse + dirSpecular;":"",d?"totalLight += pointDiffuse + pointSpecular;": "","if ( mixEnvMap ) {\ngl_FragColor = vec4( mix( mapColor.rgb * totalLight.xyz * vLightWeighting, cubeColor.rgb, mReflectivity ), mapColor.a );\n} else {\ngl_FragColor = vec4( mapColor.rgb * cubeColor.rgb * totalLight.xyz * vLightWeighting, mapColor.a );\n}\n} else if ( material == 1 ) {\nif ( mixEnvMap ) {\ngl_FragColor = vec4( mix( mColor.rgb * mapColor.rgb * vLightWeighting, cubeColor.rgb, mReflectivity ), mColor.a * mapColor.a );\n} else {\ngl_FragColor = vec4( mColor.rgb * mapColor.rgb * cubeColor.rgb * vLightWeighting, mColor.a * mapColor.a );\n}\n} else {\nif ( mixEnvMap ) {\ngl_FragColor = mix( mColor * mapColor, cubeColor, mReflectivity );\n} else {\ngl_FragColor = mColor * mapColor * cubeColor;\n}\n}\n}"].join("\n"); -l=c(o,i);b.useProgram(l);e(l,["viewMatrix","modelViewMatrix","projectionMatrix","normalMatrix","objMatrix","cameraPosition","enableLighting","ambientLightColor","material","mColor","mAmbient","mSpecular","mShininess","mOpacity","enableMap","tMap","enableCubeMap","tCube","mixEnvMap","mReflectivity","mRefractionRatio","useRefract","m2Near","mFarPlusNear","mFarMinusNear"]);f&&e(l,["directionalLightNumber","directionalLightColor","directionalLightDirection"]);d&&e(l,["pointLightNumber","pointLightColor", +l=c(o,i);b.useProgram(l);e(l,["viewMatrix","modelViewMatrix","projectionMatrix","normalMatrix","objectMatrix","cameraPosition","enableLighting","ambientLightColor","material","mColor","mAmbient","mSpecular","mShininess","mOpacity","enableMap","tMap","enableCubeMap","tCube","mixEnvMap","mReflectivity","mRefractionRatio","useRefract","m2Near","mFarPlusNear","mFarMinusNear"]);f&&e(l,["directionalLightNumber","directionalLightColor","directionalLightDirection"]);d&&e(l,["pointLightNumber","pointLightColor", "pointLightPosition"]);b.uniform1i(l.uniforms.enableMap,0);b.uniform1i(l.uniforms.tMap,0);b.uniform1i(l.uniforms.enableCubeMap,0);b.uniform1i(l.uniforms.tCube,1);b.uniform1i(l.uniforms.mixEnvMap,0);b.uniform1i(l.uniforms.useRefract,0);g(l)})(a.directional,a.point);this.setSize=function(f,d){m.width=f;m.height=d;b.viewport(0,0,m.width,m.height)};this.clear=function(){b.clear(b.COLOR_BUFFER_BIT|b.DEPTH_BUFFER_BIT)};this.setupLights=function(f,d){var i,o,k,j,n,t=[],Q=[],r=[];j=[];n=[];b.uniform1i(f.uniforms.enableLighting, d.lights.length);i=0;for(o=d.lights.length;i=0;i--){o=f.__webGLObjects[i].__object;d==o&&f.__webGLObjects.splice(i,1)}};this.setupMatrices=function(f, -d){f.autoUpdateMatrix&&f.updateMatrix();E.multiply(d.matrix,f.matrix);w.set(d.matrix.flatten());F.set(E.flatten());P.set(d.projectionMatrix.flatten());H=THREE.Matrix4.makeInvert3x3(E).transpose();N.set(H.m);O.set(f.matrix.flatten())};this.loadMatrices=function(f){b.uniformMatrix4fv(f.uniforms.viewMatrix,false,w);b.uniformMatrix4fv(f.uniforms.modelViewMatrix,false,F);b.uniformMatrix4fv(f.uniforms.projectionMatrix,false,P);b.uniformMatrix3fv(f.uniforms.normalMatrix,false,N);b.uniformMatrix4fv(f.uniforms.objMatrix, +d){f.autoUpdateMatrix&&f.updateMatrix();E.multiply(d.matrix,f.matrix);w.set(d.matrix.flatten());F.set(E.flatten());P.set(d.projectionMatrix.flatten());H=THREE.Matrix4.makeInvert3x3(E).transpose();N.set(H.m);O.set(f.matrix.flatten())};this.loadMatrices=function(f){b.uniformMatrix4fv(f.uniforms.viewMatrix,false,w);b.uniformMatrix4fv(f.uniforms.modelViewMatrix,false,F);b.uniformMatrix4fv(f.uniforms.projectionMatrix,false,P);b.uniformMatrix3fv(f.uniforms.normalMatrix,false,N);b.uniformMatrix4fv(f.uniforms.objectMatrix, false,O)};this.loadCamera=function(f,d){b.uniform3f(f.uniforms.cameraPosition,d.position.x,d.position.y,d.position.z)};this.setBlending=function(f){switch(f){case THREE.AdditiveBlending:b.blendEquation(b.FUNC_ADD);b.blendFunc(b.ONE,b.ONE);break;case THREE.SubtractiveBlending:b.blendFunc(b.DST_COLOR,b.ZERO);break;default:b.blendEquation(b.FUNC_ADD);b.blendFunc(b.ONE,b.ONE_MINUS_SRC_ALPHA)}};this.setFaceCulling=function(f,d){if(f){!d||d=="ccw"?b.frontFace(b.CCW):b.frontFace(b.CW);if(f=="back")b.cullFace(b.BACK); else f=="front"?b.cullFace(b.FRONT):b.cullFace(b.FRONT_AND_BACK);b.enable(b.CULL_FACE)}else b.disable(b.CULL_FACE)}}; THREE.RenderableFace3=function(){this.z=null;this.v1=new THREE.Vertex;this.v2=new THREE.Vertex;this.v3=new THREE.Vertex;this.centroidWorld=new THREE.Vector3;this.centroidScreen=new THREE.Vector3;this.normalWorld=new THREE.Vector3;this.vertexNormalsWorld=[new THREE.Vector3,new THREE.Vector3,new THREE.Vector3];this.faceMaterial=this.meshMaterial=null;this.overdraw=false;this.uvs=[null,null,null]}; diff --git a/build/ThreeDebug.js b/build/ThreeDebug.js index 31a02bf3b1..856c4eab62 100644 --- a/build/ThreeDebug.js +++ b/build/ThreeDebug.js @@ -124,13 +124,12 @@ x.setAttribute("y1",V.positionScreen.y);x.setAttribute("x2",ba.positionScreen.x) -I;F.positionScreen.x*=E;F.positionScreen.y*=-I;Q.positionScreen.x*=E;Q.positionScreen.y*=-I;r.addPoint(w.positionScreen.x,w.positionScreen.y);r.addPoint(F.positionScreen.x,F.positionScreen.y);r.addPoint(Q.positionScreen.x,Q.positionScreen.y);if(P.instersects(r)){C=0;for(A=H.meshMaterial.length;C= 0.0 )":"",f?"dirSpecularWeight = pow( dirDotNormalHalf, mShininess );":"",f?"dirDiffuse += mColor * dirDiffuseWeight;":"",f?"dirSpecular += mSpecular * dirSpecularWeight;":"",f?"}":"","vec4 totalLight = mAmbient;",f?"totalLight += dirDiffuse + dirSpecular;":"",d?"totalLight += pointDiffuse + pointSpecular;": "","if ( mixEnvMap ) {\ngl_FragColor = vec4( mix( mapColor.rgb * totalLight.xyz * vLightWeighting, cubeColor.rgb, mReflectivity ), mapColor.a );\n} else {\ngl_FragColor = vec4( mapColor.rgb * cubeColor.rgb * totalLight.xyz * vLightWeighting, mapColor.a );\n}\n} else if ( material == 1 ) {\nif ( mixEnvMap ) {\ngl_FragColor = vec4( mix( mColor.rgb * mapColor.rgb * vLightWeighting, cubeColor.rgb, mReflectivity ), mColor.a * mapColor.a );\n} else {\ngl_FragColor = vec4( mColor.rgb * mapColor.rgb * cubeColor.rgb * vLightWeighting, mColor.a * mapColor.a );\n}\n} else {\nif ( mixEnvMap ) {\ngl_FragColor = mix( mColor * mapColor, cubeColor, mReflectivity );\n} else {\ngl_FragColor = mColor * mapColor * cubeColor;\n}\n}\n}"].join("\n"); -l=c(o,i);b.useProgram(l);e(l,["viewMatrix","modelViewMatrix","projectionMatrix","normalMatrix","objMatrix","cameraPosition","enableLighting","ambientLightColor","material","mColor","mAmbient","mSpecular","mShininess","mOpacity","enableMap","tMap","enableCubeMap","tCube","mixEnvMap","mReflectivity","mRefractionRatio","useRefract","m2Near","mFarPlusNear","mFarMinusNear"]);f&&e(l,["directionalLightNumber","directionalLightColor","directionalLightDirection"]);d&&e(l,["pointLightNumber","pointLightColor", +l=c(o,i);b.useProgram(l);e(l,["viewMatrix","modelViewMatrix","projectionMatrix","normalMatrix","objectMatrix","cameraPosition","enableLighting","ambientLightColor","material","mColor","mAmbient","mSpecular","mShininess","mOpacity","enableMap","tMap","enableCubeMap","tCube","mixEnvMap","mReflectivity","mRefractionRatio","useRefract","m2Near","mFarPlusNear","mFarMinusNear"]);f&&e(l,["directionalLightNumber","directionalLightColor","directionalLightDirection"]);d&&e(l,["pointLightNumber","pointLightColor", "pointLightPosition"]);b.uniform1i(l.uniforms.enableMap,0);b.uniform1i(l.uniforms.tMap,0);b.uniform1i(l.uniforms.enableCubeMap,0);b.uniform1i(l.uniforms.tCube,1);b.uniform1i(l.uniforms.mixEnvMap,0);b.uniform1i(l.uniforms.useRefract,0);g(l)})(a.directional,a.point);this.setSize=function(f,d){m.width=f;m.height=d;b.viewport(0,0,m.width,m.height)};this.clear=function(){b.clear(b.COLOR_BUFFER_BIT|b.DEPTH_BUFFER_BIT)};this.setupLights=function(f,d){var i,o,k,j,n,t=[],R=[],s=[];j=[];n=[];b.uniform1i(f.uniforms.enableLighting, d.lights.length);i=0;for(o=d.lights.length;i=0;i--){o=f.__webGLObjects[i].__object;d==o&&f.__webGLObjects.splice(i,1)}};this.setupMatrices=function(f, -d){f.autoUpdateMatrix&&f.updateMatrix();E.multiply(d.matrix,f.matrix);w.set(d.matrix.flatten());F.set(E.flatten());Q.set(d.projectionMatrix.flatten());I=THREE.Matrix4.makeInvert3x3(E).transpose();O.set(I.m);P.set(f.matrix.flatten())};this.loadMatrices=function(f){b.uniformMatrix4fv(f.uniforms.viewMatrix,false,w);b.uniformMatrix4fv(f.uniforms.modelViewMatrix,false,F);b.uniformMatrix4fv(f.uniforms.projectionMatrix,false,Q);b.uniformMatrix3fv(f.uniforms.normalMatrix,false,O);b.uniformMatrix4fv(f.uniforms.objMatrix, +d){f.autoUpdateMatrix&&f.updateMatrix();E.multiply(d.matrix,f.matrix);w.set(d.matrix.flatten());F.set(E.flatten());Q.set(d.projectionMatrix.flatten());I=THREE.Matrix4.makeInvert3x3(E).transpose();O.set(I.m);P.set(f.matrix.flatten())};this.loadMatrices=function(f){b.uniformMatrix4fv(f.uniforms.viewMatrix,false,w);b.uniformMatrix4fv(f.uniforms.modelViewMatrix,false,F);b.uniformMatrix4fv(f.uniforms.projectionMatrix,false,Q);b.uniformMatrix3fv(f.uniforms.normalMatrix,false,O);b.uniformMatrix4fv(f.uniforms.objectMatrix, false,P)};this.loadCamera=function(f,d){b.uniform3f(f.uniforms.cameraPosition,d.position.x,d.position.y,d.position.z)};this.setBlending=function(f){switch(f){case THREE.AdditiveBlending:b.blendEquation(b.FUNC_ADD);b.blendFunc(b.ONE,b.ONE);break;case THREE.SubtractiveBlending:b.blendFunc(b.DST_COLOR,b.ZERO);break;default:b.blendEquation(b.FUNC_ADD);b.blendFunc(b.ONE,b.ONE_MINUS_SRC_ALPHA)}};this.setFaceCulling=function(f,d){if(f){!d||d=="ccw"?b.frontFace(b.CCW):b.frontFace(b.CW);if(f=="back")b.cullFace(b.BACK); else f=="front"?b.cullFace(b.FRONT):b.cullFace(b.FRONT_AND_BACK);b.enable(b.CULL_FACE)}else b.disable(b.CULL_FACE)}}; THREE.RenderableFace3=function(){this.z=null;this.v1=new THREE.Vertex;this.v2=new THREE.Vertex;this.v3=new THREE.Vertex;this.centroidWorld=new THREE.Vector3;this.centroidScreen=new THREE.Vector3;this.normalWorld=new THREE.Vector3;this.vertexNormalsWorld=[new THREE.Vector3,new THREE.Vector3,new THREE.Vector3];this.faceMaterial=this.meshMaterial=null;this.overdraw=false;this.uvs=[null,null,null]}; diff --git a/build/ThreeExtras.js b/build/ThreeExtras.js index bd186abc36..1eae6bd263 100644 --- a/build/ThreeExtras.js +++ b/build/ThreeExtras.js @@ -124,13 +124,12 @@ G.setAttribute("y1",X.positionScreen.y);G.setAttribute("x2",l.positionScreen.x); -D;w.positionScreen.x*=s;w.positionScreen.y*=-D;v.positionScreen.x*=s;v.positionScreen.y*=-D;u.addPoint(q.positionScreen.x,q.positionScreen.y);u.addPoint(w.positionScreen.x,w.positionScreen.y);u.addPoint(v.positionScreen.x,v.positionScreen.y);if(K.instersects(u)){M=0;for(J=n.meshMaterial.length;M= 0.0 )":"",h?"dirSpecularWeight = pow( dirDotNormalHalf, mShininess );":"",h?"dirDiffuse += mColor * dirDiffuseWeight;":"",h?"dirSpecular += mSpecular * dirSpecularWeight;":"",h?"}":"","vec4 totalLight = mAmbient;",h?"totalLight += dirDiffuse + dirSpecular;":"",e?"totalLight += pointDiffuse + pointSpecular;": "","if ( mixEnvMap ) {\ngl_FragColor = vec4( mix( mapColor.rgb * totalLight.xyz * vLightWeighting, cubeColor.rgb, mReflectivity ), mapColor.a );\n} else {\ngl_FragColor = vec4( mapColor.rgb * cubeColor.rgb * totalLight.xyz * vLightWeighting, mapColor.a );\n}\n} else if ( material == 1 ) {\nif ( mixEnvMap ) {\ngl_FragColor = vec4( mix( mColor.rgb * mapColor.rgb * vLightWeighting, cubeColor.rgb, mReflectivity ), mColor.a * mapColor.a );\n} else {\ngl_FragColor = vec4( mColor.rgb * mapColor.rgb * cubeColor.rgb * vLightWeighting, mColor.a * mapColor.a );\n}\n} else {\nif ( mixEnvMap ) {\ngl_FragColor = mix( mColor * mapColor, cubeColor, mReflectivity );\n} else {\ngl_FragColor = mColor * mapColor * cubeColor;\n}\n}\n}"].join("\n"); -j=b(r,m);c.useProgram(j);d(j,["viewMatrix","modelViewMatrix","projectionMatrix","normalMatrix","objMatrix","cameraPosition","enableLighting","ambientLightColor","material","mColor","mAmbient","mSpecular","mShininess","mOpacity","enableMap","tMap","enableCubeMap","tCube","mixEnvMap","mReflectivity","mRefractionRatio","useRefract","m2Near","mFarPlusNear","mFarMinusNear"]);h&&d(j,["directionalLightNumber","directionalLightColor","directionalLightDirection"]);e&&d(j,["pointLightNumber","pointLightColor", +j=b(r,m);c.useProgram(j);d(j,["viewMatrix","modelViewMatrix","projectionMatrix","normalMatrix","objectMatrix","cameraPosition","enableLighting","ambientLightColor","material","mColor","mAmbient","mSpecular","mShininess","mOpacity","enableMap","tMap","enableCubeMap","tCube","mixEnvMap","mReflectivity","mRefractionRatio","useRefract","m2Near","mFarPlusNear","mFarMinusNear"]);h&&d(j,["directionalLightNumber","directionalLightColor","directionalLightDirection"]);e&&d(j,["pointLightNumber","pointLightColor", "pointLightPosition"]);c.uniform1i(j.uniforms.enableMap,0);c.uniform1i(j.uniforms.tMap,0);c.uniform1i(j.uniforms.enableCubeMap,0);c.uniform1i(j.uniforms.tCube,1);c.uniform1i(j.uniforms.mixEnvMap,0);c.uniform1i(j.uniforms.useRefract,0);f(j)})(a.directional,a.point);this.setSize=function(h,e){k.width=h;k.height=e;c.viewport(0,0,k.width,k.height)};this.clear=function(){c.clear(c.COLOR_BUFFER_BIT|c.DEPTH_BUFFER_BIT)};this.setupLights=function(h,e){var m,r,p,o,x,C=[],S=[],B=[];o=[];x=[];c.uniform1i(h.uniforms.enableLighting, e.lights.length);m=0;for(r=e.lights.length;m=0;m--){r=h.__webGLObjects[m].__object;e==r&&h.__webGLObjects.splice(m,1)}};this.setupMatrices=function(h, -e){h.autoUpdateMatrix&&h.updateMatrix();s.multiply(e.matrix,h.matrix);q.set(e.matrix.flatten());w.set(s.flatten());v.set(e.projectionMatrix.flatten());D=THREE.Matrix4.makeInvert3x3(s).transpose();z.set(D.m);K.set(h.matrix.flatten())};this.loadMatrices=function(h){c.uniformMatrix4fv(h.uniforms.viewMatrix,false,q);c.uniformMatrix4fv(h.uniforms.modelViewMatrix,false,w);c.uniformMatrix4fv(h.uniforms.projectionMatrix,false,v);c.uniformMatrix3fv(h.uniforms.normalMatrix,false,z);c.uniformMatrix4fv(h.uniforms.objMatrix, +e){h.autoUpdateMatrix&&h.updateMatrix();s.multiply(e.matrix,h.matrix);q.set(e.matrix.flatten());w.set(s.flatten());v.set(e.projectionMatrix.flatten());D=THREE.Matrix4.makeInvert3x3(s).transpose();z.set(D.m);K.set(h.matrix.flatten())};this.loadMatrices=function(h){c.uniformMatrix4fv(h.uniforms.viewMatrix,false,q);c.uniformMatrix4fv(h.uniforms.modelViewMatrix,false,w);c.uniformMatrix4fv(h.uniforms.projectionMatrix,false,v);c.uniformMatrix3fv(h.uniforms.normalMatrix,false,z);c.uniformMatrix4fv(h.uniforms.objectMatrix, false,K)};this.loadCamera=function(h,e){c.uniform3f(h.uniforms.cameraPosition,e.position.x,e.position.y,e.position.z)};this.setBlending=function(h){switch(h){case THREE.AdditiveBlending:c.blendEquation(c.FUNC_ADD);c.blendFunc(c.ONE,c.ONE);break;case THREE.SubtractiveBlending:c.blendFunc(c.DST_COLOR,c.ZERO);break;default:c.blendEquation(c.FUNC_ADD);c.blendFunc(c.ONE,c.ONE_MINUS_SRC_ALPHA)}};this.setFaceCulling=function(h,e){if(h){!e||e=="ccw"?c.frontFace(c.CCW):c.frontFace(c.CW);if(h=="back")c.cullFace(c.BACK); else h=="front"?c.cullFace(c.FRONT):c.cullFace(c.FRONT_AND_BACK);c.enable(c.CULL_FACE)}else c.disable(c.CULL_FACE)}}; THREE.RenderableFace3=function(){this.z=null;this.v1=new THREE.Vertex;this.v2=new THREE.Vertex;this.v3=new THREE.Vertex;this.centroidWorld=new THREE.Vector3;this.centroidScreen=new THREE.Vector3;this.normalWorld=new THREE.Vector3;this.vertexNormalsWorld=[new THREE.Vector3,new THREE.Vector3,new THREE.Vector3];this.faceMaterial=this.meshMaterial=null;this.overdraw=false;this.uvs=[null,null,null]}; @@ -173,7 +172,7 @@ f;b.position.y=g;b.position.z=i;b.rotation.x=k;b.rotation.y=c;b.rotation.z=j;a.a f.push(new THREE.MeshBasicMaterial({map:new THREE.Texture(d[4])}));f.push(new THREE.MeshBasicMaterial({map:new THREE.Texture(d[5])}));mesh=new THREE.Mesh(new Cube(b,b,b,1,1,f,true),new THREE.MeshFaceMaterial);a.addObject(mesh);return mesh},addPanoramaCubePlanes:function(a,b,d){var f=b/2;b=new Plane(b,b);var g=Math.PI/2,i=Math.PI;SceneUtils.addMesh(a,b,1,0,0,-f,0,0,0,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[5])}));SceneUtils.addMesh(a,b,1,-f,0,0,0,g,0,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[0])})); SceneUtils.addMesh(a,b,1,f,0,0,0,-g,0,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[1])}));SceneUtils.addMesh(a,b,1,0,f,0,g,0,i,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[2])}));SceneUtils.addMesh(a,b,1,0,-f,0,-g,0,i,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[3])}))}},ShaderUtils={lib:{fresnel:{uniforms:{mRefractionRatio:{type:"f",value:1.02},mFresnelBias:{type:"f",value:0.1},mFresnelPower:{type:"f",value:2},mFresnelScale:{type:"f",value:1},tCube:{type:"t",value:1,texture:null}}, fragment_shader:"uniform samplerCube tCube;\nvarying vec3 vReflect;\nvarying vec3 vRefract[3];\nvarying float vReflectionFactor;\nvoid main() {\nvec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );\nvec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );\nrefractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;\nrefractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;\nrefractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;\nrefractedColor.a = 1.0;\ngl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );\n}", -vertex_shader:"attribute vec3 position;\nattribute vec3 normal;\nattribute vec3 uv;\nuniform mat4 objMatrix;\nuniform mat4 modelViewMatrix;\nuniform mat4 projectionMatrix;\nuniform vec3 cameraPosition;\nuniform float mRefractionRatio;\nuniform float mFresnelBias;\nuniform float mFresnelScale;\nuniform float mFresnelPower;\nvarying vec3 vReflect;\nvarying vec3 vRefract[3];\nvarying float vReflectionFactor;\nvoid main(void) {\nvec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );\nvec4 mPosition = objMatrix * vec4( position, 1.0 );\nvec3 nWorld = normalize ( mat3( objMatrix[0].xyz, objMatrix[1].xyz, objMatrix[2].xyz ) * normal );\nvec3 I = mPosition.xyz - cameraPosition;\nvReflect = reflect( I, nWorld );\nvRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );\nvRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );\nvRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );\nvReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );\ngl_Position = projectionMatrix * mvPosition;\n}"}}}, +vertex_shader:"attribute vec3 position;\nattribute vec3 normal;\nattribute vec3 uv;\nuniform float mRefractionRatio;\nuniform float mFresnelBias;\nuniform float mFresnelScale;\nuniform float mFresnelPower;\nvarying vec3 vReflect;\nvarying vec3 vRefract[3];\nvarying float vReflectionFactor;\nvoid main(void) {\nvec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );\nvec4 mPosition = objectMatrix * vec4( position, 1.0 );\nvec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );\nvec3 I = mPosition.xyz - cameraPosition;\nvReflect = reflect( I, nWorld );\nvRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );\nvRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );\nvRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );\nvReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );\ngl_Position = projectionMatrix * mvPosition;\n}"}}}, Cube=function(a,b,d,f,g,i,k){function c(w,v,z,K,u,h,e,m){var r=f||1,p=g||1,o=r+1,x=p+1;u=u/r;h=h/p;var C=j.vertices.length,S;if(w=="x"&&v=="y"||w=="y"&&v=="x")S="z";else if(w=="x"&&v=="z"||w=="z"&&v=="x")S="y";else if(w=="z"&&v=="y"||w=="y"&&v=="z")S="x";for(iy=0;iy, * vertex_shader: , - * uniforms: [ "parameter1": value1, "parameter2": value2 ], + * uniforms: { "parameter1": { type: "f", value: 1.0 }, "parameter2": { type: "i" value2: 2 } }, * shading: THREE.SmoothShading, * blending: THREE.NormalBlending, diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 8b337f0836..14869a2592 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -27,7 +27,7 @@ THREE.WebGLRenderer = function ( scene ) { _modelViewMatrixArray = new Float32Array(16), _projectionMatrixArray = new Float32Array(16), _normalMatrixArray = new Float32Array(9), - _objMatrixArray = new Float32Array(16), + _objectMatrixArray = new Float32Array(16), // ubershader material constants @@ -332,7 +332,7 @@ THREE.WebGLRenderer = function ( scene ) { material.program = buildProgram( material.fragment_shader, material.vertex_shader ); - identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objMatrix', 'cameraPosition' ]; + identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition' ]; for( u in material.uniforms ) identifiers.push(u); cacheUniformLocations( material.program, identifiers ); cacheAttributeLocations( material.program ); @@ -740,7 +740,7 @@ THREE.WebGLRenderer = function ( scene ) { _normalMatrix = THREE.Matrix4.makeInvert3x3( _modelViewMatrix ).transpose(); _normalMatrixArray.set( _normalMatrix.m ); - _objMatrixArray.set( object.matrix.flatten() ); + _objectMatrixArray.set( object.matrix.flatten() ); }; @@ -750,7 +750,7 @@ THREE.WebGLRenderer = function ( scene ) { _gl.uniformMatrix4fv( program.uniforms.modelViewMatrix, false, _modelViewMatrixArray ); _gl.uniformMatrix4fv( program.uniforms.projectionMatrix, false, _projectionMatrixArray ); _gl.uniformMatrix3fv( program.uniforms.normalMatrix, false, _normalMatrixArray ); - _gl.uniformMatrix4fv( program.uniforms.objMatrix, false, _objMatrixArray ); + _gl.uniformMatrix4fv( program.uniforms.objectMatrix, false, _objectMatrixArray ); }; @@ -1086,8 +1086,6 @@ THREE.WebGLRenderer = function ( scene ) { "attribute vec3 normal;", "attribute vec2 uv;", - "uniform vec3 cameraPosition;", - "uniform bool enableLighting;", "uniform bool useRefract;", @@ -1102,10 +1100,7 @@ THREE.WebGLRenderer = function ( scene ) { maxPointLights ? "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];" : "", maxPointLights ? "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];" : "", - "uniform mat4 objMatrix;", "uniform mat4 viewMatrix;", - "uniform mat4 modelViewMatrix;", - "uniform mat4 projectionMatrix;", "uniform mat3 normalMatrix;", "varying vec3 vNormal;", @@ -1124,12 +1119,12 @@ THREE.WebGLRenderer = function ( scene ) { // world space - "vec4 mPosition = objMatrix * vec4( position, 1.0 );", + "vec4 mPosition = objectMatrix * vec4( position, 1.0 );", "vViewPosition = cameraPosition - mPosition.xyz;", // this doesn't work on Mac - //"vec3 nWorld = mat3(objMatrix) * normal;", - "vec3 nWorld = mat3( objMatrix[0].xyz, objMatrix[1].xyz, objMatrix[2].xyz ) * normal;", + //"vec3 nWorld = mat3(objectMatrix) * normal;", + "vec3 nWorld = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;", // eye space @@ -1188,14 +1183,23 @@ THREE.WebGLRenderer = function ( scene ) { var program = _gl.createProgram(), - prefix = [ "#ifdef GL_ES", - "precision highp float;", - "#endif", - "" - ].join("\n"); - - _gl.attachShader( program, getShader( "fragment", prefix + fragment_shader ) ); - _gl.attachShader( program, getShader( "vertex", vertex_shader ) ); + prefix_fragment = [ + "#ifdef GL_ES", + "precision highp float;", + "#endif", + "" + ].join("\n"), + + prefix_vertex = [ + "uniform mat4 objectMatrix;", + "uniform mat4 modelViewMatrix;", + "uniform mat4 projectionMatrix;", + "uniform vec3 cameraPosition;", + "" + ].join("\n"); + + _gl.attachShader( program, getShader( "fragment", prefix_fragment + fragment_shader ) ); + _gl.attachShader( program, getShader( "vertex", prefix_vertex + vertex_shader ) ); _gl.linkProgram( program ); @@ -1322,7 +1326,7 @@ THREE.WebGLRenderer = function ( scene ) { // material properties (Basic / Lambert / Blinn-Phong shader) // material properties (Depth) - cacheUniformLocations( _program, [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objMatrix', 'cameraPosition', + cacheUniformLocations( _program, [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition', 'enableLighting', 'ambientLightColor', 'material', 'mColor', 'mAmbient', 'mSpecular', 'mShininess', 'mOpacity', 'enableMap', 'tMap', -- GitLab