提交 84d1b582 编写于 作者: A alteredq

Moved textured quad screen shader into ShaderUtils.

上级 418a6da7
......@@ -223,17 +223,18 @@ e,g,b,l,j,h,o){c=new THREE.Mesh(c,o);c.scale.x=c.scale.y=c.scale.z=d;c.position.
[];e.push(new THREE.MeshBasicMaterial({map:new THREE.Texture(d[0])}));e.push(new THREE.MeshBasicMaterial({map:new THREE.Texture(d[1])}));e.push(new THREE.MeshBasicMaterial({map:new THREE.Texture(d[2])}));e.push(new THREE.MeshBasicMaterial({map:new THREE.Texture(d[3])}));e.push(new THREE.MeshBasicMaterial({map:new THREE.Texture(d[4])}));e.push(new THREE.MeshBasicMaterial({map:new THREE.Texture(d[5])}));c=new THREE.Mesh(new Cube(c,c,c,1,1,e,true),new THREE.MeshFaceMaterial);a.addObject(c);return c},
addPanoramaCubePlanes:function(a,c,d){var e=c/2;c=new Plane(c,c);var g=Math.PI/2,b=Math.PI;SceneUtils.addMesh(a,c,1,0,0,-e,0,0,0,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[5])}));SceneUtils.addMesh(a,c,1,-e,0,0,0,g,0,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[0])}));SceneUtils.addMesh(a,c,1,e,0,0,0,-g,0,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[1])}));SceneUtils.addMesh(a,c,1,0,e,0,g,0,b,new THREE.MeshBasicMaterial({map:new THREE.Texture(d[2])}));SceneUtils.addMesh(a,
c,1,0,-e,0,-g,0,b,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:"uniform 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}"},
vertex_shader:"uniform float mRefractionRatio;\nuniform float mFresnelBias;\nuniform float mFresnelScale;\nuniform float mFresnelPower;\nvarying vec3 vReflect;\nvarying vec3 vRefract[3];\nvarying float vReflectionFactor;\nvoid main() {\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}"},
normal:{uniforms:{enableAO:{type:"i",value:0},enableDiffuse:{type:"i",value:0},tDiffuse:{type:"t",value:0,texture:null},tNormal:{type:"t",value:2,texture:null},tAO:{type:"t",value:3,texture:null},uNormalScale:{type:"f",value:1},tDisplacement:{type:"t",value:4,texture:null},uDisplacementBias:{type:"f",value:-0.5},uDisplacementScale:{type:"f",value:2.5},uPointLightPos:{type:"v3",value:new THREE.Vector3},uPointLightColor:{type:"c",value:new THREE.Color(15658734)},uDirLightPos:{type:"v3",value:new THREE.Vector3},
uDirLightColor:{type:"c",value:new THREE.Color(15658734)},uAmbientLightColor:{type:"c",value:new THREE.Color(328965)},uDiffuseColor:{type:"c",value:new THREE.Color(15658734)},uSpecularColor:{type:"c",value:new THREE.Color(1118481)},uAmbientColor:{type:"c",value:new THREE.Color(328965)},uShininess:{type:"f",value:30}},fragment_shader:"uniform vec3 uDirLightPos;\nuniform vec3 uAmbientLightColor;\nuniform vec3 uDirLightColor;\nuniform vec3 uPointLightColor;\nuniform vec3 uAmbientColor;\nuniform vec3 uDiffuseColor;\nuniform vec3 uSpecularColor;\nuniform float uShininess;\nuniform bool enableDiffuse;\nuniform bool enableAO;\nuniform sampler2D tDiffuse;\nuniform sampler2D tNormal;\nuniform sampler2D tAO;\nuniform float uNormalScale;\nvarying vec3 vTangent;\nvarying vec3 vBinormal;\nvarying vec3 vNormal;\nvarying vec2 vUv;\nvarying vec3 vPointLightVector;\nvarying vec3 vViewPosition;\nvoid main() {\nvec3 diffuseTex = vec3( 1.0, 1.0, 1.0 );\nvec3 aoTex = vec3( 1.0, 1.0, 1.0 );\nvec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;\nnormalTex.xy *= uNormalScale;\nnormalTex = normalize( normalTex );\nif( enableDiffuse )\ndiffuseTex = texture2D( tDiffuse, vUv ).xyz;\nif( enableAO )\naoTex = texture2D( tAO, vUv ).xyz;\nmat3 tsb = mat3( vTangent, vBinormal, vNormal );\nvec3 finalNormal = tsb * normalTex;\nvec3 normal = normalize( finalNormal );\nvec3 viewPosition = normalize( vViewPosition );\nvec4 pointDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );\nvec4 pointSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );\nvec3 pointVector = normalize( vPointLightVector );\nvec3 pointHalfVector = normalize( vPointLightVector + vViewPosition );\nfloat pointDotNormalHalf = dot( normal, pointHalfVector );\nfloat pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );\nfloat pointSpecularWeight = 0.0;\nif ( pointDotNormalHalf >= 0.0 )\npointSpecularWeight = pow( pointDotNormalHalf, uShininess );\npointDiffuse += vec4( uDiffuseColor, 1.0 ) * pointDiffuseWeight;\npointSpecular += vec4( uSpecularColor, 1.0 ) * pointSpecularWeight;\nvec4 dirDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );\nvec4 dirSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );\nvec4 lDirection = viewMatrix * vec4( uDirLightPos, 0.0 );\nvec3 dirVector = normalize( lDirection.xyz );\nvec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );\nfloat dirDotNormalHalf = dot( normal, dirHalfVector );\nfloat dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );\nfloat dirSpecularWeight = 0.0;\nif ( dirDotNormalHalf >= 0.0 )\ndirSpecularWeight = pow( dirDotNormalHalf, uShininess );\ndirDiffuse += vec4( uDiffuseColor, 1.0 ) * dirDiffuseWeight;\ndirSpecular += vec4( uSpecularColor, 1.0 ) * dirSpecularWeight;\nvec4 totalLight = vec4( uAmbientLightColor * uAmbientColor, 1.0 );\ntotalLight += vec4( uDirLightColor, 1.0 ) * ( dirDiffuse + dirSpecular );\ntotalLight += vec4( uPointLightColor, 1.0 ) * ( pointDiffuse + pointSpecular );\ngl_FragColor = vec4( totalLight.xyz * aoTex * diffuseTex, 1.0 );\n}",
vertex_shader:"attribute vec4 tangent;\nuniform vec3 uPointLightPos;\n#ifdef VERTEX_TEXTURES\nuniform sampler2D tDisplacement;\nuniform float uDisplacementScale;\nuniform float uDisplacementBias;\n#endif\nvarying vec3 vTangent;\nvarying vec3 vBinormal;\nvarying vec3 vNormal;\nvarying vec2 vUv;\nvarying vec3 vPointLightVector;\nvarying vec3 vViewPosition;\nvoid main() {\nvec4 mPosition = objectMatrix * vec4( position, 1.0 );\nvViewPosition = cameraPosition - mPosition.xyz;\nvec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );\nvNormal = normalize( normalMatrix * normal );\nvTangent = normalize( normalMatrix * tangent.xyz );\nvBinormal = cross( vNormal, vTangent ) * tangent.w;\nvBinormal = normalize( vBinormal );\nvUv = uv;\nvec4 lPosition = viewMatrix * vec4( uPointLightPos, 1.0 );\nvPointLightVector = normalize( lPosition.xyz - mvPosition.xyz );\n#ifdef VERTEX_TEXTURES\nvec3 dv = texture2D( tDisplacement, uv ).xyz;\nfloat df = uDisplacementScale * dv.x + uDisplacementBias;\nvec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;\ngl_Position = projectionMatrix * displacedPosition;\n#else\ngl_Position = projectionMatrix * mvPosition;\n#endif\n}"},
cube:{uniforms:{tCube:{type:"t",value:1,texture:null}},vertex_shader:"varying vec3 vViewPosition;\nvoid main() {\nvec4 mPosition = objectMatrix * vec4( position, 1.0 );\nvViewPosition = cameraPosition - mPosition.xyz;\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}",fragment_shader:"uniform samplerCube tCube;\nvarying vec3 vViewPosition;\nvoid main() {\nvec3 wPos = cameraPosition - vViewPosition;\ngl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );\n}"},convolution:{uniforms:{tDiffuse:{type:"t",
value:0,texture:null},uImageIncrement:{type:"v2",value:new THREE.Vector2(0.001953125,0)},cKernel:{type:"fv1",value:[]}},vertex_shader:"varying vec2 vUv;\nuniform vec2 uImageIncrement;\n#define KERNEL_SIZE 25.0\nvoid main(void) {\nvUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}",fragment_shader:"varying vec2 vUv;\nuniform sampler2D tDiffuse;\nuniform vec2 uImageIncrement;\n#define KERNEL_SIZE 25\nuniform float cKernel[KERNEL_SIZE];\nvoid main(void) {\nvec2 imageCoord = vUv;\nvec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );\nfor( int i=0; i<KERNEL_SIZE; ++i ) {\nsum += texture2D( tDiffuse, imageCoord ) * cKernel[i];\nimageCoord += uImageIncrement;\n}\ngl_FragColor = sum;\n}"},
basic:{uniforms:{},vertex_shader:"void main() {\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}",fragment_shader:"void main() {\ngl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);\n}"}},buildKernel:function(a){var c,d,e,g,b=2*Math.ceil(a*3)+1;if(b>25)b=25;g=(b-1)*0.5;d=Array(b);for(c=e=0;c<b;++c){d[c]=Math.exp(-((c-g)*(c-g))/(2*a*a));e+=d[c]}for(c=0;c<b;++c)d[c]/=e;return d}},Cube=function(a,c,d,e,g,b,l,j){function h(B,G,t,k,f,m,n,C){var w,p,F=e||1,y=g||1,J=F+1,E=y+1,A=f/2,M=m/
2;f=f/F;var aa=m/y,N=o.vertices.length;if(B=="x"&&G=="y"||B=="y"&&G=="x")w="z";else if(B=="x"&&G=="z"||B=="z"&&G=="x")w="y";else if(B=="z"&&G=="y"||B=="y"&&G=="z")w="x";for(p=0;p<E;p++)for(m=0;m<J;m++){var U=new THREE.Vector3;U[B]=(m*f-A)*t;U[G]=(p*aa-M)*k;U[w]=n;o.vertices.push(new THREE.Vertex(U))}for(p=0;p<y;p++)for(m=0;m<F;m++){o.faces.push(new THREE.Face4(m+J*p+N,m+J*(p+1)+N,m+1+J*(p+1)+N,m+1+J*p+N,null,C));o.uvs.push([new THREE.UV(m/F,p/y),new THREE.UV(m/F,(p+1)/y),new THREE.UV((m+1)/F,(p+1)/
y),new THREE.UV((m+1)/F,p/y)])}}THREE.Geometry.call(this);var o=this,v=a/2,z=c/2,u=d/2;l=l?-1:1;if(b!==undefined)if(b instanceof Array)this.materials=b;else{this.materials=[];for(var q=0;q<6;q++)this.materials.push([b])}else this.materials=[];this.sides={px:true,nx:true,py:true,ny:true,pz:true,nz:true};if(j!=undefined)for(var x in j)if(this.sides[x]!=undefined)this.sides[x]=j[x];this.sides.px&&h("z","y",1*l,-1,d,c,-v,this.materials[0]);this.sides.nx&&h("z","y",-1*l,-1,d,c,v,this.materials[1]);this.sides.py&&
h("x","z",1*l,1,a,d,z,this.materials[2]);this.sides.ny&&h("x","z",1*l,-1,a,d,-z,this.materials[3]);this.sides.pz&&h("x","y",1*l,-1,a,c,u,this.materials[4]);this.sides.nz&&h("x","y",-1*l,-1,a,c,-u,this.materials[5]);(function(){for(var B=[],G=[],t=0,k=o.vertices.length;t<k;t++){for(var f=o.vertices[t],m=false,n=0,C=B.length;n<C;n++){var w=B[n];if(f.position.x==w.position.x&&f.position.y==w.position.y&&f.position.z==w.position.z){G[t]=n;m=true;break}}if(!m){G[t]=B.length;B.push(new THREE.Vertex(f.position.clone()))}}t=
0;for(k=o.faces.length;t<k;t++){f=o.faces[t];f.a=G[f.a];f.b=G[f.b];f.c=G[f.c];f.d=G[f.d]}o.vertices=B})();this.computeCentroids();this.computeFaceNormals();this.sortFacesByMaterial()};Cube.prototype=new THREE.Geometry;Cube.prototype.constructor=Cube;
screen:{uniforms:{tDiffuse:{type:"t",value:0,texture:null},opacity:{type:"f",value:1}},vertex_shader:"varying vec2 vUv;\nvoid main() {\nvUv = vec2( uv.x, 1.0 - uv.y );\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}",fragment_shader:"varying vec2 vUv;\nuniform sampler2D tDiffuse;\nuniform float opacity;\nvoid main() {\nvec4 texel = texture2D( tDiffuse, vUv );\ngl_FragColor = opacity * texel;\n}"},basic:{uniforms:{},vertex_shader:"void main() {\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}",
fragment_shader:"void main() {\ngl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );\n}"}},buildKernel:function(a){var c,d,e,g,b=2*Math.ceil(a*3)+1;if(b>25)b=25;g=(b-1)*0.5;d=Array(b);for(c=e=0;c<b;++c){d[c]=Math.exp(-((c-g)*(c-g))/(2*a*a));e+=d[c]}for(c=0;c<b;++c)d[c]/=e;return d}},Cube=function(a,c,d,e,g,b,l,j){function h(B,G,t,k,f,m,n,C){var w,p,F=e||1,y=g||1,J=F+1,E=y+1,A=f/2,M=m/2;f=f/F;var aa=m/y,N=o.vertices.length;if(B=="x"&&G=="y"||B=="y"&&G=="x")w="z";else if(B=="x"&&G=="z"||B=="z"&&G=="x")w="y";
else if(B=="z"&&G=="y"||B=="y"&&G=="z")w="x";for(p=0;p<E;p++)for(m=0;m<J;m++){var U=new THREE.Vector3;U[B]=(m*f-A)*t;U[G]=(p*aa-M)*k;U[w]=n;o.vertices.push(new THREE.Vertex(U))}for(p=0;p<y;p++)for(m=0;m<F;m++){o.faces.push(new THREE.Face4(m+J*p+N,m+J*(p+1)+N,m+1+J*(p+1)+N,m+1+J*p+N,null,C));o.uvs.push([new THREE.UV(m/F,p/y),new THREE.UV(m/F,(p+1)/y),new THREE.UV((m+1)/F,(p+1)/y),new THREE.UV((m+1)/F,p/y)])}}THREE.Geometry.call(this);var o=this,v=a/2,z=c/2,u=d/2;l=l?-1:1;if(b!==undefined)if(b instanceof
Array)this.materials=b;else{this.materials=[];for(var q=0;q<6;q++)this.materials.push([b])}else this.materials=[];this.sides={px:true,nx:true,py:true,ny:true,pz:true,nz:true};if(j!=undefined)for(var x in j)if(this.sides[x]!=undefined)this.sides[x]=j[x];this.sides.px&&h("z","y",1*l,-1,d,c,-v,this.materials[0]);this.sides.nx&&h("z","y",-1*l,-1,d,c,v,this.materials[1]);this.sides.py&&h("x","z",1*l,1,a,d,z,this.materials[2]);this.sides.ny&&h("x","z",1*l,-1,a,d,-z,this.materials[3]);this.sides.pz&&h("x",
"y",1*l,-1,a,c,u,this.materials[4]);this.sides.nz&&h("x","y",-1*l,-1,a,c,-u,this.materials[5]);(function(){for(var B=[],G=[],t=0,k=o.vertices.length;t<k;t++){for(var f=o.vertices[t],m=false,n=0,C=B.length;n<C;n++){var w=B[n];if(f.position.x==w.position.x&&f.position.y==w.position.y&&f.position.z==w.position.z){G[t]=n;m=true;break}}if(!m){G[t]=B.length;B.push(new THREE.Vertex(f.position.clone()))}}t=0;for(k=o.faces.length;t<k;t++){f=o.faces[t];f.a=G[f.a];f.b=G[f.b];f.c=G[f.c];f.d=G[f.d]}o.vertices=
B})();this.computeCentroids();this.computeFaceNormals();this.sortFacesByMaterial()};Cube.prototype=new THREE.Geometry;Cube.prototype.constructor=Cube;
var Cylinder=function(a,c,d,e,g){function b(o,v,z){l.vertices.push(new THREE.Vertex(new THREE.Vector3(o,v,z)))}THREE.Geometry.call(this);var l=this,j=Math.PI,h;for(h=0;h<a;h++)b(Math.sin(2*j*h/a)*c,Math.cos(2*j*h/a)*c,0);for(h=0;h<a;h++)b(Math.sin(2*j*h/a)*d,Math.cos(2*j*h/a)*d,e);for(h=0;h<a;h++)l.faces.push(new THREE.Face4(h,h+a,a+(h+1)%a,(h+1)%a));if(d!=0){b(0,0,-g);for(h=a;h<a+a/2;h++)l.faces.push(new THREE.Face4(2*a,(2*h-2*a)%a,(2*h-2*a+1)%a,(2*h-2*a+2)%a))}if(c!=0){b(0,0,e+g);for(h=a+a/2;h<
2*a;h++)l.faces.push(new THREE.Face4((2*h-2*a+2)%a+a,(2*h-2*a+1)%a+a,(2*h-2*a)%a+a,2*a+1))}this.computeCentroids();this.computeFaceNormals();this.sortFacesByMaterial()};Cylinder.prototype=new THREE.Geometry;Cylinder.prototype.constructor=Cylinder;
var Plane=function(a,c,d,e){THREE.Geometry.call(this);var g,b=a/2,l=c/2;d=d||1;e=e||1;var j=d+1,h=e+1;a=a/d;var o=c/e;for(g=0;g<h;g++)for(c=0;c<j;c++)this.vertices.push(new THREE.Vertex(new THREE.Vector3(c*a-b,-(g*o-l),0)));for(g=0;g<e;g++)for(c=0;c<d;c++){this.faces.push(new THREE.Face4(c+j*g,c+j*(g+1),c+1+j*(g+1),c+1+j*g));this.uvs.push([new THREE.UV(c/d,g/e),new THREE.UV(c/d,(g+1)/e),new THREE.UV((c+1)/d,(g+1)/e),new THREE.UV((c+1)/d,g/e)])}this.computeCentroids();this.computeFaceNormals();this.sortFacesByMaterial()};
......
......@@ -127,20 +127,6 @@
gl_FragColor = vec4( cResult, cTextureScreen.a );
}
</script>
<!-- Render parameter modulated texture fragment shader -->
<script id="fs-screen" type="x-shader/x-fragment">
varying vec2 vUv;
uniform sampler2D tDiffuse;
uniform float opacity;
void main(void)
{
vec4 texel = texture2D( tDiffuse, vUv );
gl_FragColor = opacity * texel;
}
</script>
<!-- Time modulated procedural color fragment shader -->
<script id="fs-colors" type="x-shader/x-fragment">
......@@ -219,13 +205,17 @@
} );
var screen_shader = ShaderUtils.lib["screen"];
var screen_uniforms = Uniforms.clone( screen_shader.uniforms );
screen_uniforms["tDiffuse"].texture = rtTexture1;
screen_uniforms["opacity"].value = 0.4;
materialScreen = new THREE.MeshShaderMaterial( {
uniforms: { tDiffuse: { type: "t", value: 0, texture: rtTexture1 },
opacity: { type: "f", value: 0.4 }
},
vertex_shader: getText( 'vs-generic' ),
fragment_shader: getText( 'fs-screen' ),
uniforms: screen_uniforms,
vertex_shader: screen_shader.vertex_shader,
fragment_shader: screen_shader.fragment_shader,
blending: THREE.AdditiveBlending
} );
......@@ -245,18 +235,18 @@
blurx = new THREE.Vector2( increment, 0.0 ),
blury = new THREE.Vector2( 0.0, increment );
var shader = ShaderUtils.lib["convolution"];
var uniforms = Uniforms.clone( shader.uniforms );
var convolution_shader = ShaderUtils.lib["convolution"];
var convolution_uniforms = Uniforms.clone( convolution_shader.uniforms );
uniforms["tDiffuse"].texture = rtTexture1;
uniforms["uImageIncrement"].value = blurx;
uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
convolution_uniforms["tDiffuse"].texture = rtTexture1;
convolution_uniforms["uImageIncrement"].value = blurx;
convolution_uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
materialConvolution = new THREE.MeshShaderMaterial( {
uniforms: uniforms,
vertex_shader: shader.vertex_shader,
fragment_shader: shader.fragment_shader
uniforms: convolution_uniforms,
vertex_shader: convolution_shader.vertex_shader,
fragment_shader: convolution_shader.fragment_shader
} );
......
......@@ -7,69 +7,73 @@ var ShaderUtils = {
- based on Nvidia Cg tutorial
------------------------------------------------------------------------- */
'fresnel': {
'fresnel': {
uniforms: {
uniforms: {
"mRefractionRatio": { type: "f", value: 1.02 },
"mFresnelBias": { type: "f", value: 0.1 },
"mFresnelPower": { type: "f", value: 2.0 },
"mFresnelScale": { type: "f", value: 1.0 },
"tCube": { type: "t", value: 1, texture: null }
"mRefractionRatio": { type: "f", value: 1.02 },
"mFresnelBias": { type: "f", value: 0.1 },
"mFresnelPower": { type: "f", value: 2.0 },
"mFresnelScale": { type: "f", value: 1.0 },
"tCube": { type: "t", value: 1, texture: null }
},
},
fragment_shader: [
fragment_shader: [
"uniform samplerCube tCube;",
"uniform samplerCube tCube;",
"varying vec3 vReflect;",
"varying vec3 vRefract[3];",
"varying float vReflectionFactor;",
"varying vec3 vReflect;",
"varying vec3 vRefract[3];",
"varying float vReflectionFactor;",
"void main() {",
"vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
"vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
"void main() {",
"vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
"vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
"refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
"refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
"refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
"refractedColor.a = 1.0;",
"refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
"refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
"refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
"refractedColor.a = 1.0;",
"gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
"}"
"gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
"}"
].join("\n"),
].join("\n"),
vertex_shader: [
vertex_shader: [
"uniform float mRefractionRatio;",
"uniform float mFresnelBias;",
"uniform float mFresnelScale;",
"uniform float mFresnelPower;",
"uniform float mRefractionRatio;",
"uniform float mFresnelBias;",
"uniform float mFresnelScale;",
"uniform float mFresnelPower;",
"varying vec3 vReflect;",
"varying vec3 vRefract[3];",
"varying float vReflectionFactor;",
"varying vec3 vReflect;",
"varying vec3 vRefract[3];",
"varying float vReflectionFactor;",
"void main(void) {",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
"void main() {",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
"vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
"vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
"vec3 I = mPosition.xyz - cameraPosition;",
"vec3 I = mPosition.xyz - cameraPosition;",
"vReflect = reflect( I, nWorld );",
"vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
"vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
"vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
"vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
"vReflect = reflect( I, nWorld );",
"vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
"vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
"vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
"vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
"gl_Position = projectionMatrix * mvPosition;",
"}"
"gl_Position = projectionMatrix * mvPosition;",
"}"
].join("\n")
].join("\n")
},
......@@ -340,8 +344,10 @@ var ShaderUtils = {
"#define KERNEL_SIZE 25.0",
"void main(void) {",
"vUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
......@@ -357,6 +363,7 @@ var ShaderUtils = {
"uniform float cKernel[KERNEL_SIZE];",
"void main(void) {",
"vec2 imageCoord = vUv;",
"vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
"for( int i=0; i<KERNEL_SIZE; ++i ) {",
......@@ -364,13 +371,55 @@ var ShaderUtils = {
"imageCoord += uImageIncrement;",
"}",
"gl_FragColor = sum;",
"}"
].join("\n")
},
/* -------------------------------------------------------------------------
Full-screen textured quad shader
------------------------------------------------------------------------- */
'screen': {
uniforms: { tDiffuse: { type: "t", value: 0, texture: null },
opacity: { type: "f", value: 1.0 }
},
vertex_shader: [
"varying vec2 vUv;",
"void main() {",
"vUv = vec2( uv.x, 1.0 - uv.y );",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragment_shader: [
"varying vec2 vUv;",
"uniform sampler2D tDiffuse;",
"uniform float opacity;",
"void main() {",
"vec4 texel = texture2D( tDiffuse, vUv );",
"gl_FragColor = opacity * texel;",
"}"
].join("\n")
},
/* -------------------------------------------------------------------------
Simple test shader
------------------------------------------------------------------------- */
......@@ -393,7 +442,7 @@ var ShaderUtils = {
"void main() {",
"gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);",
"gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
"}"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册