Skip to content

Instantly share code, notes, and snippets.

@zz85
Created July 5, 2011 16:26
Show Gist options
  • Save zz85/1065179 to your computer and use it in GitHub Desktop.
Save zz85/1065179 to your computer and use it in GitHub Desktop.
Prototype Path & Shape & Extrude Geometry Class
/**
* @author zz85 / http://www.lab4games.net/zz85/blog
* Creates free form path.
**/
THREE.Path = function (path) {
this.path = path || [];
};
var ACTIONS = {
MOVE_TO: 'moveTo',
LINE_TO: 'lineTo',
QUADRATIC_CURVE_TO: 'quadraticCurveTo', //BEZIER CURVE
CUBIC_CURVE_TO: 'bezierCurveTo', //BEZIER CURVE
CSPLINE_TO: 'cSplineTo' // TODO cardinal splines
};
/* create path using straight lines to connect all points */
THREE.Path.prototype.fromPoints = function(vectors) {
this.moveTo(vectors[0].x, vectors[0].y);w
for (var v in vectors) {
this.lineTo(vectors[v].x, vectors[v].y);
};
};
THREE.Path.prototype.moveTo = function(x,y) {
var args = Array.prototype.slice.call(arguments);
this.path.push({action:ACTIONS.MOVE_TO, args:args});
};
THREE.Path.prototype.lineTo = function(x,y) {
var args = Array.prototype.slice.call(arguments);
this.path.push({action:ACTIONS.LINE_TO, args:args});
};
THREE.Path.prototype.quadraticCurveTo = function(aCPx, aCPy, aX, aY) {
var args = Array.prototype.slice.call(arguments);
this.path.push({action:ACTIONS.QUADRATIC_CURVE_TO, args:args});
};
THREE.Path.prototype.bezierCurveTo = function(aCP1x, aCP1y,
aCP2x, aCP2y,
aX, aY) {
var args = Array.prototype.slice.call(arguments);
this.path.push({action:ACTIONS.CUBIC_CURVE_TO, args:args});
};
/* Return an array of vectors based on contour of the path */
THREE.Path.prototype.getPoints = function(divisions) {
var pts = [];
var x,o, args;
for (x in this.path) {
o = this.path[x];
args = o.args;
switch( action = o.action ) {
case ACTIONS.MOVE_TO:
pts.push( new THREE.Vector2( args[0], args[1] ) );
break;
case ACTIONS.LINE_TO:
pts.push( new THREE.Vector2( args[0], args[1] ) );
break;
case ACTIONS.QUADRATIC_CURVE_TO:
cpx = args[0];
cpy = args[1];
cpx1 = args[2];
cpy1 = args[3];
laste = pts[ pts.length - 1 ];
if ( laste ) {
cpx0 = laste.x;
cpy0 = laste.y;
for ( i2 = 1; i2 <= divisions; i2++ ) {
// TODO use LOD for divions
var t = i2 / divisions;
var tx = THREE.FontUtils.b2( t, cpx0, cpx1, cpx );
var ty = THREE.FontUtils.b2( t, cpy0, cpy1, cpy );
pts.push( new THREE.Vector2( tx, ty ) );
}
}
break;
case CUBIC_CURVE_TO:
cpx = args[0];
cpy = args[1];
cpx1 = args[2];
cpy1 = args[3];
cpx2 = args[4];
cpy2 = args[5];
laste = pts[ pts.length - 1 ];
if ( laste ) {
cpx0 = laste.x;
cpy0 = laste.y;
for ( i2 = 1; i2 <= divisions; i2++ ) {
var t = i2 / divisions;
var tx = THREE.FontUtils.b3( t, cpx0, cpx1, cpx2, cpx );
var ty = THREE.FontUtils.b3( t, cpy0, cpy1, cpy2, cpy );
pts.push( new THREE.Vector2( tx, ty ) );
}
}
break;
}
}
return pts;
};
/* Draws this path onto a 2d canvas easily */
THREE.Path.prototype.debug = function(canvas) {
// JUST A STUB
// debugPath
// debugPoints
};
/* Defines a 2d shape plane using paths */
THREE.Shape = function ( ) {
THREE.Path.call( this );
this.holes = [];
};
THREE.Shape.prototype = new THREE.Path();
THREE.Shape.prototype.constructor = THREE.Path;
/* Returns vertices of triangulated faces */
THREE.Shape.prototype.triangulate = function() {
// JUST A STUB
};
/* Convienence Method to return ExtrudeGeometry */
THREE.Shape.prototype.extrude = function() {
// JUST A STUB
// var extruded = new THREE.ExtrudeGeometry(this, args);
// return extruded;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment