Browse Source

ParametricGeometry: Fix examples

Mugen87 7 years ago
parent
commit
644d350e5a

+ 14 - 10
examples/js/Cloth.js

@@ -46,15 +46,13 @@ var lastTime;
 
 
 function plane( width, height ) {
 function plane( width, height ) {
 
 
-	return function ( u, v, optionalTarget ) {
-
-		var result = optionalTarget || new THREE.Vector3();
+	return function ( u, v, target ) {
 
 
 		var x = ( u - 0.5 ) * width;
 		var x = ( u - 0.5 ) * width;
 		var y = ( v + 0.5 ) * height;
 		var y = ( v + 0.5 ) * height;
 		var z = 0;
 		var z = 0;
 
 
-		return result.set( x, y, z );
+		return target.set( x, y, z );
 
 
 	};
 	};
 
 
@@ -62,20 +60,26 @@ function plane( width, height ) {
 
 
 function Particle( x, y, z, mass ) {
 function Particle( x, y, z, mass ) {
 
 
-	this.position = clothFunction( x, y ); // position
-	this.previous = clothFunction( x, y ); // previous
-	this.original = clothFunction( x, y );
+	this.position = new THREE.Vector3();
+	this.previous = new THREE.Vector3();
+	this.original = new THREE.Vector3();
 	this.a = new THREE.Vector3( 0, 0, 0 ); // acceleration
 	this.a = new THREE.Vector3( 0, 0, 0 ); // acceleration
 	this.mass = mass;
 	this.mass = mass;
 	this.invMass = 1 / mass;
 	this.invMass = 1 / mass;
 	this.tmp = new THREE.Vector3();
 	this.tmp = new THREE.Vector3();
 	this.tmp2 = new THREE.Vector3();
 	this.tmp2 = new THREE.Vector3();
 
 
+	// init
+
+	clothFunction( x, y, this.position ); // position
+	clothFunction( x, y, this.previous ); // previous
+	clothFunction( x, y, this.original );
+
 }
 }
 
 
 // Force -> Acceleration
 // Force -> Acceleration
 
 
-Particle.prototype.addForce = function( force ) {
+Particle.prototype.addForce = function ( force ) {
 
 
 	this.a.add(
 	this.a.add(
 		this.tmp2.copy( force ).multiplyScalar( this.invMass )
 		this.tmp2.copy( force ).multiplyScalar( this.invMass )
@@ -86,7 +90,7 @@ Particle.prototype.addForce = function( force ) {
 
 
 // Performs Verlet integration
 // Performs Verlet integration
 
 
-Particle.prototype.integrate = function( timesq ) {
+Particle.prototype.integrate = function ( timesq ) {
 
 
 	var newPos = this.tmp.subVectors( this.position, this.previous );
 	var newPos = this.tmp.subVectors( this.position, this.previous );
 	newPos.multiplyScalar( DRAG ).add( this.position );
 	newPos.multiplyScalar( DRAG ).add( this.position );
@@ -280,7 +284,7 @@ function simulate( time ) {
 
 
 	// Ball Constraints
 	// Ball Constraints
 
 
-	ballPosition.z = - Math.sin( Date.now() / 600 ) * 90 ; //+ 40;
+	ballPosition.z = - Math.sin( Date.now() / 600 ) * 90; //+ 40;
 	ballPosition.x = Math.cos( Date.now() / 400 ) * 70;
 	ballPosition.x = Math.cos( Date.now() / 400 ) * 70;
 
 
 	if ( sphere.visible ) {
 	if ( sphere.visible ) {

+ 2 - 4
examples/js/curves/NURBSSurface.js

@@ -42,14 +42,12 @@ THREE.NURBSSurface.prototype = {
 
 
 	constructor: THREE.NURBSSurface,
 	constructor: THREE.NURBSSurface,
 
 
-	getPoint: function ( t1, t2 ) {
+	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 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
 		var v = this.knots2[ 0 ] + t2 * ( this.knots2[ this.knots2.length - 1 ] - this.knots2[ 0 ] ); // linear mapping t2->u
 
 
-		return THREE.NURBSUtils.calcSurfacePoint( this.degree1, this.degree2, this.knots1, this.knots2, this.controlPoints, u, v );
+		return THREE.NURBSUtils.calcSurfacePoint( this.degree1, this.degree2, this.knots1, this.knots2, this.controlPoints, u, v, target );
 
 
 	}
 	}
 };
 };
-
-

+ 11 - 14
examples/js/curves/NURBSUtils.js

@@ -19,7 +19,7 @@ THREE.NURBSUtils = {
 	p : degree
 	p : degree
 	u : parametric value
 	u : parametric value
 	U : knot vector
 	U : knot vector
-	
+
 	returns the span
 	returns the span
 	*/
 	*/
 	findSpan: function( p,  u,  U ) {
 	findSpan: function( p,  u,  U ) {
@@ -43,7 +43,7 @@ THREE.NURBSUtils = {
 		var mid = Math.floor( ( low + high ) / 2 );
 		var mid = Math.floor( ( low + high ) / 2 );
 
 
 		while ( u < U[ mid ] || u >= U[ mid + 1 ] ) {
 		while ( u < U[ mid ] || u >= U[ mid + 1 ] ) {
-		  
+
 			if ( u < U[ mid ] ) {
 			if ( u < U[ mid ] ) {
 
 
 				high = mid;
 				high = mid;
@@ -61,16 +61,16 @@ THREE.NURBSUtils = {
 		return mid;
 		return mid;
 
 
 	},
 	},
-    
-		
+
+
 	/*
 	/*
 	Calculate basis functions. See The NURBS Book, page 70, algorithm A2.2
 	Calculate basis functions. See The NURBS Book, page 70, algorithm A2.2
-   
+
 	span : span in which u lies
 	span : span in which u lies
 	u    : parametric point
 	u    : parametric point
 	p    : degree
 	p    : degree
 	U    : knot vector
 	U    : knot vector
-	
+
 	returns array[p+1] with basis functions values.
 	returns array[p+1] with basis functions values.
 	*/
 	*/
 	calcBasisFunctions: function( span, u, p, U ) {
 	calcBasisFunctions: function( span, u, p, U ) {
@@ -81,7 +81,7 @@ THREE.NURBSUtils = {
 		N[ 0 ] = 1.0;
 		N[ 0 ] = 1.0;
 
 
 		for ( var j = 1; j <= p; ++ j ) {
 		for ( var j = 1; j <= p; ++ j ) {
-	   
+
 			left[ j ] = u - U[ span + 1 - j ];
 			left[ j ] = u - U[ span + 1 - j ];
 			right[ j ] = U[ span + j ] - u;
 			right[ j ] = U[ span + j ] - u;
 
 
@@ -108,7 +108,7 @@ THREE.NURBSUtils = {
 
 
 	/*
 	/*
 	Calculate B-Spline curve points. See The NURBS Book, page 82, algorithm A3.1.
 	Calculate B-Spline curve points. See The NURBS Book, page 82, algorithm A3.1.
- 
+
 	p : degree of B-Spline
 	p : degree of B-Spline
 	U : knot vector
 	U : knot vector
 	P : control points (x, y, z, w)
 	P : control points (x, y, z, w)
@@ -422,7 +422,7 @@ THREE.NURBSUtils = {
 
 
 	/*
 	/*
 	Calculate rational B-Spline surface point. See The NURBS Book, page 134, algorithm A4.3.
 	Calculate rational B-Spline surface point. See The NURBS Book, page 134, algorithm A4.3.
- 
+
 	p1, p2 : degrees of B-Spline surface
 	p1, p2 : degrees of B-Spline surface
 	U1, U2 : knot vectors
 	U1, U2 : knot vectors
 	P      : control points (x, y, z, w)
 	P      : control points (x, y, z, w)
@@ -430,7 +430,7 @@ THREE.NURBSUtils = {
 
 
 	returns point for given (u, v)
 	returns point for given (u, v)
 	*/
 	*/
-	calcSurfacePoint: function( p, q, U, V, P, u, v ) {
+	calcSurfacePoint: function ( p, q, U, V, P, u, v, target ) {
 
 
 		var uspan = this.findSpan( p, u, U );
 		var uspan = this.findSpan( p, u, U );
 		var vspan = this.findSpan( q, v, V );
 		var vspan = this.findSpan( q, v, V );
@@ -462,11 +462,8 @@ THREE.NURBSUtils = {
 		}
 		}
 
 
 		Sw.divideScalar( Sw.w );
 		Sw.divideScalar( Sw.w );
-		return new THREE.Vector3( Sw.x, Sw.y, Sw.z );
+		return target.set( Sw.x, Sw.y, Sw.z );
 
 
 	}
 	}
 
 
 };
 };
-
-
-

+ 3 - 3
examples/webgl_geometry_nurbs.html

@@ -68,7 +68,7 @@
 
 
 				scene = new THREE.Scene();
 				scene = new THREE.Scene();
 				scene.background = new THREE.Color( 0xf0f0f0 );
 				scene.background = new THREE.Color( 0xf0f0f0 );
-				
+
 				scene.add( new THREE.AmbientLight( 0x808080 ) );
 				scene.add( new THREE.AmbientLight( 0x808080 ) );
 
 
 				var light = new THREE.DirectionalLight( 0xffffff, 1 );
 				var light = new THREE.DirectionalLight( 0xffffff, 1 );
@@ -159,9 +159,9 @@
 				map.wrapS = map.wrapT = THREE.RepeatWrapping;
 				map.wrapS = map.wrapT = THREE.RepeatWrapping;
 				map.anisotropy = 16;
 				map.anisotropy = 16;
 
 
-				function getSurfacePoint( u, v ) {
+				function getSurfacePoint( u, v, target ) {
 
 
-					return nurbsSurface.getPoint( u, v );
+					return nurbsSurface.getPoint( u, v, target );
 
 
 				}
 				}