提交 39c7e8da 编写于 作者: A alteredq

Synced with mrdoob's branch.

此差异已折叠。
此差异已折叠。
此差异已折叠。
......@@ -13,7 +13,7 @@ THREE.Geometry = function () {
this.geometryChunks = {};
this.hasTangents = false;
};
THREE.Geometry.prototype = {
......@@ -170,35 +170,35 @@ THREE.Geometry.prototype = {
},
computeTangents: function() {
// based on http://www.terathon.com/code/tangent.html
// tangents go to vertices
var f, fl, v, vl, face, uv, vA, vB, vC, uvA, uvB, uvC,
x1, x2, y1, y2, z1, z2,
s1, s2, t1, t2, r, t, n,
tan1 = [], tan2 = [],
sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
n = new THREE.Vector3(), w;
for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
tan1[ v ] = new THREE.Vector3();
tan2[ v ] = new THREE.Vector3();
}
function handleTriangle( context, a, b, c ) {
vA = context.vertices[ a ].position;
vB = context.vertices[ b ].position;
vC = context.vertices[ c ].position;
uvA = uv[ 0 ];
uvB = uv[ 1 ];
uvC = uv[ 2 ];
x1 = vB.x - vA.x;
x2 = vC.x - vA.x;
y1 = vB.y - vA.y;
......@@ -212,41 +212,41 @@ THREE.Geometry.prototype = {
t2 = uvC.v - uvA.v;
r = 1.0 / ( s1 * t2 - s2 * t1 );
sdir.set( ( t2 * x1 - t1 * x2 ) * r,
sdir.set( ( t2 * x1 - t1 * x2 ) * r,
( t2 * y1 - t1 * y2 ) * r,
( t2 * z1 - t1 * z2 ) * r );
tdir.set( ( s1 * x2 - s2 * x1 ) * r,
tdir.set( ( s1 * x2 - s2 * x1 ) * r,
( s1 * y2 - s2 * y1 ) * r,
( s1 * z2 - s2 * z1 ) * r );
tan1[ a ].addSelf( sdir );
tan1[ b ].addSelf( sdir );
tan1[ c ].addSelf( sdir );
tan2[ a ].addSelf( tdir );
tan2[ b ].addSelf( tdir );
tan2[ c ].addSelf( tdir );
}
for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
face = this.faces[ f ];
uv = this.uvs[ f ];
if ( face instanceof THREE.Face3 ) {
handleTriangle( this, face.a, face.b, face.c );
this.vertices[ face.a ].normal.copy( face.vertexNormals[ 0 ] );
this.vertices[ face.b ].normal.copy( face.vertexNormals[ 1 ] );
this.vertices[ face.c ].normal.copy( face.vertexNormals[ 2 ] );
} else if ( face instanceof THREE.Face4 ) {
handleTriangle( this, face.a, face.b, face.c );
// this messes up everything
// quads need to be handled differently
//handleTriangle( this, face.a, face.c, face.d );
......@@ -255,41 +255,41 @@ THREE.Geometry.prototype = {
this.vertices[ face.b ].normal.copy( face.vertexNormals[ 1 ] );
this.vertices[ face.c ].normal.copy( face.vertexNormals[ 2 ] );
this.vertices[ face.d ].normal.copy( face.vertexNormals[ 3 ] );
}
}
for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
n.copy( this.vertices[ v ].normal );
t = tan1[ v ];
// Gram-Schmidt orthogonalize
tmp.copy( t );
tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
// Calculate handedness
tmp2.cross( this.vertices[ v ].normal, t );
test = tmp2.dot( tan2[ v ] );
w = (test < 0.0) ? -1.0 : 1.0;
this.vertices[ v ].tangent.set( tmp.x, tmp.y, tmp.z, w );
}
this.hasTangents = true;
},
computeBoundingBox: function () {
if ( this.vertices.length > 0 ) {
this.bbox = { 'x': [ this.vertices[ 0 ].position.x, this.vertices[ 0 ].position.x ],
'y': [ this.vertices[ 0 ].position.y, this.vertices[ 0 ].position.y ],
'y': [ this.vertices[ 0 ].position.y, this.vertices[ 0 ].position.y ],
'z': [ this.vertices[ 0 ].position.z, this.vertices[ 0 ].position.z ] };
for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
......
......@@ -64,13 +64,16 @@ var Cube = function ( width, height, depth, segments_width, segments_height, mat
function buildPlane( u, v, udir, vdir, width, height, depth, material ) {
var gridX = segments_width || 1,
var w,
gridX = segments_width || 1,
gridY = segments_height || 1,
gridX1 = gridX + 1,
gridY1 = gridY + 1,
width_half = width / 2,
height_half = height / 2,
segment_width = width / gridX,
segment_height = height / gridY,
offset = scope.vertices.length, w;
offset = scope.vertices.length;
if ( ( u == 'x' && v == 'y' ) || ( u == 'y' && v == 'x' ) ) {
......
......@@ -3,7 +3,7 @@
* @author alteredq / http://alteredqualia.com/
*/
THREE.Mesh = function ( geometry, material, normUVs ) {
THREE.Mesh = function ( geometry, material/*, normUVs*/ ) {
THREE.Object3D.call( this );
......@@ -15,7 +15,7 @@ THREE.Mesh = function ( geometry, material, normUVs ) {
this.overdraw = false;
if ( normUVs ) this.normalizeUVs();
// if ( normUVs ) this.normalizeUVs();
this.geometry.computeBoundingBox();
......@@ -24,6 +24,9 @@ THREE.Mesh = function ( geometry, material, normUVs ) {
THREE.Mesh.prototype = new THREE.Object3D();
THREE.Mesh.prototype.constructor = THREE.Mesh;
/*
TODO: This doesn't completely fix the issue. Needs to be handled directly in the CanvasRenderer
THREE.Mesh.prototype.normalizeUVs = function () {
var i, il, j, jl, uvs;
......@@ -45,3 +48,4 @@ THREE.Mesh.prototype.normalizeUVs = function () {
}
};
*/
......@@ -99,6 +99,7 @@ THREE.WebGLRenderer2 = function () {
_normalMatrix = THREE.Matrix4.makeInvert3x3( _modelViewMatrix ).transpose();
_normalMatrixArray.set( _normalMatrix.m );
if ( object instanceof THREE.Mesh ) {
geometry = object.geometry;
......@@ -158,43 +159,51 @@ THREE.WebGLRenderer2 = function () {
_gl.uniformMatrix4fv( program.uniforms.modelViewMatrix, false, _modelViewMatrixArray );
_gl.uniformMatrix3fv( program.uniforms.normalMatrix, false, _normalMatrixArray );
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglBuffers.vertexBuffer );
_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
var buffer, buffers = geometry.__webglBuffers;
if ( attributes.normal >= 0 ) {
for ( var i = 0, l = buffers.length; i < l; i ++ ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglBuffers.normalBuffer );
_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
buffer = buffers[ i ];
}
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.vertices );
_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
if ( attributes.normal >= 0 ) {
if ( attributes.uv >= 0 ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.normals );
_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
if ( geometry.__webglBuffers.uvBuffer ) {
}
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglBuffers.uvBuffer );
if ( attributes.uv >= 0 ) {
_gl.enableVertexAttribArray( attributes.uv );
_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
if ( buffer.uvs ) {
} else {
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.uvs );
_gl.enableVertexAttribArray( attributes.uv );
_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
_gl.disableVertexAttribArray( attributes.uv );
} else {
_gl.disableVertexAttribArray( attributes.uv );
}
}
}
if ( ! material.wireframe ) {
if ( ! material.wireframe ) {
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffer.faces );
_gl.drawElements( _gl.TRIANGLES, buffer.faceCount, _gl.UNSIGNED_SHORT, 0 );
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometry.__webglBuffers.faceBuffer );
_gl.drawElements( _gl.TRIANGLES, geometry.__webglBuffers.faceCount, _gl.UNSIGNED_SHORT, 0 );
} else {
} else {
_gl.lineWidth( material.wireframe_linewidth );
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffer.lines );
_gl.drawElements( _gl.LINES, buffer.lineCount, _gl.UNSIGNED_SHORT, 0 );
_gl.lineWidth( material.wireframe_linewidth );
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometry.__webglBuffers.lineBuffer );
_gl.drawElements( _gl.LINES, geometry.__webglBuffers.lineCount, _gl.UNSIGNED_SHORT, 0 );
}
}
......@@ -208,9 +217,11 @@ THREE.WebGLRenderer2 = function () {
function buildBuffers( geometry ) {
var f, fl, face, v1, v2, v3, vertexNormals, normal, uv,
vertexIndex = 0, verticesArray = [], facesArray = [], linesArray = [],
normalsArray = [], uvsArray = [],
// TODO: Handle 65535
var vertexIndex = 0, group,
f, fl, face, v1, v2, v3, vertexNormals, normal, uv,
vertices = [], faces = [], lines = [], normals = [], uvs = [],
buffers = {};
for ( f = 0, fl = geometry.faces.length; f < fl; f++ ) {
......@@ -226,15 +237,15 @@ THREE.WebGLRenderer2 = function () {
v2 = geometry.vertices[ face.b ].position;
v3 = geometry.vertices[ face.c ].position;
verticesArray.push( v1.x, v1.y, v1.z );
verticesArray.push( v2.x, v2.y, v2.z );
verticesArray.push( v3.x, v3.y, v3.z );
vertices.push( v1.x, v1.y, v1.z );
vertices.push( v2.x, v2.y, v2.z );
vertices.push( v3.x, v3.y, v3.z );
if ( vertexNormals.length == 3 ) {
for ( i = 0; i < 3; i ++ ) {
normalsArray.push( vertexNormals[ i ].x, vertexNormals[ i ].y, vertexNormals[ i ].z );
normals.push( vertexNormals[ i ].x, vertexNormals[ i ].y, vertexNormals[ i ].z );
}
......@@ -242,7 +253,7 @@ THREE.WebGLRenderer2 = function () {
for ( i = 0; i < 3; i ++ ) {
normalsArray.push( faceNormal.x, faceNormal.y, faceNormal.z );
normals.push( faceNormal.x, faceNormal.y, faceNormal.z );
}
......@@ -252,39 +263,51 @@ THREE.WebGLRenderer2 = function () {
for ( i = 0; i < 3; i ++ ) {
uvsArray.push( uv[ i ].u, uv[ i ].v );
uvs.push( uv[ i ].u, uv[ i ].v );
}
}
facesArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
faces.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
// TODO: don't add lines that already exist (faces sharing edge)
linesArray.push( vertexIndex, vertexIndex + 1 );
linesArray.push( vertexIndex, vertexIndex + 2 );
linesArray.push( vertexIndex + 1, vertexIndex + 2 );
lines.push( vertexIndex, vertexIndex + 1 );
lines.push( vertexIndex, vertexIndex + 2 );
lines.push( vertexIndex + 1, vertexIndex + 2 );
vertexIndex += 3;
} else if ( face instanceof THREE.Face4 ) {
group = Math.floor( vertexIndex / 65535 );
if ( !vertices ) {
vertices = [];
faces = [];
normals = [];
lines = [];
uvs = [];
}
v1 = geometry.vertices[ face.a ].position;
v2 = geometry.vertices[ face.b ].position;
v3 = geometry.vertices[ face.c ].position;
v4 = geometry.vertices[ face.d ].position;
verticesArray.push( v1.x, v1.y, v1.z );
verticesArray.push( v2.x, v2.y, v2.z );
verticesArray.push( v3.x, v3.y, v3.z );
verticesArray.push( v4.x, v4.y, v4.z );
vertices.push( v1.x, v1.y, v1.z );
vertices.push( v2.x, v2.y, v2.z );
vertices.push( v3.x, v3.y, v3.z );
vertices.push( v4.x, v4.y, v4.z );
if ( vertexNormals.length == 4 ) {
for ( i = 0; i < 4; i ++ ) {
normalsArray.push( vertexNormals[ i ].x, vertexNormals[ i ].y, vertexNormals[ i ].z );
normals.push( vertexNormals[ i ].x, vertexNormals[ i ].y, vertexNormals[ i ].z );
}
......@@ -292,7 +315,7 @@ THREE.WebGLRenderer2 = function () {
for ( i = 0; i < 4; i ++ ) {
normalsArray.push( faceNormal.x, faceNormal.y, faceNormal.z );
normals.push( faceNormal.x, faceNormal.y, faceNormal.z );
}
......@@ -302,22 +325,22 @@ THREE.WebGLRenderer2 = function () {
for ( i = 0; i < 4; i ++ ) {
uvsArray.push( uv[ i ].u, uv[ i ].v );
uvs.push( uv[ i ].u, uv[ i ].v );
}
}
facesArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
facesArray.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );
faces.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
faces.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );
// TODO: don't add lines that already exist (faces sharing edge)
linesArray.push( vertexIndex, vertexIndex + 1 );
linesArray.push( vertexIndex, vertexIndex + 2 );
linesArray.push( vertexIndex, vertexIndex + 3 );
linesArray.push( vertexIndex + 1, vertexIndex + 2 );
linesArray.push( vertexIndex + 2, vertexIndex + 3 );
lines.push( vertexIndex, vertexIndex + 1 );
lines.push( vertexIndex, vertexIndex + 2 );
lines.push( vertexIndex, vertexIndex + 3 );
lines.push( vertexIndex + 1, vertexIndex + 2 );
lines.push( vertexIndex + 2, vertexIndex + 3 );
vertexIndex += 4;
......@@ -325,34 +348,49 @@ THREE.WebGLRenderer2 = function () {
}
if ( !verticesArray.length ) return false;
if ( !vertices.length ) return false;
buffers.vertexBuffer = _gl.createBuffer();
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.vertexBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( verticesArray ), _gl.STATIC_DRAW );
var buffer, buffers = [];
buffers.normalBuffer = _gl.createBuffer();
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normalBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalsArray ), _gl.STATIC_DRAW );
// for ( var i = 0, l = group; i <= l; i ++ ) {
if ( uvsArray.length > 0 ) {
buffer = {
vertices: null,
faces: null,
faceCount: faces.length,
normals: null,
lines: null,
lineCount: lines.length,
uvs: null
};
buffers.uvBuffer = _gl.createBuffer();
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uvBuffer );
_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( uvsArray ), _gl.STATIC_DRAW );
buffer.vertices = _gl.createBuffer();
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.vertices );
_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( vertices ), _gl.STATIC_DRAW );
}
buffer.normals = _gl.createBuffer();
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.normals );
_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normals ), _gl.STATIC_DRAW );
if ( uvs.length > 0 ) {
buffer.uvs = _gl.createBuffer();
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.uvs );
_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( uvs ), _gl.STATIC_DRAW );
}
buffer.faces = _gl.createBuffer();
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffer.faces );
_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( faces ), _gl.STATIC_DRAW );
buffers.faceBuffer = _gl.createBuffer();
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffers.faceBuffer );
_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( facesArray ), _gl.STATIC_DRAW );
buffer.lines = _gl.createBuffer();
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffer.lines );
_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( lines ), _gl.STATIC_DRAW );
buffers.lineBuffer = _gl.createBuffer();
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffers.lineBuffer );
_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( linesArray ), _gl.STATIC_DRAW );
buffers.push( buffer );
buffers.faceCount = facesArray.length;
buffers.lineCount = linesArray.length;
// }
geometry.__webglBuffers = buffers;
......
......@@ -9,7 +9,13 @@ THREE.Scene = function () {
this.addObject = function ( object ) {
this.objects.push( object );
var i = this.objects.indexOf( object );
if ( i === -1 ) {
this.objects.push( object );
}
};
......@@ -22,12 +28,18 @@ THREE.Scene = function () {
this.objects.splice( i, 1 );
}
};
this.addLight = function ( light ) {
this.lights.push( light );
var i = this.lights.indexOf( light );
if ( i === -1 ) {
this.lights.push( light );
}
};
......@@ -46,6 +58,7 @@ THREE.Scene = function () {
this.toString = function () {
return 'THREE.Scene ( ' + this.objects + ' )';
};
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册