1
0

webgl_loader_stl.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  34. camera.position.set( 3, 0.15, 3 );
  35. cameraTarget = new THREE.Vector3( 0, - 0.25, 0 );
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0x72645b );
  38. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  39. // Ground
  40. const plane = new THREE.Mesh(
  41. new THREE.PlaneGeometry( 40, 40 ),
  42. new THREE.MeshPhongMaterial( { color: 0xcbcbcb, specular: 0x474747 } )
  43. );
  44. plane.rotation.x = - Math.PI / 2;
  45. plane.position.y = - 0.5;
  46. scene.add( plane );
  47. plane.receiveShadow = true;
  48. // ASCII file
  49. const loader = new STLLoader();
  50. loader.load( './models/stl/ascii/slotted_disk.stl', function ( geometry ) {
  51. const material = new THREE.MeshPhongMaterial( { color: 0xff9c7c, specular: 0x494949, shininess: 200 } );
  52. const mesh = new THREE.Mesh( geometry, material );
  53. mesh.position.set( 0, - 0.25, 0.6 );
  54. mesh.rotation.set( 0, - Math.PI / 2, 0 );
  55. mesh.scale.set( 0.5, 0.5, 0.5 );
  56. mesh.castShadow = true;
  57. mesh.receiveShadow = true;
  58. scene.add( mesh );
  59. } );
  60. // Binary files
  61. const material = new THREE.MeshPhongMaterial( { color: 0xd5d5d5, specular: 0x494949, shininess: 200 } );
  62. loader.load( './models/stl/binary/pr2_head_pan.stl', function ( geometry ) {
  63. const mesh = new THREE.Mesh( geometry, material );
  64. mesh.position.set( 0, - 0.37, - 0.6 );
  65. mesh.rotation.set( - Math.PI / 2, 0, 0 );
  66. mesh.scale.set( 2, 2, 2 );
  67. mesh.castShadow = true;
  68. mesh.receiveShadow = true;
  69. scene.add( mesh );
  70. } );
  71. loader.load( './models/stl/binary/pr2_head_tilt.stl', function ( geometry ) {
  72. const mesh = new THREE.Mesh( geometry, material );
  73. mesh.position.set( 0.136, - 0.37, - 0.6 );
  74. mesh.rotation.set( - Math.PI / 2, 0.3, 0 );
  75. mesh.scale.set( 2, 2, 2 );
  76. mesh.castShadow = true;
  77. mesh.receiveShadow = true;
  78. scene.add( mesh );
  79. } );
  80. // Colored binary STL
  81. loader.load( './models/stl/binary/colored.stl', function ( geometry ) {
  82. let meshMaterial = material;
  83. if ( geometry.hasColors ) {
  84. meshMaterial = new THREE.MeshPhongMaterial( { opacity: geometry.alpha, vertexColors: true } );
  85. }
  86. const mesh = new THREE.Mesh( geometry, meshMaterial );
  87. mesh.position.set( 0.5, 0.2, 0 );
  88. mesh.rotation.set( - Math.PI / 2, Math.PI / 2, 0 );
  89. mesh.scale.set( 0.3, 0.3, 0.3 );
  90. mesh.castShadow = true;
  91. mesh.receiveShadow = true;
  92. scene.add( mesh );
  93. } );
  94. // Lights
  95. scene.add( new THREE.HemisphereLight( 0x8d7c7c, 0x494966, 3 ) );
  96. addShadowedLight( 1, 1, 1, 0xffffff, 3.5 );
  97. addShadowedLight( 0.5, 1, - 1, 0xffd500, 3 );
  98. // renderer
  99. renderer = new THREE.WebGLRenderer( { antialias: true } );
  100. renderer.setPixelRatio( window.devicePixelRatio );
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. renderer.setAnimationLoop( animate );
  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. render();
  132. stats.update();
  133. }
  134. function render() {
  135. const timer = Date.now() * 0.0005;
  136. camera.position.x = Math.cos( timer ) * 3;
  137. camera.position.z = Math.sin( timer ) * 3;
  138. camera.lookAt( cameraTarget );
  139. renderer.render( scene, camera );
  140. }
  141. </script>
  142. </body>
  143. </html>