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