stlViewer.html 8.0 KB

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