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