webgl_loader_collada.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #info {
  15. color: #fff;
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. a { color: skyblue }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://threejs.org" target="_blank">three.js</a> -
  29. monster by <a href="http://www.3drt.com/downloads.htm" target="_blank">3drt</a>
  30. </div>
  31. <script src="../build/three.min.js"></script>
  32. <script src="js/loaders/ColladaLoader.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var container, stats;
  38. var camera, scene, renderer, objects;
  39. var particleLight;
  40. var dae;
  41. var loader = new THREE.ColladaLoader();
  42. loader.options.convertUpAxis = true;
  43. loader.load( './models/collada/monster/monster.dae', function ( collada ) {
  44. dae = collada.scene;
  45. dae.traverse( function ( child ) {
  46. if ( child instanceof THREE.SkinnedMesh ) {
  47. var animation = new THREE.Animation( child, child.geometry.animation );
  48. animation.play();
  49. }
  50. } );
  51. dae.scale.x = dae.scale.y = dae.scale.z = 0.002;
  52. dae.updateMatrix();
  53. init();
  54. animate();
  55. } );
  56. function init() {
  57. container = document.createElement( 'div' );
  58. document.body.appendChild( container );
  59. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  60. camera.position.set( 2, 2, 3 );
  61. scene = new THREE.Scene();
  62. // Grid
  63. var size = 14, step = 1;
  64. var geometry = new THREE.Geometry();
  65. var material = new THREE.LineBasicMaterial( { color: 0x303030 } );
  66. for ( var i = - size; i <= size; i += step ) {
  67. geometry.vertices.push( new THREE.Vector3( - size, - 0.04, i ) );
  68. geometry.vertices.push( new THREE.Vector3( size, - 0.04, i ) );
  69. geometry.vertices.push( new THREE.Vector3( i, - 0.04, - size ) );
  70. geometry.vertices.push( new THREE.Vector3( i, - 0.04, size ) );
  71. }
  72. var line = new THREE.Line( geometry, material, THREE.LinePieces );
  73. scene.add( line );
  74. // Add the COLLADA
  75. scene.add( dae );
  76. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  77. scene.add( particleLight );
  78. // Lights
  79. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  80. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xeeeeee );
  81. directionalLight.position.x = Math.random() - 0.5;
  82. directionalLight.position.y = Math.random() - 0.5;
  83. directionalLight.position.z = Math.random() - 0.5;
  84. directionalLight.position.normalize();
  85. scene.add( directionalLight );
  86. var pointLight = new THREE.PointLight( 0xffffff, 4 );
  87. particleLight.add( pointLight );
  88. renderer = new THREE.WebGLRenderer();
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. container.appendChild( renderer.domElement );
  92. stats = new Stats();
  93. stats.domElement.style.position = 'absolute';
  94. stats.domElement.style.top = '0px';
  95. container.appendChild( stats.domElement );
  96. //
  97. window.addEventListener( 'resize', onWindowResize, false );
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. //
  105. function animate() {
  106. requestAnimationFrame( animate );
  107. render();
  108. stats.update();
  109. }
  110. var clock = new THREE.Clock();
  111. function render() {
  112. var timer = Date.now() * 0.0005;
  113. camera.position.x = Math.cos( timer ) * 10;
  114. camera.position.y = 2;
  115. camera.position.z = Math.sin( timer ) * 10;
  116. camera.lookAt( scene.position );
  117. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  118. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  119. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  120. THREE.AnimationHandler.update( clock.getDelta() );
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>