Cloth.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Cloth Simulation using a relaxed constraints solver
  3. */
  4. // Suggested Readings
  5. // Advanced Character Physics by Thomas Jakobsen Character
  6. // http://freespace.virgin.net/hugo.elias/models/m_cloth.htm
  7. // http://en.wikipedia.org/wiki/Cloth_modeling
  8. // http://cg.alexandra.dk/tag/spring-mass-system/
  9. // Real-time Cloth Animation http://www.darwin3d.com/gamedev/articles/col0599.pdf
  10. var DAMPING = 0.03;
  11. var DRAG = 1 - DAMPING;
  12. var MASS = 0.1;
  13. var restDistance = 25;
  14. var xSegs = 10;
  15. var ySegs = 10;
  16. var clothFunction = plane( restDistance * xSegs, restDistance * ySegs );
  17. var cloth = new Cloth( xSegs, ySegs );
  18. var GRAVITY = 981 * 1.4;
  19. var gravity = new THREE.Vector3( 0, - GRAVITY, 0 ).multiplyScalar( MASS );
  20. var TIMESTEP = 18 / 1000;
  21. var TIMESTEP_SQ = TIMESTEP * TIMESTEP;
  22. var pins = [];
  23. var wind = true;
  24. var windStrength = 2;
  25. var windForce = new THREE.Vector3( 0, 0, 0 );
  26. var ballPosition = new THREE.Vector3( 0, - 45, 0 );
  27. var ballSize = 60; //40
  28. var tmpForce = new THREE.Vector3();
  29. var lastTime;
  30. function plane( width, height ) {
  31. return function ( u, v, optionalTarget ) {
  32. var result = optionalTarget || new THREE.Vector3();
  33. var x = ( u - 0.5 ) * width;
  34. var y = ( v + 0.5 ) * height;
  35. var z = 0;
  36. return result.set( x, y, z );
  37. };
  38. }
  39. function Particle( x, y, z, mass ) {
  40. this.position = clothFunction( x, y ); // position
  41. this.previous = clothFunction( x, y ); // previous
  42. this.original = clothFunction( x, y );
  43. this.a = new THREE.Vector3( 0, 0, 0 ); // acceleration
  44. this.mass = mass;
  45. this.invMass = 1 / mass;
  46. this.tmp = new THREE.Vector3();
  47. this.tmp2 = new THREE.Vector3();
  48. }
  49. // Force -> Acceleration
  50. Particle.prototype.addForce = function( force ) {
  51. this.a.add(
  52. this.tmp2.copy( force ).multiplyScalar( this.invMass )
  53. );
  54. };
  55. // Performs Verlet integration
  56. Particle.prototype.integrate = function( timesq ) {
  57. var newPos = this.tmp.subVectors( this.position, this.previous );
  58. newPos.multiplyScalar( DRAG ).add( this.position );
  59. newPos.add( this.a.multiplyScalar( timesq ) );
  60. this.tmp = this.previous;
  61. this.previous = this.position;
  62. this.position = newPos;
  63. this.a.set( 0, 0, 0 );
  64. };
  65. var diff = new THREE.Vector3();
  66. function satisfyConstraints( p1, p2, distance ) {
  67. diff.subVectors( p2.position, p1.position );
  68. var currentDist = diff.length();
  69. if ( currentDist === 0 ) return; // prevents division by 0
  70. var correction = diff.multiplyScalar( 1 - distance / currentDist );
  71. var correctionHalf = correction.multiplyScalar( 0.5 );
  72. p1.position.add( correctionHalf );
  73. p2.position.sub( correctionHalf );
  74. }
  75. function Cloth( w, h ) {
  76. w = w || 10;
  77. h = h || 10;
  78. this.w = w;
  79. this.h = h;
  80. var particles = [];
  81. var constraints = [];
  82. var u, v;
  83. // Create particles
  84. for ( v = 0; v <= h; v ++ ) {
  85. for ( u = 0; u <= w; u ++ ) {
  86. particles.push(
  87. new Particle( u / w, v / h, 0, MASS )
  88. );
  89. }
  90. }
  91. // Structural
  92. for ( v = 0; v < h; v ++ ) {
  93. for ( u = 0; u < w; u ++ ) {
  94. constraints.push( [
  95. particles[ index( u, v ) ],
  96. particles[ index( u, v + 1 ) ],
  97. restDistance
  98. ] );
  99. constraints.push( [
  100. particles[ index( u, v ) ],
  101. particles[ index( u + 1, v ) ],
  102. restDistance
  103. ] );
  104. }
  105. }
  106. for ( u = w, v = 0; v < h; v ++ ) {
  107. constraints.push( [
  108. particles[ index( u, v ) ],
  109. particles[ index( u, v + 1 ) ],
  110. restDistance
  111. ] );
  112. }
  113. for ( v = h, u = 0; u < w; u ++ ) {
  114. constraints.push( [
  115. particles[ index( u, v ) ],
  116. particles[ index( u + 1, v ) ],
  117. restDistance
  118. ] );
  119. }
  120. // While many systems use shear and bend springs,
  121. // the relaxed constraints model seems to be just fine
  122. // using structural springs.
  123. // Shear
  124. // var diagonalDist = Math.sqrt(restDistance * restDistance * 2);
  125. // for (v=0;v<h;v++) {
  126. // for (u=0;u<w;u++) {
  127. // constraints.push([
  128. // particles[index(u, v)],
  129. // particles[index(u+1, v+1)],
  130. // diagonalDist
  131. // ]);
  132. // constraints.push([
  133. // particles[index(u+1, v)],
  134. // particles[index(u, v+1)],
  135. // diagonalDist
  136. // ]);
  137. // }
  138. // }
  139. this.particles = particles;
  140. this.constraints = constraints;
  141. function index( u, v ) {
  142. return u + v * ( w + 1 );
  143. }
  144. this.index = index;
  145. }
  146. function simulate( time ) {
  147. if ( ! lastTime ) {
  148. lastTime = time;
  149. return;
  150. }
  151. var i, il, particles, particle, pt, constraints, constraint;
  152. // Aerodynamics forces
  153. if ( wind ) {
  154. var face, faces = clothGeometry.faces, normal;
  155. particles = cloth.particles;
  156. for ( i = 0, il = faces.length; i < il; i ++ ) {
  157. face = faces[ i ];
  158. normal = face.normal;
  159. tmpForce.copy( normal ).normalize().multiplyScalar( normal.dot( windForce ) );
  160. particles[ face.a ].addForce( tmpForce );
  161. particles[ face.b ].addForce( tmpForce );
  162. particles[ face.c ].addForce( tmpForce );
  163. }
  164. }
  165. for ( particles = cloth.particles, i = 0, il = particles.length; i < il; i ++ ) {
  166. particle = particles[ i ];
  167. particle.addForce( gravity );
  168. particle.integrate( TIMESTEP_SQ );
  169. }
  170. // Start Constraints
  171. constraints = cloth.constraints;
  172. il = constraints.length;
  173. for ( i = 0; i < il; i ++ ) {
  174. constraint = constraints[ i ];
  175. satisfyConstraints( constraint[ 0 ], constraint[ 1 ], constraint[ 2 ] );
  176. }
  177. // Ball Constraints
  178. ballPosition.z = - Math.sin( Date.now() / 600 ) * 90 ; //+ 40;
  179. ballPosition.x = Math.cos( Date.now() / 400 ) * 70;
  180. if ( sphere.visible ) {
  181. for ( particles = cloth.particles, i = 0, il = particles.length; i < il; i ++ ) {
  182. particle = particles[ i ];
  183. var pos = particle.position;
  184. diff.subVectors( pos, ballPosition );
  185. if ( diff.length() < ballSize ) {
  186. // collided
  187. diff.normalize().multiplyScalar( ballSize );
  188. pos.copy( ballPosition ).add( diff );
  189. }
  190. }
  191. }
  192. // Floor Constraints
  193. for ( particles = cloth.particles, i = 0, il = particles.length; i < il; i ++ ) {
  194. particle = particles[ i ];
  195. pos = particle.position;
  196. if ( pos.y < - 250 ) {
  197. pos.y = - 250;
  198. }
  199. }
  200. // Pin Constraints
  201. for ( i = 0, il = pins.length; i < il; i ++ ) {
  202. var xy = pins[ i ];
  203. var p = particles[ xy ];
  204. p.position.copy( p.original );
  205. p.previous.copy( p.original );
  206. }
  207. }