Merge remote-tracking branch 'zz85/master' into dev

This commit is contained in:
Mr.doob
2011-10-25 06:39:53 +02:00
4 changed files with 338 additions and 86 deletions
+35 -15
View File
@@ -26,6 +26,40 @@
<script src="js/RequestAnimationFrame.js"></script>
<script src="js/Stats.js"></script>
<div style="position: absolute; top: 10px; width: 100%; text-align: center; ">
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - Combo Camera<br>
View: <a href="#" onclick="setOrthographic();return false;"> Orthographic</a> |
<a href="#" onclick="setPerspective();return false;">Perspective</a><br>
Lens: <a href="#" onclick="setLens(12);return false;">12mm</a> |
<a href="#" onclick="setLens(16);return false;">16mm</a> |
<a href="#" onclick="setLens(24);return false;">24mm</a> |
<a href="#" onclick="setLens(35);return false;">35mm</a> |
<a href="#" onclick="setLens(50);return false;">50mm</a> |
<a href="#" onclick="setLens(60);return false;">60mm</a> |
<a href="#" onclick="setLens(85);return false;">85mm</a> |
<a href="#" onclick="setLens(105);return false;">105mm</a><br>
Fov: <a href="#" onclick="setFov(30);return false;">30°</a> |
<a href="#" onclick="setFov(50);return false;">50°</a> |
<a href="#" onclick="setFov(70);return false;">70°</a> |
<a href="#" onclick="setFov(100);return false;">100°</a><br>
Zoom: <a href="#" onclick="camera.setZoom(0.5);return false;">0.5x</a> |
<a href="#" onclick="camera.setZoom(1);return false;">1x</a> |
<a href="#" onclick="camera.setZoom(2);return false;">2x</a> |
<br/>
Views: <a href="#" onclick="camera.toTopView();return false;">Top view</a> |
<a href="#" onclick="camera.toBottomView();return false;">Bottom view</a> |
<a href="#" onclick="camera.toLeftView();return false;">Left view</a> |
<a href="#" onclick="camera.toRightView();return false;">Right view</a> |
<a href="#" onclick="camera.toFrontView();return false;">Front view</a> |
<a href="#" onclick="camera.toBackView();return false;">Back view</a> |
<a href="#" onclick="camera.rotationAutoUpdate = true;return false;">Look at Scene</a>
<br/>
<div id="fov"></div>
</div>
<script>
var container, stats;
@@ -64,21 +98,7 @@
container = document.createElement( 'div' );
document.body.appendChild( container );
var info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - Combo Camera' +
'<br/>View: <a href="#" onclick="setOrthographic();return false;"> Orthographic</a> | <a href="#" onclick="setPerspective();return false;">Perspective</a>' +
'<br/>Lens: <a href="#" onclick="setLens(12);return false;">12mm</a> | <a href="#" onclick="setLens(16);return false;">16mm</a> | <a href="#" onclick="setLens(24);return false;">24mm</a> | ' +
'<a href="#" onclick="setLens(35);return false;">35mm</a> | <a href="#" onclick="setLens(50);return false;">50mm</a> | <a href="#" onclick="setLens(60);return false;">60mm</a> |' +
' <a href="#" onclick="setLens(85);return false;">85mm</a> | <a href="#" onclick="setLens(105);return false;">105mm</a>' +
'<br/>Fov: <a href="#" onclick="setFov(30);return false;">30&deg;</a> | <a href="#" onclick="setFov(50);return false;">50&deg;</a> | <a href="#" onclick="setFov(70);return false;">70&deg;</a> | <a href="#" onclick="setFov(100);return false;">100&deg;</a> <br/><div id="fov"/>';
container.appendChild( info );
camera = new THREE.CombinedCamera( window.innerWidth, window.innerHeight, 70, 1, 1000, -1000, 1000, 1000 );
camera = new THREE.CombinedCamera( window.innerWidth /2, window.innerHeight/2, 70, 1, 1000, -1000, 1000, 1000 );
camera.position.x = 200;
camera.position.y = 100;
+49 -9
View File
@@ -72,14 +72,24 @@
materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
}
var geometriesParams = [
{type: 'CubeGeometry', args: [ 200, 200, 200, 2, 2, 2, materials ] },
{type: 'TorusGeometry', args: [ 100, 60, 4, 8, Math.PI*2 ] },
{type: 'SphereGeometry', args: [ 100, 3, 3 ] },
{type: 'IcosahedronGeometry', args: [ 1 ], scale: 200 },
{type: 'TorusKnotGeometry', args: [ ], scale:0.25, meshScale:4 },
{type: 'SphereGeometry', args: [ 100, 3, 3 ], meshScale:2 },
{type: 'IcosahedronGeometry', args: [ 1 ], scale: 100, meshScale:2 },
{type: 'CylinderGeometry', args: [ 25, 75, 200, 8, 3 ]} ,
{type: 'OctahedronGeometry', args: [200, 0], meshScale:2 },
{type: 'LatheGeometry', args: [ [
new THREE.Vector3(0,0,0),
new THREE.Vector3(0,50,50),
new THREE.Vector3(0,0,100),
new THREE.Vector3(0,50,150),
new THREE.Vector3(0,0,200) ] ]},
{type: 'TextGeometry', args: ['&', {
size: 200,
height: 50,
@@ -88,8 +98,33 @@
}]},
{type: 'PlaneGeometry', args: [ 200, 200, 4, 4 ] }
];
if (location.protocol !== "file:") {
var loader = new THREE.JSONLoader();
loader.load( 'obj/WaltHeadLo.js', function ( geometry ) {
geometriesParams.push({type: 'WaltHead', args: [ ], meshScale: 6 });
THREE.WaltHead = function() {
return THREE.GeometryUtils.clone(geometry);
};
});
var loader2 = new THREE.JSONLoader();
loader2.load( 'obj/Suzanne.js', function ( geometry ) {
geometriesParams.push({type: 'Suzanne', args: [ ], scale: 100, meshScale:2 });
THREE.Suzanne = function() {
return THREE.GeometryUtils.clone(geometry);
};
} );
}
var info;
@@ -138,6 +173,11 @@
var params = geometriesParams[ geometryIndex ];
geometry = createSomething( THREE[ params.type ], params.args );
// Scale Geometry
if (params.scale) {
geometry.applyMatrix( new THREE.Matrix4().setScale( params.scale, params.scale, params.scale ) );
}
// Cloning original geometry for debuging
@@ -257,11 +297,11 @@
cube = THREE.SceneUtils.createMultiMaterialObject( smooth, meshmaterials );
var toscale = params.scale ? params.scale : 1;
cube.scale.x = toscale;
cube.scale.y = toscale;
cube.scale.z = toscale;
var meshScale = params.meshScale ? params.meshScale : 1;
cube.scale.x = meshScale;
cube.scale.y = meshScale;
cube.scale.z = meshScale;
scene.add( cube );
@@ -388,7 +428,7 @@
group.rotation.x = cube.rotation.x += ( targetXRotation - cube.rotation.x ) * 0.05;
group.rotation.y = cube.rotation.y += ( targetYRotation - cube.rotation.y ) * 0.05;
renderer.render( scene, camera );
}
+125 -5
View File
@@ -11,11 +11,26 @@ THREE.CombinedCamera = function ( width, height, fov, near, far, orthonear, orth
THREE.Camera.call( this );
this.fov = fov;
this.left = -width / 2;
this.right = width / 2
this.top = height / 2;
this.bottom = -height / 2;
// We could also handle the projectionMatrix internally, but just wanted to test nested camera objects
this.cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, orthonear, orthofar );
this.cameraP = new THREE.PerspectiveCamera( fov, width/height, near, far );
this.zoom = 1;
this.toPerspective();
var aspect = width/height;
};
@@ -26,23 +41,69 @@ THREE.CombinedCamera.prototype.toPerspective = function () {
this.near = this.cameraP.near;
this.far = this.cameraP.far;
this.cameraP.fov = this.fov / this.zoom ;
this.cameraP.updateProjectionMatrix();
this.projectionMatrix = this.cameraP.projectionMatrix;
this.inPersepectiveMode = true;
this.inOrthographicMode = false;
};
THREE.CombinedCamera.prototype.toOrthographic = function () {
// Orthographic from Perspective
var fov = this.fov;
var aspect = this.cameraP.aspect;
var near = this.cameraP.near;
var far = this.cameraP.far;
// Just pretend we want the mid plane of the viewing frustum
var hyperfocus = ( near + far ) / 2;
var halfHeight = Math.tan( fov / 2 ) * hyperfocus;
var planeHeight = 2 * halfHeight;
var planeWidth = planeHeight * aspect;
var halfWidth = planeWidth / 2;
halfHeight /= this.zoom;
halfWidth /= this.zoom;
this.cameraO.left = -halfWidth;
this.cameraO.right = halfWidth;
this.cameraO.top = halfHeight;
this.cameraO.bottom = -halfHeight;
// this.cameraO.left = -farHalfWidth;
// this.cameraO.right = farHalfWidth;
// this.cameraO.top = farHalfHeight;
// this.cameraO.bottom = -farHalfHeight;
// this.cameraO.left = this.left / this.zoom;
// this.cameraO.right = this.right / this.zoom;
// this.cameraO.top = this.top / this.zoom;
// this.cameraO.bottom = this.bottom / this.zoom;
this.cameraO.updateProjectionMatrix();
this.near = this.cameraO.near;
this.far = this.cameraO.far;
this.projectionMatrix = this.cameraO.projectionMatrix;
this.inPersepectiveMode = false;
this.inOrthographicMode = true;
};
THREE.CombinedCamera.prototype.setFov = function(fov) {
this.cameraP.fov = fov;
this.cameraP.updateProjectionMatrix();
this.toPerspective();
THREE.CombinedCamera.prototype.setFov = function(fov) {
this.fov = fov;
if (this.inPersepectiveMode) {
this.toPerspective();
} else {
this.toOrthographic();
}
};
@@ -61,3 +122,62 @@ THREE.CombinedCamera.prototype.setLens = function(focalLength, framesize) {
return fov;
};
THREE.CombinedCamera.prototype.setZoom = function(zoom) {
this.zoom = zoom;
if (this.inPersepectiveMode) {
this.toPerspective();
} else {
this.toOrthographic();
}
};
THREE.CombinedCamera.prototype.toFrontView = function() {
this.rotation.x = 0;
this.rotation.y = 0;
this.rotation.z = 0;
//TODO: Better way to disable camera.lookAt()?
this.rotationAutoUpdate = false;
};
THREE.CombinedCamera.prototype.toBackView = function() {
this.rotation.x = 0;
this.rotation.y = Math.PI;
this.rotation.z = 0;
this.rotationAutoUpdate = false;
};
THREE.CombinedCamera.prototype.toLeftView = function() {
this.rotation.x = 0;
this.rotation.y = - Math.PI / 2;
this.rotation.z = 0;
this.rotationAutoUpdate = false;
};
THREE.CombinedCamera.prototype.toRightView = function() {
this.rotation.x = 0;
this.rotation.y = Math.PI / 2;
this.rotation.z = 0;
this.rotationAutoUpdate = false;
};
THREE.CombinedCamera.prototype.toTopView = function() {
this.rotation.x = - Math.PI / 2;
this.rotation.y = 0;
this.rotation.z = 0;
this.rotationAutoUpdate = false;
};
THREE.CombinedCamera.prototype.toBottomView = function() {
this.rotation.x = Math.PI / 2;
this.rotation.y = 0;
this.rotation.z = 0;
this.rotationAutoUpdate = false;
};
+129 -57
View File
@@ -11,6 +11,15 @@
* Readings:
* http://en.wikipedia.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
* http://www.rorydriscoll.com/2008/08/01/catmull-clark-subdivision-the-basics/
* http://xrt.wikidot.com/blog:31
* "Subdivision Surfaces in Character Animation"
*
* Supports:
* Closed and Open geometries.
*
* TODO:
* crease vertex and "semi-sharp" features
* selective subdivision
*/
THREE.SubdivisionModifier = function( subdivisions ) {
@@ -186,6 +195,8 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
var facePoints = [], edgePoints = {};
var sharpEdges = {}, sharpVertices = [], sharpFaces = [];
var uvForVertices = [];
// Step 1
@@ -253,6 +264,80 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
var edgeCount = 0;
var originalVerticesLength = originalPoints.length;
var edgeVertex, edgeVertexA, edgeVertexB;
////
var vertexEdgeMap = {};
var vertexFaceMap = {};
var addVertexEdgeMap = function(vertex, edge) {
if (vertexEdgeMap[vertex]===undefined) {
vertexEdgeMap[vertex] = [];
}
vertexEdgeMap[vertex].push(edge);
};
var addVertexFaceMap = function(vertex, face, edge) {
if (vertexFaceMap[vertex]===undefined) {
vertexFaceMap[vertex] = {};
}
//vertexFaceMap[vertex][face] = edge;
vertexFaceMap[vertex][face] = null;
};
// Prepares vertexEdgeMap and vertexFaceMap
for (i in vfMap) { // This is for every edge
edge = vfMap[i];
edgeVertex = i.split('_');
edgeVertexA = edgeVertex[0];
edgeVertexB = edgeVertex[1];
// Maps an edgeVertex to connecting edges
addVertexEdgeMap(edgeVertexA, [edgeVertexA, edgeVertexB] );
addVertexEdgeMap(edgeVertexB, [edgeVertexA, edgeVertexB] );
// faceIndexA = edge[0]; // face index a
// faceIndexB = edge[1]; // face index b
//
// // Add connecting faces for edge
// addVertexFaceMap(edgeVertexA, faceIndexA);
// addVertexFaceMap(edgeVertexB, faceIndexA);
//
//
// if (faceIndexB) {
// addVertexFaceMap(edgeVertexA, faceIndexB);
// addVertexFaceMap(edgeVertexB, faceIndexB);
// } else {
// addVertexFaceMap(edgeVertexA, faceIndexA);
// addVertexFaceMap(edgeVertexB, faceIndexA);
// }
for (j=0,jl=edge.length;j<jl;j++) {
face = edge[j];
addVertexFaceMap(edgeVertexA, face, i);
addVertexFaceMap(edgeVertexB, face, i);
}
if (edge.length < 2) {
// edge is "sharp";
sharpEdges[i] = true;
sharpVertices[edgeVertexA] = true;
sharpVertices[edgeVertexB] = true;
}
}
//console.log('vertexEdgeMap',vertexEdgeMap, 'vertexFaceMap', vertexFaceMap);
for (i in vfMap) {
edge = vfMap[i];
@@ -268,14 +353,17 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
//console.log(i, faceIndexB,facePoints[faceIndexB]);
if (edge.length!=2) {
if (sharpEdges[i]) {
//console.log('warning, ', i, 'edge has only 1 connecting face', edge);
// For a sharp edge, average the edge end points.
avg.addSelf(originalPoints[edgeVertexA].position);
avg.addSelf(originalPoints[edgeVertexB].position);
avg.multiplyScalar(0.5);
sharpVertices[newPoints.length] = true;
} else {
avg.addSelf(facePoints[faceIndexA]);
@@ -289,6 +377,7 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
}
edgePoints[i] = originalVerticesLength + originalFaces.length + edgeCount;
//console.log(edgePoints[i], newPoints.length);
newPoints.push( new THREE.Vertex(avg) );
@@ -318,6 +407,14 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
var hashAB, hashBC, hashCD, hashDA, hashCA;
var abc123 = ['123', '12', '2', '23'];
var bca123 = ['123', '23', '3', '31'];
var cab123 = ['123', '31', '1', '12'];
var abc1234 = ['1234', '12', '2', '23'];
var bcd1234 = ['1234', '23', '3', '34'];
var cda1234 = ['1234', '34', '4', '41'];
var dab1234 = ['1234', '41', '1', '12'];
for (i=0, il = facePoints.length; i<il ;i++) { // for every face
facePt = facePoints[i];
@@ -332,9 +429,9 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
hashBC = edge_hash( face.b, face.c );
hashCA = edge_hash( face.c, face.a );
f4( currentVerticeIndex, edgePoints[hashAB], face.b, edgePoints[hashBC], face, ['123', '12', '2', '23'] );
f4( currentVerticeIndex, edgePoints[hashBC], face.c, edgePoints[hashCA], face, ['123', '23', '3', '31'] );
f4( currentVerticeIndex, edgePoints[hashCA], face.a, edgePoints[hashAB], face, ['123', '31', '1', '12'] );
f4( currentVerticeIndex, edgePoints[hashAB], face.b, edgePoints[hashBC], face, abc123 );
f4( currentVerticeIndex, edgePoints[hashBC], face.c, edgePoints[hashCA], face, bca123 );
f4( currentVerticeIndex, edgePoints[hashCA], face.a, edgePoints[hashAB], face, cab123 );
} else if ( face instanceof THREE.Face4 ) {
// create 4 face4s
@@ -344,10 +441,10 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
hashCD = edge_hash( face.c, face.d );
hashDA = edge_hash( face.d, face.a );
f4( currentVerticeIndex, edgePoints[hashAB], face.b, edgePoints[hashBC], face, ['1234', '12', '2', '23'] );
f4( currentVerticeIndex, edgePoints[hashBC], face.c, edgePoints[hashCD], face, ['1234', '23', '3', '34'] );
f4( currentVerticeIndex, edgePoints[hashCD], face.d, edgePoints[hashDA], face, ['1234', '34', '4', '41'] );
f4( currentVerticeIndex, edgePoints[hashDA], face.a, edgePoints[hashAB], face, ['1234', '41', '1', '12'] );
f4( currentVerticeIndex, edgePoints[hashAB], face.b, edgePoints[hashBC], face, abc1234 );
f4( currentVerticeIndex, edgePoints[hashBC], face.c, edgePoints[hashCD], face, bcd1234 );
f4( currentVerticeIndex, edgePoints[hashCD], face.d, edgePoints[hashDA], face, cda1234 );
f4( currentVerticeIndex, edgePoints[hashDA], face.a, edgePoints[hashAB], face, dab1234 );
} else {
@@ -367,53 +464,7 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
// and take the average R of all n edge midpoints for edges touching P,
// where each edge midpoint is the average of its two endpoint vertices.
// Move each original point to the point
var vertexEdgeMap = {};
var vertexFaceMap = {};
var addVertexEdgeMap = function(vertex, edge) {
if (vertexEdgeMap[vertex]===undefined) {
vertexEdgeMap[vertex] = [];
}
vertexEdgeMap[vertex].push(edge);
};
var addVertexFaceMap = function(vertex, face) {
if (vertexFaceMap[vertex]===undefined) {
vertexFaceMap[vertex] = {};
}
vertexFaceMap[vertex][face] = null;
};
// Prepares vertexEdgeMap and vertexFaceMap
for (i in vfMap) { // This is for every edge
edge = vfMap[i];
edgeVertex = i.split('_');
edgeVertexA = edgeVertex[0];
edgeVertexB = edgeVertex[1];
addVertexEdgeMap(edgeVertexA, [edgeVertexA, edgeVertexB] );
addVertexEdgeMap(edgeVertexB, [edgeVertexA, edgeVertexB] );
faceIndexA = edge[0]; // face index a
faceIndexB = edge[1]; // face index b
addVertexFaceMap(edgeVertexA, faceIndexA);
if (faceIndexB) addVertexFaceMap(edgeVertexA, faceIndexB);
else addVertexFaceMap(edgeVertexA, faceIndexA);
addVertexFaceMap(edgeVertexB, faceIndexA);
if (faceIndexB) addVertexFaceMap(edgeVertexB, faceIndexB);
else addVertexFaceMap(edgeVertexB, faceIndexA);
}
//console.log('vertexEdgeMap',vertexEdgeMap, 'vertexFaceMap', vertexFaceMap);
var F = new THREE.Vector3();
var R = new THREE.Vector3();
@@ -430,15 +481,36 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
var f =0;
for (j in vertexFaceMap[i]) {
F.addSelf(facePoints[j]);
f++;
}
F.divideScalar(f);
var sharpEdgeCount = 0;
n = vertexEdgeMap[i].length;
for (j=0;j<n;j++) {
if (
sharpEdges[
edge_hash(vertexEdgeMap[i][j][0],vertexEdgeMap[i][j][1])
]) {
sharpEdgeCount++;
}
}
if ( sharpEdgeCount==2 ) {
continue;
// Do not move vertex if there's 2 connecting sharp edges.
}
if (sharpEdgeCount>2) {
// TODO
}
F.divideScalar(f);
for (j=0; j<n;j++) {
edge = vertexEdgeMap[i][j];
var midPt = originalPoints[edge[0]].position.clone().addSelf(originalPoints[edge[1]].position).divideScalar(2);