webgl_loader_stl_module.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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="js/WebGL.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script type='module'>
  41. import {
  42. PerspectiveCamera,
  43. Scene,
  44. WebGLRenderer,
  45. Mesh,
  46. HemisphereLight,
  47. DirectionalLight,
  48. Vector3,
  49. Color,
  50. VertexColors,
  51. Fog,
  52. PlaneBufferGeometry,
  53. MeshPhongMaterial
  54. } from '../build/three.module.js';
  55. import { STLLoader } from './jsm/loaders/STLLoader.js';
  56. if ( WEBGL.isWebGLAvailable() === false ) {
  57. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  58. }
  59. var container, stats;
  60. var camera, cameraTarget, scene, renderer;
  61. init();
  62. animate();
  63. function init() {
  64. container = document.createElement( 'div' );
  65. document.body.appendChild( container );
  66. camera = new PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  67. camera.position.set( 3, 0.15, 3 );
  68. cameraTarget = new Vector3( 0, - 0.25, 0 );
  69. scene = new Scene();
  70. scene.background = new Color( 0x72645b );
  71. scene.fog = new Fog( 0x72645b, 2, 15 );
  72. // Ground
  73. var plane = new Mesh(
  74. new PlaneBufferGeometry( 40, 40 ),
  75. new MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  76. );
  77. plane.rotation.x = - Math.PI / 2;
  78. plane.position.y = - 0.5;
  79. scene.add( plane );
  80. plane.receiveShadow = true;
  81. // ASCII file
  82. var loader = new STLLoader();
  83. loader.load( './models/stl/ascii/slotted_disk.stl', function ( geometry ) {
  84. var material = new MeshPhongMaterial( { color: 0xff5533, specular: 0x111111, shininess: 200 } );
  85. var mesh = new Mesh( geometry, material );
  86. mesh.position.set( 0, - 0.25, 0.6 );
  87. mesh.rotation.set( 0, - Math.PI / 2, 0 );
  88. mesh.scale.set( 0.5, 0.5, 0.5 );
  89. mesh.castShadow = true;
  90. mesh.receiveShadow = true;
  91. scene.add( mesh );
  92. } );
  93. // Binary files
  94. var material = new MeshPhongMaterial( { color: 0xAAAAAA, specular: 0x111111, shininess: 200 } );
  95. loader.load( './models/stl/binary/pr2_head_pan.stl', function ( geometry ) {
  96. var mesh = new Mesh( geometry, material );
  97. mesh.position.set( 0, - 0.37, - 0.6 );
  98. mesh.rotation.set( - Math.PI / 2, 0, 0 );
  99. mesh.scale.set( 2, 2, 2 );
  100. mesh.castShadow = true;
  101. mesh.receiveShadow = true;
  102. scene.add( mesh );
  103. } );
  104. loader.load( './models/stl/binary/pr2_head_tilt.stl', function ( geometry ) {
  105. var mesh = new Mesh( geometry, material );
  106. mesh.position.set( 0.136, - 0.37, - 0.6 );
  107. mesh.rotation.set( - Math.PI / 2, 0.3, 0 );
  108. mesh.scale.set( 2, 2, 2 );
  109. mesh.castShadow = true;
  110. mesh.receiveShadow = true;
  111. scene.add( mesh );
  112. } );
  113. // Colored binary STL
  114. loader.load( './models/stl/binary/colored.stl', function ( geometry ) {
  115. var meshMaterial = material;
  116. if ( geometry.hasColors ) {
  117. meshMaterial = new MeshPhongMaterial( { opacity: geometry.alpha, vertexColors: VertexColors } );
  118. }
  119. var mesh = new Mesh( geometry, meshMaterial );
  120. mesh.position.set( 0.5, 0.2, 0 );
  121. mesh.rotation.set( - Math.PI / 2, Math.PI / 2, 0 );
  122. mesh.scale.set( 0.3, 0.3, 0.3 );
  123. mesh.castShadow = true;
  124. mesh.receiveShadow = true;
  125. scene.add( mesh );
  126. } );
  127. // Lights
  128. scene.add( new HemisphereLight( 0x443333, 0x111122 ) );
  129. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  130. addShadowedLight( 0.5, 1, - 1, 0xffaa00, 1 );
  131. // renderer
  132. renderer = new WebGLRenderer( { antialias: true } );
  133. renderer.setPixelRatio( window.devicePixelRatio );
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. renderer.gammaInput = true;
  136. renderer.gammaOutput = true;
  137. renderer.shadowMap.enabled = true;
  138. container.appendChild( renderer.domElement );
  139. // stats
  140. stats = new Stats();
  141. container.appendChild( stats.dom );
  142. //
  143. window.addEventListener( 'resize', onWindowResize, false );
  144. }
  145. function addShadowedLight( x, y, z, color, intensity ) {
  146. var directionalLight = new DirectionalLight( color, intensity );
  147. directionalLight.position.set( x, y, z );
  148. scene.add( directionalLight );
  149. directionalLight.castShadow = true;
  150. var d = 1;
  151. directionalLight.shadow.camera.left = - d;
  152. directionalLight.shadow.camera.right = d;
  153. directionalLight.shadow.camera.top = d;
  154. directionalLight.shadow.camera.bottom = - d;
  155. directionalLight.shadow.camera.near = 1;
  156. directionalLight.shadow.camera.far = 4;
  157. directionalLight.shadow.mapSize.width = 1024;
  158. directionalLight.shadow.mapSize.height = 1024;
  159. directionalLight.shadow.bias = - 0.002;
  160. }
  161. function onWindowResize() {
  162. camera.aspect = window.innerWidth / window.innerHeight;
  163. camera.updateProjectionMatrix();
  164. renderer.setSize( window.innerWidth, window.innerHeight );
  165. }
  166. function animate() {
  167. requestAnimationFrame( animate );
  168. render();
  169. stats.update();
  170. }
  171. function render() {
  172. var timer = Date.now() * 0.0005;
  173. camera.position.x = Math.cos( timer ) * 3;
  174. camera.position.z = Math.sin( timer ) * 3;
  175. camera.lookAt( cameraTarget );
  176. renderer.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>