stlViewer.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>STLViewer</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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: white;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #fff;
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. a { color: skyblue }
  24. .button { background:#999; color:#eee; padding:0.2em 0.5em; cursor:pointer }
  25. .highlight { background:orange; color:#fff; }
  26. span {
  27. display: inline-block;
  28. width: 60px;
  29. text-align: center;
  30. }
  31. #infotab{
  32. position:fixed;
  33. z-index:999;
  34. right:10px;
  35. bottom:0px;
  36. max-width:480px;
  37. word-break: break-all;
  38. }
  39. p{
  40. margin:5px !important;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <script src="script/Threejs/build/three.js"></script>
  46. <script src="../../script/jquery.min.js"></script>
  47. <script src="script/Threejs/STLLoader.js"></script>
  48. <script src="script/Threejs/OrbitControls.js"></script>
  49. <script src="script/Threejs/WebGL.js"></script>
  50. <script src="script/Threejs/stats.min.js"></script>
  51. <script src="../../script/ao_module.js"></script>
  52. <div id="infotab">
  53. <p id="filename"></p>
  54. <p id="filepath"></p>
  55. <p>{model_dimension}</p>
  56. <p id="filesize"></p>
  57. </div>
  58. <script>
  59. //Get file information from the hash info
  60. var files = ao_module_loadInputFiles();
  61. var file = "";
  62. if (files.length > 0){
  63. file = files[0];
  64. $("#filename").text(file.filename);
  65. $("#filepath").text("../../media?file=" + file.filepath);
  66. //Get filesize info
  67. $.ajax({
  68. url: "../../system/file_system/getProperties",
  69. data: {path: file.filepath},
  70. success: function(data){
  71. var filesize = ao_module_utils.formatBytes(data.Filesize, 2);
  72. $("#filesize").text(filesize);
  73. }
  74. });
  75. }
  76. ao_module_setWindowTitle("STLviewer - " + $("#filename").text().trim());
  77. var objectSize;
  78. if ( WEBGL.isWebGLAvailable() === false ) {
  79. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  80. }
  81. var container, stats;
  82. var camera, cameraTarget, scene, renderer,controls;
  83. var filename = $("#filepath").text().trim();
  84. init();
  85. animate();
  86. function fillValue(tag,value){
  87. var newcontent = $("#infotab").html();
  88. newcontent = newcontent.split("{" + tag + "}").join(value);
  89. $("#infotab").html(newcontent);
  90. }
  91. function round(value){
  92. return Math.round(value * 100) / 100;
  93. }
  94. function init() {
  95. container = document.createElement( 'div' );
  96. document.body.appendChild( container );
  97. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1500 );
  98. camera.position.set( 0, 0, 30 );
  99. cameraTarget = new THREE.Vector3( 0, 0, 0 );
  100. scene = new THREE.Scene();
  101. scene.background = new THREE.Color( 0xf7f7f7 );
  102. //scene.fog = new THREE.Fog( 0xc9c9c9, 2, 15 );
  103. var loader = new THREE.STLLoader();
  104. loader.load( filename, function ( geometry ) {
  105. var material = new THREE.MeshPhongMaterial( { color: 0x545454, specular: 0x0c0c0c, shininess: 100 } );
  106. var mesh = new THREE.Mesh( geometry, material );
  107. const center = new THREE.Vector3();
  108. mesh.position.set( 0, 0, 0 );
  109. mesh.rotation.set( 0, 0, 0);
  110. mesh.scale.set( 0.1, 0.1, 0.1 );
  111. var box = new THREE.Box3().setFromObject(mesh);
  112. console.log( box.min, box.max, box.getSize(center) );
  113. objectSize = box.getSize(center);
  114. fillValue("model_dimension","W,D,H: " + round(objectSize.x * 10) + ", " + round(objectSize.z * 10) + ", " + round(objectSize.y * 10) + " mm");
  115. var helper = new THREE.Box3Helper( box, 0xffff00 );
  116. scene.add( helper );
  117. mesh.castShadow = true;
  118. mesh.receiveShadow = true;
  119. scene.add( mesh );
  120. } );
  121. // Lights
  122. scene.add( new THREE.HemisphereLight( 0x898989, 0x3f3f3f ) );
  123. addShadowedLight( 1, 1, 1, 0x898989, 1.35 );
  124. addShadowedLight( 0.5, 1, - 1, 0xcccccc, 1 );
  125. // renderer
  126. renderer = new THREE.WebGLRenderer( { antialias: true } );
  127. renderer.setPixelRatio( window.devicePixelRatio );
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. renderer.gammaInput = true;
  130. renderer.gammaOutput = true;
  131. renderer.shadowMap.enabled = true;
  132. container.appendChild( renderer.domElement );
  133. window.addEventListener( 'resize', onWindowResize, false );
  134. controls = new THREE.OrbitControls( camera, renderer.domElement );
  135. controls.minDistance = 3;
  136. controls.maxDistance = 100;
  137. }
  138. function addShadowedLight( x, y, z, color, intensity ) {
  139. var directionalLight = new THREE.DirectionalLight( color, intensity );
  140. directionalLight.position.set( x, y, z );
  141. scene.add( directionalLight );
  142. directionalLight.castShadow = true;
  143. var d = 1;
  144. directionalLight.shadow.camera.left = - d;
  145. directionalLight.shadow.camera.right = d;
  146. directionalLight.shadow.camera.top = d;
  147. directionalLight.shadow.camera.bottom = - d;
  148. directionalLight.shadow.camera.near = 1;
  149. directionalLight.shadow.camera.far = 4;
  150. directionalLight.shadow.mapSize.width = 1024;
  151. directionalLight.shadow.mapSize.height = 1024;
  152. directionalLight.shadow.bias = - 0.002;
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. function animate() {
  160. requestAnimationFrame( animate );
  161. render();
  162. }
  163. function render() {
  164. var timer = Date.now() * 0.0005;
  165. //camera.position.x = Math.cos( timer ) * 3;
  166. //camera.position.z = Math.sin( timer ) * 3;
  167. camera.lookAt( cameraTarget );
  168. renderer.render( scene, camera );
  169. }
  170. /*
  171. var fov = camera.fov, zoom = 1.0, inc = -0.05;
  172. $(document).bind('mousewheel DOMMouseScroll', function(event){
  173. if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
  174. // scroll up --> Zoom in
  175. if (camera.fov > 1){
  176. camera.fov = fov * zoom;
  177. camera.updateProjectionMatrix();
  178. zoom += inc;
  179. }else{
  180. //Cannot be zoomed in anymore!
  181. }
  182. }
  183. else {
  184. if (camera.fov < 100){
  185. // scroll down --> Zoom out
  186. camera.fov = fov * zoom;
  187. camera.updateProjectionMatrix();
  188. zoom -= inc;
  189. }else{
  190. //Cannot be zoomed out anymore!
  191. }
  192. }
  193. });
  194. */
  195. </script>
  196. </body>
  197. </html>