webgl_loader_stl.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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="https://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. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { STLLoader } from 'three/addons/loaders/STLLoader.js';
  30. let container, stats;
  31. let camera, cameraTarget, scene, renderer;
  32. init();
  33. animate();
  34. function init() {
  35. container = document.createElement( 'div' );
  36. document.body.appendChild( container );
  37. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  38. camera.position.set( 3, 0.15, 3 );
  39. cameraTarget = new THREE.Vector3( 0, - 0.25, 0 );
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0x72645b );
  42. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  43. // Ground
  44. const plane = new THREE.Mesh(
  45. new THREE.PlaneGeometry( 40, 40 ),
  46. new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  47. );
  48. plane.rotation.x = - Math.PI / 2;
  49. plane.position.y = - 0.5;
  50. scene.add( plane );
  51. plane.receiveShadow = true;
  52. // ASCII file
  53. const loader = new STLLoader();
  54. loader.load( './models/stl/ascii/slotted_disk.stl', function ( geometry ) {
  55. const material = new THREE.MeshPhongMaterial( { color: 0xff5533, specular: 0x111111, shininess: 200 } );
  56. const mesh = new THREE.Mesh( geometry, material );
  57. mesh.position.set( 0, - 0.25, 0.6 );
  58. mesh.rotation.set( 0, - Math.PI / 2, 0 );
  59. mesh.scale.set( 0.5, 0.5, 0.5 );
  60. mesh.castShadow = true;
  61. mesh.receiveShadow = true;
  62. scene.add( mesh );
  63. } );
  64. // Binary files
  65. const material = new THREE.MeshPhongMaterial( { color: 0xAAAAAA, specular: 0x111111, shininess: 200 } );
  66. loader.load( './models/stl/binary/pr2_head_pan.stl', function ( geometry ) {
  67. const mesh = new THREE.Mesh( geometry, material );
  68. mesh.position.set( 0, - 0.37, - 0.6 );
  69. mesh.rotation.set( - Math.PI / 2, 0, 0 );
  70. mesh.scale.set( 2, 2, 2 );
  71. mesh.castShadow = true;
  72. mesh.receiveShadow = true;
  73. scene.add( mesh );
  74. } );
  75. loader.load( './models/stl/binary/pr2_head_tilt.stl', function ( geometry ) {
  76. const mesh = new THREE.Mesh( geometry, material );
  77. mesh.position.set( 0.136, - 0.37, - 0.6 );
  78. mesh.rotation.set( - Math.PI / 2, 0.3, 0 );
  79. mesh.scale.set( 2, 2, 2 );
  80. mesh.castShadow = true;
  81. mesh.receiveShadow = true;
  82. scene.add( mesh );
  83. } );
  84. // Colored binary STL
  85. loader.load( './models/stl/binary/colored.stl', function ( geometry ) {
  86. let meshMaterial = material;
  87. if ( geometry.hasColors ) {
  88. meshMaterial = new THREE.MeshPhongMaterial( { opacity: geometry.alpha, vertexColors: true } );
  89. }
  90. const mesh = new THREE.Mesh( geometry, meshMaterial );
  91. mesh.position.set( 0.5, 0.2, 0 );
  92. mesh.rotation.set( - Math.PI / 2, Math.PI / 2, 0 );
  93. mesh.scale.set( 0.3, 0.3, 0.3 );
  94. mesh.castShadow = true;
  95. mesh.receiveShadow = true;
  96. scene.add( mesh );
  97. } );
  98. // Lights
  99. scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
  100. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  101. addShadowedLight( 0.5, 1, - 1, 0xffaa00, 1 );
  102. // renderer
  103. renderer = new THREE.WebGLRenderer( { antialias: true } );
  104. renderer.setPixelRatio( window.devicePixelRatio );
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. renderer.outputEncoding = THREE.sRGBEncoding;
  107. renderer.shadowMap.enabled = true;
  108. container.appendChild( renderer.domElement );
  109. // stats
  110. stats = new Stats();
  111. container.appendChild( stats.dom );
  112. //
  113. window.addEventListener( 'resize', onWindowResize );
  114. }
  115. function addShadowedLight( x, y, z, color, intensity ) {
  116. const directionalLight = new THREE.DirectionalLight( color, intensity );
  117. directionalLight.position.set( x, y, z );
  118. scene.add( directionalLight );
  119. directionalLight.castShadow = true;
  120. const d = 1;
  121. directionalLight.shadow.camera.left = - d;
  122. directionalLight.shadow.camera.right = d;
  123. directionalLight.shadow.camera.top = d;
  124. directionalLight.shadow.camera.bottom = - d;
  125. directionalLight.shadow.camera.near = 1;
  126. directionalLight.shadow.camera.far = 4;
  127. directionalLight.shadow.bias = - 0.002;
  128. }
  129. function onWindowResize() {
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. function animate() {
  135. requestAnimationFrame( animate );
  136. render();
  137. stats.update();
  138. }
  139. function render() {
  140. const timer = Date.now() * 0.0005;
  141. camera.position.x = Math.cos( timer ) * 3;
  142. camera.position.z = Math.sin( timer ) * 3;
  143. camera.lookAt( cameraTarget );
  144. renderer.render( scene, camera );
  145. }
  146. </script>
  147. </body>
  148. </html>