-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCHRtoObj.1sc
495 lines (420 loc) · 9.96 KB
/
CHRtoObj.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//------------------------------------------------
//--- 010 Editor v8.0.1 Script File
//
// File:
// Authors:
// Version:
// Purpose:
// Category:
// History:
//------------------------------------------------
local float x1, y1, z1, x2, y2, z2, x3, y3, z3;
local float x1_, y1_, z1_, x2_, y2_, z2_, x3_, y3_, z3_;
local float XAxisX, XAxisY, XAxisZ, YAxisX, YAxisY, YAxisZ, ZAxisX, ZAxisY, ZAxisZ, PosX, PosY, PosZ;
local byte DetailedDarkmap, DetailTexture;
local int MaterialIndex, isTextured, TextureIndex, DetailTextureIndex;
local int isTiled, AdvTransparencyType, SelfIlluminationMap, SpecularMap, BumpMap, ReflectionMap, MapCount;
local uint Line;
local uchar Materials[3000], MaterialsIndex[1000], Textures[3000], TexturesIndex[1000];
local uint VertexesLine, UVLine, IndexesLine, NormalsLine, matpos, texpos;
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 MaterialName, TextureName, TextureFileName;
local byte twoSided;
local string FileName;
string FileNameRemoveExt(string FileName)
{
return SubStr(FileName, 0, Strchr(FileName, '.'));
};
string ReadName()
{
local string Name;
local uint length;
length = ReadInt(FTell());
FSkip(4);
Name = ReadString(FTell(), length);
FSkip(length);
return Name;
};
void ReadFileHeader()
{
local uint length;
FSkip(4); // skip date var
length = ReadInt(4);
FSkip(4);
FSkip(length);
}
// Header
string ReadHeader()
{
local string Name;
local uint size, id, value;
size = ReadInt(FTell());
FSkip(4);
id = ReadInt(FTell());
FSkip(4);
Name = ReadName();
value = ReadInt(FTell());
FSkip(4);
Name = ReadName();
return Name;
};
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 addNametoArray(string Name, uchar Array[], uchar ArrayIndex[], uint pos, uint index)
{
local uint size;
size = sizeof(Name);
Array[pos] = size;
ArrayIndex[index] = matpos;
++pos;
Memcpy(Array, Name, size, pos , 0);
pos += size;
};
// Material
void ReadMaterial(uint index)
{
local uint size, Unknown;
MaterialName = ReadHeader();
addNametoArray(MaterialName, Materials, MaterialsIndex, matpos, index);
//
Opacity = ReadFloat(FTell());
FSkip(4);
Unknown = ReadInt(FTell());
FSkip(4);
//
AmbientR = ReadInt(FTell());
FSkip(4);
AmbientG = ReadInt(FTell());
FSkip(4);
AmbientB = ReadInt(FTell());
FSkip(4 + 4);
//
DiffuseR = ReadInt(FTell());
FSkip(4);
DiffuseG = ReadInt(FTell());
FSkip(4);
DiffuseB = ReadInt(FTell());
FSkip(4 + 4);
//
SpecularR = ReadInt(FTell());
FSkip(4);
SpecularG = ReadInt(FTell());
FSkip(4);
SpecularB = ReadInt(FTell());
FSkip(4 + 4);
SpecularLevel = ReadFloat(FTell());
FSkip(4);
twoSided = ReadByte(FTell());
FSkip(1);
};
void ReadTexture(uint index)
{
local uint size, Unknown;
TextureName = ReadHeader();
FSkip(1);
TextureFileName = ReadName();
addNametoArray(TextureName, Textures, TexturesIndex, texpos, index);
AdvTransparencyType = ReadInt(FTell());
FSkip(4);
isTiled = ReadInt(FTell());
FSkip(4);
SelfUllumination = ReadFloat(FTell());
FSkip(4);
};
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;
Str = "\n"; // empty line for texture name
MTLString += Str;
TextWriteLine(MTLString, Line);
Line += 8;
FileSelect(FileIndex);
};
void AddTexturetoMTLFile(uint index)
{
local string str;
FileSelect(MTL);
SPrintf(str, "map_Kd %s\n", TextureFileName);
TextWriteLine(str, index*8);
FileSelect(FileIndex);
};
void ReadMaterialList()
{
local uint count, i;
ReadHeader();
MTL = FileNew("Text"); // create .mtl file
TextWriteLine("# MaterialList\n", Line);
++Line;
FileSelect(FileIndex);
count = ReadInt(FTell());
FSkip(4);
for ( i = 0; i < count; ++i )
{
ReadMaterial(i);
AddMaterialtoMTLFile();
};
count = ReadInt(FTell());
FSkip(4);
for ( i = 0; i < count; ++i )
{
ReadTexture(i);
AddTexturetoMTLFile(i + 1);
};
FileSelect(MTL);
FileSave(FileNameRemoveExt(FileName) + ".mtl");
//FileClose();
FileSelect(FileIndex);
};
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;
};
FileSelect(OBJ);
TextWriteLine("# Vertexes\n", Line);
++Line;
TextWriteLine(VertexesString, Line);
Line += VertexesLine;
TextWriteLine("\n", Line);
++Line;
FileSelect(FileIndex);
};
void ReadVertexData()
{
local uint count1, count2, i;
local float X, Y, Z, U, V;
local string Str;
count1 = ReadInt(FTell());
FSkip(4);
count2 = ReadInt(FTell());
FSkip(4);
NormalsLine = count1;
UVLine = count1*count2;
for ( i = 0; i < count1; ++i )
{
X = ReadFloat(FTell());
FSkip(4);
Y = ReadFloat(FTell());
FSkip(4);
Z = ReadFloat(FTell());
FSkip(4);
//
SPrintf(Str, "vn %f, %f, %f\n", X, Y, Z);
NormalsString += Str;
};
for ( i = 0; i < UVLine; ++i )
{
U = ReadFloat(FTell());
FSkip(4);
V = ReadFloat(FTell());
FSkip(4);
SPrintf(Str, "vt %f, %f\n", U, V);
TCoordinatesString += Str;
};
FSkip(count1*16); // skip vertex colors
};
string getNameFromArray(uint index, uchar Array[], uchar ArrayIndex[])
{
local uint pos, length;
pos = MaterialsIndex[index];
length = Materials[pos];
local uchar Name[length];
Memcpy(Name, Materials, length, 0, pos + 1);
return Name;
}
void ReadFaces()
{
local uint count, i;
local uint16 V1, V2, V3, Vt1, Vt2, Vt3;
local string Str, Name;
count = ReadInt(FTell());
FSkip(4);
FSkip(count*16); // skip face normals
if (MaterialIndex > 0)
Name = getNameFromArray(MaterialIndex, Materials, MaterialsIndex);
else
Name = "none";
SPrintf(Str, "usemtl %s\n", Name);
IndexesString += Str;
// face indexes
IndexesLine = count;
for ( i = 0; i < count; ++i )
{
V1 = ReadShort(FTell()) + 1;
Vt1 = ReadShort(FTell() + 6*count) + 1;
FSkip(2);
V2 = ReadShort(FTell()) + 1;
Vt2 = ReadShort(FTell() + 6*count) + 1;
FSkip(2);
V3 = ReadShort(FTell()) + 1;
Vt3 = ReadShort(FTell() + 6*count) + 1;
FSkip(2);
SPrintf(Str, "f %d/%d %d/%d %d/%d\n", V1, Vt1, V2, Vt2, V3, Vt3);
IndexesString += Str;
};
FSkip(count*6);
};
void ReadParameters()
{
FSkip(1);
DetailedDarkmap = ReadByte(FTell());
FSkip(1);
DetailTexture = ReadByte(FTell());
FSkip(1);
MaterialIndex = ReadInt(FTell());
FSkip(4);
isTextured = ReadInt(FTell());
FSkip(4);
if (isTextured)
{
TextureIndex = ReadInt(FTell());
FSkip(4);
if (DetailTexture)
{
DetailTextureIndex = ReadInt(FTell());
FSkip(4);
};
};
FSkip(4);
SelfIlluminationMap = ReadFloat(FTell());
FSkip(4);
SpecularMap = ReadFloat(FTell());
FSkip(4);
BumpMap = ReadFloat(FTell());
FSkip(4);
ReflectionMap = ReadFloat(FTell());
FSkip(4);
MapCount = ReadFloat(FTell());
FSkip(4);
};
void ReadMesh()
{
ReadParameters();
ReadFaces();
ReadVertexData();
};
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;
FileSelect(FileIndex);
};
void AddMeshToObj(uint index)
{
local string Str;
FileSelect(OBJ);
TextWriteLine("\n", Line);
++Line;
SPrintf(Str, "g Mesh %d\n", index);
TextWriteLine(Str, Line);
++Line;
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);
Line += IndexesLine + 1;
NormalsString = " ";
IndexesString = " ";
TCoordinatesString = " ";
FileSelect(FileIndex);
};
void ReadObject()
{
local uint count, i;
ObjectName = ReadHeader();
FSkip(2); // DarkMapped, CastShadows
AddObjectToObj();
ReadVertexes();
count = ReadInt(FTell());
FSkip(4);
for ( i = 0; i < count; ++i )
{
ReadMesh();
AddMeshToObj(i);
}
}
void ReadGeometryList()
{
local uint count, i;
Line = 0;
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();
FileSelect(OBJ);
FileSave(FileNameRemoveExt(FileName) + ".obj");
//FileClose();
FileSelect(FileIndex);
};
FileIndex = GetFileNum();
FileName = GetFileName();
ReadFileHeader();
ReadMaterialList();
ReadGeometryList();