webgl_animation_cloth.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/WebGL.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 ( WEBGL.isWebGLAvailable() === false ) {
  57. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  58. }
  59. var container, stats;
  60. var camera, scene, renderer;
  61. var clothGeometry;
  62. var sphere;
  63. var object;
  64. init();
  65. animate();
  66. function init() {
  67. container = document.createElement( 'div' );
  68. document.body.appendChild( container );
  69. // scene
  70. scene = new THREE.Scene();
  71. scene.background = new THREE.Color( 0xcce0ff );
  72. scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );
  73. // camera
  74. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  75. camera.position.set( 1000, 50, 1500 );
  76. // lights
  77. scene.add( new THREE.AmbientLight( 0x666666 ) );
  78. var light = new THREE.DirectionalLight( 0xdfebff, 1 );
  79. light.position.set( 50, 200, 100 );
  80. light.position.multiplyScalar( 1.3 );
  81. light.castShadow = true;
  82. light.shadow.mapSize.width = 1024;
  83. light.shadow.mapSize.height = 1024;
  84. var d = 300;
  85. light.shadow.camera.left = - d;
  86. light.shadow.camera.right = d;
  87. light.shadow.camera.top = d;
  88. light.shadow.camera.bottom = - d;
  89. light.shadow.camera.far = 1000;
  90. scene.add( light );
  91. // cloth material
  92. var loader = new THREE.TextureLoader();
  93. var clothTexture = loader.load( 'textures/patterns/circuit_pattern.png' );
  94. clothTexture.anisotropy = 16;
  95. var clothMaterial = new THREE.MeshLambertMaterial( {
  96. map: clothTexture,
  97. side: THREE.DoubleSide,
  98. alphaTest: 0.5
  99. } );
  100. // cloth geometry
  101. clothGeometry = new THREE.ParametricBufferGeometry( clothFunction, cloth.w, cloth.h );
  102. // cloth mesh
  103. object = new THREE.Mesh( clothGeometry, clothMaterial );
  104. object.position.set( 0, 0, 0 );
  105. object.castShadow = true;
  106. scene.add( object );
  107. object.customDepthMaterial = new THREE.MeshDepthMaterial( {
  108. depthPacking: THREE.RGBADepthPacking,
  109. map: clothTexture,
  110. alphaTest: 0.5
  111. } );
  112. // sphere
  113. var ballGeo = new THREE.SphereBufferGeometry( ballSize, 32, 16 );
  114. var ballMaterial = new THREE.MeshLambertMaterial();
  115. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  116. sphere.castShadow = true;
  117. sphere.receiveShadow = true;
  118. scene.add( sphere );
  119. // ground
  120. var groundTexture = loader.load( 'textures/terrain/grasslight-big.jpg' );
  121. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  122. groundTexture.repeat.set( 25, 25 );
  123. groundTexture.anisotropy = 16;
  124. var groundMaterial = new THREE.MeshLambertMaterial( { map: groundTexture } );
  125. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
  126. mesh.position.y = - 250;
  127. mesh.rotation.x = - Math.PI / 2;
  128. mesh.receiveShadow = true;
  129. scene.add( mesh );
  130. // poles
  131. var poleGeo = new THREE.BoxBufferGeometry( 5, 375, 5 );
  132. var poleMat = new THREE.MeshLambertMaterial();
  133. var mesh = new THREE.Mesh( poleGeo, poleMat );
  134. mesh.position.x = - 125;
  135. mesh.position.y = - 62;
  136. mesh.receiveShadow = true;
  137. mesh.castShadow = true;
  138. scene.add( mesh );
  139. var mesh = new THREE.Mesh( poleGeo, poleMat );
  140. mesh.position.x = 125;
  141. mesh.position.y = - 62;
  142. mesh.receiveShadow = true;
  143. mesh.castShadow = true;
  144. scene.add( mesh );
  145. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 255, 5, 5 ), poleMat );
  146. mesh.position.y = - 250 + ( 750 / 2 );
  147. mesh.position.x = 0;
  148. mesh.receiveShadow = true;
  149. mesh.castShadow = true;
  150. scene.add( mesh );
  151. var gg = new THREE.BoxBufferGeometry( 10, 10, 10 );
  152. var mesh = new THREE.Mesh( gg, poleMat );
  153. mesh.position.y = - 250;
  154. mesh.position.x = 125;
  155. mesh.receiveShadow = true;
  156. mesh.castShadow = true;
  157. scene.add( mesh );
  158. var mesh = new THREE.Mesh( gg, poleMat );
  159. mesh.position.y = - 250;
  160. mesh.position.x = - 125;
  161. mesh.receiveShadow = true;
  162. mesh.castShadow = true;
  163. scene.add( mesh );
  164. // renderer
  165. renderer = new THREE.WebGLRenderer( { antialias: true } );
  166. renderer.setPixelRatio( window.devicePixelRatio );
  167. renderer.setSize( window.innerWidth, window.innerHeight );
  168. container.appendChild( renderer.domElement );
  169. renderer.gammaInput = true;
  170. renderer.gammaOutput = true;
  171. renderer.shadowMap.enabled = true;
  172. // controls
  173. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  174. controls.maxPolarAngle = Math.PI * 0.5;
  175. controls.minDistance = 1000;
  176. controls.maxDistance = 5000;
  177. // performance monitor
  178. stats = new Stats();
  179. container.appendChild( stats.dom );
  180. //
  181. window.addEventListener( 'resize', onWindowResize, false );
  182. sphere.visible = ! true;
  183. }
  184. //
  185. function onWindowResize() {
  186. camera.aspect = window.innerWidth / window.innerHeight;
  187. camera.updateProjectionMatrix();
  188. renderer.setSize( window.innerWidth, window.innerHeight );
  189. }
  190. //
  191. function animate() {
  192. requestAnimationFrame( animate );
  193. var time = Date.now();
  194. var windStrength = Math.cos( time / 7000 ) * 20 + 40;
  195. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) )
  196. windForce.normalize()
  197. windForce.multiplyScalar( windStrength );
  198. simulate( time );
  199. render();
  200. stats.update();
  201. }
  202. function render() {
  203. var p = cloth.particles;
  204. for ( var i = 0, il = p.length; i < il; i ++ ) {
  205. var v = p[ i ].position;
  206. clothGeometry.attributes.position.setXYZ( i, v.x, v.y, v.z );
  207. }
  208. clothGeometry.attributes.position.needsUpdate = true;
  209. clothGeometry.computeVertexNormals();
  210. sphere.position.copy( ballPosition );
  211. renderer.render( scene, camera );
  212. }
  213. </script>
  214. </body>
  215. </html>