webgl_loader_stl.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  12. STL loader test by <a href="https://github.com/aleeper" target="_blank" rel="noopener">aleeper</a>.<br/>
  13. PR2 head from <a href="http://www.ros.org/wiki/pr2_description">www.ros.org</a>
  14. </div>
  15. <script type="module">
  16. import {
  17. Color,
  18. DirectionalLight,
  19. Fog,
  20. HemisphereLight,
  21. Mesh,
  22. MeshPhongMaterial,
  23. PerspectiveCamera,
  24. PlaneBufferGeometry,
  25. Scene,
  26. Vector3,
  27. VertexColors,
  28. WebGLRenderer,
  29. } from "../build/three.module.js";
  30. import Stats from './jsm/libs/stats.module.js';
  31. import { STLLoader } from './jsm/loaders/STLLoader.js';
  32. var container, stats;
  33. var camera, cameraTarget, scene, renderer;
  34. init();
  35. animate();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. camera = new PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  40. camera.position.set( 3, 0.15, 3 );
  41. cameraTarget = new Vector3( 0, - 0.25, 0 );
  42. scene = new Scene();
  43. scene.background = new Color( 0x72645b );
  44. scene.fog = new Fog( 0x72645b, 2, 15 );
  45. // Ground
  46. var plane = new Mesh(
  47. new PlaneBufferGeometry( 40, 40 ),
  48. new MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  49. );
  50. plane.rotation.x = - Math.PI / 2;
  51. plane.position.y = - 0.5;
  52. scene.add( plane );
  53. plane.receiveShadow = true;
  54. // ASCII file
  55. var loader = new STLLoader();
  56. loader.load( './models/stl/ascii/slotted_disk.stl', function ( geometry ) {
  57. var material = new MeshPhongMaterial( { color: 0xff5533, specular: 0x111111, shininess: 200 } );
  58. var mesh = new Mesh( geometry, material );
  59. mesh.position.set( 0, - 0.25, 0.6 );
  60. mesh.rotation.set( 0, - Math.PI / 2, 0 );
  61. mesh.scale.set( 0.5, 0.5, 0.5 );
  62. mesh.castShadow = true;
  63. mesh.receiveShadow = true;
  64. scene.add( mesh );
  65. } );
  66. // Binary files
  67. var material = new MeshPhongMaterial( { color: 0xAAAAAA, specular: 0x111111, shininess: 200 } );
  68. loader.load( './models/stl/binary/pr2_head_pan.stl', function ( geometry ) {
  69. var mesh = new Mesh( geometry, material );
  70. mesh.position.set( 0, - 0.37, - 0.6 );
  71. mesh.rotation.set( - Math.PI / 2, 0, 0 );
  72. mesh.scale.set( 2, 2, 2 );
  73. mesh.castShadow = true;
  74. mesh.receiveShadow = true;
  75. scene.add( mesh );
  76. } );
  77. loader.load( './models/stl/binary/pr2_head_tilt.stl', function ( geometry ) {
  78. var mesh = new Mesh( geometry, material );
  79. mesh.position.set( 0.136, - 0.37, - 0.6 );
  80. mesh.rotation.set( - Math.PI / 2, 0.3, 0 );
  81. mesh.scale.set( 2, 2, 2 );
  82. mesh.castShadow = true;
  83. mesh.receiveShadow = true;
  84. scene.add( mesh );
  85. } );
  86. // Colored binary STL
  87. loader.load( './models/stl/binary/colored.stl', function ( geometry ) {
  88. var meshMaterial = material;
  89. if ( geometry.hasColors ) {
  90. meshMaterial = new MeshPhongMaterial( { opacity: geometry.alpha, vertexColors: VertexColors } );
  91. }
  92. var mesh = new Mesh( geometry, meshMaterial );
  93. mesh.position.set( 0.5, 0.2, 0 );
  94. mesh.rotation.set( - Math.PI / 2, Math.PI / 2, 0 );
  95. mesh.scale.set( 0.3, 0.3, 0.3 );
  96. mesh.castShadow = true;
  97. mesh.receiveShadow = true;
  98. scene.add( mesh );
  99. } );
  100. // Lights
  101. scene.add( new HemisphereLight( 0x443333, 0x111122 ) );
  102. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  103. addShadowedLight( 0.5, 1, - 1, 0xffaa00, 1 );
  104. // renderer
  105. renderer = new WebGLRenderer( { antialias: true } );
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. renderer.gammaInput = true;
  109. renderer.gammaOutput = true;
  110. renderer.shadowMap.enabled = true;
  111. container.appendChild( renderer.domElement );
  112. // stats
  113. stats = new Stats();
  114. container.appendChild( stats.dom );
  115. //
  116. window.addEventListener( 'resize', onWindowResize, false );
  117. }
  118. function addShadowedLight( x, y, z, color, intensity ) {
  119. var directionalLight = new DirectionalLight( color, intensity );
  120. directionalLight.position.set( x, y, z );
  121. scene.add( directionalLight );
  122. directionalLight.castShadow = true;
  123. var d = 1;
  124. directionalLight.shadow.camera.left = - d;
  125. directionalLight.shadow.camera.right = d;
  126. directionalLight.shadow.camera.top = d;
  127. directionalLight.shadow.camera.bottom = - d;
  128. directionalLight.shadow.camera.near = 1;
  129. directionalLight.shadow.camera.far = 4;
  130. directionalLight.shadow.mapSize.width = 1024;
  131. directionalLight.shadow.mapSize.height = 1024;
  132. directionalLight.shadow.bias = - 0.002;
  133. }
  134. function onWindowResize() {
  135. camera.aspect = window.innerWidth / window.innerHeight;
  136. camera.updateProjectionMatrix();
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. }
  139. function animate() {
  140. requestAnimationFrame( animate );
  141. render();
  142. stats.update();
  143. }
  144. function render() {
  145. var timer = Date.now() * 0.0005;
  146. camera.position.x = Math.cos( timer ) * 3;
  147. camera.position.z = Math.sin( timer ) * 3;
  148. camera.lookAt( cameraTarget );
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>