webgl_loader_json_blender.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada - blender</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: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/Three.js"></script>
  18. <script src="js/Detector.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script>
  21. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  22. var container, stats;
  23. var camera, scene, renderer, objects;
  24. var particleLight, pointLight;
  25. var dae, skin;
  26. var clock = new THREE.Clock();
  27. var morphs = [];
  28. // Collada model
  29. var loader = new THREE.ColladaLoader();
  30. loader.options.convertUpAxis = true;
  31. loader.load( 'models/monster.dae', function colladaReady( collada ) {
  32. dae = collada.scene;
  33. skin = collada.skins[ 0 ];
  34. dae.scale.x = dae.scale.y = dae.scale.z = 0.002;
  35. dae.position.x = -1;
  36. dae.updateMatrix();
  37. init();
  38. animate();
  39. } );
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  44. camera.position.set( 2, 4, 5 );
  45. scene = new THREE.Scene();
  46. scene.fog = new THREE.FogExp2( 0x000000, 0.035 );
  47. // Add Blender exported Collada model
  48. var loader = new THREE.JSONLoader();
  49. loader.load( "models/animated/monster/monster.js", function ( geometry ) {
  50. // adjust color a bit
  51. var material = geometry.materials[ 0 ];
  52. material.morphTargets = true;
  53. material.color.setHex( 0xffaaaa );
  54. material.ambient.setHex( 0x222222 );
  55. var faceMaterial = new THREE.MeshFaceMaterial();
  56. for ( var i = 0; i < 729; i ++ ) {
  57. // random placement in a grid
  58. var x = ( ( i % 27 ) - 13.5 ) * 2 + THREE.Math.randFloatSpread( 1 );
  59. var z = ( Math.floor( i / 27 ) - 13.5 ) * 2 + THREE.Math.randFloatSpread( 1 );
  60. // leave space for big monster
  61. if ( Math.abs( x ) < 2 && Math.abs( z ) < 2 ) continue;
  62. morph = new THREE.MorphAnimMesh( geometry, faceMaterial );
  63. // one second duration
  64. morph.duration = 1000;
  65. // random animation offset
  66. morph.time = 1000 * Math.random();
  67. var s = THREE.Math.randFloat( 0.00075, 0.001 );
  68. morph.scale.set( s, s, s );
  69. morph.position.set( x, 0, z );
  70. morph.rotation.y = THREE.Math.randFloat( -0.25, 0.25 );
  71. morph.matrixAutoUpdate = false;
  72. morph.updateMatrix();
  73. scene.add( morph );
  74. morphs.push( morph );
  75. }
  76. } );
  77. // Add the COLLADA
  78. scene.add( dae );
  79. // Lights
  80. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  81. pointLight = new THREE.PointLight( 0xff4400, 5, 30 );
  82. pointLight.position.set( 5, 0, 0 );
  83. scene.add( pointLight );
  84. // Renderer
  85. renderer = new THREE.WebGLRenderer();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. container.appendChild( renderer.domElement );
  88. // Stats
  89. stats = new Stats();
  90. stats.domElement.style.position = 'absolute';
  91. stats.domElement.style.top = '0px';
  92. container.appendChild( stats.domElement );
  93. stats.domElement.children[ 0 ].children[ 0 ].style.color = "#aaa";
  94. stats.domElement.children[ 0 ].style.background = "transparent";
  95. stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
  96. // Events
  97. window.addEventListener( 'resize', onWindowResize, false );
  98. }
  99. //
  100. function onWindowResize( event ) {
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. }
  105. //
  106. var t = 0;
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. // animate Collada model
  110. if ( t > 30 ) t = 0;
  111. if ( skin ) {
  112. // guess this can be done smarter...
  113. // (Indeed, there are way more frames than needed and interpolation is not used at all
  114. // could be something like - one morph per each skinning pose keyframe, or even less,
  115. // animation could be resampled, morphing interpolation handles sparse keyframes quite well.
  116. // Simple animation cycles like this look ok with 10-15 frames instead of 100 ;)
  117. for ( var i = 0; i < skin.morphTargetInfluences.length; i++ ) {
  118. skin.morphTargetInfluences[ i ] = 0;
  119. }
  120. skin.morphTargetInfluences[ Math.floor( t ) ] = 1;
  121. t += 0.5;
  122. }
  123. // animate morphs
  124. var delta = clock.getDelta();
  125. if ( morphs.length ) {
  126. for ( var i = 0; i < morphs.length; i ++ )
  127. morphs[ i ].updateAnimation( 1000 * delta );
  128. }
  129. render();
  130. stats.update();
  131. }
  132. function render() {
  133. var timer = Date.now() * 0.0005;
  134. camera.position.x = Math.cos( timer ) * 10;
  135. camera.position.y = 4;
  136. camera.position.z = Math.sin( timer ) * 10;
  137. camera.lookAt( scene.position );
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>