-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshipwrightToObj.user.js
157 lines (114 loc) · 4.06 KB
/
shipwrightToObj.user.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
// ==UserScript==
// @name shipwrightToObj
// @version 0.1
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @description ship ripper
// @author You
// @match http://ship.shapewright.com/*
// @grant none
// ==/UserScript==
name = $('#name').val();
if (!name){name = "ship";}
Downloadify.create('fb-buttons',{
filename: name + '.obj',
data: function(){
return objExport();
},
onComplete: function(){
},
onCancel: function(){
},
onError: function(){
alert('Sorry, something went wrong!');
},
transparent: false,
swf: 'http://ship.shapewright.com/js/downloadify.swf',
downloadImage: 'https://dl.dropboxusercontent.com/u/249929/downloadObj.png',
width: 114,
height: 26,
transparent: true,
append: true
});
function safeName(str, nameLst){
var n = str.replace(/[^A-Z0-9]/ig, "_");
if (nameLst.indexOf(n) == -1){
return n;
}
for (var i = 1; i < 10; i++){
n2 = n + "_" + i;
if (nameLst.indexOf(n2) == -1){
return n2;
}
}
return n + "_" + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
}
function objExport(){
var output = '';
var indexVertex = 0;
var indexVertexUvs = 0;
var indexNormals = 0;
var vOffset = 0;
var nOffset = 0;
var nLst = [];
var nameLst = [];
modelParent.rotation.x = -(pi * 0.5); //this doesnt appear to work
modelParent.rotation.z = 0.0;
console.log('getting',Models.length,'models');
for (var m = 0; m < Models.length; m++ ){
Model = Models[m];
var partName = safeName(Model.geometry.part.name, nameLst);
nameLst.push(partName);
console.log(partName);
output += 'o ' + partName + '\n';
verts = Model.geometry.vertices;
mat = Model.matrixWorld;
vLen = verts.length;
Model.geometry.computeVertexNormals();
//verts
for (var i = 0; i < vLen; i ++ ){
p = mat.multiplyVector3(verts[i].position);
x = roundIsh(p.x);
y = roundIsh(p.y);
z = roundIsh(p.z);
output += 'v ' + x + ' ' + y + ' ' + z + '\n';
}
faces = Model.geometry.faces;
fLen = faces.length;
//normals
//for (var i = 0; i < fLen; i ++ ){
// var normals = faces[i].vertexNormals;
// for ( var j = 0; j < normals.length; j ++ ) {
// var normal = normals[ j ];
//output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
//nLst.push(normal); //record their position
//}
//}
//v//vn v//vn v//vn
output += 'g ' + partName + '\n';
// faces
for (var i = 0; i < fLen; i ++ ){
face = faces[i];
a = face.a+vOffset + 1;
b = face.b+vOffset + 1;
c = face.c+vOffset + 1;
d = face.d+vOffset + 1;
//an = face.vertexNormals[0];
//bn = face.vertexNormals[1];
//cn = face.vertexNormals[2];
//dn = face.vertexNormals[3];
//anPos = nLst.indexOf(an) + 1;
//bnPos = nLst.indexOf(bn) + 1;
//cnPos = nLst.indexOf(cn) + 1;
//dnPos = nLst.indexOf(dn) + 1;
//output += 'f ' + a + '//' + anPos + ' ' + b + '//' + bnPos + ' ' + c + '//' + cnPos + ' \n';
output += 'f ' + a + ' ' + b + ' ' + c + '\n';
if(!isNaN(d)){
output += 'f ' + a + ' ' + c + ' ' + d + '\n';
//output += 'f ' + a + '//' + anPos + ' ' + c + '//' + cnPos + ' ' + d + '//' + dnPos + ' \n';
}
}
nOffset += fLen;
vOffset += vLen;
}
return output;
}