-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility.js
153 lines (112 loc) · 4.66 KB
/
utility.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function listsceneobjects() {
var viewer = document.getElementsByTagName('model-viewer')[0];
console.log(viewer.scene.children);
}
function getsubobject() {
var scenes = document.getElementsByTagName('model-viewer');
var viewer = scenes[0];
var obj = viewer.scene.getObjectByName("Demo STL");
var nonbufferobj = new THREE.Geometry();
nonbufferobj.fromBufferGeometry(obj.geometry);
console.log("Faces " + nonbufferobj.faces.length);
console.log("Vertices " + nonbufferobj.vertices.length);
nonbufferobj.mergeVertices();
console.log("Faces " + nonbufferobj.faces.length);
console.log("Vertices " + nonbufferobj.vertices.length);
console.log("Face 0: " + nonbufferobj.faces[0].a + "," + nonbufferobj.faces[0].b + "," + nonbufferobj.faces[0].c);
var rfaces = nonbufferobj.faces;
var vertset = {}
var vertsetlength = 3;
var lastvertsetlength = 0;
var newfaces = [];
vertset[rfaces[0].a] = 1;
vertset[rfaces[0].b] = 1;
vertset[rfaces[0].c] = 1;
newfaces.push(rfaces[0]);
for(var passes = 0; passes < 6; passes++) {
for(var i = 1; i < rfaces.length; i++) {
if(rfaces[i].a in vertset
|| rfaces[i].b in vertset
|| rfaces[i].c in vertset) {
if(rfaces[i].a in vertset == false)
vertsetlength++;
if(rfaces[i].b in vertset == false)
vertsetlength++;
if(rfaces[i].c in vertset == false)
vertsetlength++;
if(newfaces.indexOf(rfaces[i]) < 0) {
newfaces.push(rfaces[i]);
rfaces[i].color.set(0xffffff);
}
vertset[rfaces[i].a] = vertset[rfaces[i].a] != null ? vertset[rfaces[i].a] + 1 : 1;
vertset[rfaces[i].b] = vertset[rfaces[i].b] != null ? vertset[rfaces[i].b] + 1 : 1;
vertset[rfaces[i].c] = vertset[rfaces[i].c] != null ? vertset[rfaces[i].c] + 1 : 1;
}
}
if(nonbufferobj.vertices.length == vertsetlength
|| lastvertsetlength == vertsetlength) {
console.log("Break in pass " + passes);
break;
}
}
nonbufferobj.faces = newfaces;
nonbufferobj.elementsNeedUpdate = true;
var matsolid = new THREE.MeshPhongMaterial({ color: 0xff0000, shading: THREE.FlatShading });
viewer.scene.add(new THREE.Mesh(nonbufferobj, matsolid));
console.log("Newfaces " + newfaces.length);
}
function testexport() {
// Unused, old code to be removed.
var scenes = document.getElementsByTagName('model-viewer');
var viewer = scenes[0];
var meshes = [];
console.log("Meshes: ");
for(var i = 0; i < viewer.scene.children.length; i++) {
if(viewer.scene.children[i] instanceof THREE.Mesh) {
console.log(" " + viewer.scene.children[i].name);
meshes.push(viewer.scene.children[i]);
}
}
var faces = null, vertices = null;
var nbogeometry = null;
var mesh = meshes[0];
if(mesh.geometry instanceof THREE.BufferGeometry) {
nbogeometry = new THREE.Geometry();
nbogeometry.fromBufferGeometry(mesh.geometry);
} else if(mesh.geometry instanceof THREE.Geometry) {
nbogeometry = mesh.geometry.clone();
}
var sizeofstl = 80+4+nbogeometry.faces.length*50;
console.log("Size of style: " + sizeofstl);
var stlbuffer = new dcodeIO.ByteBuffer(capacity=sizeofstl, littleEndian=true);
var headerstring = "TamarinTech Model-Viewer STL Exporter: " + mesh.name;
stlbuffer.writeUTF8String(headerstring);
stlbuffer.skip(80 - headerstring.length);
stlbuffer.writeUint32(nbogeometry.faces.length);
for(var facei = 0; facei < nbogeometry.faces.length; facei++) {
face = nbogeometry.faces[facei];
stlbuffer.writeFloat32(0); // Normal a
stlbuffer.writeFloat32(0); // Normal b
stlbuffer.writeFloat32(0); // Normal c
nbogeometry.vertices[face.a].applyMatrix4(mesh.matrix);
stlbuffer.writeFloat32(nbogeometry.vertices[face.a].x);
stlbuffer.writeFloat32(nbogeometry.vertices[face.a].y);
stlbuffer.writeFloat32(nbogeometry.vertices[face.a].z);
nbogeometry.vertices[face.b].applyMatrix4(mesh.matrix);
stlbuffer.writeFloat32(nbogeometry.vertices[face.b].x);
stlbuffer.writeFloat32(nbogeometry.vertices[face.b].y);
stlbuffer.writeFloat32(nbogeometry.vertices[face.b].z);
nbogeometry.vertices[face.c].applyMatrix4(mesh.matrix);
stlbuffer.writeFloat32(nbogeometry.vertices[face.c].x);
stlbuffer.writeFloat32(nbogeometry.vertices[face.c].y);
stlbuffer.writeFloat32(nbogeometry.vertices[face.c].z);
stlbuffer.writeUint16(0); // Attribute
}
nbogeometry.dispose();
stlbuffer.flip();
var a = document.createElement('a');
var stlblob = new Blob([stlbuffer.toArrayBuffer()], {type: "octet/stream"});
a.href = window.URL.createObjectURL(stlblob);
a.download = mesh.name + ".stl";
a.click();
}