stlviewer.js

changeset 11
098335a1d510
child 12
b3cf0176512e
equal deleted inserted replaced
10:d26669bf424e 11:098335a1d510
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) {
28 v.material.dispose();
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';
62 } else {
63 // cylinder
64 filename = "stl/cylinder_" + obj[5] + '.stl';
65 }
66 position = [data.scale3d * 0.01 * obj[0], 0, 0];
67 loadSTL(filename, null, position);
68 }
69
70 }
71 });
72 }
73
74 function loadSTL(filename, material, position, rotation, scale) {
75 //console.log(filename, position);
76 var loader = new THREE.STLLoader();
77 loader.load( filename, function ( geometry ) {
78 if (!material)
79 var material = new THREE.MeshPhongMaterial( {
80 color: 0xff5533, specular: 0x111111, shininess: 200 } );
81 var mesh = new THREE.Mesh( geometry, material );
82 mesh["stlfile"] = true;
83 if (!position) mesh.position.set( 0, -0.25, 0 ); else
84 mesh.position.set( position[0], position[1], position[2] );
85 if (!rotation) mesh.rotation.set( 0, 0, 0 ); else
86 mesh.rotation.set( rotation[0], rotation[1], rotation[2] );
87 if (!scale) mesh.scale.set( 0.01, 0.01, 0.01 ); else
88 mesh.scale.set( scale[0], scale[1], scale[2] );
89
90 mesh.castShadow = true;
91 mesh.receiveShadow = true;
92
93 scene.add( mesh );
94
95 } );
96
97 }
98
99 function init() {
100 container = document.createElement( 'div' );
101 document.body.appendChild( container );
102
103 camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 20 );
104 camera.position.set( 0.5, 0.35, 2 );
105
106 cameraTarget = new THREE.Vector3( 0, -0.25, 0 );
107
108 scene = new THREE.Scene();
109 scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
110
111 var controls = new THREE.OrbitControls(camera);
112
113 // Ground
114
115 var plane = new THREE.Mesh(
116 new THREE.PlaneBufferGeometry( 40, 40 ),
117 new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
118 );
119 plane.rotation.x = -Math.PI/2;
120 plane.position.y = -0.5;
121 scene.add( plane );
122
123 plane.receiveShadow = true;
124
125 // Lights
126
127 scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
128
129 addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
130 //addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 );
131 // renderer
132
133 renderer = new THREE.WebGLRenderer( { antialias: true } );
134 renderer.setClearColor( scene.fog.color );
135 renderer.setPixelRatio( window.devicePixelRatio );
136 renderer.setSize( window.innerWidth, window.innerHeight );
137
138 renderer.gammaInput = true;
139 renderer.gammaOutput = true;
140
141 renderer.shadowMap.enabled = true;
142 renderer.shadowMap.renderReverseSided = true;
143
144 container.appendChild( renderer.domElement );
145
146 window.addEventListener( 'resize', onWindowResize, false );
147
148 }
149
150 function addShadowedLight( x, y, z, color, intensity ) {
151
152 var directionalLight = new THREE.DirectionalLight( color, intensity );
153 directionalLight.position.set( x, y, z );
154 scene.add( directionalLight );
155
156 directionalLight.castShadow = true;
157
158 var d = 5;
159 directionalLight.shadow.camera.left = -d;
160 directionalLight.shadow.camera.right = d;
161 directionalLight.shadow.camera.top = d;
162 directionalLight.shadow.camera.bottom = -d;
163
164 directionalLight.shadow.camera.near = 1;
165 directionalLight.shadow.camera.far = 20;
166
167 directionalLight.shadow.mapSize.width = 1024;
168 directionalLight.shadow.mapSize.height = 1024;
169
170 directionalLight.shadow.bias = -0.005;
171
172 }
173
174 function onWindowResize() {
175
176 camera.aspect = window.innerWidth / window.innerHeight;
177 camera.updateProjectionMatrix();
178
179 renderer.setSize( window.innerWidth, window.innerHeight );
180
181 }
182
183 function animate() {
184
185 requestAnimationFrame( animate );
186
187 //render();
188 renderer.render( scene, camera );
189 //stats.update();
190
191 }
192
193 function render() {
194 var timer = Date.now() * 0.0005;
195 camera.position.x = Math.cos( timer ) * 3;
196 camera.position.z = Math.sin( timer ) * 3;
197 camera.lookAt( cameraTarget );
198 renderer.render( scene, camera );
199 }

mercurial