提交 bbdea5f4 编写于 作者: Z zz85

Fixed ugly sharp corners with new bevel algorithm

上级 54abb899
因为 它太大了无法显示 source diff 。你可以改为 查看blob
此差异已折叠。
......@@ -51,7 +51,7 @@ 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
......@@ -67,8 +67,8 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
if ( !bevelEnabled ) {
bevelSegments = 0;
bevelThickness = 0;
bevelSize = 0;
//bevelThickness = 0;
//bevelSize = 0;
}
......@@ -149,57 +149,60 @@ 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 ++ ) {
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 );
}
// We no longer need centroids
//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 );
return pt.clone().addSelf( vec.clone().multiplyScalar( size ) );
return vec.clone().multiplyScalar( size ).addSelf( pt );
}
......@@ -217,7 +220,11 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
function getBevelVec( pt_i, pt_j, pt_k ) {
// Algorithm 2
return getBevelVec2( pt_i, pt_j, pt_k );
}
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 );
......@@ -227,32 +234,80 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
}
// 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);
// console.log('>?', anglea > angleb);
//
// if ( anglea < angleb) {
// angleb += Math.PI*2;
// }
//x = Math.cos(anglea) + Math.cos(angleb);
//y = Math.sin(anglea) + Math.sin(angleb);
//anglec = Math.atan2(y,x);
anglec = ( anglea + angleb ) / 2;
//console.log('angle1', anglea * RAD_TO_DEGREES,'angle2', angleb * RAD_TO_DEGREES, 'anglec', anglec *RAD_TO_DEGREES);
var x = - Math.cos( anglec );
var y = - Math.sin( anglec );
var vec = new THREE.Vector2( x, y ).normalize();
var vec = new THREE.Vector2( x, y ); //.normalize();
return vec;
}
function getBevelVec2( pt_i, pt_j, pt_k ) {
var a, b, v, w, v_hat, w_hat,
p, q, v_dot_w_hat, q_sub_p_dot_w_hat,
s,intersection;
// define a as vector j->i
// define b as vectot k->i
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
v = a.normalize();
w = b.normalize();
// 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
// pts from i
p = pt_i.clone().addSelf(v_hat);
q = pt_i.clone().addSelf(w_hat);
if (p.equals(q)) {
//console.log("Warning: lines are straight");
return w_hat;
}
// Points from j, k. helps prevents points cross overover most of the time
p = pt_j.clone().addSelf(v_hat);
q = pt_k.clone().addSelf(w_hat);
// good reading for line-line intersection
//http://sputsoft.com/blog/2010/03/line-line-intersection.html
v_dot_w_hat = v.dot(w_hat);
q_sub_p_dot_w_hat = q.clone().subSelf(p).dot(w_hat);
// We shoud not get these
if (v_dot_w_hat ==0 ) {
console.log("Either infinite or no solutions!");
if (q_sub_p_dot_w_hat ==0 ) {
console.log("Its finite solutions.");
} 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); // .normalize() Don't normalize!
}
var contourMovements = [];
......@@ -344,8 +399,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,9 +422,8 @@ 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 ) {
v( vert.x, vert.y, amount / steps * s );
......@@ -545,6 +598,8 @@ THREE.ExtrudeGeometry.prototype.addShape = function( shape, options ) {
this.computeCentroids();
this.computeFaceNormals();
//this.computeVertexNormals();
//console.log("took", (Date.now()- startTime) );
function v( x, y, z ) {
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册