webgl_animation_skinning.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - skinning</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. color: #000;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #f00;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div id="info">
  30. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - animation - skinning -
  31. buffalo model from <a href="http://ro.me">ro.me</a><br/>
  32. click to start animation
  33. </div>
  34. <script src="../build/three.min.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/Stats.js"></script>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. window.onload = init;
  40. var container, stats;
  41. var camera, scene, renderer;
  42. var mesh, light;
  43. var mouseX = 0, mouseY = 0;
  44. var windowHalfX = window.innerWidth / 2;
  45. var windowHalfY = window.innerHeight / 2;
  46. var animations = [];
  47. var buffalos = [];
  48. var offset = [];
  49. var floor, dz = 0, dstep = -10, playback = false;
  50. var clock = new THREE.Clock();
  51. function init() {
  52. container = document.getElementById( 'container' );
  53. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 1, 10000 );
  54. camera.position.set( 0, 185, 2500 );
  55. scene = new THREE.Scene();
  56. scene.fog = new THREE.FogExp2( 0xffffff, 0.0003 );
  57. scene.fog.color.setHSV( 0.1, 0.10, 1 );
  58. light = new THREE.DirectionalLight( 0xffffff, 1.5 );
  59. light.position.set( 0, 1, 1 ).normalize();
  60. scene.add( light );
  61. var planeSimple = new THREE.PlaneGeometry( 200, 300 );
  62. var planeTesselated = new THREE.PlaneGeometry( 100, 300, 25, 40 );
  63. var matWire = new THREE.MeshBasicMaterial( { color :0x110000, wireframe: true, wireframeLinewidth: 2 } );
  64. var matSolid = new THREE.MeshBasicMaterial( { color :0x330000 } );
  65. matSolid.color.setHSV( 0.1, 0.75, 1 );
  66. floor = new THREE.Mesh( planeSimple, matSolid );
  67. floor.position.y = -10;
  68. floor.rotation.x = - Math.PI / 2;
  69. floor.scale.set( 25, 25, 25 );
  70. scene.add( floor );
  71. floor = new THREE.Mesh( planeTesselated, matWire );
  72. floor.rotation.x = - Math.PI / 2;
  73. floor.scale.set( 25, 25, 25 );
  74. scene.add( floor );
  75. renderer = new THREE.WebGLRenderer( { clearColor: 0xffffff, clearAlpha: 1, antialias: true } );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. renderer.setClearColor( scene.fog.color, 1 );
  78. renderer.sortObjects = false;
  79. container.appendChild( renderer.domElement );
  80. stats = new Stats();
  81. stats.domElement.style.position = 'absolute';
  82. stats.domElement.style.top = '0px';
  83. container.appendChild( stats.domElement );
  84. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  85. document.addEventListener( 'click', startAnimation, false );
  86. var loader = new THREE.JSONLoader();
  87. loader.load( "obj/buffalo/buffalo.js", createScene );
  88. //
  89. window.addEventListener( 'resize', onWindowResize, false );
  90. //
  91. loop();
  92. }
  93. function onWindowResize() {
  94. windowHalfX = window.innerWidth / 2;
  95. windowHalfY = window.innerHeight / 2;
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function createScene( geometry ) {
  101. buffalos = [];
  102. animations = [];
  103. var x, y,
  104. buffalo, animation,
  105. gridx = 25, gridz = 15,
  106. sepx = 150, sepz = 300;
  107. var material = new THREE.MeshFaceMaterial();
  108. var originalMaterial = geometry.materials[ 0 ];
  109. originalMaterial.skinning = true;
  110. originalMaterial.transparent = true;
  111. originalMaterial.alphaTest = 0.75;
  112. THREE.AnimationHandler.add( geometry.animation );
  113. for( x = 0; x < gridx; x ++ ) {
  114. for( z = 0; z < gridz; z ++ ) {
  115. buffalo = new THREE.SkinnedMesh( geometry, material, false );
  116. buffalo.position.x = - ( gridx - 1 ) * sepx * 0.5 + x * sepx + Math.random() * 0.5 * sepx;
  117. buffalo.position.z = - ( gridz - 1 ) * sepz * 0.5 + z * sepz + Math.random() * 0.5 * sepz - 500;
  118. buffalo.position.y = buffalo.boundRadius * 0.5;
  119. buffalo.rotation.y = 0.2 - Math.random() * 0.4;
  120. scene.add( buffalo );
  121. buffalos.push( buffalo );
  122. animation = new THREE.Animation( buffalo, "take_001" );
  123. animations.push( animation );
  124. offset.push( Math.random() );
  125. }
  126. }
  127. }
  128. function startAnimation() {
  129. for( var i = 0; i < animations.length; i ++ ) {
  130. animations[ i ].offset = 0.05 * Math.random();
  131. animations[ i ].play();
  132. }
  133. dz = dstep;
  134. playback = true;
  135. }
  136. function onDocumentMouseMove( event ) {
  137. mouseX = ( event.clientX - windowHalfX );
  138. mouseY = ( event.clientY - windowHalfY );
  139. }
  140. function loop() {
  141. requestAnimationFrame( loop, renderer.domElement );
  142. var delta = clock.getDelta();
  143. THREE.AnimationHandler.update( delta );
  144. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  145. camera.lookAt( scene.position );
  146. if ( buffalos && playback ) {
  147. var elapsed = clock.getElapsedTime();
  148. camera.position.z += 2 * Math.sin( elapsed );
  149. for( i = 0; i < buffalos.length; i++ ) {
  150. buffalos[ i ].position.z += 2 * Math.sin( elapsed + offset[ i ] );
  151. }
  152. }
  153. floor.position.z += dz;
  154. if( floor.position.z < -500 ) floor.position.z = 0;
  155. renderer.render( scene, camera );
  156. stats.update();
  157. }
  158. </script>
  159. </body>
  160. </html>