webgl_animation_skinning.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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://threejs.org" 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/libs/stats.min.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( 0xfff4e5, 0.0003 );
  57. light = new THREE.DirectionalLight( 0xffffff, 1.5 );
  58. light.position.set( 0, 1, 1 ).normalize();
  59. scene.add( light );
  60. var planeSimple = new THREE.PlaneGeometry( 200, 300 );
  61. var planeTesselated = new THREE.PlaneGeometry( 100, 300, 25, 40 );
  62. var matWire = new THREE.MeshBasicMaterial( { color: 0x110000, wireframe: true, wireframeLinewidth: 2 } );
  63. var matSolid = new THREE.MeshBasicMaterial( { color: 0xffb23f } );
  64. floor = new THREE.Mesh( planeSimple, matSolid );
  65. floor.position.y = -10;
  66. floor.rotation.x = - Math.PI / 2;
  67. floor.scale.set( 25, 25, 25 );
  68. scene.add( floor );
  69. floor = new THREE.Mesh( planeTesselated, matWire );
  70. floor.rotation.x = - Math.PI / 2;
  71. floor.scale.set( 25, 25, 25 );
  72. scene.add( floor );
  73. renderer = new THREE.WebGLRenderer( { clearColor: 0xffffff, clearAlpha: 1, antialias: true } );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.setClearColor( scene.fog.color, 1 );
  76. renderer.sortObjects = false;
  77. container.appendChild( renderer.domElement );
  78. stats = new Stats();
  79. stats.domElement.style.position = 'absolute';
  80. stats.domElement.style.top = '0px';
  81. container.appendChild( stats.domElement );
  82. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  83. document.addEventListener( 'click', startAnimation, false );
  84. var loader = new THREE.JSONLoader();
  85. loader.load( "obj/buffalo/buffalo.js", createScene );
  86. //
  87. window.addEventListener( 'resize', onWindowResize, false );
  88. //
  89. loop();
  90. }
  91. function onWindowResize() {
  92. windowHalfX = window.innerWidth / 2;
  93. windowHalfY = window.innerHeight / 2;
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function createScene( geometry, materials ) {
  99. buffalos = [];
  100. animations = [];
  101. var x, y,
  102. buffalo, animation,
  103. gridx = 25, gridz = 15,
  104. sepx = 150, sepz = 300;
  105. var material = new THREE.MeshFaceMaterial( materials );
  106. var originalMaterial = materials[ 0 ];
  107. originalMaterial.skinning = true;
  108. originalMaterial.transparent = true;
  109. originalMaterial.alphaTest = 0.75;
  110. THREE.AnimationHandler.add( geometry.animation );
  111. for( x = 0; x < gridx; x ++ ) {
  112. for( z = 0; z < gridz; z ++ ) {
  113. buffalo = new THREE.SkinnedMesh( geometry, material, false );
  114. buffalo.position.x = - ( gridx - 1 ) * sepx * 0.5 + x * sepx + Math.random() * 0.5 * sepx;
  115. buffalo.position.z = - ( gridz - 1 ) * sepz * 0.5 + z * sepz + Math.random() * 0.5 * sepz - 500;
  116. buffalo.position.y = buffalo.geometry.boundingSphere.radius * 0.5;
  117. buffalo.rotation.y = 0.2 - Math.random() * 0.4;
  118. scene.add( buffalo );
  119. buffalos.push( buffalo );
  120. animation = new THREE.Animation( buffalo, "take_001" );
  121. animations.push( animation );
  122. offset.push( Math.random() );
  123. }
  124. }
  125. }
  126. function startAnimation() {
  127. for( var i = 0; i < animations.length; i ++ ) {
  128. animations[ i ].offset = 0.05 * Math.random();
  129. animations[ i ].play();
  130. }
  131. dz = dstep;
  132. playback = true;
  133. }
  134. function onDocumentMouseMove( event ) {
  135. mouseX = ( event.clientX - windowHalfX );
  136. mouseY = ( event.clientY - windowHalfY );
  137. }
  138. function loop() {
  139. requestAnimationFrame( loop, renderer.domElement );
  140. var delta = clock.getDelta();
  141. THREE.AnimationHandler.update( delta );
  142. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  143. camera.lookAt( scene.position );
  144. if ( buffalos && playback ) {
  145. var elapsed = clock.getElapsedTime();
  146. camera.position.z += 2 * Math.sin( elapsed );
  147. for( i = 0; i < buffalos.length; i++ ) {
  148. buffalos[ i ].position.z += 2 * Math.sin( elapsed + offset[ i ] );
  149. }
  150. }
  151. floor.position.z += dz;
  152. if( floor.position.z < -500 ) floor.position.z = 0;
  153. renderer.render( scene, camera );
  154. stats.update();
  155. }
  156. </script>
  157. </body>
  158. </html>