webgl_loader_stl.html 5.4 KB

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