webgl_loader_stl.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. animate();
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  46. camera.position.set( 2, 2, 3 );
  47. scene = new THREE.Scene();
  48. // Grid
  49. var size = 14, step = 1;
  50. var geometry = new THREE.Geometry();
  51. var material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } );
  52. for ( var i = - size; i <= size; i += step ) {
  53. geometry.vertices.push( new THREE.Vector3( - size, - 0.04, i ) );
  54. geometry.vertices.push( new THREE.Vector3( size, - 0.04, i ) );
  55. geometry.vertices.push( new THREE.Vector3( i, - 0.04, - size ) );
  56. geometry.vertices.push( new THREE.Vector3( i, - 0.04, size ) );
  57. }
  58. var line = new THREE.Line( geometry, material, THREE.LinePieces );
  59. scene.add( line );
  60. var loader = new THREE.STLLoader();
  61. loader.addEventListener( 'load', function ( event ) {
  62. var geometry = event.content;
  63. var material = new THREE.MeshPhongMaterial( { ambient: 0xff0000, color: 0xff0000, specular: 0xffffff } );
  64. scene.add( new THREE.Mesh( geometry, material ) );
  65. } );
  66. loader.load( './models/stl/slotted_disk.stl' );
  67. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  68. scene.add( particleLight );
  69. // Lights
  70. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  71. var directionalLight = new THREE.DirectionalLight( 0xeeeeee );
  72. directionalLight.position.x = Math.random() - 0.5;
  73. directionalLight.position.y = Math.random() - 0.5;
  74. directionalLight.position.z = Math.random() - 0.5;
  75. directionalLight.position.normalize();
  76. scene.add( directionalLight );
  77. pointLight = new THREE.PointLight( 0xffffff, 4 );
  78. pointLight.position = particleLight.position;
  79. scene.add( pointLight );
  80. renderer = new THREE.WebGLRenderer();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( renderer.domElement );
  83. stats = new Stats();
  84. stats.domElement.style.position = 'absolute';
  85. stats.domElement.style.top = '0px';
  86. container.appendChild( stats.domElement );
  87. //
  88. window.addEventListener( 'resize', onWindowResize, false );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. //
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. render();
  99. stats.update();
  100. }
  101. function render() {
  102. var timer = Date.now() * 0.0005;
  103. camera.position.x = Math.cos( timer ) * 3;
  104. camera.position.y = 1;
  105. camera.position.z = Math.sin( timer ) * 3;
  106. camera.lookAt( scene.position );
  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. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>