webgl_collada.html 4.5 KB

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