webgl_loader_stl.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - STL</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. .button { background:#999; color:#eee; padding:0.2em 0.5em; cursor:pointer }
  25. .highlight { background:orange; color:#fff; }
  26. span {
  27. display: inline-block;
  28. width: 60px;
  29. float: left;
  30. text-align: center;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <a href="http://threejs.org" target="_blank">three.js</a> -
  37. STL loader test by <a href="https://github.com/aleeper">aleeper</a>. PR2 head from <a href="http://www.ros.org/wiki/pr2_description">www.ros.org</a>
  38. </div>
  39. <script src="../build/three.min.js"></script>
  40. <script src="js/loaders/STLLoader.js"></script>
  41. <script src="js/wip/TypedGeometry.js"></script>
  42. <script src="js/Detector.js"></script>
  43. <script src="js/libs/stats.min.js"></script>
  44. <script>
  45. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  46. var container, stats;
  47. var camera, cameraTarget, scene, renderer;
  48. init();
  49. animate();
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  54. camera.position.set( 3, 0.15, 3 );
  55. cameraTarget = new THREE.Vector3( 0, -0.25, 0 );
  56. scene = new THREE.Scene();
  57. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  58. // Ground
  59. var plane = new THREE.Mesh( new THREE.PlaneGeometry( 40, 40 ), new THREE.MeshPhongMaterial( { ambient: 0x999999, color: 0x999999, specular: 0x101010 } ) );
  60. plane.rotation.x = -Math.PI/2;
  61. plane.position.y = -0.5;
  62. scene.add( plane );
  63. plane.receiveShadow = true;
  64. // ASCII file
  65. var loader = new THREE.STLLoader();
  66. loader.addEventListener( 'load', function ( event ) {
  67. var geometry = event.content;
  68. var material = new THREE.MeshPhongMaterial( { ambient: 0xff5533, color: 0xff5533, specular: 0x111111, shininess: 200 } );
  69. var mesh = new THREE.Mesh( geometry, material );
  70. mesh.position.set( 0, - 0.25, 0.6 );
  71. mesh.rotation.set( 0, - Math.PI / 2, 0 );
  72. mesh.scale.set( 0.5, 0.5, 0.5 );
  73. mesh.castShadow = true;
  74. mesh.receiveShadow = true;
  75. scene.add( mesh );
  76. } );
  77. loader.load( './models/stl/ascii/slotted_disk.stl' );
  78. // Binary files
  79. var material = new THREE.MeshPhongMaterial( { ambient: 0x555555, color: 0xAAAAAA, specular: 0x111111, shininess: 200 } );
  80. var loader = new THREE.STLLoader();
  81. loader.addEventListener( 'load', function ( event ) {
  82. var geometry = event.content;
  83. var mesh = new THREE.Mesh( geometry, material );
  84. mesh.position.set( 0, - 0.37, - 0.6 );
  85. mesh.rotation.set( - Math.PI / 2, 0, 0 );
  86. mesh.scale.set( 2, 2, 2 );
  87. mesh.castShadow = true;
  88. mesh.receiveShadow = true;
  89. scene.add( mesh );
  90. } );
  91. loader.load( './models/stl/binary/pr2_head_pan.stl' );
  92. var loader = new THREE.STLLoader();
  93. loader.addEventListener( 'load', function ( event ) {
  94. var geometry = event.content;
  95. var mesh = new THREE.Mesh( geometry, material );
  96. mesh.position.set( 0.136, - 0.37, - 0.6 );
  97. mesh.rotation.set( - Math.PI / 2, 0.3, 0 );
  98. mesh.scale.set( 2, 2, 2 );
  99. mesh.castShadow = true;
  100. mesh.receiveShadow = true;
  101. scene.add( mesh );
  102. } );
  103. loader.load( './models/stl/binary/pr2_head_tilt.stl' );
  104. // Lights
  105. scene.add( new THREE.AmbientLight( 0x777777 ) );
  106. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  107. addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 );
  108. // renderer
  109. renderer = new THREE.WebGLRenderer( { antialias: true } );
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. renderer.setClearColor( scene.fog.color, 1 );
  112. renderer.gammaInput = true;
  113. renderer.gammaOutput = true;
  114. renderer.shadowMapEnabled = true;
  115. renderer.shadowMapCullFace = THREE.CullFaceBack;
  116. container.appendChild( renderer.domElement );
  117. // stats
  118. stats = new Stats();
  119. stats.domElement.style.position = 'absolute';
  120. stats.domElement.style.top = '0px';
  121. container.appendChild( stats.domElement );
  122. //
  123. window.addEventListener( 'resize', onWindowResize, false );
  124. }
  125. function addShadowedLight( x, y, z, color, intensity ) {
  126. var directionalLight = new THREE.DirectionalLight( color, intensity );
  127. directionalLight.position.set( x, y, z )
  128. scene.add( directionalLight );
  129. directionalLight.castShadow = true;
  130. // directionalLight.shadowCameraVisible = true;
  131. var d = 1;
  132. directionalLight.shadowCameraLeft = -d;
  133. directionalLight.shadowCameraRight = d;
  134. directionalLight.shadowCameraTop = d;
  135. directionalLight.shadowCameraBottom = -d;
  136. directionalLight.shadowCameraNear = 1;
  137. directionalLight.shadowCameraFar = 4;
  138. directionalLight.shadowMapWidth = 1024;
  139. directionalLight.shadowMapHeight = 1024;
  140. directionalLight.shadowBias = -0.005;
  141. directionalLight.shadowDarkness = 0.15;
  142. }
  143. function onWindowResize() {
  144. camera.aspect = window.innerWidth / window.innerHeight;
  145. camera.updateProjectionMatrix();
  146. renderer.setSize( window.innerWidth, window.innerHeight );
  147. }
  148. function animate() {
  149. requestAnimationFrame( animate );
  150. render();
  151. stats.update();
  152. }
  153. function render() {
  154. var timer = Date.now() * 0.0005;
  155. camera.position.x = Math.cos( timer ) * 3;
  156. camera.position.z = Math.sin( timer ) * 3;
  157. camera.lookAt( cameraTarget );
  158. renderer.render( scene, camera );
  159. }
  160. </script>
  161. </body>
  162. </html>