Thu, 06 Apr 2017 15:41:04 +0200
added text info box
11 | 1 | $( function() { |
2 | $( "#sortable1 li" ).draggable({ | |
3 | helper: "clone", | |
4 | containment:"document" | |
5 | }); | |
6 | $( "#sortable2" ).droppable({ | |
7 | accept: "#sortable1 li", | |
8 | drop: function( event, ui ) { | |
9 | ui.draggable.clone(false).appendTo($(this)); | |
10 | for (i =0; i<10; i++) clear_scene(); | |
11 | window.setTimeout(update_scene,500); | |
12 | } | |
13 | }); | |
14 | $( "#sortable2" ).sortable({ | |
15 | placeholder: "ui-state-highlight", | |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
16 | forcePlaceholderSize: true, |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
17 | connectWith: '#sortable1', |
11 | 18 | start: clear_scene, |
19 | stop: function() { | |
20 | for (i =0; i<10; i++) clear_scene(); | |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
21 | window.setTimeout(update_scene,500); |
11 | 22 | } |
23 | }); | |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
24 | c = $("<li key='10'>").html("Steel, 10ℓ"); |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
25 | $( "#sortable2" ) |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
26 | .disableSelection() |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
27 | .append(c.clone()) |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
28 | .append(c.clone()); |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
29 | |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
30 | $("#clearselected").click(function(){ |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
31 | $( "#sortable2 li" ).remove(); |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
32 | for (i =0; i<10; i++) clear_scene(); |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
33 | }); |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
34 | update_scene(); |
11 | 35 | } ); |
36 | ||
37 | function clear_scene() { | |
38 | scene.children.forEach(function(v){ | |
39 | if(v.stlfile === true) { | |
12 | 40 | //v.material.dispose(); |
11 | 41 | v.geometry.dispose(); |
42 | scene.remove(v); | |
43 | } | |
44 | //scene.remove(object); | |
45 | }); | |
46 | } | |
47 | ||
48 | function update_scene() { | |
49 | var cylinders = []; | |
50 | $("#sortable2 li").each(function(idx, e){ | |
51 | cylinders.push($(e).attr('key')); | |
52 | }); | |
53 | if (cylinders.length < 2) return; | |
54 | ||
55 | // fetch new objects list | |
56 | $.ajax({ | |
57 | url: "#", | |
58 | method: 'post', | |
59 | dataType: 'json', | |
60 | data: { | |
61 | action: 'calculate', | |
62 | cylinders: cylinders | |
63 | }, | |
64 | success: function(data) { | |
65 | //console.log(data); | |
66 | // remove all meshes | |
67 | clear_scene(); | |
17 | 68 | var content = "<h2>Material:</h2><ul>"; |
69 | var weight = 0.0; | |
70 | var length = 0; | |
71 | var volume = 0; | |
11 | 72 | // append the objects with positioning |
73 | for (i = 0; i<data.objects.length; i++) { | |
74 | var obj = data.objects[i]; | |
17 | 75 | content += "<li>"; |
11 | 76 | if (obj[5] == "") { |
77 | // spacer | |
78 | filename = "stl/spacer_" + obj[4][0] + '.stl'; | |
12 | 79 | material = 'm_spacer'; |
17 | 80 | content += "Spacer: " + obj[4][0] + " (" + obj[4][1] + "mm)" |
11 | 81 | } else { |
82 | // cylinder | |
83 | filename = "stl/cylinder_" + obj[5] + '.stl'; | |
12 | 84 | material = 'm_cylinder'; |
17 | 85 | content += "Cylinder: " + obj[4][4] + " (" + obj[4][0] + "mm)" |
86 | weight += obj[4][3]; | |
87 | volume += obj[4][2]; | |
88 | if (obj[4][1] > length) length = obj[4][1]; | |
11 | 89 | } |
90 | position = [data.scale3d * 0.01 * obj[0], 0, 0]; | |
12 | 91 | loadSTL(filename, material + "", position); |
17 | 92 | content += "</li>" |
11 | 93 | } |
94 | ||
17 | 95 | content += "</ul><br/>Total cylinder weight: " + Math.ceil(weight) + " kg<br/>"; |
96 | content += "Total cylinder volume: " + Math.floor(volume) + " ℓ<br/>"; | |
97 | content += "Max cylinder length: " + Math.ceil(length / 10) + " cm<br/>" | |
98 | ||
99 | $("#infocontainer").html(content); | |
100 | ||
11 | 101 | } |
102 | }); | |
103 | } | |
104 | ||
105 | function loadSTL(filename, material, position, rotation, scale) { | |
106 | //console.log(filename, position); | |
107 | var loader = new THREE.STLLoader(); | |
108 | loader.load( filename, function ( geometry ) { | |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
109 | if (material == 'm_spacer') material = m_spacer; |
12 | 110 | else if (material == 'm_cylinder') material = m_cylinder; |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
111 | |
11 | 112 | var mesh = new THREE.Mesh( geometry, material ); |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
113 | |
11 | 114 | mesh["stlfile"] = true; |
115 | if (!position) mesh.position.set( 0, -0.25, 0 ); else | |
116 | mesh.position.set( position[0], position[1], position[2] ); | |
117 | if (!rotation) mesh.rotation.set( 0, 0, 0 ); else | |
118 | mesh.rotation.set( rotation[0], rotation[1], rotation[2] ); | |
119 | if (!scale) mesh.scale.set( 0.01, 0.01, 0.01 ); else | |
120 | mesh.scale.set( scale[0], scale[1], scale[2] ); | |
121 | ||
122 | mesh.castShadow = true; | |
123 | mesh.receiveShadow = true; | |
124 | ||
125 | scene.add( mesh ); | |
126 | ||
127 | } ); | |
128 | ||
129 | } | |
130 | ||
131 | function init() { | |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
132 | container = document.getElementById( 'rendercontainer' ); |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
133 | //document.body.appendChild( container ); |
11 | 134 | |
135 | camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 20 ); | |
136 | camera.position.set( 0.5, 0.35, 2 ); | |
137 | ||
138 | cameraTarget = new THREE.Vector3( 0, -0.25, 0 ); | |
139 | ||
140 | scene = new THREE.Scene(); | |
141 | scene.fog = new THREE.Fog( 0x72645b, 2, 15 ); | |
142 | ||
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
143 | controls = new THREE.OrbitControls(camera); |
11 | 144 | |
145 | // Ground | |
146 | ||
147 | var plane = new THREE.Mesh( | |
148 | new THREE.PlaneBufferGeometry( 40, 40 ), | |
149 | new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } ) | |
150 | ); | |
151 | plane.rotation.x = -Math.PI/2; | |
152 | plane.position.y = -0.5; | |
153 | scene.add( plane ); | |
154 | ||
155 | plane.receiveShadow = true; | |
156 | ||
157 | // Lights | |
158 | ||
159 | scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) ); | |
160 | ||
161 | addShadowedLight( 1, 1, 1, 0xffffff, 1.35 ); | |
162 | //addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 ); | |
163 | // renderer | |
164 | ||
165 | renderer = new THREE.WebGLRenderer( { antialias: true } ); | |
166 | renderer.setClearColor( scene.fog.color ); | |
167 | renderer.setPixelRatio( window.devicePixelRatio ); | |
168 | renderer.setSize( window.innerWidth, window.innerHeight ); | |
169 | ||
170 | renderer.gammaInput = true; | |
171 | renderer.gammaOutput = true; | |
172 | ||
173 | renderer.shadowMap.enabled = true; | |
12 | 174 | renderer.shadowMap.renderReverseSided = false; |
11 | 175 | |
176 | container.appendChild( renderer.domElement ); | |
177 | ||
178 | window.addEventListener( 'resize', onWindowResize, false ); | |
179 | ||
180 | } | |
181 | ||
182 | function addShadowedLight( x, y, z, color, intensity ) { | |
183 | ||
184 | var directionalLight = new THREE.DirectionalLight( color, intensity ); | |
185 | directionalLight.position.set( x, y, z ); | |
186 | scene.add( directionalLight ); | |
187 | ||
188 | directionalLight.castShadow = true; | |
189 | ||
190 | var d = 5; | |
191 | directionalLight.shadow.camera.left = -d; | |
192 | directionalLight.shadow.camera.right = d; | |
193 | directionalLight.shadow.camera.top = d; | |
194 | directionalLight.shadow.camera.bottom = -d; | |
195 | ||
196 | directionalLight.shadow.camera.near = 1; | |
197 | directionalLight.shadow.camera.far = 20; | |
198 | ||
199 | directionalLight.shadow.mapSize.width = 1024; | |
200 | directionalLight.shadow.mapSize.height = 1024; | |
201 | ||
202 | directionalLight.shadow.bias = -0.005; | |
203 | ||
204 | } | |
205 | ||
206 | function onWindowResize() { | |
207 | ||
208 | camera.aspect = window.innerWidth / window.innerHeight; | |
209 | camera.updateProjectionMatrix(); | |
210 | ||
211 | renderer.setSize( window.innerWidth, window.innerHeight ); | |
212 | ||
213 | } | |
214 | ||
215 | function animate() { | |
216 | ||
217 | requestAnimationFrame( animate ); | |
218 | ||
219 | //render(); | |
220 | renderer.render( scene, camera ); | |
221 | //stats.update(); | |
222 | ||
223 | } | |
224 | ||
225 | function render() { | |
226 | var timer = Date.now() * 0.0005; | |
227 | camera.position.x = Math.cos( timer ) * 3; | |
228 | camera.position.z = Math.sin( timer ) * 3; | |
229 | camera.lookAt( cameraTarget ); | |
230 | renderer.render( scene, camera ); | |
12 | 231 | } |