webgl_collada.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 type="text/css">
  8. body {
  9. font-family: Monospace;
  10. background-color: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #log { color:#fff; position:absolute; top:50px; text-align:left; display:block; z-index:100; pointer-events:none; }
  15. </style>
  16. </head>
  17. <body>
  18. <pre id="log"></pre>
  19. <script type="text/javascript" src="../build/Three.js"></script>
  20. <script type="text/javascript" src="js/Detector.js"></script>
  21. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  22. <script type="text/javascript" src="js/Stats.js"></script>
  23. <script type="text/javascript">
  24. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  25. var container, stats;
  26. var camera, scene, renderer, objects;
  27. var particleLight, pointLight;
  28. var dae, skin;
  29. var loader = new THREE.ColladaLoader();
  30. loader.load( './models/monster.dae', function colladaReady( collada ) {
  31. dae = collada.scene;
  32. skin = collada.skins[ 0 ];
  33. dae.scale.x = dae.scale.y = dae.scale.z = 0.002;
  34. dae.rotation.x = -Math.PI/2;
  35. dae.updateMatrix();
  36. init();
  37. animate();
  38. } );
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  43. camera.position.x = 2;
  44. camera.position.y = 2;
  45. camera.position.z = 3;
  46. scene = new THREE.Scene();
  47. // Grid
  48. var line_material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } ),
  49. geometry = new THREE.Geometry(),
  50. floor = -0.04, step = 1, size = 14;
  51. for ( var i = 0; i <= size / step * 2; i ++ ) {
  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( size, floor, i * step - size ) ) );
  54. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, -size ) ) );
  55. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, size ) ) );
  56. }
  57. var line = new THREE.Line( geometry, line_material, THREE.LinePieces );
  58. scene.add( line );
  59. // Add the COLLADA
  60. scene.add( dae );
  61. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  62. scene.add( particleLight );
  63. // Lights
  64. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  65. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xeeeeee );
  66. directionalLight.position.x = Math.random() - 0.5;
  67. directionalLight.position.y = Math.random() - 0.5;
  68. directionalLight.position.z = Math.random() - 0.5;
  69. directionalLight.position.normalize();
  70. scene.add( directionalLight );
  71. pointLight = new THREE.PointLight( 0xffffff, 4 );
  72. pointLight.position.x = 10000;
  73. scene.add( pointLight );
  74. renderer = new THREE.WebGLRenderer();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. container.appendChild( renderer.domElement );
  77. stats = new Stats();
  78. stats.domElement.style.position = 'absolute';
  79. stats.domElement.style.top = '0px';
  80. container.appendChild( stats.domElement );
  81. }
  82. //
  83. var t = 0;
  84. function animate() {
  85. requestAnimationFrame( animate );
  86. if ( t > 30 ) t = 0;
  87. if ( skin ) {
  88. // guess this can be done smarter...
  89. // (Indeed, there are way more frames than needed and interpolation is not used at all
  90. // could be something like - one morph per each skinning pose keyframe, or even less,
  91. // animation could be resampled, morphing interpolation handles sparse keyframes quite well.
  92. // Simple animation cycles like this look ok with 10-15 frames instead of 100 ;)
  93. for ( var i = 0; i < skin.morphTargetInfluences.length; i++ ) {
  94. skin.morphTargetInfluences[ i ] = 0;
  95. }
  96. skin.morphTargetInfluences[ Math.floor( t ) ] = 1;
  97. t += 0.5;
  98. }
  99. render();
  100. stats.update();
  101. }
  102. function render() {
  103. var timer = new Date().getTime() * 0.0005;
  104. camera.position.x = Math.cos( timer ) * 10;
  105. camera.position.y = 2;
  106. camera.position.z = Math.sin( timer ) * 10;
  107. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  108. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  109. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  110. pointLight.position.x = particleLight.position.x;
  111. pointLight.position.y = particleLight.position.y;
  112. pointLight.position.z = particleLight.position.z;
  113. renderer.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>