webgl_animation_cloth.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - cloth simulation</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #000;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. padding: 10px;
  18. width: 100%;
  19. text-align: center;
  20. }
  21. a {
  22. text-decoration: underline;
  23. cursor: pointer;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">Simple Cloth Simulation<br/>
  29. Verlet integration with relaxed constraints<br/>
  30. <a onclick="wind = !wind;">Wind</a> |
  31. <a onclick="sphere.visible = !sphere.visible;">Ball</a> |
  32. <a onclick="togglePins();">Pins</a>
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/controls/OrbitControls.js"></script>
  37. <script src="js/libs/stats.min.js"></script>
  38. <script src="js/Cloth.js"></script>
  39. <script>
  40. /* testing cloth simulation */
  41. var pinsFormation = [];
  42. var pins = [ 6 ];
  43. pinsFormation.push( pins );
  44. pins = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
  45. pinsFormation.push( pins );
  46. pins = [ 0 ];
  47. pinsFormation.push( pins );
  48. pins = []; // cut the rope ;)
  49. pinsFormation.push( pins );
  50. pins = [ 0, cloth.w ]; // classic 2 pins
  51. pinsFormation.push( pins );
  52. pins = pinsFormation[ 1 ];
  53. function togglePins() {
  54. pins = pinsFormation[ ~~ ( Math.random() * pinsFormation.length ) ];
  55. }
  56. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  57. var container, stats;
  58. var camera, scene, renderer;
  59. var clothGeometry;
  60. var sphere;
  61. var object;
  62. init();
  63. animate();
  64. function init() {
  65. container = document.createElement( 'div' );
  66. document.body.appendChild( container );
  67. // scene
  68. scene = new THREE.Scene();
  69. scene.background = new THREE.Color( 0xcce0ff );
  70. scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );
  71. // camera
  72. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  73. camera.position.set( 1000, 50, 1500 );
  74. // lights
  75. var light, materials;
  76. scene.add( new THREE.AmbientLight( 0x666666 ) );
  77. light = new THREE.DirectionalLight( 0xdfebff, 1 );
  78. light.position.set( 50, 200, 100 );
  79. light.position.multiplyScalar( 1.3 );
  80. light.castShadow = true;
  81. light.shadow.mapSize.width = 1024;
  82. light.shadow.mapSize.height = 1024;
  83. var d = 300;
  84. light.shadow.camera.left = - d;
  85. light.shadow.camera.right = d;
  86. light.shadow.camera.top = d;
  87. light.shadow.camera.bottom = - d;
  88. light.shadow.camera.far = 1000;
  89. scene.add( light );
  90. // cloth material
  91. var loader = new THREE.TextureLoader();
  92. var clothTexture = loader.load( 'textures/patterns/circuit_pattern.png' );
  93. clothTexture.anisotropy = 16;
  94. var clothMaterial = new THREE.MeshLambertMaterial( {
  95. map: clothTexture,
  96. side: THREE.DoubleSide,
  97. alphaTest: 0.5
  98. } );
  99. // cloth geometry
  100. clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h );
  101. // cloth mesh
  102. object = new THREE.Mesh( clothGeometry, clothMaterial );
  103. object.position.set( 0, 0, 0 );
  104. object.castShadow = true;
  105. scene.add( object );
  106. object.customDepthMaterial = new THREE.MeshDepthMaterial( {
  107. depthPacking: THREE.RGBADepthPacking,
  108. map: clothTexture,
  109. alphaTest: 0.5
  110. } );
  111. // sphere
  112. var ballGeo = new THREE.SphereBufferGeometry( ballSize, 32, 16 );
  113. var ballMaterial = new THREE.MeshLambertMaterial();
  114. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  115. sphere.castShadow = true;
  116. sphere.receiveShadow = true;
  117. scene.add( sphere );
  118. // ground
  119. var groundTexture = loader.load( 'textures/terrain/grasslight-big.jpg' );
  120. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  121. groundTexture.repeat.set( 25, 25 );
  122. groundTexture.anisotropy = 16;
  123. var groundMaterial = new THREE.MeshLambertMaterial( { map: groundTexture } );
  124. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
  125. mesh.position.y = - 250;
  126. mesh.rotation.x = - Math.PI / 2;
  127. mesh.receiveShadow = true;
  128. scene.add( mesh );
  129. // poles
  130. var poleGeo = new THREE.BoxBufferGeometry( 5, 375, 5 );
  131. var poleMat = new THREE.MeshLambertMaterial();
  132. var mesh = new THREE.Mesh( poleGeo, poleMat );
  133. mesh.position.x = - 125;
  134. mesh.position.y = - 62;
  135. mesh.receiveShadow = true;
  136. mesh.castShadow = true;
  137. scene.add( mesh );
  138. var mesh = new THREE.Mesh( poleGeo, poleMat );
  139. mesh.position.x = 125;
  140. mesh.position.y = - 62;
  141. mesh.receiveShadow = true;
  142. mesh.castShadow = true;
  143. scene.add( mesh );
  144. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 255, 5, 5 ), poleMat );
  145. mesh.position.y = - 250 + ( 750 / 2 );
  146. mesh.position.x = 0;
  147. mesh.receiveShadow = true;
  148. mesh.castShadow = true;
  149. scene.add( mesh );
  150. var gg = new THREE.BoxBufferGeometry( 10, 10, 10 );
  151. var mesh = new THREE.Mesh( gg, poleMat );
  152. mesh.position.y = - 250;
  153. mesh.position.x = 125;
  154. mesh.receiveShadow = true;
  155. mesh.castShadow = true;
  156. scene.add( mesh );
  157. var mesh = new THREE.Mesh( gg, poleMat );
  158. mesh.position.y = - 250;
  159. mesh.position.x = - 125;
  160. mesh.receiveShadow = true;
  161. mesh.castShadow = true;
  162. scene.add( mesh );
  163. // renderer
  164. renderer = new THREE.WebGLRenderer( { antialias: true } );
  165. renderer.setPixelRatio( window.devicePixelRatio );
  166. renderer.setSize( window.innerWidth, window.innerHeight );
  167. container.appendChild( renderer.domElement );
  168. renderer.gammaInput = true;
  169. renderer.gammaOutput = true;
  170. renderer.shadowMap.enabled = true;
  171. // controls
  172. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  173. controls.maxPolarAngle = Math.PI * 0.5;
  174. controls.minDistance = 1000;
  175. controls.maxDistance = 5000;
  176. // performance monitor
  177. stats = new Stats();
  178. container.appendChild( stats.dom );
  179. //
  180. window.addEventListener( 'resize', onWindowResize, false );
  181. sphere.visible = ! true;
  182. }
  183. //
  184. function onWindowResize() {
  185. camera.aspect = window.innerWidth / window.innerHeight;
  186. camera.updateProjectionMatrix();
  187. renderer.setSize( window.innerWidth, window.innerHeight );
  188. }
  189. //
  190. function animate() {
  191. requestAnimationFrame( animate );
  192. var time = Date.now();
  193. var windStrength = Math.cos( time / 7000 ) * 20 + 40;
  194. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) )
  195. windForce.normalize()
  196. windForce.multiplyScalar( windStrength );
  197. simulate( time );
  198. render();
  199. stats.update();
  200. }
  201. function render() {
  202. var p = cloth.particles;
  203. for ( var i = 0, il = p.length; i < il; i ++ ) {
  204. clothGeometry.vertices[ i ].copy( p[ i ].position );
  205. }
  206. clothGeometry.verticesNeedUpdate = true;
  207. clothGeometry.computeFaceNormals();
  208. clothGeometry.computeVertexNormals();
  209. sphere.position.copy( ballPosition );
  210. renderer.render( scene, camera );
  211. }
  212. </script>
  213. </body>
  214. </html>