webgl_loader_collada.html 4.3 KB

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