Cloth.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 ) {
  32. var x = ( u - 0.5 ) * width;
  33. var y = ( v + 0.5 ) * height;
  34. var z = 0;
  35. return new THREE.Vector3( x, y, z );
  36. };
  37. }
  38. function Particle( x, y, z, mass ) {
  39. this.position = clothFunction( x, y ); // position
  40. this.previous = clothFunction( x, y ); // previous
  41. this.original = clothFunction( x, y );
  42. this.a = new THREE.Vector3( 0, 0, 0 ); // acceleration
  43. this.mass = mass;
  44. this.invMass = 1 / mass;
  45. this.tmp = new THREE.Vector3();
  46. this.tmp2 = new THREE.Vector3();
  47. }
  48. // Force -> Acceleration
  49. Particle.prototype.addForce = function( force ) {
  50. this.a.add(
  51. this.tmp2.copy( force ).multiplyScalar( this.invMass )
  52. );
  53. };
  54. // Performs Verlet integration
  55. Particle.prototype.integrate = function( timesq ) {
  56. var newPos = this.tmp.subVectors( this.position, this.previous );
  57. newPos.multiplyScalar( DRAG ).add( this.position );
  58. newPos.add( this.a.multiplyScalar( timesq ) );
  59. this.previous = this.position;
  60. this.position = newPos;
  61. this.a.set( 0, 0, 0 );
  62. };
  63. var diff = new THREE.Vector3();
  64. function satisfyConstraints( p1, p2, distance ) {
  65. diff.subVectors( p2.position, p1.position );
  66. var currentDist = diff.length();
  67. if ( currentDist === 0 ) return; // prevents division by 0
  68. var correction = diff.multiplyScalar( 1 - distance / currentDist );
  69. var correctionHalf = correction.multiplyScalar( 0.5 );
  70. p1.position.add( correctionHalf );
  71. p2.position.sub( correctionHalf );
  72. }
  73. function Cloth( w, h ) {
  74. w = w || 10;
  75. h = h || 10;
  76. this.w = w;
  77. this.h = h;
  78. var particles = [];
  79. var constraints = [];
  80. var u, v;
  81. // Create particles
  82. for ( v = 0; v <= h; v ++ ) {
  83. for ( u = 0; u <= w; u ++ ) {
  84. particles.push(
  85. new Particle( u / w, v / h, 0, MASS )
  86. );
  87. }
  88. }
  89. // Structural
  90. for ( v = 0; v < h; v ++ ) {
  91. for ( u = 0; u < w; u ++ ) {
  92. constraints.push( [
  93. particles[ index( u, v ) ],
  94. particles[ index( u, v + 1 ) ],
  95. restDistance
  96. ] );
  97. constraints.push( [
  98. particles[ index( u, v ) ],
  99. particles[ index( u + 1, v ) ],
  100. restDistance
  101. ] );
  102. }
  103. }
  104. for ( u = w, v = 0; v < h; v ++ ) {
  105. constraints.push( [
  106. particles[ index( u, v ) ],
  107. particles[ index( u, v + 1 ) ],
  108. restDistance
  109. ] );
  110. }
  111. for ( v = h, u = 0; u < w; u ++ ) {
  112. constraints.push( [
  113. particles[ index( u, v ) ],
  114. particles[ index( u + 1, v ) ],
  115. restDistance
  116. ] );
  117. }
  118. // While many systems use shear and bend springs,
  119. // the relaxed constraints model seems to be just fine
  120. // using structural springs.
  121. // Shear
  122. // var diagonalDist = Math.sqrt(restDistance * restDistance * 2);
  123. // for (v=0;v<h;v++) {
  124. // for (u=0;u<w;u++) {
  125. // constraints.push([
  126. // particles[index(u, v)],
  127. // particles[index(u+1, v+1)],
  128. // diagonalDist
  129. // ]);
  130. // constraints.push([
  131. // particles[index(u+1, v)],
  132. // particles[index(u, v+1)],
  133. // diagonalDist
  134. // ]);
  135. // }
  136. // }
  137. this.particles = particles;
  138. this.constraints = constraints;
  139. function index( u, v ) {
  140. return u + v * ( w + 1 );
  141. }
  142. this.index = index;
  143. }
  144. function simulate( time ) {
  145. if ( ! lastTime ) {
  146. lastTime = time;
  147. return;
  148. }
  149. var i, il, particles, particle, pt, constraints, constraint;
  150. // Aerodynamics forces
  151. if ( wind ) {
  152. var face, faces = clothGeometry.faces, normal;
  153. particles = cloth.particles;
  154. for ( i = 0, il = faces.length; i < il; i ++ ) {
  155. face = faces[ i ];
  156. normal = face.normal;
  157. tmpForce.copy( normal ).normalize().multiplyScalar( normal.dot( windForce ) );
  158. particles[ face.a ].addForce( tmpForce );
  159. particles[ face.b ].addForce( tmpForce );
  160. particles[ face.c ].addForce( tmpForce );
  161. }
  162. }
  163. for ( particles = cloth.particles, i = 0, il = particles.length; i < il; i ++ ) {
  164. particle = particles[ i ];
  165. particle.addForce( gravity );
  166. particle.integrate( TIMESTEP_SQ );
  167. }
  168. // Start Constraints
  169. constraints = cloth.constraints;
  170. il = constraints.length;
  171. for ( i = 0; i < il; i ++ ) {
  172. constraint = constraints[ i ];
  173. satisfyConstraints( constraint[ 0 ], constraint[ 1 ], constraint[ 2 ] );
  174. }
  175. // Ball Constraints
  176. ballPosition.z = - Math.sin( Date.now() / 600 ) * 90 ; //+ 40;
  177. ballPosition.x = Math.cos( Date.now() / 400 ) * 70;
  178. if ( sphere.visible ) {
  179. for ( particles = cloth.particles, i = 0, il = particles.length; i < il; i ++ ) {
  180. particle = particles[ i ];
  181. pos = particle.position;
  182. diff.subVectors( pos, ballPosition );
  183. if ( diff.length() < ballSize ) {
  184. // collided
  185. diff.normalize().multiplyScalar( ballSize );
  186. pos.copy( ballPosition ).add( diff );
  187. }
  188. }
  189. }
  190. // Floor Constraints
  191. for ( particles = cloth.particles, i = 0, il = particles.length; i < il; i ++ ) {
  192. particle = particles[ i ];
  193. pos = particle.position;
  194. if ( pos.y < - 250 ) {
  195. pos.y = - 250;
  196. }
  197. }
  198. // Pin Constraints
  199. for ( i = 0, il = pins.length; i < il; i ++ ) {
  200. var xy = pins[ i ];
  201. var p = particles[ xy ];
  202. p.position.copy( p.original );
  203. p.previous.copy( p.original );
  204. }
  205. }