stlViewer.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. function init() {
  105. container = document.createElement( 'div' );
  106. document.body.appendChild( container );
  107. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1500 );
  108. camera.position.set( 0, 0, 30 );
  109. cameraTarget = new THREE.Vector3( 0, 0, 0 );
  110. scene = new THREE.Scene();
  111. scene.background = new THREE.Color( 0xf7f7f7 );
  112. //scene.fog = new THREE.Fog( 0xc9c9c9, 2, 15 );
  113. var loader = new THREE.STLLoader();
  114. loader.load( filename, function ( geometry ) {
  115. var material = new THREE.MeshPhongMaterial( { color: 0x70721e, specular: 0x0c0c0c, shininess: 100 } );
  116. var mesh = new THREE.Mesh( geometry, material );
  117. const center = new THREE.Vector3();
  118. //Scsale it down to original's 10%
  119. mesh.scale.set( 0.1, 0.1, 0.1 );
  120. render();
  121. var bbox = new THREE.Box3().setFromObject(mesh);
  122. //Move the mesh to scene center
  123. console.log(bbox);
  124. mesh.updateMatrixWorld();
  125. console.log(-1 * (bbox.max.x + bbox.min.x));
  126. 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);
  127. mesh.updateMatrixWorld();
  128. console.log("Setting mesh world position: ", mesh.getWorldPosition())
  129. mesh.rotation.set( 0, 0, 0);
  130. render();
  131. var box = new THREE.Box3().setFromObject(mesh);
  132. console.log( box.min, box.max, box.getSize(center) );
  133. objectSize = box.getSize(center);
  134. fillValue("model_dimension","W,D,H: " + round(objectSize.x * 10) + ", " + round(objectSize.z * 10) + ", " + round(objectSize.y * 10) + " mm");
  135. var helper = new THREE.Box3Helper( box, 0xffff00 );
  136. scene.add( helper );
  137. mesh.castShadow = true;
  138. mesh.receiveShadow = true;
  139. scene.add( mesh );
  140. } );
  141. // Lights
  142. scene.add( new THREE.HemisphereLight( 0x898989, 0x3f3f3f ) );
  143. addShadowedLight( 1, 1, 1, 0x898989, 1.35 );
  144. addShadowedLight( 0.5, 1, - 1, 0xcccccc, 1 );
  145. // renderer
  146. renderer = new THREE.WebGLRenderer( { antialias: true } );
  147. renderer.setPixelRatio( window.devicePixelRatio );
  148. renderer.setSize( window.innerWidth, window.innerHeight );
  149. renderer.gammaInput = true;
  150. renderer.gammaOutput = true;
  151. renderer.shadowMap.enabled = true;
  152. container.appendChild( renderer.domElement );
  153. window.addEventListener( 'resize', onWindowResize, false );
  154. controls = new THREE.OrbitControls( camera, renderer.domElement );
  155. controls.minDistance = 3;
  156. controls.maxDistance = 100;
  157. }
  158. function addShadowedLight( x, y, z, color, intensity ) {
  159. var directionalLight = new THREE.DirectionalLight( color, intensity );
  160. directionalLight.position.set( x, y, z );
  161. scene.add( directionalLight );
  162. directionalLight.castShadow = true;
  163. var d = 1;
  164. directionalLight.shadow.camera.left = - d;
  165. directionalLight.shadow.camera.right = d;
  166. directionalLight.shadow.camera.top = d;
  167. directionalLight.shadow.camera.bottom = - d;
  168. directionalLight.shadow.camera.near = 1;
  169. directionalLight.shadow.camera.far = 4;
  170. directionalLight.shadow.mapSize.width = 1024;
  171. directionalLight.shadow.mapSize.height = 1024;
  172. directionalLight.shadow.bias = - 0.002;
  173. }
  174. function onWindowResize() {
  175. camera.aspect = window.innerWidth / window.innerHeight;
  176. camera.updateProjectionMatrix();
  177. renderer.setSize( window.innerWidth, window.innerHeight );
  178. }
  179. function animate() {
  180. requestAnimationFrame( animate );
  181. render();
  182. }
  183. function render() {
  184. var timer = Date.now() * 0.0005;
  185. //camera.position.x = Math.cos( timer ) * 3;
  186. //camera.position.z = Math.sin( timer ) * 3;
  187. camera.lookAt( cameraTarget );
  188. renderer.render( scene, camera );
  189. }
  190. /*
  191. var fov = camera.fov, zoom = 1.0, inc = -0.05;
  192. $(document).bind('mousewheel DOMMouseScroll', function(event){
  193. if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
  194. // scroll up --> Zoom in
  195. if (camera.fov > 1){
  196. camera.fov = fov * zoom;
  197. camera.updateProjectionMatrix();
  198. zoom += inc;
  199. }else{
  200. //Cannot be zoomed in anymore!
  201. }
  202. }
  203. else {
  204. if (camera.fov < 100){
  205. // scroll down --> Zoom out
  206. camera.fov = fov * zoom;
  207. camera.updateProjectionMatrix();
  208. zoom -= inc;
  209. }else{
  210. //Cannot be zoomed out anymore!
  211. }
  212. }
  213. });
  214. */
  215. </script>
  216. </body>
  217. </html>