stlViewer.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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" style="display:none;"></p>
  55. <a id="displayFilepath" style="cursor: pointer;" onclick="openThisFileInFM(this);"></a>
  56. <p>{model_dimension}</p>
  57. <p id="filesize"></p>
  58. </div>
  59. <script>
  60. //Get file information from the hash info
  61. var files = ao_module_loadInputFiles();
  62. var file = "";
  63. if (files.length > 0){
  64. file = files[0];
  65. $("#filename").text(file.filename);
  66. $("#filepath").text("../../media?file=" + file.filepath);
  67. $("#displayFilepath").text(file.filepath);
  68. //Get filesize info
  69. $.ajax({
  70. url: "../../system/file_system/getProperties",
  71. data: {path: file.filepath},
  72. success: function(data){
  73. var filesize = ao_module_utils.formatBytes(data.Filesize, 2);
  74. $("#filesize").text(filesize);
  75. }
  76. });
  77. }
  78. ao_module_setWindowTitle("STLviewer - " + $("#filename").text().trim());
  79. var objectSize;
  80. if ( WEBGL.isWebGLAvailable() === false ) {
  81. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  82. }
  83. var container, stats;
  84. var camera, cameraTarget, scene, renderer,controls;
  85. var filename = $("#filepath").text().trim();
  86. init();
  87. animate();
  88. function fillValue(tag,value){
  89. var newcontent = $("#infotab").html();
  90. newcontent = newcontent.split("{" + tag + "}").join(value);
  91. $("#infotab").html(newcontent);
  92. }
  93. function openThisFileInFM(link){
  94. var fileLocation = $(link).text();
  95. //Open the file using ao_module function call
  96. var tmp = fileLocation.split("/");
  97. var targetFilename = tmp.pop();
  98. var targetFolder = tmp.join("/");
  99. ao_module_openPath(targetFolder, targetFilename);
  100. }
  101. function round(value){
  102. return Math.round(value * 100) / 100;
  103. }
  104. var stlMesh = undefined;
  105. function init() {
  106. container = document.createElement( 'div' );
  107. document.body.appendChild( container );
  108. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1500 );
  109. camera.position.set( 0, 30, 30 );
  110. //cameraTarget = new THREE.Vector3( 0, 0, 0 );
  111. scene = new THREE.Scene();
  112. scene.background = new THREE.Color( 0xf7f7f7 );
  113. //scene.fog = new THREE.Fog( 0xc9c9c9, 2, 15 );
  114. var loader = new THREE.STLLoader();
  115. loader.load( filename, function ( geometry ) {
  116. var material = new THREE.MeshPhongMaterial( { color: 0x70721e, specular: 0x0c0c0c, shininess: 100 } );
  117. var mesh = new THREE.Mesh( geometry, material );
  118. stlMesh = mesh;
  119. const center = new THREE.Vector3();
  120. //Scsale it down to original's 10%
  121. mesh.scale.set( 0.1, 0.1, 0.1 );
  122. render();
  123. var bbox = new THREE.Box3().setFromObject(mesh);
  124. //Move the mesh to scene center
  125. console.log(bbox);
  126. mesh.updateMatrixWorld();
  127. console.log((bbox.max.x - bbox.min.x));
  128. mesh.rotateOnAxis(new THREE.Vector3( 1, 0, 0 ), -1.57079633);
  129. //mesh.position.set(-1 * (bbox.max.x + bbox.min.x) / 2, -1 * (bbox.max.y + bbox.min.y) / 2, -1 * (bbox.max.z + bbox.min.z) / 2);
  130. mesh.geometry.center()
  131. mesh.updateMatrixWorld();
  132. console.log("Setting mesh world position: ", mesh.getWorldPosition())
  133. //mesh.rotation.set( 0, 0, 0);
  134. render();
  135. var box = new THREE.Box3().setFromObject(mesh);
  136. console.log( box.min, box.max, box.getSize(center) );
  137. objectSize = box.getSize(center);
  138. fillValue("model_dimension","W,D,H: " + round(objectSize.x * 10) + ", " + round(objectSize.z * 10) + ", " + round(objectSize.y * 10) + " mm");
  139. var helper = new THREE.Box3Helper( box, 0xffff00 );
  140. scene.add( helper );
  141. mesh.castShadow = true;
  142. mesh.receiveShadow = true;
  143. scene.add( mesh );
  144. controls.target = mesh.getWorldPosition();
  145. } );
  146. // Lights
  147. scene.add( new THREE.HemisphereLight( 0x898989, 0x3f3f3f ) );
  148. addShadowedLight( 1, 1, 1, 0x898989, 1.35 );
  149. addShadowedLight( 0.5, 1, - 1, 0xcccccc, 1 );
  150. //Create renderer
  151. renderer = new THREE.WebGLRenderer( { antialias: true } );
  152. renderer.setPixelRatio( window.devicePixelRatio );
  153. renderer.setSize( window.innerWidth, window.innerHeight );
  154. renderer.gammaInput = true;
  155. renderer.gammaOutput = true;
  156. renderer.shadowMap.enabled = true;
  157. container.appendChild( renderer.domElement );
  158. window.addEventListener( 'resize', onWindowResize, false );
  159. controls = new THREE.OrbitControls( camera, renderer.domElement );
  160. controls.minDistance = 3;
  161. controls.maxDistance = 100;
  162. controls.update();
  163. }
  164. function addShadowedLight( x, y, z, color, intensity ) {
  165. var directionalLight = new THREE.DirectionalLight( color, intensity );
  166. directionalLight.position.set( x, y, z );
  167. scene.add( directionalLight );
  168. directionalLight.castShadow = true;
  169. var d = 1;
  170. directionalLight.shadow.camera.left = - d;
  171. directionalLight.shadow.camera.right = d;
  172. directionalLight.shadow.camera.top = d;
  173. directionalLight.shadow.camera.bottom = - d;
  174. directionalLight.shadow.camera.near = 1;
  175. directionalLight.shadow.camera.far = 4;
  176. directionalLight.shadow.mapSize.width = 1024;
  177. directionalLight.shadow.mapSize.height = 1024;
  178. directionalLight.shadow.bias = - 0.002;
  179. }
  180. function onWindowResize() {
  181. camera.aspect = window.innerWidth / window.innerHeight;
  182. camera.updateProjectionMatrix();
  183. renderer.setSize( window.innerWidth, window.innerHeight );
  184. }
  185. function animate() {
  186. requestAnimationFrame( animate );
  187. render();
  188. }
  189. function render() {
  190. var timer = Date.now() * 0.0005;
  191. //camera.position.x = Math.cos( timer ) * 3;
  192. //camera.position.z = Math.sin( timer ) * 3;
  193. //camera.lookAt( cameraTarget );
  194. controls.update();
  195. renderer.render( scene, camera );
  196. }
  197. /*
  198. var fov = camera.fov, zoom = 1.0, inc = -0.05;
  199. $(document).bind('mousewheel DOMMouseScroll', function(event){
  200. if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
  201. // scroll up --> Zoom in
  202. if (camera.fov > 1){
  203. camera.fov = fov * zoom;
  204. camera.updateProjectionMatrix();
  205. zoom += inc;
  206. }else{
  207. //Cannot be zoomed in anymore!
  208. }
  209. }
  210. else {
  211. if (camera.fov < 100){
  212. // scroll down --> Zoom out
  213. camera.fov = fov * zoom;
  214. camera.updateProjectionMatrix();
  215. zoom -= inc;
  216. }else{
  217. //Cannot be zoomed out anymore!
  218. }
  219. }
  220. });
  221. */
  222. </script>
  223. </body>
  224. </html>