webgl_animation_cloth.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. text-align: center;
  17. padding: 10px;
  18. z-index: 10;
  19. width: 100%;
  20. position: absolute;
  21. }
  22. a {
  23. text-decoration: underline;
  24. cursor: pointer;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info">Simple Cloth Simulation #3<br/>
  30. Verlet based with Constrains relaxation<br/>
  31. Toggle: <a onclick="rotate = !rotate;">Camera</a> |
  32. <a onclick="wind = !wind;">Wind</a> |
  33. <a onclick="sphere.visible = !sphere.visible;">Ball</a> |
  34. <a onclick="togglePins();">Pins</a>
  35. </div>
  36. <script src="../build/three.min.js"></script>
  37. <script src="js/Detector.js"></script>
  38. <script src="js/Stats.js"></script>
  39. <script src="js/ParametricGeometries.js"></script>
  40. <script src="js/Cloth.js"></script>
  41. <script type="x-shader/x-fragment" id="fragmentShaderDepth">
  42. uniform sampler2D texture;
  43. varying vec2 vUV;
  44. vec4 pack_depth( const in float depth ) {
  45. const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );
  46. const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );
  47. vec4 res = fract( depth * bit_shift );
  48. res -= res.xxyz * bit_mask;
  49. return res;
  50. }
  51. void main() {
  52. vec4 pixel = texture2D( texture, vUV );
  53. if ( pixel.a < 0.5 ) discard;
  54. gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );
  55. }
  56. </script>
  57. <script type="x-shader/x-vertex" id="vertexShaderDepth">
  58. varying vec2 vUV;
  59. void main() {
  60. vUV = 0.75 * uv;
  61. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  62. gl_Position = projectionMatrix * mvPosition;
  63. }
  64. </script>
  65. <script>
  66. /* testing cloth simulation */
  67. var pinsFormation = [];
  68. var pins = [];
  69. pins[6] = true;
  70. pinsFormation.push( pins );
  71. pins = [ true, true, true, true, true, true, true, true, true, true, true, true ];
  72. pinsFormation.push( pins );
  73. pins = [ true ];
  74. pinsFormation.push( pins );
  75. pins = []; // cut the rope ;)
  76. pinsFormation.push( pins );
  77. pins = [ true ]; // classic 2 pins
  78. pins[ cloth.w ] = true;
  79. pinsFormation.push( pins );
  80. pins = pinsFormation[ 1 ];
  81. function togglePins() {
  82. pins = pinsFormation[ ~~( Math.random() * pinsFormation.length ) ];
  83. }
  84. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  85. var container, stats;
  86. var camera, scene, renderer;
  87. var clothGeometry;
  88. var sphere;
  89. var object, arrow;
  90. var rotate = true;
  91. init();
  92. animate();
  93. function init() {
  94. container = document.createElement( 'div' );
  95. document.body.appendChild( container );
  96. // scene
  97. scene = new THREE.Scene();
  98. scene.fog = new THREE.Fog( 0x000000, 500, 10000 );
  99. scene.fog.color.setHSV( 0.6, 0.2, 1 );
  100. // camera
  101. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  102. camera.position.y = 50;
  103. camera.position.z = 1500;
  104. scene.add( camera );
  105. // lights
  106. var light, materials;
  107. scene.add( new THREE.AmbientLight( 0x666666 ) );
  108. light = new THREE.DirectionalLight( 0xffffff, 1.75 );
  109. light.color.setHSV( 0.6, 0.125, 1 );
  110. light.position.set( 50, 200, 100 );
  111. light.position.multiplyScalar( 1.3 );
  112. light.castShadow = true;
  113. //light.shadowCameraVisible = true;
  114. light.shadowMapWidth = 2048;
  115. light.shadowMapHeight = 2048;
  116. var d = 300;
  117. light.shadowCameraLeft = -d;
  118. light.shadowCameraRight = d;
  119. light.shadowCameraTop = d;
  120. light.shadowCameraBottom = -d;
  121. light.shadowCameraFar = 1000;
  122. light.shadowDarkness = 0.5;
  123. scene.add( light );
  124. light = new THREE.DirectionalLight( 0xffffff, 0.35 );
  125. light.color.setHSV( 0.3, 0.95, 1 );
  126. light.position.set( 0, -1, 0 );
  127. scene.add( light );
  128. // cloth material
  129. var clothTexture = THREE.ImageUtils.loadTexture( 'textures/patterns/circuit_pattern.png' );
  130. clothTexture.wrapS = clothTexture.wrapT = THREE.RepeatWrapping;
  131. clothTexture.anisotropy = 16;
  132. materials = [
  133. new THREE.MeshPhongMaterial( { alphaTest: 0.5, ambient: 0xffffff, color: 0xffffff, specular: 0x030303, emissive: 0x111111, shiness: 10, perPixel: true, metal: false, map: clothTexture, side: THREE.DoubleSide } ),
  134. new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true, transparent: true, opacity: 0.9 } )
  135. ];
  136. // cloth geometry
  137. clothGeometry = new THREE.ParametricGeometry( THREE.ParametricGeometries.plane( 200, 200 ), cloth.w, cloth.h, true );
  138. clothGeometry.dynamic = true;
  139. clothGeometry.computeFaceNormals();
  140. var uniforms = { texture: { type: "t", value: 0, texture: clothTexture } };
  141. var vertexShader = document.getElementById( 'vertexShaderDepth' ).textContent;
  142. var fragmentShader = document.getElementById( 'fragmentShaderDepth' ).textContent;
  143. // cloth mesh
  144. object = new THREE.Mesh( clothGeometry, materials[ 0 ] );
  145. object.position.set( 0, 0, 0 );
  146. object.castShadow = true;
  147. object.receiveShadow = true;
  148. scene.add( object );
  149. object.customDepthMaterial = new THREE.ShaderMaterial( { uniforms: uniforms, vertexShader: vertexShader, fragmentShader: fragmentShader } );
  150. // sphere
  151. var ballGeo = new THREE.SphereGeometry( ballSize, 20, 20 );
  152. var ballMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  153. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  154. sphere.castShadow = true;
  155. sphere.receiveShadow = true;
  156. scene.add( sphere );
  157. // arrow
  158. arrow = new THREE.ArrowHelper( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 0 ), 50, 0xff0000 );
  159. arrow.position.set( -200, 0, -200 );
  160. //scene.add( arrow );
  161. var axis;
  162. axis = new THREE.AxisHelper();
  163. axis.position.set( 200, 0, -200 );
  164. axis.scale.x = axis.scale.y = axis.scale.z = 0.5;
  165. //scene.add( axis );
  166. // ground
  167. var initColor = new THREE.Color( 0x00ff00 );
  168. initColor.setHSV( 0.25, 0.85, 0.5 );
  169. var initTexture = THREE.ImageUtils.generateDataTexture( 1, 1, initColor );
  170. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: initTexture, perPixel: true } );
  171. var groundTexture = THREE.ImageUtils.loadTexture( "textures/terrain/grasslight-big.jpg", undefined, function() { groundMaterial.map = groundTexture } );
  172. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  173. groundTexture.repeat.set( 25, 25 );
  174. groundTexture.anisotropy = 16;
  175. var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 20000, 20000 ), groundMaterial );
  176. mesh.position.y = -250;
  177. mesh.rotation.x = - Math.PI / 2;
  178. mesh.receiveShadow = true;
  179. scene.add( mesh );
  180. // poles
  181. var poleGeo = new THREE.CubeGeometry( 5, 750, 5 );
  182. var poleMat = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shiness: 100, perPixel: true } );
  183. var mesh = new THREE.Mesh( poleGeo, poleMat );
  184. mesh.position.y = -250;
  185. mesh.position.x = -125;
  186. mesh.receiveShadow = true;
  187. mesh.castShadow = true;
  188. scene.add( mesh );
  189. var mesh = new THREE.Mesh( poleGeo, poleMat );
  190. mesh.position.y = -250;
  191. mesh.position.x = 125;
  192. mesh.receiveShadow = true;
  193. mesh.castShadow = true;
  194. scene.add( mesh );
  195. var mesh = new THREE.Mesh( new THREE.CubeGeometry( 255, 5, 5 ), poleMat );
  196. mesh.position.y = -250 + 750/2;
  197. mesh.position.x = 0;
  198. mesh.receiveShadow = true;
  199. mesh.castShadow = true;
  200. scene.add( mesh );
  201. var gg = new THREE.CubeGeometry( 10, 10, 10 );
  202. var mesh = new THREE.Mesh( gg, poleMat );
  203. mesh.position.y = -250;
  204. mesh.position.x = 125;
  205. mesh.receiveShadow = true;
  206. mesh.castShadow = true;
  207. scene.add( mesh );
  208. var mesh = new THREE.Mesh( gg, poleMat );
  209. mesh.position.y = -250;
  210. mesh.position.x = -125;
  211. mesh.receiveShadow = true;
  212. mesh.castShadow = true;
  213. scene.add( mesh );
  214. //
  215. renderer = new THREE.WebGLRenderer( { antialias: true } );
  216. renderer.setSize( window.innerWidth, window.innerHeight );
  217. renderer.setClearColor( scene.fog.color );
  218. container.appendChild( renderer.domElement );
  219. renderer.gammaInput = true;
  220. renderer.gammaOutput = true;
  221. renderer.physicallyBasedShading = true;
  222. renderer.shadowMapEnabled = true;
  223. //
  224. stats = new Stats();
  225. stats.domElement.style.position = 'absolute';
  226. stats.domElement.style.top = '0px';
  227. container.appendChild( stats.domElement );
  228. stats.domElement.children[ 0 ].children[ 0 ].style.color = "#aaa";
  229. stats.domElement.children[ 0 ].style.background = "transparent";
  230. stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
  231. //
  232. window.addEventListener( 'resize', onWindowResize, false );
  233. sphere.visible = !true
  234. }
  235. //
  236. function onWindowResize() {
  237. camera.aspect = window.innerWidth / window.innerHeight;
  238. camera.updateProjectionMatrix();
  239. renderer.setSize( window.innerWidth, window.innerHeight );
  240. }
  241. //
  242. function animate() {
  243. requestAnimationFrame( animate );
  244. var time = Date.now();
  245. windStrength = Math.cos( time / 7000 ) * 20 + 40;
  246. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) ).normalize().multiplyScalar( windStrength );
  247. arrow.setLength( windStrength );
  248. arrow.setDirection( windForce );
  249. simulate();
  250. render();
  251. stats.update();
  252. }
  253. function render() {
  254. var timer = Date.now() * 0.0002;
  255. var p = cloth.particles;
  256. for ( var i = 0, il = p.length; i < il; i ++ ) {
  257. clothGeometry.vertices[ i ].copy( p[ i ].position );
  258. }
  259. clothGeometry.computeFaceNormals();
  260. clothGeometry.computeVertexNormals();
  261. clothGeometry.normalsNeedUpdate = true;
  262. clothGeometry.verticesNeedUpdate = true;
  263. sphere.position.copy( ballPosition );
  264. if ( rotate ) {
  265. camera.position.x = Math.cos( timer ) * 1500;
  266. camera.position.z = Math.sin( timer ) * 1500;
  267. }
  268. camera.lookAt( scene.position );
  269. renderer.render( scene, camera );
  270. }
  271. </script>
  272. </body>
  273. </html>