webgl_loader_stl.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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://github.com/mrdoob/three.js" target="_blank">three.js</a> -
  29. STL loader test by <a href="https://github.com/aleeper">aleeper</a>
  30. </div>
  31. <script src="../build/three.min.js"></script>
  32. <script src="js/loaders/STLLoader.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/Stats.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var container, stats;
  38. var camera, scene, renderer, objects;
  39. var particleLight, pointLight;
  40. init();
  41. function init() {
  42. container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  45. camera.position.set( 2, 2, 3 );
  46. scene = new THREE.Scene();
  47. // Grid
  48. var size = 14, step = 1;
  49. var geometry = new THREE.Geometry();
  50. var material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } );
  51. for ( var i = - size; i <= size; i += step ) {
  52. geometry.vertices.push( new THREE.Vector3( - size, - 0.04, i ) );
  53. geometry.vertices.push( new THREE.Vector3( size, - 0.04, i ) );
  54. geometry.vertices.push( new THREE.Vector3( i, - 0.04, - size ) );
  55. geometry.vertices.push( new THREE.Vector3( i, - 0.04, size ) );
  56. }
  57. var line = new THREE.Line( geometry, material, THREE.LinePieces );
  58. scene.add( line );
  59. var loader = new THREE.STLLoader();
  60. loader.addEventListener( 'load', function ( event ) {
  61. var object = event.content;
  62. var material = new THREE.MeshPhongMaterial( { ambient: 0xff0000, color: 0xff0000, specular: 0xffffff } );
  63. console.log( object );
  64. for ( var i = 0; i < object.children.length; i ++ ) {
  65. object.children[ i ].material = material;
  66. }
  67. scene.add( object );
  68. animate();
  69. } );
  70. loader.load( './models/stl/slotted_disk.stl' );
  71. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  72. scene.add( particleLight );
  73. // Lights
  74. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  75. var directionalLight = new THREE.DirectionalLight( 0xeeeeee );
  76. directionalLight.position.x = Math.random() - 0.5;
  77. directionalLight.position.y = Math.random() - 0.5;
  78. directionalLight.position.z = Math.random() - 0.5;
  79. directionalLight.position.normalize();
  80. scene.add( directionalLight );
  81. pointLight = new THREE.PointLight( 0xffffff, 4 );
  82. pointLight.position = particleLight.position;
  83. scene.add( pointLight );
  84. renderer = new THREE.WebGLRenderer();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. container.appendChild( renderer.domElement );
  87. stats = new Stats();
  88. stats.domElement.style.position = 'absolute';
  89. stats.domElement.style.top = '0px';
  90. container.appendChild( stats.domElement );
  91. //
  92. window.addEventListener( 'resize', onWindowResize, false );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. //
  100. var t = 0;
  101. var clock = new THREE.Clock();
  102. function animate() {
  103. var delta = clock.getDelta();
  104. requestAnimationFrame( animate );
  105. if ( t > 1 ) t = 0;
  106. render();
  107. stats.update();
  108. }
  109. function render() {
  110. var timer = Date.now() * 0.0005;
  111. camera.position.x = Math.cos( timer ) * 3;
  112. camera.position.y = 1;
  113. camera.position.z = Math.sin( timer ) * 3;
  114. camera.lookAt( scene.position );
  115. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  116. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  117. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  118. renderer.render( scene, camera );
  119. }
  120. </script>
  121. </body>
  122. </html>