stlviewer.js

Wed, 05 Apr 2017 00:59:45 +0200

author
mdd
date
Wed, 05 Apr 2017 00:59:45 +0200
changeset 14
ba3d8c56e6f5
parent 13
39fb313ba27c
child 17
dee15870206a
permissions
-rw-r--r--

code cleanup

11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
1 $( function() {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
2 $( "#sortable1 li" ).draggable({
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
3 helper: "clone",
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
4 containment:"document"
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
5 });
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
6 $( "#sortable2" ).droppable({
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
7 accept: "#sortable1 li",
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
8 drop: function( event, ui ) {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
9 ui.draggable.clone(false).appendTo($(this));
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
10 for (i =0; i<10; i++) clear_scene();
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
11 window.setTimeout(update_scene,500);
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
12 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
13 });
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
14 $( "#sortable2" ).sortable({
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
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
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
18 start: clear_scene,
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
19 stop: function() {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
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
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
22 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
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
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
35 } );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
36
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
37 function clear_scene() {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
38 scene.children.forEach(function(v){
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
39 if(v.stlfile === true) {
12
b3cf0176512e finished webrenderer
mdd
parents: 11
diff changeset
40 //v.material.dispose();
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
41 v.geometry.dispose();
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
42 scene.remove(v);
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
43 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
44 //scene.remove(object);
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
45 });
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
46 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
47
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
48 function update_scene() {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
49 var cylinders = [];
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
50 $("#sortable2 li").each(function(idx, e){
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
51 cylinders.push($(e).attr('key'));
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
52 });
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
53 if (cylinders.length < 2) return;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
54
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
55 // fetch new objects list
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
56 $.ajax({
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
57 url: "#",
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
58 method: 'post',
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
59 dataType: 'json',
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
60 data: {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
61 action: 'calculate',
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
62 cylinders: cylinders
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
63 },
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
64 success: function(data) {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
65 //console.log(data);
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
66 // remove all meshes
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
67 clear_scene();
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
68 // append the objects with positioning
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
69 for (i = 0; i<data.objects.length; i++) {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
70 var obj = data.objects[i];
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
71 if (obj[5] == "") {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
72 // spacer
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
73 filename = "stl/spacer_" + obj[4][0] + '.stl';
12
b3cf0176512e finished webrenderer
mdd
parents: 11
diff changeset
74 material = 'm_spacer';
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
75 } else {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
76 // cylinder
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
77 filename = "stl/cylinder_" + obj[5] + '.stl';
12
b3cf0176512e finished webrenderer
mdd
parents: 11
diff changeset
78 material = 'm_cylinder';
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
79 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
80 position = [data.scale3d * 0.01 * obj[0], 0, 0];
12
b3cf0176512e finished webrenderer
mdd
parents: 11
diff changeset
81 loadSTL(filename, material + "", position);
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
82 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
83
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
84 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
85 });
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
86 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
87
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
88 function loadSTL(filename, material, position, rotation, scale) {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
89 //console.log(filename, position);
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
90 var loader = new THREE.STLLoader();
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
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
b3cf0176512e finished webrenderer
mdd
parents: 11
diff changeset
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
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
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
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
97 mesh["stlfile"] = true;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
98 if (!position) mesh.position.set( 0, -0.25, 0 ); else
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
99 mesh.position.set( position[0], position[1], position[2] );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
100 if (!rotation) mesh.rotation.set( 0, 0, 0 ); else
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
101 mesh.rotation.set( rotation[0], rotation[1], rotation[2] );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
102 if (!scale) mesh.scale.set( 0.01, 0.01, 0.01 ); else
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
103 mesh.scale.set( scale[0], scale[1], scale[2] );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
104
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
105 mesh.castShadow = true;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
106 mesh.receiveShadow = true;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
107
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
108 scene.add( mesh );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
109
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
110 } );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
111
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
112 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
113
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
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
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
117
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
118 camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 20 );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
119 camera.position.set( 0.5, 0.35, 2 );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
120
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
121 cameraTarget = new THREE.Vector3( 0, -0.25, 0 );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
122
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
123 scene = new THREE.Scene();
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
124 scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
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
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
127
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
128 // Ground
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
129
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
130 var plane = new THREE.Mesh(
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
131 new THREE.PlaneBufferGeometry( 40, 40 ),
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
132 new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
133 );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
134 plane.rotation.x = -Math.PI/2;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
135 plane.position.y = -0.5;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
136 scene.add( plane );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
137
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
138 plane.receiveShadow = true;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
139
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
140 // Lights
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
141
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
142 scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
143
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
144 addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
145 //addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
146 // renderer
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
147
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
148 renderer = new THREE.WebGLRenderer( { antialias: true } );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
149 renderer.setClearColor( scene.fog.color );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
150 renderer.setPixelRatio( window.devicePixelRatio );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
151 renderer.setSize( window.innerWidth, window.innerHeight );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
152
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
153 renderer.gammaInput = true;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
154 renderer.gammaOutput = true;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
155
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
156 renderer.shadowMap.enabled = true;
12
b3cf0176512e finished webrenderer
mdd
parents: 11
diff changeset
157 renderer.shadowMap.renderReverseSided = false;
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
158
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
159 container.appendChild( renderer.domElement );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
160
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
161 window.addEventListener( 'resize', onWindowResize, false );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
162
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
163 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
164
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
165 function addShadowedLight( x, y, z, color, intensity ) {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
166
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
167 var directionalLight = new THREE.DirectionalLight( color, intensity );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
168 directionalLight.position.set( x, y, z );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
169 scene.add( directionalLight );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
170
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
171 directionalLight.castShadow = true;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
172
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
173 var d = 5;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
174 directionalLight.shadow.camera.left = -d;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
175 directionalLight.shadow.camera.right = d;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
176 directionalLight.shadow.camera.top = d;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
177 directionalLight.shadow.camera.bottom = -d;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
178
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
179 directionalLight.shadow.camera.near = 1;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
180 directionalLight.shadow.camera.far = 20;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
181
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
182 directionalLight.shadow.mapSize.width = 1024;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
183 directionalLight.shadow.mapSize.height = 1024;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
184
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
185 directionalLight.shadow.bias = -0.005;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
186
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
187 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
188
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
189 function onWindowResize() {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
190
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
191 camera.aspect = window.innerWidth / window.innerHeight;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
192 camera.updateProjectionMatrix();
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
193
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
194 renderer.setSize( window.innerWidth, window.innerHeight );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
195
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
196 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
197
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
198 function animate() {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
199
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
200 requestAnimationFrame( animate );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
201
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
202 //render();
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
203 renderer.render( scene, camera );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
204 //stats.update();
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
205
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
206 }
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
207
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
208 function render() {
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
209 var timer = Date.now() * 0.0005;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
210 camera.position.x = Math.cos( timer ) * 3;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
211 camera.position.z = Math.sin( timer ) * 3;
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
212 camera.lookAt( cameraTarget );
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
213 renderer.render( scene, camera );
12
b3cf0176512e finished webrenderer
mdd
parents: 11
diff changeset
214 }

mercurial