webgl_loader_stl.html 5.3 KB

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