提交 ce8b942e 编写于 作者: A alteredq

Merge remote-tracking branch 'remotes/zz85/experimental' into experimental

因为 它太大了无法显示 source diff 。你可以改为 查看blob
此差异已折叠。
......@@ -125,7 +125,7 @@
}
var extrudeSettings = { amount: 20, bevelEnabled: true, bevelSegments: 2, steps: 2 }; // amount: 40, steps: 2
var extrudeSettings = { amount: 20, bevelEnabled: true, bevelSegments: 2, steps: 2 }; // bevelSegments: 2, steps: 2 , bevelSegments: 5, bevelSize: 8, bevelThickness:5,
// California
......@@ -266,12 +266,12 @@
var fishShape = new THREE.Shape();
fishShape.moveTo( x, y );
fishShape.quadraticCurveTo( x + 50, y + 80, x + 90, y + 10 );
fishShape.quadraticCurveTo( x + 100, y + 10, x + 115, y + 40 );
fishShape.quadraticCurveTo( x + 100, y, x + 115, y - 40 );
fishShape.quadraticCurveTo( x + 100, y + 10, x + 90, y - 10 );
fishShape.quadraticCurveTo( x + 50, y - 80, x, y );
fishShape.moveTo(x,y);
fishShape.quadraticCurveTo(x + 50, y - 80, x + 90, y - 10);
fishShape.quadraticCurveTo(x + 100, y - 10, x + 115, y - 40);
fishShape.quadraticCurveTo(x + 115, y, x + 115, y + 40);
fishShape.quadraticCurveTo(x + 100, y + 10, x + 90, y + 10);
fishShape.quadraticCurveTo(x + 50, y + 80, x, y);
var fish3d = fishShape.extrude( extrudeSettings );
var fishPoints = fishShape.createPointsGeometry();
......@@ -291,6 +291,44 @@
var arc3d = arcShape.extrude( extrudeSettings );
var arcPoints = arcShape.createPointsGeometry();
var arcSpacedPoints = arcShape.createSpacedPointsGeometry();
// Smiley
var smileyShape = new THREE.Shape();
smileyShape.moveTo( 0, 0 );
smileyShape.arc( 40, 40, 40, 0, Math.PI*2, false );
var smileyEye1Path = new THREE.Path();
smileyEye1Path.moveTo( 0, 0 );
smileyEye1Path.arc( 25, 20, 10, 0, Math.PI*2, true );
smileyShape.holes.push( smileyEye1Path );
var smileyEye2Path = new THREE.Path();
smileyEye2Path.moveTo( 0, 0 );
smileyEye2Path.arc( 55, 20, 10, 0, Math.PI*2, true );
smileyShape.holes.push( smileyEye2Path );
var smileyMouthPath = new THREE.Path();
// ugly box mouth
// smileyMouthPath.moveTo( 20, 40 );
// smileyMouthPath.lineTo( 60, 40 );
// smileyMouthPath.lineTo( 60, 60 );
// smileyMouthPath.lineTo( 20, 60 );
// smileyMouthPath.lineTo( 20, 40 );
smileyMouthPath.moveTo( 20, 40 );
smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
smileyShape.holes.push( smileyMouthPath );
var smiley3d = smileyShape.extrude( extrudeSettings );
var smileyPoints = smileyShape.createPointsGeometry();
var smileySpacedPoints = smileyShape.createSpacedPointsGeometry();
// Spline shape + path extrusion
......@@ -326,10 +364,12 @@
addGeometry( square3d, squarePoints, squareSpacedPoints, 0x0055ff, 150, 100, 0, 0, 0, 0, 1 );
addGeometry( heart3d, heartPoints, heartSpacedPoints, 0xff1100, 0, 100, 0, 3.14, 0, 0, 1 );
addGeometry( circle3d, circlePoints, circleSpacedPoints, 0x00ff11, 120, 250, 0, 0, 0, 0, 1 );
addGeometry( fish3d, fishPoints, fishSpacedPoints, 0x222222, -50, 200, 0, 0, 0, 0, 1 );
addGeometry( fish3d, fishPoints, fishSpacedPoints, 0x222222, -60, 200, 0, 0, 0, 0, 1 );
addGeometry( splineShape3d, splinePoints, splineSpacedPoints, 0x888888, -50, -100, -50, 0, 0, 0, 0.2 );
addGeometry( arc3d, arcPoints, arcSpacedPoints, 0xbb4422, 150, 0, 0, 0, 0, 0, 1 );
addGeometry( smiley3d, smileyPoints, smileySpacedPoints, 0xee00ff, -270, 250, 0, Math.PI, 0, 0, 1 );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
......
......@@ -38,8 +38,6 @@ THREE.ExtrudeGeometry = function( shapes, options ) {
shape = shapes[ s ];
//console.log(shape);
this.addShape( shape, options );
}
......@@ -52,6 +50,8 @@ THREE.ExtrudeGeometry.prototype.constructor = THREE.ExtrudeGeometry;
THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
//var startTime = Date.now();
var amount = options.amount !== undefined ? options.amount : 100;
var bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 6; // 10
......@@ -60,17 +60,6 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
var bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true; // false
// We should set bevel segments to 0 if bevel is not enabled.
// (also bevelThickness and bevelSize make mess when bevel is not enabled,
// whole shape gets thicker)
if ( !bevelEnabled ) {
bevelSegments = 0;
bevelThickness = 0;
bevelSize = 0;
}
var steps = options.steps !== undefined ? options.steps : 1;
......@@ -82,6 +71,17 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
extrudePts = extrudePath.getPoints();
steps = extrudePts.length;
extrudeByPath = true;
bevelEnabled = false; // bevels not supported for path extrusion
}
// Safeguards if bevels are not enabled
if ( !bevelEnabled ) {
bevelSegments = 0;
bevelThickness = 0;
bevelSize = 0;
}
......@@ -96,7 +96,6 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
var shapesOffset = this.vertices.length;
//extractAllPoints
var shapePoints = shape.extractAllPoints(); // use shape.extractAllSpacedPoints() for points with equal divisions
var vertices = shapePoints.shape;
......@@ -110,7 +109,7 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
// Maybe we should also check if holes are in the opposite direction, just to be safe ...
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
ahole = holes[ h ];
......@@ -146,111 +145,191 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
}
// Find all centroids of shapes and holes
var i, il;
var sum = new THREE.Vector2();
for ( i = 0, il = contour.length; i < il; i ++ ) {
// We no longer need centroids
sum.addSelf( contour[ i ] );
// Find all centroids of shapes and holes
//var sum = new THREE.Vector2();
// for ( i = 0, il = contour.length; i < il; i ++ ) {
//
// sum.addSelf( contour[ i ] );
//
// }
//
// var contourCentroid = sum.divideScalar( contour.length );
//
// var holesCentroids = [];
//
// for ( h = 0, hl = holes.length; h < hl; h ++ ) {
//
// sum = new THREE.Vector2();
// ahole = holes[ h ];
//
// for ( i=0, il = ahole.length; i < il; i ++ ) {
//
// sum.addSelf( ahole[ i ] );
//
// }
//
// holesCentroids[ h ] = sum.divideScalar( ahole.length );
//
// }
//
// function scalePt ( pt, centroid, size, expandOutwards /* Boolean */ ) {
//
// var vectorFromCentroid = pt.clone().subSelf( centroid );
// var adj = size / vectorFromCentroid.length();
//
// if ( expandOutwards ) {
//
// adj = 1 + adj;
//
// } else {
//
// adj = 1 - adj;
//
// }
//
// return vectorFromCentroid.multiplyScalar( adj ).addSelf( centroid );
//
// }
function scalePt2 ( pt, vec, size ) {
if ( !vec ) console.log( "die" );
return vec.clone().multiplyScalar( size ).addSelf( pt );
}
var contourCentroid = sum.divideScalar( contour.length );
var b, bs, t, z,
vert, vlen = vertices.length,
face, flen = faces.length,
cont, clen = contour.length;
var holesCentroids = [];
for ( h = 0, hl = holes.length; h < hl; h ++ ) {
//------
// Find directions for point movement
//
sum = new THREE.Vector2();
ahole = holes[ h ];
var RAD_TO_DEGREES = 180 / Math.PI;
for ( i=0, il = ahole.length; i < il; i ++ ) {
sum.addSelf( ahole[ i ] );
function getBevelVec( pt_i, pt_j, pt_k ) {
}
// Algorithm 2
holesCentroids[ h ] = sum.divideScalar( ahole.length );
return getBevelVec2( pt_i, pt_j, pt_k );
}
function scalePt ( pt, centroid, size, expandOutwards /* Boolean */ ) {
function getBevelVec1( pt_i, pt_j, pt_k ) {
var anglea = Math.atan2( pt_j.y - pt_i.y, pt_j.x - pt_i.x );
var angleb = Math.atan2( pt_k.y - pt_i.y, pt_k.x - pt_i.x );
var vectorFromCentroid = pt.clone().subSelf( centroid );
var adj = size / vectorFromCentroid.length();
if ( anglea > angleb ) {
if ( expandOutwards ) {
angleb += Math.PI * 2;
adj = 1 + adj;
}
} else {
anglec = ( anglea + angleb ) / 2;
adj = 1 - adj;
}
//console.log('angle1', anglea * RAD_TO_DEGREES,'angle2', angleb * RAD_TO_DEGREES, 'anglec', anglec *RAD_TO_DEGREES);
return vectorFromCentroid.multiplyScalar( adj ).addSelf( centroid );
var x = - Math.cos( anglec );
var y = - Math.sin( anglec );
var vec = new THREE.Vector2( x, y ); //.normalize();
return vec;
}
function getBevelVec2( pt_i, pt_j, pt_k ) {
function scalePt2 ( pt, vec, size ) {
var a, b, v, w, v_hat, w_hat,
p, q, v_dot_w_hat, q_sub_p_dot_w_hat,
s, intersection;
//return vec.clone().multiplyScalar( size ).addSelf( pt );
return pt.clone().addSelf( vec.clone().multiplyScalar( size ) );
// good reading for line-line intersection
// http://sputsoft.com/blog/2010/03/line-line-intersection.html
}
// define a as vector j->i
// define b as vectot k->i
var b, bs, t, z,
vert, vlen = vertices.length,
face, flen = faces.length,
cont, clen = contour.length;
a = new THREE.Vector2( pt_i.x - pt_j.x, pt_i.y - pt_j.y );
b = new THREE.Vector2( pt_i.x - pt_k.x, pt_i.y - pt_k.y );
// get unit vectors
//------
// Find directions for point movement
//
v = a.normalize();
w = b.normalize();
var RAD_TO_DEGREES = 180 / Math.PI;
// normals from pt i
v_hat = new THREE.Vector2( -v.y, v.x ); // v hat
w_hat = new THREE.Vector2( w.y, -w.x ); // w hat
function getBevelVec( pt_i, pt_j, pt_k ) {
// pts from i
var anglea = Math.atan2( pt_j.y - pt_i.y, pt_j.x - pt_i.x );
var angleb = Math.atan2( pt_k.y - pt_i.y, pt_k.x - pt_i.x );
p = pt_i.clone().addSelf( v_hat );
q = pt_i.clone().addSelf( w_hat );
if ( anglea > angleb ) {
if ( p.equals( q ) ) {
angleb += Math.PI * 2;
//console.log("Warning: lines are straight");
return w_hat;
}
// var anglea = Math.atan2(pt_i.y - pt_j.y, pt_i.x - pt_j.x);
// var angleb = Math.atan2(pt_i.y - pt_k.y, pt_i.x - pt_k.x);
// Points from j, k. helps prevents points cross overover most of the time
// console.log('>?', anglea > angleb);
//
// if ( anglea < angleb) {
// angleb += Math.PI*2;
// }
p = pt_j.clone().addSelf( v_hat );
q = pt_k.clone().addSelf( w_hat );
v_dot_w_hat = v.dot( w_hat );
q_sub_p_dot_w_hat = q.clone().subSelf( p ).dot( w_hat );
//x = Math.cos(anglea) + Math.cos(angleb);
//y = Math.sin(anglea) + Math.sin(angleb);
//anglec = Math.atan2(y,x);
// We should not reach these conditions
anglec = ( anglea + angleb ) / 2;
if ( v_dot_w_hat == 0 ) {
//console.log('angle1', anglea * RAD_TO_DEGREES,'angle2', angleb * RAD_TO_DEGREES, 'anglec', anglec *RAD_TO_DEGREES);
console.log( "Either infinite or no solutions!" );
var x = - Math.cos( anglec );
var y = - Math.sin( anglec );
if (q_sub_p_dot_w_hat == 0 ) {
var vec = new THREE.Vector2( x, y ).normalize();
console.log( "Its finite solutions." );
return vec;
} else {
console.log( "Too bad, no solutions." );
}
}
s = q_sub_p_dot_w_hat / v_dot_w_hat;
if ( s < 0) {
// in case of emergecy, revert to algorithm 1.
// console.log("opps");
return getBevelVec1( pt_i, pt_j, pt_k );
}
intersection = v.clone().multiplyScalar( s ).addSelf( p );
return intersection.subSelf( pt_i ); // Don't normalize!, otherwise sharp corners become ugly
}
......@@ -344,8 +423,7 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
for ( i = 0; i < vlen; i ++ ) {
//vert = vertices[ i ];
vert = scalePt2( vertices[ i ], verticesMovements[ i ], bs );
vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
if ( !extrudeByPath ) {
......@@ -368,8 +446,7 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
for ( i = 0; i < vlen; i ++ ) {
//vert = vertices[ i ];
vert = scalePt2(vertices[ i ], verticesMovements[i], bs);
vert = bevelEnabled ? scalePt2(vertices[ i ], verticesMovements[i], bs) : vertices[ i ];
if ( !extrudeByPath ) {
......@@ -400,9 +477,9 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
for ( i = 0, il = contour.length; i < il; i ++ ) {
vert = scalePt2(contour[i], contourMovements[i], bs);
vert = scalePt2(contour[ i ], contourMovements[ i ], bs );
//vert = scalePt( contour[ i ], contourCentroid, bs, false );
v( vert.x, vert.y, amount + z);
v( vert.x, vert.y, amount + z );
}
......@@ -416,7 +493,7 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
for ( i = 0, il = ahole.length; i < il; i++ ) {
//vert = scalePt( ahole[ i ], holesCentroids[h], bs, true );
vert = scalePt2(ahole[i], oneHoleMovements[i], bs);
vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
if ( !extrudeByPath ) {
......@@ -424,7 +501,7 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
} else {
v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x +z );
v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z );
}
......@@ -541,11 +618,14 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
}
// UVs to be added
// How can we create UVs on this?
this.computeCentroids();
this.computeFaceNormals();
//this.computeVertexNormals();
//console.log("took", (Date.now()- startTime) );
function v( x, y, z ) {
scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
......
......@@ -149,21 +149,6 @@ THREE.Path.prototype.arc = function ( aX, aY, aRadius,
};
/*
// FUTURE ENHANCEMENTS
example usage?
Path.addExprFunc('sineCurveTo', sineCurveGetPtFunction)
Path.sineCurveTo(x,y, amptitude);
sineCurve.getPoint(t);
return sine(disnt) * ampt
// Create a new func eg. sin (theta) x
THREE.Path.prototype.addExprFunc = function(exprName, func) {
};
*/
THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
......@@ -384,7 +369,10 @@ THREE.Path.prototype.getPoints = function( divisions, closedPath ) {
};
THREE.Path.prototype.getMinAndMax = function () {
// Returns min and max coordinates, as well as centroid
THREE.Path.prototype.getBoundingBox = function () {
var points = this.getPoints();
......@@ -394,7 +382,9 @@ THREE.Path.prototype.getMinAndMax = function () {
maxX = maxY = Number.NEGATIVE_INFINITY;
minX = minY = Number.POSITIVE_INFINITY;
var p, i, il;
var p, i, il, sum;
sum = new THREE.Vector2();
for ( i = 0, il = points.length; i < il; i ++ ) {
......@@ -406,16 +396,17 @@ THREE.Path.prototype.getMinAndMax = function () {
if ( p.y > maxY ) maxY = p.y;
else if ( p.y < maxY ) minY = p.y;
}
sum.addSelf( p.x, p.y );
// TODO Include CG or find mid-pt?
}
return {
minX: minX,
minY: minY,
maxX: maxX,
maxY: maxY
maxY: maxY,
centroid: sum.divideScalar( il )
};
......@@ -534,7 +525,7 @@ THREE.Path.prototype.transform = function( path ) {
console.log( path.cacheArcLengths() );
var thisBounds = this.getMinAndMax();
var thisBounds = this.getBoundingBox();
var oldPts = this.getPoints();
var i, il, p, oldX, oldY, xNorm;
......@@ -605,7 +596,7 @@ THREE.Path.prototype.nltransform = function( a, b, c, d, e, f ) {
THREE.Path.prototype.debug = function( canvas ) {
var bounds = this.getMinAndMax();
var bounds = this.getBoundingBox();
if ( !canvas ) {
......
......@@ -136,7 +136,8 @@ THREE.Shape.Utils = {
// Find the shortest pair of pts between shape and hole
// Note: Actually, I'm not sure now if we could optimize this to be faster than O(m*n)
// But one thing is that we could speed this up by not running square roots on the pts differences
// Using distanceToSquared() intead of distanceTo() should speed a little
// since running square roots operations are reduced.
// http://en.wikipedia.org/wiki/Closest_pair_of_points
// http://stackoverflow.com/questions/1602164/shortest-distance-between-points-algorithm
......@@ -148,7 +149,7 @@ THREE.Shape.Utils = {
for ( p = 0; p < shape.length; p++ ) {
pts2 = shape[ p ];
d = pts1.distanceTo( pts2 );
d = pts1.distanceToSquared( pts2 );
dist.push( d );
if ( d < shortest ) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册