提交 418a6da7 编写于 作者: A alteredq

Moved convolution shader from example into ShaderUtils.

上级 1d9c3405
......@@ -227,12 +227,13 @@ vertex_shader:"uniform float mRefractionRatio;\nuniform float mFresnelBias;\nuni
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}"},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}"}}},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;
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;
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()};
......
......@@ -65,48 +65,6 @@
<script type="text/javascript" src="js/Stats.js"></script>
<script type="text/javascript" src="../build/ThreeExtras.js"></script>
<!-- -----------------------------------------------------------------
Convolution shader
- ported from o3d sample to WebGL / GLSL
http://o3d.googlecode.com/svn/trunk/samples/convolution.html
----------------------------------------------------------------- ->
<!-- Convolution fragment shader -->
<script id="fs-convolution" type="x-shader/x-fragment">
varying vec2 vUv;
uniform sampler2D tDiffuse;
uniform vec2 uImageIncrement;
#define KERNEL_SIZE 25
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 ) {
sum += texture2D( tDiffuse, imageCoord ) * cKernel[i];
imageCoord += uImageIncrement;
}
gl_FragColor = sum;
}
</script>
<!-- Convolution vertex shader -->
<script id="vs-convolution" type="x-shader/x-vertex">
varying vec2 vUv;
uniform vec2 uImageIncrement;
#define KERNEL_SIZE 25.0
void main(void) {
//vUv = vec2( uv.x, 1.0 - uv.y ) - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;
vUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<!-- -----------------------------------------------------------------
Film grain & scanlines shader
......@@ -282,22 +240,26 @@
} );
var kernel = buildKernel( 4.0 ), increment = 0.001953125;
var increment = 0.001953125;
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 );
uniforms["tDiffuse"].texture = rtTexture1;
uniforms["uImageIncrement"].value = blurx;
uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
materialConvolution = new THREE.MeshShaderMaterial( {
uniforms: { tDiffuse: { type: "t", value: 0, texture: rtTexture1 },
uImageIncrement: { type: "v2", value: blury },
cKernel: { type: "fv1", value: kernel }
},
vertex_shader: document.getElementById( 'vs-convolution' ).textContent,
fragment_shader: document.getElementById( 'fs-convolution' ).textContent
uniforms: uniforms,
vertex_shader: shader.vertex_shader,
fragment_shader: shader.fragment_shader
} );
var plane = new Plane( window.innerWidth, window.innerHeight );
quadBG = new THREE.Mesh( plane, materialColor );
......@@ -375,42 +337,6 @@
}
/*****************************************************************************************/
// Convolution
// - ported from o3d convolution shader sample
// http://o3d.googlecode.com/svn/trunk/samples/convolution.html
/*****************************************************************************************/
// We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
function gauss( x, sigma ) {
return Math.exp( - (x * x) / (2.0 * sigma * sigma) );
}
function buildKernel( sigma ) {
var kMaxKernelSize = 25;
var kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
var halfWidth = ( kernelSize - 1 ) * 0.5
var values = new Array( kernelSize );
var sum = 0.0;
for( var i = 0; i < kernelSize; ++i ) {
values[ i ] = gauss( i - halfWidth, sigma );
sum += values[ i ];
}
// normalize the kernel
for( var i = 0; i<kernelSize; ++i ) values[ i ] /= sum;
return values;
}
var delta = 0.01;
var start = 0;
......
var ShaderUtils = {
lib: { 'fresnel': {
lib: {
/* -------------------------------------------------------------------------
Fresnel shader
- based on Nvidia Cg tutorial
------------------------------------------------------------------------- */
'fresnel': {
uniforms: {
......@@ -66,265 +73,366 @@ var ShaderUtils = {
},
/* -------------------------------------------------------------------------
Normal map shader
- Blinn-Phong
- normal + diffuse + AO + displacement maps
- 1 point and 1 directional lights
------------------------------------------------------------------------- */
'normal' : {
uniforms: {
uniforms: {
"enableAO": { type: "i", value: 0 },
"enableDiffuse": { type: "i", value: 0 },
"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 },
"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.0 },
"uNormalScale": { type: "f", value: 1.0 },
"tDisplacement": { type: "t", value: 4, texture: null },
"uDisplacementBias": { type: "f", value: -0.5 },
"uDisplacementScale": { type: "f", value: 2.5 },
"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( 0xeeeeee ) },
"uPointLightPos": { type: "v3", value: new THREE.Vector3() },
"uPointLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
"uDirLightPos": { type: "v3", value: new THREE.Vector3() },
"uDirLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
"uDirLightPos": { type: "v3", value: new THREE.Vector3() },
"uDirLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
"uAmbientLightColor": { type: "c", value: new THREE.Color( 0x050505 ) },
"uAmbientLightColor": { type: "c", value: new THREE.Color( 0x050505 ) },
"uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
"uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
"uShininess": { type: "f", value: 30 }
"uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
"uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
"uShininess": { type: "f", value: 30 }
},
},
fragment_shader: [
fragment_shader: [
"uniform vec3 uDirLightPos;",
"uniform vec3 uDirLightPos;",
"uniform vec3 uAmbientLightColor;",
"uniform vec3 uDirLightColor;",
"uniform vec3 uPointLightColor;",
"uniform vec3 uAmbientLightColor;",
"uniform vec3 uDirLightColor;",
"uniform vec3 uPointLightColor;",
"uniform vec3 uAmbientColor;",
"uniform vec3 uDiffuseColor;",
"uniform vec3 uSpecularColor;",
"uniform float uShininess;",
"uniform vec3 uAmbientColor;",
"uniform vec3 uDiffuseColor;",
"uniform vec3 uSpecularColor;",
"uniform float uShininess;",
"uniform bool enableDiffuse;",
"uniform bool enableAO;",
"uniform bool enableDiffuse;",
"uniform bool enableAO;",
"uniform sampler2D tDiffuse;",
"uniform sampler2D tNormal;",
"uniform sampler2D tAO;",
"uniform sampler2D tDiffuse;",
"uniform sampler2D tNormal;",
"uniform sampler2D tAO;",
"uniform float uNormalScale;",
"uniform float uNormalScale;",
"varying vec3 vTangent;",
"varying vec3 vBinormal;",
"varying vec3 vNormal;",
"varying vec2 vUv;",
"varying vec3 vTangent;",
"varying vec3 vBinormal;",
"varying vec3 vNormal;",
"varying vec2 vUv;",
"varying vec3 vPointLightVector;",
"varying vec3 vViewPosition;",
"varying vec3 vPointLightVector;",
"varying vec3 vViewPosition;",
"void main() {",
"void main() {",
"vec3 diffuseTex = vec3( 1.0, 1.0, 1.0 );",
"vec3 aoTex = vec3( 1.0, 1.0, 1.0 );",
"vec3 diffuseTex = vec3( 1.0, 1.0, 1.0 );",
"vec3 aoTex = vec3( 1.0, 1.0, 1.0 );",
"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
"normalTex.xy *= uNormalScale;",
"normalTex = normalize( normalTex );",
"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
"normalTex.xy *= uNormalScale;",
"normalTex = normalize( normalTex );",
"if( enableDiffuse )",
"diffuseTex = texture2D( tDiffuse, vUv ).xyz;",
"if( enableDiffuse )",
"diffuseTex = texture2D( tDiffuse, vUv ).xyz;",
"if( enableAO )",
"aoTex = texture2D( tAO, vUv ).xyz;",
"if( enableAO )",
"aoTex = texture2D( tAO, vUv ).xyz;",
"mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
"vec3 finalNormal = tsb * normalTex;",
"mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
"vec3 finalNormal = tsb * normalTex;",
"vec3 normal = normalize( finalNormal );",
"vec3 viewPosition = normalize( vViewPosition );",
"vec3 normal = normalize( finalNormal );",
"vec3 viewPosition = normalize( vViewPosition );",
// point light
// point light
"vec4 pointDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 pointSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 pointDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 pointSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec3 pointVector = normalize( vPointLightVector );",
"vec3 pointHalfVector = normalize( vPointLightVector + vViewPosition );",
"vec3 pointVector = normalize( vPointLightVector );",
"vec3 pointHalfVector = normalize( vPointLightVector + vViewPosition );",
"float pointDotNormalHalf = dot( normal, pointHalfVector );",
"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
"float pointDotNormalHalf = dot( normal, pointHalfVector );",
"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
"float pointSpecularWeight = 0.0;",
"if ( pointDotNormalHalf >= 0.0 )",
"pointSpecularWeight = pow( pointDotNormalHalf, uShininess );",
"float pointSpecularWeight = 0.0;",
"if ( pointDotNormalHalf >= 0.0 )",
"pointSpecularWeight = pow( pointDotNormalHalf, uShininess );",
"pointDiffuse += vec4( uDiffuseColor, 1.0 ) * pointDiffuseWeight;",
"pointSpecular += vec4( uSpecularColor, 1.0 ) * pointSpecularWeight;",
"pointDiffuse += vec4( uDiffuseColor, 1.0 ) * pointDiffuseWeight;",
"pointSpecular += vec4( uSpecularColor, 1.0 ) * pointSpecularWeight;",
// directional light
// directional light
"vec4 dirDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 dirSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 dirDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 dirSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 lDirection = viewMatrix * vec4( uDirLightPos, 0.0 );",
"vec4 lDirection = viewMatrix * vec4( uDirLightPos, 0.0 );",
"vec3 dirVector = normalize( lDirection.xyz );",
"vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );",
"vec3 dirVector = normalize( lDirection.xyz );",
"vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );",
"float dirDotNormalHalf = dot( normal, dirHalfVector );",
"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
"float dirDotNormalHalf = dot( normal, dirHalfVector );",
"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
"float dirSpecularWeight = 0.0;",
"if ( dirDotNormalHalf >= 0.0 )",
"dirSpecularWeight = pow( dirDotNormalHalf, uShininess );",
"float dirSpecularWeight = 0.0;",
"if ( dirDotNormalHalf >= 0.0 )",
"dirSpecularWeight = pow( dirDotNormalHalf, uShininess );",
"dirDiffuse += vec4( uDiffuseColor, 1.0 ) * dirDiffuseWeight;",
"dirSpecular += vec4( uSpecularColor, 1.0 ) * dirSpecularWeight;",
"dirDiffuse += vec4( uDiffuseColor, 1.0 ) * dirDiffuseWeight;",
"dirSpecular += vec4( uSpecularColor, 1.0 ) * dirSpecularWeight;",
// all lights contribution summation
// all lights contribution summation
"vec4 totalLight = vec4( uAmbientLightColor * uAmbientColor, 1.0 );",
"totalLight += vec4( uDirLightColor, 1.0 ) * ( dirDiffuse + dirSpecular );",
"totalLight += vec4( uPointLightColor, 1.0 ) * ( pointDiffuse + pointSpecular );",
"vec4 totalLight = vec4( uAmbientLightColor * uAmbientColor, 1.0 );",
"totalLight += vec4( uDirLightColor, 1.0 ) * ( dirDiffuse + dirSpecular );",
"totalLight += vec4( uPointLightColor, 1.0 ) * ( pointDiffuse + pointSpecular );",
"gl_FragColor = vec4( totalLight.xyz * aoTex * diffuseTex, 1.0 );",
"gl_FragColor = vec4( totalLight.xyz * aoTex * diffuseTex, 1.0 );",
"}"
].join("\n"),
"}"
].join("\n"),
vertex_shader: [
vertex_shader: [
"attribute vec4 tangent;",
"attribute vec4 tangent;",
"uniform vec3 uPointLightPos;",
"uniform vec3 uPointLightPos;",
"#ifdef VERTEX_TEXTURES",
"#ifdef VERTEX_TEXTURES",
"uniform sampler2D tDisplacement;",
"uniform float uDisplacementScale;",
"uniform float uDisplacementBias;",
"uniform sampler2D tDisplacement;",
"uniform float uDisplacementScale;",
"uniform float uDisplacementBias;",
"#endif",
"#endif",
"varying vec3 vTangent;",
"varying vec3 vBinormal;",
"varying vec3 vNormal;",
"varying vec2 vUv;",
"varying vec3 vTangent;",
"varying vec3 vBinormal;",
"varying vec3 vNormal;",
"varying vec2 vUv;",
"varying vec3 vPointLightVector;",
"varying vec3 vViewPosition;",
"varying vec3 vPointLightVector;",
"varying vec3 vViewPosition;",
"void main() {",
"void main() {",
"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
"vViewPosition = cameraPosition - mPosition.xyz;",
"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
"vViewPosition = cameraPosition - mPosition.xyz;",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
"vNormal = normalize( normalMatrix * normal );",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
"vNormal = normalize( normalMatrix * normal );",
// tangent and binormal vectors
// tangent and binormal vectors
"vTangent = normalize( normalMatrix * tangent.xyz );",
"vTangent = normalize( normalMatrix * tangent.xyz );",
"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
"vBinormal = normalize( vBinormal );",
"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
"vBinormal = normalize( vBinormal );",
"vUv = uv;",
"vUv = uv;",
// point light
// point light
"vec4 lPosition = viewMatrix * vec4( uPointLightPos, 1.0 );",
"vPointLightVector = normalize( lPosition.xyz - mvPosition.xyz );",
"vec4 lPosition = viewMatrix * vec4( uPointLightPos, 1.0 );",
"vPointLightVector = normalize( lPosition.xyz - mvPosition.xyz );",
// displacement mapping
// displacement mapping
"#ifdef VERTEX_TEXTURES",
"#ifdef VERTEX_TEXTURES",
"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
"float df = uDisplacementScale * dv.x + uDisplacementBias;",
"vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
"gl_Position = projectionMatrix * displacedPosition;",
"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
"float df = uDisplacementScale * dv.x + uDisplacementBias;",
"vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
"gl_Position = projectionMatrix * displacedPosition;",
"#else",
"#else",
"gl_Position = projectionMatrix * mvPosition;",
"gl_Position = projectionMatrix * mvPosition;",
"#endif",
"#endif",
"}"
"}"
].join("\n")
].join("\n")
},
/* -------------------------------------------------------------------------
Cube map shader
------------------------------------------------------------------------- */
'cube': {
uniforms: { "tCube": { type: "t", value: 1, texture: null } },
uniforms: { "tCube": { type: "t", value: 1, texture: null } },
vertex_shader: [
vertex_shader: [
"varying vec3 vViewPosition;",
"varying vec3 vViewPosition;",
"void main() {",
"void main() {",
"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
"vViewPosition = cameraPosition - mPosition.xyz;",
"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
"vViewPosition = cameraPosition - mPosition.xyz;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
"}"
].join("\n"),
].join("\n"),
fragment_shader: [
fragment_shader: [
"uniform samplerCube tCube;",
"uniform samplerCube tCube;",
"varying vec3 vViewPosition;",
"varying vec3 vViewPosition;",
"void main() {",
"void main() {",
"vec3 wPos = cameraPosition - vViewPosition;",
"gl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );",
"vec3 wPos = cameraPosition - vViewPosition;",
"gl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );",
"}"
"}"
].join("\n")
].join("\n")
},
/* ------------------------------------------------------------------------
Convolution shader
- ported from o3d sample to WebGL / GLSL
http://o3d.googlecode.com/svn/trunk/samples/convolution.html
------------------------------------------------------------------------ */
'convolution': {
uniforms: {
"tDiffuse" : { type: "t", value: 0, texture: null },
"uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
"cKernel" : { type: "fv1", value: [] }
},
vertex_shader: [
"varying vec2 vUv;",
"uniform vec2 uImageIncrement;",
"#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"),
fragment_shader: [
"varying vec2 vUv;",
"uniform sampler2D tDiffuse;",
"uniform vec2 uImageIncrement;",
"#define KERNEL_SIZE 25",
"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 ) {",
"sum += texture2D( tDiffuse, imageCoord ) * cKernel[i];",
"imageCoord += uImageIncrement;",
"}",
"gl_FragColor = sum;",
"}"
].join("\n")
},
/* -------------------------------------------------------------------------
Simple test shader
------------------------------------------------------------------------- */
'basic': {
uniforms: {},
uniforms: {},
vertex_shader: [
vertex_shader: [
"void main() {",
"void main() {",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
"}"
].join("\n"),
].join("\n"),
fragment_shader: [
fragment_shader: [
"void main() {",
"void main() {",
"gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);",
"gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);",
"}"
"}"
].join("\n")
].join("\n")
}
},
buildKernel: function( sigma ) {
// We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
function gauss( x, sigma ) {
return Math.exp( - (x * x) / (2.0 * sigma * sigma) );
}
var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
halfWidth = ( kernelSize - 1 ) * 0.5
values = new Array( kernelSize );
sum = 0.0;
for( i = 0; i < kernelSize; ++i ) {
values[ i ] = gauss( i - halfWidth, sigma );
sum += values[ i ];
}
// normalize the kernel
for( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
return values;
}
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册