added threejs stl viewer html testing

Mon, 03 Apr 2017 09:10:41 +0200

author
mdd
date
Mon, 03 Apr 2017 09:10:41 +0200
changeset 8
63b6f80e09ef
parent 7
9b6867df00c9
child 9
a01a3fd32073

added threejs stl viewer html testing

cylindertransport.py file | annotate | diff | comparison | revisions
stlviewer.html file | annotate | diff | comparison | revisions
--- a/cylindertransport.py	Mon Apr 03 05:21:33 2017 +0200
+++ b/cylindertransport.py	Mon Apr 03 09:10:41 2017 +0200
@@ -26,10 +26,42 @@
         self.cylinders = cylinders
         self.space_min = space_min
         self.font = FONTBASE + "arial.ttf"
-        self.scad = "// Color support only in compile mode (F5)\n"
+        self.width = 0 # will be calculated
+        self.scad = {
+            "tmpl": """// Color support only in compile mode (F5)
+// cylindertransport.py OpenSCAD script
+module tank(x, r, h) {
+  color("SteelBlue") render() { //rotate([90,0,0]) {
+    translate([x, r, r]) {
+        sphere(r = r); // bottom
+        cylinder(h = h-2*r, r = r);
+    }
+    translate([x, r, h-r]) {
+        sphere(r = r); // top
+        cylinder(h = r*1.4, r = r*0.25);
+    }
+  }
+}
+
+module spacer(x, r, rcyl, h) {
+  color("DarkGrey") render() { //rotate([90,0,0]) {
+    translate([x, r, rcyl]) {
+        difference() {
+            cylinder(h = h-2*rcyl, r = r);
+            cylinder(h = h-2*rcyl, r = r*0.8);
+        }
+    }
+  }
+}
+$fn = 10;
+""",
+            "spacer": "",
+            "cylinder": ""
+            }
         self.circles = []
         self.spacings = []
         self.margin = 20
