webgl_loader_collada.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada</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 loader = new THREE.ColladaLoader();
  27. loader.options.convertUpAxis = true;
  28. loader.load( './models/monster.dae', function colladaReady( collada ) {
  29. dae = collada.scene;
  30. skin = collada.skins[ 0 ];
  31. dae.scale.x = dae.scale.y = dae.scale.z = 0.002;
  32. dae.updateMatrix();
  33. init();
  34. animate();
  35. } );
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. scene = new THREE.Scene();
  40. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  41. camera.position.set( 2, 2, 3 );
  42. scene.add( camera );
  43. // Grid
  44. var line_material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } ),
  45. geometry = new THREE.Geometry(),
  46. floor = -0.04, step = 1, size = 14;
  47. for ( var i = 0; i <= size / step * 2; i ++ ) {
  48. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - size, floor, i * step - size ) ) );
  49. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( size, floor, i * step - size ) ) );
  50. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, -size ) ) );
  51. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, size ) ) );
  52. }
  53. var line = new THREE.Line( geometry, line_material, THREE.LinePieces );
  54. scene.add( line );
  55. // Add the COLLADA
  56. scene.add( dae );
  57. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  58. scene.add( particleLight );
  59. // Lights
  60. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  61. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xeeeeee );
  62. directionalLight.position.x = Math.random() - 0.5;
  63. directionalLight.position.y = Math.random() - 0.5;
  64. directionalLight.position.z = Math.random() - 0.5;
  65. directionalLight.position.normalize();
  66. scene.add( directionalLight );
  67. pointLight = new THREE.PointLight( 0xffffff, 4 );
  68. pointLight.position = particleLight.position;
  69. scene.add( pointLight );
  70. renderer = new THREE.WebGLRenderer();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. container.appendChild( renderer.domElement );
  73. stats = new Stats();
  74. stats.domElement.style.position = 'absolute';
  75. stats.domElement.style.top = '0px';
  76. container.appendChild( stats.domElement );
  77. }
  78. //
  79. var t = 0;
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. if ( t > 30 ) t = 0;
  83. if ( skin ) {
  84. // guess this can be done smarter...
  85. // (Indeed, there are way more frames than needed and interpolation is not used at all
  86. // could be something like - one morph per each skinning pose keyframe, or even less,
  87. // animation could be resampled, morphing interpolation handles sparse keyframes quite well.
  88. // Simple animation cycles like this look ok with 10-15 frames instead of 100 ;)
  89. for ( var i = 0; i < skin.morphTargetInfluences.length; i++ ) {
  90. skin.morphTargetInfluences[ i ] = 0;
  91. }
  92. skin.morphTargetInfluences[ Math.floor( t ) ] = 1;
  93. t += 0.5;
  94. }
  95. render();
  96. stats.update();
  97. }
  98. function render() {
  99. var timer = Date.now() * 0.0005;
  100. camera.position.x = Math.cos( timer ) * 10;
  101. camera.position.y = 2;
  102. camera.position.z = Math.sin( timer ) * 10;
  103. camera.lookAt( scene.position );
  104. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  105. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  106. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>