webgl_loader_json_blender.html 5.1 KB

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