-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSOBtoObj.1sc
371 lines (324 loc) · 7.22 KB
/
SOBtoObj.1sc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//------------------------------------------------
//--- 010 Editor v8.0.1 Script File
//
// File:
// Authors:
// Version:
// Purpose:
// Category:
// History:
//
//------------------------------------------------
local uint Line;
local uchar Materials[3000], MaterialsIndex[1000];
local uint VertexesLine, UVLine, IndexesLine, NormalsLine, MaterialIndex, matpos;
local int OBJ = -1, FileIndex;
local string VertexesString, NormalsString, IndexesString, TCoordinatesString, ObjectName;
local int MTL = -1;
local float AmbientR, AmbientG, AmbientB, DiffuseR, DiffuseG, DiffuseB, SpecularR, SpecularG, SpecularB;
local float Opacity, SpecularLevel, SelfUllumination;
local string TextureName, MaterialName;
local byte twoSided;
local string FileName;
string ReadName()
{
local string Name;
local uint length;
length = ReadInt(FTell());
FSkip(4);
Name = ReadString(FTell(), length);
FSkip(length);
return Name;
};
string FileNameRemoveExt(string FileName)
{
return SubStr(FileName, 0, Strchr(FileName, '.'));
};
// Header
string ReadHeader()
{
local string Name;
local uint size, id, value;
size = ReadInt(FTell());
FSkip(4);
id = ReadInt(FTell());
FSkip(4);
Name = ReadName();
if (Name == "Version")
{
value = ReadInt(FTell());
FSkip(4);
Name = ReadName();
if (id == 7)
{
FSkip(8);
};
};
return Name;
};
// Material
void ReadMaterial(uint i)
{
local uint size, Unknown;
MaterialName = ReadHeader();
size = sizeof(MaterialName);
Materials[matpos] = size;
MaterialsIndex[i] = matpos;
++matpos;
Memcpy(Materials, MaterialName, size, matpos , 0);
matpos += size;
TextureName = ReadName();
//
Opacity = ReadFloat(FTell());
FSkip(4);
SelfUllumination = ReadFloat(FTell());
FSkip(4);
Unknown = ReadInt(FTell());
FSkip(4);
//
AmbientR = ReadInt(FTell());
FSkip(4);
AmbientG = ReadInt(FTell());
FSkip(4);
AmbientB = ReadInt(FTell());
FSkip(4);
//
DiffuseR = ReadInt(FTell());
FSkip(4);
DiffuseG = ReadInt(FTell());
FSkip(4);
DiffuseB = ReadInt(FTell());
FSkip(4);
//
SpecularR = ReadInt(FTell());
FSkip(4);
SpecularG = ReadInt(FTell());
FSkip(4);
SpecularB = ReadInt(FTell());
FSkip(4);
SpecularLevel = ReadFloat(FTell());
FSkip(4);
twoSided = ReadByte(FTell());
FSkip(1);
};
void ReadFileHeader()
{
local uint length;
length = ReadInt(0);
FSkip(4);
FSkip(length);
}
void AddMaterialtoMTLFile()
{
local string MTLString, Str;
FileSelect(MTL);
Str = "newmtl " + MaterialName + "\n";
MTLString += Str;
SPrintf(Str, "Ka %f, %f, %f\n", AmbientR/255, AmbientG/255, AmbientB/255);
MTLString += Str;
SPrintf(Str, "Kd %f, %f, %f\n", DiffuseR/255, DiffuseG/255, DiffuseB/255);
MTLString += Str;
SPrintf(Str, "Ks %f, %f, %f\n", SpecularR/255, SpecularG/255, SpecularB/255);
MTLString += Str;
SPrintf(Str, "Ns %f\n", SpecularLevel*1000);
MTLString += Str;
SPrintf(Str, "d %f\n", Opacity);
MTLString += Str;
SPrintf(Str, "illum %d\n", 2);
MTLString += Str;
SPrintf(Str, "map_Kd %s\n", TextureName);
MTLString += Str;
TextWriteLine(MTLString, Line);
Line += 8;
FileSelect(FileIndex);
};
void ReadMaterialList()
{
local uint count, i;
ReadHeader();
count = ReadInt(FTell());
FSkip(4);
MTL = FileNew("Text"); // create .mtl file
TextWriteLine("# MaterialList\n", Line);
++Line;
FileSelect(FileIndex);
for ( i = 0; i < count; ++i )
{
ReadMaterial(i);
AddMaterialtoMTLFile();
};
FileSelect(MTL);
FileSave(FileNameRemoveExt(FileName) + ".mtl");
//FileClose();
FileSelect(FileIndex);
};
// Mesh
void ReadMesh()
{
};
// Mesh
void ReadVertexes()
{
local uint count, i;
local float X, Y, Z;
local string Str;
count = ReadInt(FTell());
FSkip(4);
VertexesLine = count;
for ( i = 0; i < count; ++i )
{
X = ReadFloat(FTell());
FSkip(4);
Y = ReadFloat(FTell());
FSkip(4);
Z = ReadFloat(FTell());
FSkip(4);
SPrintf(Str, "v %f, %f, %f\n", X, Y, Z);
VertexesString += Str;
};
};
void ReadUVNormals()
{
local uint count, i;
local float X, Y, Z, U, V;
local string Str;
count = ReadInt(FTell());
FSkip(4);
NormalsLine = count;
UVLine = count;
for ( i = 0; i < count; ++i )
{
X = ReadFloat(FTell());
FSkip(4);
Y = ReadFloat(FTell());
FSkip(4);
Z = ReadFloat(FTell());
FSkip(8);
SPrintf(Str, "vn %f, %f, %f\n", X, Y, Z);
NormalsString += Str;
U = ReadFloat(FTell());
FSkip(4);
V = ReadFloat(FTell());
FSkip(16);
SPrintf(Str, "vt %f, %f\n", U, V);
TCoordinatesString += Str;
};
};
string getMaterialName(uint index)
{
local uint pos, length;
pos = MaterialsIndex[index];
length = Materials[pos];
local uchar Name[length];
Memcpy(Name, Materials, length, 0, pos + 1);
return Name;
}
void ReadIndexes()
{
local uint count, i;
local uint V1, V2, V3, Vt1, Vt2, Vt3;
local string Str, Name;
count = ReadInt(FTell());
FSkip(4);
IndexesLine = 2*count;
for ( i = 0; i < count; ++i )
{
V1 = ReadInt(FTell()) + 1;
FSkip(4);
V2 = ReadInt(FTell()) + 1;
FSkip(4);
V3 = ReadInt(FTell()) + 1;
FSkip(4);
Vt1 = ReadInt(FTell()) + 1;
FSkip(4);
Vt2 = ReadInt(FTell()) + 1;
FSkip(4);
Vt3 = ReadInt(FTell()) + 1;
FSkip(20);
MaterialIndex = ReadInt(FTell());
FSkip(4);
//
if (MaterialIndex < 255)
{
Name = getMaterialName(MaterialIndex);
SPrintf(Str, "usemtl %s\n", Name);
}
else
SPrintf(Str, "usemtl %s\n", "none");
IndexesString += Str;
SPrintf(Str, "f %u/%u %u/%u %u/%u\n", V1, Vt1, V2, Vt2, V3, Vt3);
IndexesString += Str;
};
};
void ReadObject()
{
ObjectName = ReadHeader();
ReadVertexes();
ReadUVNormals();
ReadIndexes();
//ReadMeshes();
}
void AddObjectToObj()
{
local string Str;
FileSelect(OBJ);
TextWriteLine("# " + FileName + "\n", Line);
++Line;
TextWriteLine("mtllib " + FileNameRemoveExt(FileNameGetBase(FileName)) + ".mtl\n", Line);
++Line;
TextWriteLine("\n", Line);
++Line;
Str = "o " + ObjectName + "\n";
TextWriteLine(Str, Line);
++Line;
TextWriteLine("\n", Line);
++Line;
TextWriteLine("# Vertexes\n", Line);
++Line;
TextWriteLine(VertexesString, Line);
Line += VertexesLine;
TextWriteLine("\n", Line);
++Line;
TextWriteLine("# Normals\n", Line);
++Line;
TextWriteLine(NormalsString, Line);
Line += NormalsLine;
TextWriteLine("\n", Line);
++Line;
TextWriteLine("# Texture coordinates\n", Line);
++Line;
TextWriteLine(TCoordinatesString, Line);
Line += UVLine;
TextWriteLine("\n", Line);
++Line;
TextWriteLine("# Faces\n", Line);
++Line;
TextWriteLine(IndexesString, Line);
FileSelect(FileIndex);
};
void ReadGeometryList()
{
local uint count, i;
Line = 0;
ObjectName = ReadHeader();
count = ReadInt(FTell());
FSkip(4);
OBJ = FileNew("Text"); // create .obj file
TextWriteLine("# GeometryList\n", Line);
++Line;
FileSelect(FileIndex);
for ( i = 0; i < count; ++i )
{
ReadObject();
AddObjectToObj();
};
FileSelect(OBJ);
FileSave(FileNameRemoveExt(FileName) + ".obj");
//FileClose();
FileSelect(FileIndex);
};
FileIndex = GetFileNum();
FileName = GetFileName();
ReadFileHeader();
ReadMaterialList();
ReadGeometryList();