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