webgl_animation_skinning.html 5.7 KB

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