Wed, 05 Apr 2017 00:59:45 +0200
code cleanup
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(); | |
68 | // append the objects with positioning | |
69 | for (i = 0; i<data.objects.length; i++) { | |
70 | var obj = data.objects[i]; | |
71 | if (obj[5] == "") { | |
72 | // spacer | |
73 | filename = "stl/spacer_" + obj[4][0] + '.stl'; | |
12 | 74 | material = 'm_spacer'; |
11 | 75 | } else { |
76 | // cylinder | |
77 | filename = "stl/cylinder_" + obj[5] + '.stl'; | |
12 | 78 | material = 'm_cylinder'; |
11 | 79 | } |
80 | position = [data.scale3d * 0.01 * obj[0], 0, 0]; | |
12 | 81 | loadSTL(filename, material + "", position); |
11 | 82 | } |
83 | ||
84 | } | |
85 | }); | |
86 | } | |
87 | ||
88 | function loadSTL(filename, material, position, rotation, scale) { | |
89 | //console.log(filename, position); | |
90 | var loader = new THREE.STLLoader(); | |
91 | loader.load( filename, function ( geometry ) { | |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
92 | if (material == 'm_spacer') material = m_spacer; |
12 | 93 | 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
|
94 | |
11 | 95 | 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
|
96 | |
11 | 97 | mesh["stlfile"] = true; |
98 | if (!position) mesh.position.set( 0, -0.25, 0 ); else | |
99 | mesh.position.set( position[0], position[1], position[2] ); | |
100 | if (!rotation) mesh.rotation.set( 0, 0, 0 ); else | |
101 | mesh.rotation.set( rotation[0], rotation[1], rotation[2] ); | |
102 | if (!scale) mesh.scale.set( 0.01, 0.01, 0.01 ); else | |
103 | mesh.scale.set( scale[0], scale[1], scale[2] ); | |
104 | ||
105 | mesh.castShadow = true; | |
106 | mesh.receiveShadow = true; | |
107 | ||
108 | scene.add( mesh ); | |
109 | ||
110 | } ); | |
111 | ||
112 | } | |
113 | ||
114 | function init() { | |
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
115 | container = document.getElementById( 'rendercontainer' ); |
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
116 | //document.body.appendChild( container ); |
11 | 117 | |
118 | camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 20 ); | |
119 | camera.position.set( 0.5, 0.35, 2 ); | |
120 | ||
121 | cameraTarget = new THREE.Vector3( 0, -0.25, 0 ); | |
122 | ||
123 | scene = new THREE.Scene(); | |
124 | scene.fog = new THREE.Fog( 0x72645b, 2, 15 ); | |
125 | ||
13
39fb313ba27c
finished viewer again, giving up with texturing mesh without uv mapping
mdd
parents:
12
diff
changeset
|
126 | controls = new THREE.OrbitControls(camera); |
11 | 127 | |
128 | // Ground | |
129 | ||
130 | var plane = new THREE.Mesh( | |
131 | new THREE.PlaneBufferGeometry( 40, 40 ), | |
132 | new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } ) | |
133 | ); | |
134 | plane.rotation.x = -Math.PI/2; | |
135 | plane.position.y = -0.5; | |
136 | scene.add( plane ); | |
137 | ||
138 | plane.receiveShadow = true; | |
139 | ||
140 | // Lights | |
141 | ||
142 | scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) ); | |
143 | ||
144 | addShadowedLight( 1, 1, 1, 0xffffff, 1.35 ); | |
145 | //addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 ); | |
146 | // renderer | |
147 | ||
148 | renderer = new THREE.WebGLRenderer( { antialias: true } ); | |
149 | renderer.setClearColor( scene.fog.color ); | |
150 | renderer.setPixelRatio( window.devicePixelRatio ); | |
151 | renderer.setSize( window.innerWidth, window.innerHeight ); | |
152 | ||
153 | renderer.gammaInput = true; | |
154 | renderer.gammaOutput = true; | |
155 | ||
156 | renderer.shadowMap.enabled = true; | |
12 | 157 | renderer.shadowMap.renderReverseSided = false; |
11 | 158 | |
159 | container.appendChild( renderer.domElement ); | |
160 | ||
161 | window.addEventListener( 'resize', onWindowResize, false ); | |
162 | ||
163 | } | |
164 | ||
165 | function addShadowedLight( x, y, z, color, intensity ) { | |
166 | ||
167 | var directionalLight = new THREE.DirectionalLight( color, intensity ); | |
168 | directionalLight.position.set( x, y, z ); | |
169 | scene.add( directionalLight ); | |
170 | ||
171 | directionalLight.castShadow = true; | |
172 | ||
173 | var d = 5; | |
174 | directionalLight.shadow.camera.left = -d; | |
175 | directionalLight.shadow.camera.right = d; | |
176 | directionalLight.shadow.camera.top = d; | |
177 | directionalLight.shadow.camera.bottom = -d; | |
178 | ||
179 | directionalLight.shadow.camera.near = 1; | |
180 | directionalLight.shadow.camera.far = 20; | |
181 | ||
182 | directionalLight.shadow.mapSize.width = 1024; | |
183 | directionalLight.shadow.mapSize.height = 1024; | |
184 | ||
185 | directionalLight.shadow.bias = -0.005; | |
186 | ||
187 | } | |
188 | ||
189 | function onWindowResize() { | |
190 | ||
191 | camera.aspect = window.innerWidth / window.innerHeight; | |
192 | camera.updateProjectionMatrix(); | |
193 | ||
194 | renderer.setSize( window.innerWidth, window.innerHeight ); | |
195 | ||
196 | } | |
197 | ||
198 | function animate() { | |
199 | ||
200 | requestAnimationFrame( animate ); | |
201 | ||
202 | //render(); | |
203 | renderer.render( scene, camera ); | |
204 | //stats.update(); | |
205 | ||
206 | } | |
207 | ||
208 | function render() { | |
209 | var timer = Date.now() * 0.0005; | |
210 | camera.position.x = Math.cos( timer ) * 3; | |
211 | camera.position.z = Math.sin( timer ) * 3; | |
212 | camera.lookAt( cameraTarget ); | |
213 | renderer.render( scene, camera ); | |
12 | 214 | } |