提交 f6f47a29 编写于 作者: M Mugen87

JSM: Added module and TS files for FBXLoader and NURBS.

上级 7923c2ee
......@@ -91,6 +91,13 @@
<li>TransformControls</li>
</ul>
</li>
<li>curves
<ul>
<li>NURBSCurve</li>
<li>NURBSSurface</li>
<li>NURBSUtils</li>
</ul>
</li>
<li>exporters
<ul>
<li>ColladaExporter</li>
......@@ -106,6 +113,7 @@
<ul>
<li>BVHLoader</li>
<li>ColladaLoader</li>
<li>FBXLoader</li>
<li>GLTFLoader</li>
<li>MTLLoader</li>
<li>OBJLoader</li>
......
......@@ -22,7 +22,7 @@ THREE.NURBSUtils = {
returns the span
*/
findSpan: function( p, u, U ) {
findSpan: function ( p, u, U ) {
var n = U.length - p - 1;
......@@ -73,7 +73,7 @@ THREE.NURBSUtils = {
returns array[p+1] with basis functions values.
*/
calcBasisFunctions: function( span, u, p, U ) {
calcBasisFunctions: function ( span, u, p, U ) {
var N = [];
var left = [];
......@@ -116,7 +116,7 @@ THREE.NURBSUtils = {
returns point for given u
*/
calcBSplinePoint: function( p, U, P, u ) {
calcBSplinePoint: function ( p, U, P, u ) {
var span = this.findSpan( p, u, U );
var N = this.calcBasisFunctions( span, u, p, U );
......@@ -150,7 +150,7 @@ THREE.NURBSUtils = {
returns array[n+1][p+1] with basis functions derivatives
*/
calcBasisFunctionDerivatives: function( span, u, p, n, U ) {
calcBasisFunctionDerivatives: function ( span, u, p, n, U ) {
var zeroArr = [];
for ( var i = 0; i <= p; ++ i )
......@@ -225,7 +225,7 @@ THREE.NURBSUtils = {
}
var j1 = ( rk >= - 1 ) ? 1 : - rk;
var j2 = ( r - 1 <= pk ) ? k - 1 : p - r;
var j2 = ( r - 1 <= pk ) ? k - 1 : p - r;
for ( var j = j1; j <= j2; ++ j ) {
......@@ -280,7 +280,7 @@ THREE.NURBSUtils = {
returns array[d+1] with derivatives
*/
calcBSplineDerivatives: function( p, U, P, u, nd ) {
calcBSplineDerivatives: function ( p, U, P, u, nd ) {
var du = nd < p ? nd : p;
var CK = [];
......@@ -330,7 +330,7 @@ THREE.NURBSUtils = {
returns k!/(i!(k-i)!)
*/
calcKoverI: function( k, i ) {
calcKoverI: function ( k, i ) {
var nom = 1;
......@@ -412,7 +412,7 @@ THREE.NURBSUtils = {
returns array with derivatives.
*/
calcNURBSDerivatives: function( p, U, P, u, nd ) {
calcNURBSDerivatives: function ( p, U, P, u, nd ) {
var Pders = this.calcBSplineDerivatives( p, U, P, u, nd );
return this.calcRationalCurveDerivatives( Pders );
......
import {
Curve,
Vector2,
Vector3,
Vector4
} from '../../../src/Three';
export class NURBSCurve extends Curve<Vector3> {
constructor(degree: number, knots: number[], controlPoints: Vector2[] | Vector3[] | Vector4[], startKnot: number, endKnot: number);
}
/**
* @author renej
* NURBS curve object
*
* Derives from Curve, overriding getPoint and getTangent.
*
* Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
*
**/
import {
Curve,
Vector3,
Vector4
} from "../../../build/three.module.js";
import { NURBSUtils } from "../curves/NURBSUtils.js";
/**************************************************************
* NURBS curve
**************************************************************/
var NURBSCurve = function ( degree, knots /* array of reals */, controlPoints /* array of Vector(2|3|4) */, startKnot /* index in knots */, endKnot /* index in knots */ ) {
Curve.call( this );
this.degree = degree;
this.knots = knots;
this.controlPoints = [];
// Used by periodic NURBS to remove hidden spans
this.startKnot = startKnot || 0;
this.endKnot = endKnot || ( this.knots.length - 1 );
for ( var i = 0; i < controlPoints.length; ++ i ) {
// ensure Vector4 for control points
var point = controlPoints[ i ];
this.controlPoints[ i ] = new Vector4( point.x, point.y, point.z, point.w );
}
};
NURBSCurve.prototype = Object.create( Curve.prototype );
NURBSCurve.prototype.constructor = NURBSCurve;
NURBSCurve.prototype.getPoint = function ( t ) {
var u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
// following results in (wx, wy, wz, w) homogeneous point
var hpoint = NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
if ( hpoint.w != 1.0 ) {
// project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
hpoint.divideScalar( hpoint.w );
}
return new Vector3( hpoint.x, hpoint.y, hpoint.z );
};
NURBSCurve.prototype.getTangent = function ( t ) {
var u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
var ders = NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
var tangent = ders[ 1 ].clone();
tangent.normalize();
return tangent;
};
export { NURBSCurve };
import {
Vector2,
Vector3,
Vector4
} from '../../../src/Three';
export class NURBSSurface {
constructor(degree1: number, degree2: number, knots1: number[], knots2: number[], controlPoints: Vector2[][] | Vector3[][] | Vector4[][]);
getPoint(t1: number, t2: number, target: Vector3): void;
}
/**
* @author renej
* NURBS surface object
*
* Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
*
**/
import {
Vector4
} from "../../../build/three.module.js";
import { NURBSUtils } from "../curves/NURBSUtils.js";
/**************************************************************
* NURBS surface
**************************************************************/
var NURBSSurface = function ( degree1, degree2, knots1, knots2 /* arrays of reals */, controlPoints /* array^2 of Vector(2|3|4) */ ) {
this.degree1 = degree1;
this.degree2 = degree2;
this.knots1 = knots1;
this.knots2 = knots2;
this.controlPoints = [];
var len1 = knots1.length - degree1 - 1;
var len2 = knots2.length - degree2 - 1;
// ensure Vector4 for control points
for ( var i = 0; i < len1; ++ i ) {
this.controlPoints[ i ] = [];
for ( var j = 0; j < len2; ++ j ) {
var point = controlPoints[ i ][ j ];
this.controlPoints[ i ][ j ] = new Vector4( point.x, point.y, point.z, point.w );
}
}
};
NURBSSurface.prototype = {
constructor: NURBSSurface,
getPoint: function ( t1, t2, target ) {
var u = this.knots1[ 0 ] + t1 * ( this.knots1[ this.knots1.length - 1 ] - this.knots1[ 0 ] ); // linear mapping t1->u
var v = this.knots2[ 0 ] + t2 * ( this.knots2[ this.knots2.length - 1 ] - this.knots2[ 0 ] ); // linear mapping t2->u
NURBSUtils.calcSurfacePoint( this.degree1, this.degree2, this.knots1, this.knots2, this.controlPoints, u, v, target );
}
};
export { NURBSSurface };
import {
Vector3,
Vector4
} from '../../../src/Three';
export namespace NURBSUtils {
export function findSpan(p: number, u: number, U: number[]): number;
export function calcBasisFunctions(span: number, u: number, p: number, U: number[]): number[];
export function calcBSplinePoint(p: number, U: number[], P: Vector4[], u: number): Vector4;
export function calcBasisFunctionDerivatives(span: number,u: number, p: number, n: number, U: number[]): number[][];
export function calcBSplineDerivatives(p: number, U: number[], P: Vector4[], u: number, nd: number): Vector4[];
export function calcKoverI(k: number, i: number): number;
export function calcRationalCurveDerivatives(Pders: Vector4[]): Vector3[];
export function calcNURBSDerivatives(p: number, U: number[], P: Vector4[], u: number, nd: number): Vector3[];
export function calcSurfacePoint(p: number, q: number, U: number[], V: number[], P: Vector4[], u: number, v: number, target: Vector3): Vector3;
}
/**
* @author renej
* NURBS utils
*
* See NURBSCurve and NURBSSurface.
*
**/
import {
Vector3,
Vector4
} from "../../../build/three.module.js";
/**************************************************************
* NURBS Utils
**************************************************************/
var NURBSUtils = {
/*
Finds knot vector span.
p : degree
u : parametric value
U : knot vector
returns the span
*/
findSpan: function ( p, u, U ) {
var n = U.length - p - 1;
if ( u >= U[ n ] ) {
return n - 1;
}
if ( u <= U[ p ] ) {
return p;
}
var low = p;
var high = n;
var mid = Math.floor( ( low + high ) / 2 );
while ( u < U[ mid ] || u >= U[ mid + 1 ] ) {
if ( u < U[ mid ] ) {
high = mid;
} else {
low = mid;
}
mid = Math.floor( ( low + high ) / 2 );
}
return mid;
},
/*
Calculate basis functions. See The NURBS Book, page 70, algorithm A2.2
span : span in which u lies
u : parametric point
p : degree
U : knot vector
returns array[p+1] with basis functions values.
*/
calcBasisFunctions: function ( span, u, p, U ) {
var N = [];
var left = [];
var right = [];
N[ 0 ] = 1.0;
for ( var j = 1; j <= p; ++ j ) {
left[ j ] = u - U[ span + 1 - j ];
right[ j ] = U[ span + j ] - u;
var saved = 0.0;
for ( var r = 0; r < j; ++ r ) {
var rv = right[ r + 1 ];
var lv = left[ j - r ];
var temp = N[ r ] / ( rv + lv );
N[ r ] = saved + rv * temp;
saved = lv * temp;
}
N[ j ] = saved;
}
return N;
},
/*
Calculate B-Spline curve points. See The NURBS Book, page 82, algorithm A3.1.
p : degree of B-Spline
U : knot vector
P : control points (x, y, z, w)
u : parametric point
returns point for given u
*/
calcBSplinePoint: function ( p, U, P, u ) {
var span = this.findSpan( p, u, U );
var N = this.calcBasisFunctions( span, u, p, U );
var C = new Vector4( 0, 0, 0, 0 );
for ( var j = 0; j <= p; ++ j ) {
var point = P[ span - p + j ];
var Nj = N[ j ];
var wNj = point.w * Nj;
C.x += point.x * wNj;
C.y += point.y * wNj;
C.z += point.z * wNj;
C.w += point.w * Nj;
}
return C;
},
/*
Calculate basis functions derivatives. See The NURBS Book, page 72, algorithm A2.3.
span : span in which u lies
u : parametric point
p : degree
n : number of derivatives to calculate
U : knot vector
returns array[n+1][p+1] with basis functions derivatives
*/
calcBasisFunctionDerivatives: function ( span, u, p, n, U ) {
var zeroArr = [];
for ( var i = 0; i <= p; ++ i )
zeroArr[ i ] = 0.0;
var ders = [];
for ( var i = 0; i <= n; ++ i )
ders[ i ] = zeroArr.slice( 0 );
var ndu = [];
for ( var i = 0; i <= p; ++ i )
ndu[ i ] = zeroArr.slice( 0 );
ndu[ 0 ][ 0 ] = 1.0;
var left = zeroArr.slice( 0 );
var right = zeroArr.slice( 0 );
for ( var j = 1; j <= p; ++ j ) {
left[ j ] = u - U[ span + 1 - j ];
right[ j ] = U[ span + j ] - u;
var saved = 0.0;
for ( var r = 0; r < j; ++ r ) {
var rv = right[ r + 1 ];
var lv = left[ j - r ];
ndu[ j ][ r ] = rv + lv;
var temp = ndu[ r ][ j - 1 ] / ndu[ j ][ r ];
ndu[ r ][ j ] = saved + rv * temp;
saved = lv * temp;
}
ndu[ j ][ j ] = saved;
}
for ( var j = 0; j <= p; ++ j ) {
ders[ 0 ][ j ] = ndu[ j ][ p ];
}
for ( var r = 0; r <= p; ++ r ) {
var s1 = 0;
var s2 = 1;
var a = [];
for ( var i = 0; i <= p; ++ i ) {
a[ i ] = zeroArr.slice( 0 );
}
a[ 0 ][ 0 ] = 1.0;
for ( var k = 1; k <= n; ++ k ) {
var d = 0.0;
var rk = r - k;
var pk = p - k;
if ( r >= k ) {
a[ s2 ][ 0 ] = a[ s1 ][ 0 ] / ndu[ pk + 1 ][ rk ];
d = a[ s2 ][ 0 ] * ndu[ rk ][ pk ];
}
var j1 = ( rk >= - 1 ) ? 1 : - rk;
var j2 = ( r - 1 <= pk ) ? k - 1 : p - r;
for ( var j = j1; j <= j2; ++ j ) {
a[ s2 ][ j ] = ( a[ s1 ][ j ] - a[ s1 ][ j - 1 ] ) / ndu[ pk + 1 ][ rk + j ];
d += a[ s2 ][ j ] * ndu[ rk + j ][ pk ];
}
if ( r <= pk ) {
a[ s2 ][ k ] = - a[ s1 ][ k - 1 ] / ndu[ pk + 1 ][ r ];
d += a[ s2 ][ k ] * ndu[ r ][ pk ];
}
ders[ k ][ r ] = d;
var j = s1;
s1 = s2;
s2 = j;
}
}
var r = p;
for ( var k = 1; k <= n; ++ k ) {
for ( var j = 0; j <= p; ++ j ) {
ders[ k ][ j ] *= r;
}
r *= p - k;
}
return ders;
},
/*
Calculate derivatives of a B-Spline. See The NURBS Book, page 93, algorithm A3.2.
p : degree
U : knot vector
P : control points
u : Parametric points
nd : number of derivatives
returns array[d+1] with derivatives
*/
calcBSplineDerivatives: function ( p, U, P, u, nd ) {
var du = nd < p ? nd : p;
var CK = [];
var span = this.findSpan( p, u, U );
var nders = this.calcBasisFunctionDerivatives( span, u, p, du, U );
var Pw = [];
for ( var i = 0; i < P.length; ++ i ) {
var point = P[ i ].clone();
var w = point.w;
point.x *= w;
point.y *= w;
point.z *= w;
Pw[ i ] = point;
}
for ( var k = 0; k <= du; ++ k ) {
var point = Pw[ span - p ].clone().multiplyScalar( nders[ k ][ 0 ] );
for ( var j = 1; j <= p; ++ j ) {
point.add( Pw[ span - p + j ].clone().multiplyScalar( nders[ k ][ j ] ) );
}
CK[ k ] = point;
}
for ( var k = du + 1; k <= nd + 1; ++ k ) {
CK[ k ] = new Vector4( 0, 0, 0 );
}
return CK;
},
/*
Calculate "K over I"
returns k!/(i!(k-i)!)
*/
calcKoverI: function ( k, i ) {
var nom = 1;
for ( var j = 2; j <= k; ++ j ) {
nom *= j;
}
var denom = 1;
for ( var j = 2; j <= i; ++ j ) {
denom *= j;
}
for ( var j = 2; j <= k - i; ++ j ) {
denom *= j;
}
return nom / denom;
},
/*
Calculate derivatives (0-nd) of rational curve. See The NURBS Book, page 127, algorithm A4.2.
Pders : result of function calcBSplineDerivatives
returns array with derivatives for rational curve.
*/
calcRationalCurveDerivatives: function ( Pders ) {
var nd = Pders.length;
var Aders = [];
var wders = [];
for ( var i = 0; i < nd; ++ i ) {
var point = Pders[ i ];
Aders[ i ] = new Vector3( point.x, point.y, point.z );
wders[ i ] = point.w;
}
var CK = [];
for ( var k = 0; k < nd; ++ k ) {
var v = Aders[ k ].clone();
for ( var i = 1; i <= k; ++ i ) {
v.sub( CK[ k - i ].clone().multiplyScalar( this.calcKoverI( k, i ) * wders[ i ] ) );
}
CK[ k ] = v.divideScalar( wders[ 0 ] );
}
return CK;
},
/*
Calculate NURBS curve derivatives. See The NURBS Book, page 127, algorithm A4.2.
p : degree
U : knot vector
P : control points in homogeneous space
u : parametric points
nd : number of derivatives
returns array with derivatives.
*/
calcNURBSDerivatives: function ( p, U, P, u, nd ) {
var Pders = this.calcBSplineDerivatives( p, U, P, u, nd );
return this.calcRationalCurveDerivatives( Pders );
},
/*
Calculate rational B-Spline surface point. See The NURBS Book, page 134, algorithm A4.3.
p1, p2 : degrees of B-Spline surface
U1, U2 : knot vectors
P : control points (x, y, z, w)
u, v : parametric values
returns point for given (u, v)
*/
calcSurfacePoint: function ( p, q, U, V, P, u, v, target ) {
var uspan = this.findSpan( p, u, U );
var vspan = this.findSpan( q, v, V );
var Nu = this.calcBasisFunctions( uspan, u, p, U );
var Nv = this.calcBasisFunctions( vspan, v, q, V );
var temp = [];
for ( var l = 0; l <= q; ++ l ) {
temp[ l ] = new Vector4( 0, 0, 0, 0 );
for ( var k = 0; k <= p; ++ k ) {
var point = P[ uspan - p + k ][ vspan - q + l ].clone();
var w = point.w;
point.x *= w;
point.y *= w;
point.z *= w;
temp[ l ].add( point.multiplyScalar( Nu[ k ] ) );
}
}
var Sw = new Vector4( 0, 0, 0, 0 );
for ( var l = 0; l <= q; ++ l ) {
Sw.add( temp[ l ].multiplyScalar( Nv[ l ] ) );
}
Sw.divideScalar( Sw.w );
target.set( Sw.x, Sw.y, Sw.z );
}
};
export { NURBSUtils };
import {
Group,
LoadingManager
} from '../../../src/Three';
export class FBXLoader {
constructor(manager?: LoadingManager);
manager: LoadingManager;
crossOrigin: string;
path: string;
resourcePath: string;
load(url: string, onLoad: (object: Group) => void, onProgress?: (event: ProgressEvent) => void, onError?: (event: ErrorEvent) => void) : void;
setPath(path: string) : this;
setResourcePath(path: string) : this;
setCrossOrigin(value: string): this;
parse(FBXBuffer: ArrayBuffer | string, path: string) : Group;
}
此差异已折叠。
......@@ -21,6 +21,10 @@ var files = [
{ path: 'controls/TrackballControls.js', dependencies: [], ignoreList: [] },
{ path: 'controls/TransformControls.js', dependencies: [], ignoreList: [] },
{ path: 'curves/NURBSCurve.js', dependencies: [ { name: 'NURBSUtils', path: 'curves/NURBSUtils.js' } ], ignoreList: [] },
{ path: 'curves/NURBSSurface.js', dependencies: [ { name: 'NURBSUtils', path: 'curves/NURBSUtils.js' } ], ignoreList: [] },
{ path: 'curves/NURBSUtils.js', dependencies: [], ignoreList: [] },
{ path: 'exporters/GLTFExporter.js', dependencies: [], ignoreList: [ 'AnimationClip', 'Camera', 'Geometry', 'Material', 'Mesh', 'Object3D', 'RGBFormat', 'Scenes', 'ShaderMaterial', 'VertexColors' ] },
{ path: 'exporters/MMDExporter.js', dependencies: [], ignoreList: [] },
{ path: 'exporters/OBJExporter.js', dependencies: [], ignoreList: [] },
......@@ -30,10 +34,11 @@ var files = [
{ path: 'loaders/BVHLoader.js', dependencies: [], ignoreList: [ 'Bones' ] },
{ path: 'loaders/ColladaLoader.js', dependencies: [ { name: 'TGALoader', path: 'loaders/TGALoader.js' } ], ignoreList: [] },
{ path: 'loaders/PCDLoader.js', dependencies: [], ignoreList: [] },
{ path: 'loaders/FBXLoader.js', dependencies: [ { name: 'TGALoader', path: 'loaders/TGALoader.js' }, { name: 'NURBSCurve', path: 'curves/NURBSCurve.js' } ], ignoreList: [] },
{ path: 'loaders/GLTFLoader.js', dependencies: [], ignoreList: [ 'NoSide', 'Matrix2', 'DDSLoader' ] },
{ path: 'loaders/OBJLoader.js', dependencies: [], ignoreList: [] },
{ path: 'loaders/MTLLoader.js', dependencies: [], ignoreList: [ 'BackSide', 'DoubleSide', 'ClampToEdgeWrapping', 'MirroredRepeatWrapping' ] },
{ path: 'loaders/OBJLoader.js', dependencies: [], ignoreList: [] },
{ path: 'loaders/PCDLoader.js', dependencies: [], ignoreList: [] },
{ path: 'loaders/PLYLoader.js', dependencies: [], ignoreList: [ 'Mesh' ] },
{ path: 'loaders/STLLoader.js', dependencies: [], ignoreList: [ 'Mesh', 'MeshPhongMaterial', 'VertexColors' ] },
{ path: 'loaders/SVGLoader.js', dependencies: [], ignoreList: [] },
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册