提交 110bd08e 编写于 作者: M Mr.doob

Merge remote-tracking branch 'alteredq/dev' into dev

因为 它太大了无法显示 source diff 。你可以改为 查看blob
因为 它太大了无法显示 source diff 。你可以改为 查看blob
......@@ -23,6 +23,7 @@
* luminosity
* colorCorrection
* normalmap
* ssao
*/
THREE.ShaderExtras = {
......@@ -1383,6 +1384,258 @@ THREE.ShaderExtras = {
},
/* -------------------------------------------------------------------------
// Screen-space ambient occlusion shader
// - ported from
// SSAO GLSL shader v1.2
// assembled by Martins Upitis (martinsh) (http://devlog-martinsh.blogspot.com)
// original technique is made by ArKano22 (http://www.gamedev.net/topic/550699-ssao-no-halo-artifacts/)
// - modifications
// - modified to use RGBA packed depth texture (use clear color 1,1,1,1 for depth pass)
// - made fog more compatible with three.js linear fog
// - refactoring and optimizations
------------------------------------------------------------------------- */
'ssao': {
uniforms: {
"tDiffuse": { type: "t", value: 0, texture: null },
"tDepth": { type: "t", value: 1, texture: null },
"size": { type: "v2", value: new THREE.Vector2( 512, 512 ) },
"cameraNear": { type: "f", value: 1 },
"cameraFar": { type: "f", value: 100 },
"fogNear": { type: "f", value: 5 },
"fogFar": { type: "f", value: 100 },
"fogEnabled": { type: "i", value: 0 },
"aoClamp": { type: "f", value: 0.3 }
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
"vUv = vec2( uv.x, 1.0 - uv.y );",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform float cameraNear;",
"uniform float cameraFar;",
"uniform float fogNear;",
"uniform float fogFar;",
"uniform bool fogEnabled;",
"uniform vec2 size;", // texture width, height
"uniform float aoClamp;", // depth clamp - reduces haloing at screen edges
"uniform sampler2D tDiffuse;",
"uniform sampler2D tDepth;",
"varying vec2 vUv;",
//"#define PI 3.14159265",
"#define DL 2.399963229728653", // PI * ( 3.0 - sqrt( 5.0 ) )
"#define EULER 2.718281828459045",
// helpers
"float width = size.x;", // texture width
"float height = size.y;", // texture height
"float cameraFarPlusNear = cameraFar + cameraNear;",
"float cameraFarMinusNear = cameraFar - cameraNear;",
"float cameraCoef = 2.0 * cameraNear;",
// user variables
"const int samples = 8;", // ao sample count
"const float radius = 5.0;", // ao radius
"const bool useNoise = false;", // use noise instead of pattern for sample dithering
"const float noiseAmount = 0.0002;", // dithering amount
"const float diffArea = 0.4;", // self-shadowing reduction
"const float gDisplace = 0.4;", // gauss bell center
"const bool onlyAO = false;", // use only ambient occlusion pass?
"const float lumInfluence = 0.3;", // how much luminance affects occlusion
// RGBA depth
"float unpackDepth( const in vec4 rgba_depth ) {",
"const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
"float depth = dot( rgba_depth, bit_shift );",
"return depth;",
"}",
// generating noise / pattern texture for dithering
"vec2 rand( const vec2 coord ) {",
"vec2 noise;",
"if ( useNoise ) {",
"float nx = dot ( coord, vec2( 12.9898, 78.233 ) );",
"float ny = dot ( coord, vec2( 12.9898, 78.233 ) * 2.0 );",
"noise = clamp( fract ( 43758.5453 * sin( vec2( nx, ny ) ) ), 0.0, 1.0 );",
"} else {",
"float ff = fract( 1.0 - coord.s * ( width / 2.0 ) );",
"float gg = fract( coord.t * ( height / 2.0 ) );",
"noise = vec2( 0.25, 0.75 ) * vec2( ff ) + vec2( 0.75, 0.25 ) * gg;",
"}",
"return ( noise * 2.0 - 1.0 ) * noiseAmount;",
"}",
"float doFog() {",
"float zdepth = unpackDepth( texture2D( tDepth, vUv ) );",
"float depth = -cameraFar * cameraNear / ( zdepth * cameraFarMinusNear - cameraFar );",
"return smoothstep( fogNear, fogFar, depth );",
"}",
"float readDepth( const in vec2 coord ) {",
//"return ( 2.0 * cameraNear ) / ( cameraFar + cameraNear - unpackDepth( texture2D( tDepth, coord ) ) * ( cameraFar - cameraNear ) );",
"return cameraCoef / ( cameraFarPlusNear - unpackDepth( texture2D( tDepth, coord ) ) * cameraFarMinusNear );",
"}",
"float compareDepths( const in float depth1, const in float depth2, inout int far ) {",
"float garea = 2.0;", // gauss bell width
"float diff = ( depth1 - depth2 ) * 100.0;", // depth difference (0-100)
// reduce left bell width to avoid self-shadowing
"if ( diff < gDisplace ) {",
"garea = diffArea;",
"} else {",
"far = 1;",
"}",
"float dd = diff - gDisplace;",
"float gauss = pow( EULER, -2.0 * dd * dd / ( garea * garea ) );",
"return gauss;",
"}",
"float calcAO( float depth, float dw, float dh ) {",
"float dd = radius - depth * radius;",
"vec2 vv = vec2( dw, dh );",
"vec2 coord1 = vUv + dd * vv;",
"vec2 coord2 = vUv - dd * vv;",
"float temp1 = 0.0;",
"float temp2 = 0.0;",
"int far = 0;",
"temp1 = compareDepths( depth, readDepth( coord1 ), far );",
// DEPTH EXTRAPOLATION
"if ( far > 0 ) {",
"temp2 = compareDepths( readDepth( coord2 ), depth, far );",
"temp1 += ( 1.0 - temp1 ) * temp2;",
"}",
"return temp1;",
"}",
"void main() {",
"vec2 noise = rand( vUv );",
"float depth = readDepth( vUv );",
"float tt = clamp( depth, aoClamp, 1.0 );",
"float w = ( 1.0 / width ) / tt + ( noise.x * ( 1.0 - noise.x ) );",
"float h = ( 1.0 / height ) / tt + ( noise.y * ( 1.0 - noise.y ) );",
"float pw;",
"float ph;",
"float ao;",
"float dz = 1.0 / float( samples );",
"float z = 1.0 - dz / 2.0;",
"float l = 0.0;",
"for ( int i = 0; i <= samples; i ++ ) {",
"float r = sqrt( 1.0 - z );",
"pw = cos( l ) * r;",
"ph = sin( l ) * r;",
"ao += calcAO( depth, pw * w, ph * h );",
"z = z - dz;",
"l = l + DL;",
"}",
"ao /= float( samples );",
"ao = 1.0 - ao;",
"if ( fogEnabled ) {",
"ao = mix( ao, 1.0, doFog() );",
"}",
"vec3 color = texture2D( tDiffuse, vUv ).rgb;",
"vec3 lumcoeff = vec3( 0.299, 0.587, 0.114 );",
"float lum = dot( color.rgb, lumcoeff );",
"vec3 luminance = vec3( lum );",
"vec3 final = vec3( color * mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // mix( color * ao, white, luminance )
"if ( onlyAO ) {",
"final = vec3( mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // ambient occlusion only
"}",
"gl_FragColor = vec4( final, 1.0 );",
"}"
].join("\n")
},
// METHODS
buildKernel: function( sigma ) {
......
......@@ -75,7 +75,7 @@ THREE.CTMLoader.prototype.loadParts = function( url, callback, useWorker, useBuf
}
xhr.open( "GET", url, true );
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
if ( xhr.overrideMimeType ) xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
xhr.setRequestHeader( "Content-Type", "text/plain" );
xhr.send( null );
......
......@@ -52,7 +52,7 @@
materials = [
new THREE.MeshLambertMaterial( { ambient: 0xbbbbbb, map: THREE.ImageUtils.loadTexture( 'textures/ash_uvgrid01.jpg' ) } ),
new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true, transparent: true, opacity: 0.3 } )
new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true, transparent: true, opacity: 0.1 } )
];
object = THREE.SceneUtils.createMultiMaterialObject( new THREE.CubeGeometry( 100, 100, 100, 4, 4, 4 ), materials );
......@@ -69,6 +69,7 @@
scene.add( object );
object = THREE.SceneUtils.createMultiMaterialObject( new THREE.PlaneGeometry( 100, 100, 4, 4 ), materials );
object.children[ 0 ].doubleSided = true;
object.position.set( -200, 0, 0 );
scene.add( object );
......@@ -85,6 +86,7 @@
}
object = THREE.SceneUtils.createMultiMaterialObject( new THREE.LatheGeometry( points, 20 ), materials );
object.children[ 0 ].doubleSided = true;
object.position.set( 200, 0, 0 );
scene.add( object );
......
/**
* @author oosmoxiecode
*
* uvs are messed up in this one, and commented away for now. There is an ugly "seam" by the shared vertices
* when it "wraps" around, that needs to be fixed. It's because they share the first and the last vertices
* so it draws the entire texture on the seam-faces, I think...
*/
THREE.IcosahedronGeometry = function ( subdivisions ) {
......@@ -9,10 +13,11 @@ THREE.IcosahedronGeometry = function ( subdivisions ) {
var tempFaces;
this.subdivisions = subdivisions || 0;
THREE.Geometry.call(this);
THREE.Geometry.call( this );
// create 12 vertices of a Icosahedron
var t = (1 + Math.sqrt(5)) / 2;
var t = ( 1 + Math.sqrt( 5 ) ) / 2;
v(-1, t, 0);
v( 1, t, 0);
......@@ -30,6 +35,7 @@ THREE.IcosahedronGeometry = function ( subdivisions ) {
v(-t, 0, 1);
// 5 faces around point 0
f3(0, 11, 5, tempScope);
f3(0, 5, 1, tempScope);
f3(0, 1, 7, tempScope);
......@@ -37,6 +43,7 @@ THREE.IcosahedronGeometry = function ( subdivisions ) {
f3(0, 10, 11, tempScope);
// 5 adjacent faces
f3(1, 5, 9, tempScope);
f3(5, 11, 4, tempScope);
f3(11, 10, 2, tempScope);
......@@ -44,6 +51,7 @@ THREE.IcosahedronGeometry = function ( subdivisions ) {
f3(7, 1, 8, tempScope);
// 5 faces around point 3
f3(3, 9, 4, tempScope);
f3(3, 4, 2, tempScope);
f3(3, 2, 6, tempScope);
......@@ -51,6 +59,7 @@ THREE.IcosahedronGeometry = function ( subdivisions ) {
f3(3, 8, 9, tempScope);
// 5 adjacent faces
f3(4, 9, 5, tempScope);
f3(2, 4, 11, tempScope);
f3(6, 2, 10, tempScope);
......@@ -58,21 +67,31 @@ THREE.IcosahedronGeometry = function ( subdivisions ) {
f3(9, 8, 1, tempScope);
// subdivide faces to refine the triangles
for (var i=0; i < this.subdivisions; i++) {
for ( var i = 0; i < this.subdivisions; i ++ ) {
tempFaces = new THREE.Geometry();
for (var tri in tempScope.faces) {
for ( var tri = 0; tri < tempScope.faces.length; tri ++ ) {
// replace each triangle by 4 triangles
var a = getMiddlePoint(tempScope.faces[tri].a, tempScope.faces[tri].b);
var b = getMiddlePoint(tempScope.faces[tri].b, tempScope.faces[tri].c);
var c = getMiddlePoint(tempScope.faces[tri].c, tempScope.faces[tri].a);
f3(tempScope.faces[tri].a, a, c, tempFaces);
f3(tempScope.faces[tri].b, b, a, tempFaces);
f3(tempScope.faces[tri].c, c, b, tempFaces);
f3(a, b, c, tempFaces);
var face = tempScope.faces[ tri ];
var a = getMiddlePoint( face.a, face.b );
var b = getMiddlePoint( face.b, face.c );
var c = getMiddlePoint( face.c, face.a );
f3( face.a, a, c, tempFaces );
f3( face.b, b, a, tempFaces );
f3( face.c, c, b, tempFaces );
f3( a, b, c, tempFaces );
}
tempScope.faces = tempFaces.faces;
tempScope.faceVertexUvs[ 0 ] = tempFaces.faceVertexUvs[ 0 ];
}
scope.faces = tempScope.faces;
......@@ -84,7 +103,7 @@ THREE.IcosahedronGeometry = function ( subdivisions ) {
function v( x, y, z ) {
var length = Math.sqrt(x * x + y * y + z * z);
var length = Math.sqrt( x * x + y * y + z * z );
var i = scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x/length, y/length, z/length ) ) );
return i-1;
......@@ -109,16 +128,18 @@ THREE.IcosahedronGeometry = function ( subdivisions ) {
}
function getMiddlePoint(p1,p2) {
var pos1 = scope.vertices[p1].position;
var pos2 = scope.vertices[p2].position;
function getMiddlePoint( p1, p2 ) {
var x = (pos1.x + pos2.x) / 2;
var y = (pos1.y + pos2.y) / 2;
var z = (pos1.z + pos2.z) / 2;
var pos1 = scope.vertices[ p1 ].position;
var pos2 = scope.vertices[ p2 ].position;
var x = ( pos1.x + pos2.x ) / 2;
var y = ( pos1.y + pos2.y ) / 2;
var z = ( pos1.z + pos2.z ) / 2;
var i = v(x, y, z);
return i;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册