webgl_loader_stl.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. var stl;
  41. var material = new THREE.MeshPhongMaterial( { ambient :0xFF0000, color: 0xFF0000, specular: 0xFFFFFF } );
  42. var loader = new THREE.STLLoader();
  43. loader.addEventListener( 'load', function ( event ) {
  44. var object = event.content;
  45. if(material instanceof THREE.Material){
  46. for ( var i = 0; i < object.children.length; i ++ ) {
  47. object.children[ i ].material = material;
  48. }
  49. }
  50. object.updateMatrix();// Not sure if this is needed.
  51. stl = object;
  52. init();
  53. animate();
  54. } );
  55. var file_path = './models/stl/slotted_disk.stl';
  56. loader.load( file_path );
  57. function init() {
  58. container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  61. camera.position.set( 2, 2, 3 );
  62. scene = new THREE.Scene();
  63. // Grid
  64. var size = 14, step = 1;
  65. var geometry = new THREE.Geometry();
  66. var material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } );
  67. for ( var i = - size; i <= size; i += step ) {
  68. geometry.vertices.push( new THREE.Vector3( - size, - 0.04, i ) );
  69. geometry.vertices.push( new THREE.Vector3( size, - 0.04, i ) );
  70. geometry.vertices.push( new THREE.Vector3( i, - 0.04, - size ) );
  71. geometry.vertices.push( new THREE.Vector3( i, - 0.04, size ) );
  72. }
  73. var line = new THREE.Line( geometry, material, THREE.LinePieces );
  74. scene.add( line );
  75. // Add the STL file
  76. scene.add( stl );
  77. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  78. scene.add( particleLight );
  79. // Lights
  80. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  81. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xeeeeee );
  82. directionalLight.position.x = Math.random() - 0.5;
  83. directionalLight.position.y = Math.random() - 0.5;
  84. directionalLight.position.z = Math.random() - 0.5;
  85. directionalLight.position.normalize();
  86. scene.add( directionalLight );
  87. pointLight = new THREE.PointLight( 0xffffff, 4 );
  88. pointLight.position = particleLight.position;
  89. scene.add( pointLight );
  90. renderer = new THREE.WebGLRenderer();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. container.appendChild( renderer.domElement );
  93. stats = new Stats();
  94. stats.domElement.style.position = 'absolute';
  95. stats.domElement.style.top = '0px';
  96. container.appendChild( stats.domElement );
  97. //
  98. window.addEventListener( 'resize', onWindowResize, false );
  99. }
  100. function onWindowResize() {
  101. camera.aspect = window.innerWidth / window.innerHeight;
  102. camera.updateProjectionMatrix();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. }
  105. //
  106. var t = 0;
  107. var clock = new THREE.Clock();
  108. function animate() {
  109. var delta = clock.getDelta();
  110. requestAnimationFrame( animate );
  111. if ( t > 1 ) t = 0;
  112. render();
  113. stats.update();
  114. }
  115. function render() {
  116. var timer = Date.now() * 0.0005;
  117. camera.position.x = Math.cos( timer ) * 3;
  118. camera.position.y = 1;
  119. camera.position.z = Math.sin( timer ) * 3;
  120. camera.lookAt( scene.position );
  121. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  122. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  123. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  124. renderer.render( scene, camera );
  125. }
  126. </script>
  127. </body>
  128. </html>