+        self.scale3d = 0.1
 
     def calc_min(self, r_1, r_2):
         """
@@ -67,8 +99,9 @@
         r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
         posx = self.margin + r_2 # start offset x
         self._circle(posx, r_2, label, 0.5)
-        self.scad += "spacer(%i, %i, %i, %i);\n" % (
-            posx, r_2, r_3, CYLINDER[self.cylinders[0]][1])
+        self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
+            posx * self.scale3d, r_2 * self.scale3d, r_3 * self.scale3d,
+            CYLINDER[self.cylinders[0]][1] * self.scale3d)
         posx += offset(r_2, r_3)
 
         for i in range(0, len(self.cylinders) - 1):
@@ -77,29 +110,33 @@
             r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
             # draw cylinder
             self._circle(posx, r_1, "Tank " + self.cylinders[i])
-            self.scad += "tank(%i, %i, %i);\n" % (
-                posx, r_1, CYLINDER[self.cylinders[i]][1])
+            self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
+                posx * self.scale3d, r_1 * self.scale3d,
+                CYLINDER[self.cylinders[i]][1] * self.scale3d)
             sx1 = posx + r_1
             posx += offset(r_1, r_2)
             # draw right spacer
             self._circle(posx, r_2, label, 0.5)
-            self.scad += "spacer(%i, %i, %i, %i);\n" % (
-                posx, r_2, r_1, CYLINDER[self.cylinders[i]][1])
+            self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
+                posx * self.scale3d, r_2 * self.scale3d, r_1 * self.scale3d,
+                CYLINDER[self.cylinders[i]][1] * self.scale3d)
             posx += offset(r_2, r_3)
             sx2 = posx - r_3
             if i == (len(self.cylinders) - 2):
                 # draw last bottle
                 self._circle(posx, r_3, "Tank " + self.cylinders[i + 1])
-                self.scad += "tank(%i, %i, %i);\n" % (
-                    posx, r_3, CYLINDER[self.cylinders[i + 1]][1])
+                self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
+                    posx * self.scale3d, r_3 * self.scale3d,
+                    CYLINDER[self.cylinders[i + 1]][1] * self.scale3d)
                 posx += offset(r_2, r_3)
 
             self.spacings.append([sx1, sx2])
 
         # last bottle spacer pipe
         self._circle(posx, r_2, label, 0.5)
-        self.scad += "spacer(%i, %i, %i, %i);\n" % (
-            posx, r_2, r_3, CYLINDER[self.cylinders[-1]][1])
+        self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
+            posx * self.scale3d, r_2 * self.scale3d, r_3 * self.scale3d,
+            CYLINDER[self.cylinders[-1]][1] * self.scale3d)
         return int(posx + r_2 + self.margin)
 
     def centertext(self, draw, posx, posy, txt, size):
@@ -115,8 +152,8 @@
         """
         Start the calculation and return rendered PIL image object
         """
-        width = self._calculate()
-        image = Image.new('1', (width, 250)) # create new image
+        self.width = self._calculate()
+        image = Image.new('1', (self.width, 250)) # create new image
         draw = ImageDraw.Draw(image)
         # draw calculated circles
         for posx, radius, txt, size in self.circles:
@@ -168,34 +205,11 @@
 
     if (options.scad != ""):
         with open(options.scad, "w") as fd:
-            fd.write("""// cylindertransport.py OpenSCAD script
-module tank(x, r, h) {
-    cap = r;
-    color("SteelBlue") render() rotate([90,0,0]) {
-    translate([x, r, cap]) {
-        sphere(r = r); // bottom
-        cylinder(h = h-2*cap, r = r);
-    }
-    translate([x, r, h-cap]) {
-        sphere(r = r); // top
-        cylinder(h = 30+cap, r = 25);
-    }
-    }
-}
-
-module spacer(x, r, rcylinder, h) {
-    cap = rcylinder;
-
-    color("DarkGrey") render() rotate([90,0,0])
-    translate([x, r, cap]) {
-        difference() {
-            cylinder(h = h-2*cap, r = r);
-            cylinder(h = h-2*cap, r = r-5);
-        }
-    }
-}
-$fn = 20;\n""")
-            fd.write(worker.scad)
+            fd.write(worker.scad["tmpl"])
+            fd.write("translate([%f,0,0]) {\n" % (((worker.width - 2 * worker.margin) / -2)*worker.scale3d)) # center the object
+            fd.write(worker.scad["cylinder"])
+            fd.write(worker.scad["spacer"])
+            fd.write("}\n")
 
 if __name__ == "__main__":
     run()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stlviewer.html	Mon Apr 03 09:10:41 2017 +0200
@@ -0,0 +1,182 @@
+<!DOCTYPE html>
+<html lang="en"><head>
+<meta http-equiv="content-type" content="text/html; charset=UTF-8">
+		<title>ScubaTools Object Viewer</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<style>
+			body {
+				font-family: Monospace;
+				background-color: #000000;
+				margin: 0px;
+				overflow: hidden;
+			}
+
+			#info {
+				color: #fff;
+				position: absolute;
+				top: 10px;
+				width: 100%;
+				text-align: center;
+				z-index: 100;
+				display:block;
+
+			}
+
+			a { color: skyblue }
+			.button { background:#999; color:#eee; padding:0.2em 0.5em; cursor:pointer }
+			.highlight { background:orange; color:#fff; }
+
+			span {
+				display: inline-block;
+				width: 60px;
+				float: left;
+				text-align: center;
+			}
+
+		</style>
+    </head>
+	<body>
+		<div id="info">
+            <a href="https://neo-soft.org" target="_blank">NeoSoft</a> ScubaTools -  
+			<a href="https://threejs.org/" target="_blank">three.js</a> -
+			STL loader by <a href="https://github.com/aleeper" target="_blank">aleeper</a>.
+            <br/>
+            Left mouse: rotate camera, right mouse: move camera, middle mouse or wheel: zoom
+		</div>
+
+		<script src="https://threejs.org/build/three.js"></script>
+		<script src="https://threejs.org/examples/js/loaders/STLLoader.js"></script>
+        <script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
+		<script>
+			var container;
+			var camera, cameraTarget, scene, renderer;
+
+			init();
+			animate();
+
+			function init() {
+				container = document.createElement( 'div' );
+				document.body.appendChild( container );
+
+				camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
+				camera.position.set( 3, 0.35, 3 );
+
+				cameraTarget = new THREE.Vector3( 0, -0.25, 0 );
+
+				scene = new THREE.Scene();
+				scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
+
+                var controls = new THREE.OrbitControls(camera);
+
+				// Ground
+
+				var plane = new THREE.Mesh(
+					new THREE.PlaneBufferGeometry( 40, 40 ),
+					new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
+				);
+				plane.rotation.x = -Math.PI/2;
+				plane.position.y = -0.5;
+				scene.add( plane );
+
+				plane.receiveShadow = true;
+
+				// ASCII file
+
+				var loader = new THREE.STLLoader();
+				loader.load( './test.stl', function ( geometry ) {
+
+					var material = new THREE.MeshPhongMaterial( { color: 0xff5533, specular: 0x111111, shininess: 200 } );
+					var mesh = new THREE.Mesh( geometry, material );
+
+					mesh.position.set( 0, -0.25, 0 );
+					mesh.rotation.set( 0, Math.PI / 2, 0 );
+					mesh.scale.set( 0.01, 0.01, 0.01 );
+
+					mesh.castShadow = true;
+					//mesh.receiveShadow = true;
+
+					scene.add( mesh );
+
+				} );
+
+				// Lights
+
+				scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
+
+				addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
+				//addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 );
+				// renderer
+
+				renderer = new THREE.WebGLRenderer( { antialias: true } );
+				renderer.setClearColor( scene.fog.color );
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+				renderer.gammaInput = true;
+				renderer.gammaOutput = true;
+
+				renderer.shadowMap.enabled = true;
+				renderer.shadowMap.renderReverseSided = false;
+
+				container.appendChild( renderer.domElement );
+
+				window.addEventListener( 'resize', onWindowResize, false );
+
+			}
+
+			function addShadowedLight( x, y, z, color, intensity ) {
+
+				var directionalLight = new THREE.DirectionalLight( color, intensity );
+				directionalLight.position.set( x, y, z );
+				scene.add( directionalLight );
+
+				directionalLight.castShadow = true;
+
+				var d = 1;
+				directionalLight.shadow.camera.left = -d;
+				directionalLight.shadow.camera.right = d;
+				directionalLight.shadow.camera.top = d;
+				directionalLight.shadow.camera.bottom = -d;
+
+				directionalLight.shadow.camera.near = 1;
+				directionalLight.shadow.camera.far = 4;
+
+				directionalLight.shadow.mapSize.width = 1024;
+				directionalLight.shadow.mapSize.height = 1024;
+
+				directionalLight.shadow.bias = -0.005;
+
+			}
+
+			function onWindowResize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			function animate() {
+
+				requestAnimationFrame( animate );
+
+				//render();
+                renderer.render( scene, camera );
+				//stats.update();
+
+			}
+
+			function render() {
+				var timer = Date.now() * 0.0005;
+				camera.position.x = Math.cos( timer ) * 3;
+				camera.position.z = Math.sin( timer ) * 3;
+				camera.lookAt( cameraTarget );
+				renderer.render( scene, camera );
+			}
+
+		</script><div><canvas style="width: 1589px; height: 711px;" height="711" width="1589"></canvas><div style="position: fixed; top: 0px; left: 0px; cursor: pointer; opacity: 0.9; z-index: 10000;"><canvas style="width: 80px; height: 48px; display: block;" height="48" width="80"></canvas><canvas style="width: 80px; height: 48px; display: none;" height="48" width="80"></canvas></div></div>
+	
+
+</body></html>
\ No newline at end of file

mercurial