webgl_loader_stl.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. text-align: center;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="info">
  35. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  36. STL loader test by <a href="https://github.com/aleeper" target="_blank" rel="noopener">aleeper</a>. PR2 head from <a href="http://www.ros.org/wiki/pr2_description">www.ros.org</a>
  37. </div>
  38. <script src="../build/three.js"></script>
  39. <script src="js/loaders/STLLoader.js"></script>
  40. <script src="js/WebGL.js"></script>
  41. <script src="js/libs/stats.min.js"></script>
  42. <script>
  43. if ( WEBGL.isWebGLAvailable() === false ) {
  44. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  45. }
  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.background = new THREE.Color( 0x72645b );
  58. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  59. // Ground
  60. var plane = new THREE.Mesh(
  61. new THREE.PlaneBufferGeometry( 40, 40 ),
  62. new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  63. );
  64. plane.rotation.x = - Math.PI / 2;
  65. plane.position.y = - 0.5;
  66. scene.add( plane );
  67. plane.receiveShadow = true;
  68. // ASCII file
  69. var loader = new THREE.STLLoader();
  70. loader.load( './models/stl/ascii/slotted_disk.stl', function ( geometry ) {
  71. var material = new THREE.MeshPhongMaterial( { color: 0xff5533, specular: 0x111111, shininess: 200 } );
  72. var mesh = new THREE.Mesh( geometry, material );
  73. mesh.position.set( 0, - 0.25, 0.6 );
  74. mesh.rotation.set( 0, - Math.PI / 2, 0 );
  75. mesh.scale.set( 0.5, 0.5, 0.5 );
  76. mesh.castShadow = true;
  77. mesh.receiveShadow = true;
  78. scene.add( mesh );
  79. } );
  80. // Binary files
  81. var material = new THREE.MeshPhongMaterial( { color: 0xAAAAAA, specular: 0x111111, shininess: 200 } );
  82. loader.load( './models/stl/binary/pr2_head_pan.stl', function ( geometry ) {
  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_tilt.stl', function ( geometry ) {
  92. var mesh = new THREE.Mesh( geometry, material );
  93. mesh.position.set( 0.136, - 0.37, - 0.6 );
  94. mesh.rotation.set( - Math.PI / 2, 0.3, 0 );
  95. mesh.scale.set( 2, 2, 2 );
  96. mesh.castShadow = true;
  97. mesh.receiveShadow = true;
  98. scene.add( mesh );
  99. } );
  100. // Colored binary STL
  101. loader.load( './models/stl/binary/colored.stl', function ( geometry ) {
  102. var meshMaterial = material;
  103. if ( geometry.hasColors ) {
  104. meshMaterial = new THREE.MeshPhongMaterial( { opacity: geometry.alpha, vertexColors: THREE.VertexColors } );
  105. }
  106. var mesh = new THREE.Mesh( geometry, meshMaterial );
  107. mesh.position.set( 0.5, 0.2, 0 );
  108. mesh.rotation.set( - Math.PI / 2, Math.PI / 2, 0 );
  109. mesh.scale.set( 0.3, 0.3, 0.3 );
  110. mesh.castShadow = true;
  111. mesh.receiveShadow = true;
  112. scene.add( mesh );
  113. } );
  114. // Lights
  115. scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
  116. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  117. addShadowedLight( 0.5, 1, - 1, 0xffaa00, 1 );
  118. // renderer
  119. renderer = new THREE.WebGLRenderer( { antialias: true } );
  120. renderer.setPixelRatio( window.devicePixelRatio );
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. renderer.gammaInput = true;
  123. renderer.gammaOutput = true;
  124. renderer.shadowMap.enabled = true;
  125. container.appendChild( renderer.domElement );
  126. // stats
  127. stats = new Stats();
  128. container.appendChild( stats.dom );
  129. //
  130. window.addEventListener( 'resize', onWindowResize, false );
  131. }
  132. function addShadowedLight( x, y, z, color, intensity ) {
  133. var directionalLight = new THREE.DirectionalLight( color, intensity );
  134. directionalLight.position.set( x, y, z );
  135. scene.add( directionalLight );
  136. directionalLight.castShadow = true;
  137. var d = 1;
  138. directionalLight.shadow.camera.left = - d;
  139. directionalLight.shadow.camera.right = d;
  140. directionalLight.shadow.camera.top = d;
  141. directionalLight.shadow.camera.bottom = - d;
  142. directionalLight.shadow.camera.near = 1;
  143. directionalLight.shadow.camera.far = 4;
  144. directionalLight.shadow.mapSize.width = 1024;
  145. directionalLight.shadow.mapSize.height = 1024;
  146. directionalLight.shadow.bias = - 0.002;
  147. }
  148. function onWindowResize() {
  149. camera.aspect = window.innerWidth / window.innerHeight;
  150. camera.updateProjectionMatrix();
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. render();
  156. stats.update();
  157. }
  158. function render() {
  159. var timer = Date.now() * 0.0005;
  160. camera.position.x = Math.cos( timer ) * 3;
  161. camera.position.z = Math.sin( timer ) * 3;
  162. camera.lookAt( cameraTarget );
  163. renderer.render( scene, camera );
  164. }
  165. </script>
  166. </body>
  167. </html>