-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReduceSectionCount_CmdLine.bio2s
More file actions
251 lines (204 loc) · 5.56 KB
/
ReduceSectionCount_CmdLine.bio2s
File metadata and controls
251 lines (204 loc) · 5.56 KB
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
#include "std\lodNames.inc"
console=openStandardIO;
//example usage
//O2Script.exe -a O2Scripts\ReduceSectionCount_CmdLine.bio2s p:\test\name.p3d
//Library methods
//==================
_getTexOrMatName =
{
private["_currentLOD","_faceIndex","_isMat"];
_currentLOD = _this @ 0;
_faceIndex = _this @ 1;
_isMat = _this @ 2;
if (_isMat) then
{
getMaterial (_currentLOD face _faceIndex)
}
else
{
getTexture (_currentLOD face _faceIndex)
};
};
_getProxyFaces =
{
private ["_currentLOD","_faces","_face","_proxySel"];
_currentLOD = _this;
_faces = [];
{
if (_x @ [0,6]=="proxy:") then
{
_proxySel = _currentLOD loadSelection _x;
_face = (getSelectedFaces _proxySel) @ 0;
_faces set [count _faces,_face];
};
} forEach (getSelections _currentLOD);
_faces
};
_faceIndicesByTexture =
{
private["_currentLOD","_texName","_faceIndices"];
_currentLOD = _this @ 0;
_texName = _this @ 1;
_texIsMat = _this @ 2;
_proxyFaces = _currentLOD call _getProxyFaces;
_faceIndices = [];
_currentLOD forEachFace [{[_currentLOD,_x,_texIsMat] call _getTexOrMatName == _texName},
{
if (!(_x in _proxyFaces)) then
{
_faceIndices set [count _faceIndices,_x];
};
}];
_faceIndices
};
_selectByFaceIndices =
{
private ["_currentLOD","_faceIndices","_sel"];
_currentLOD = _this @ 0;
_facesIndices = _this @ 1;
_sel = newSelection _currentLOD;
_sel selectFaces _facesIndices;
selectPointsFromFaces _sel
};
_selectionByTexture =
{
private["_currentLOD","_texName","_texIsMat","_faceIndices"];
_currentLOD = _this @ 0;
_texName = _this @ 1;
_texIsMat = _this @ 2;
_faceIndices = [_currentLOD,_texName,_texIsMat] call _faceIndicesByTexture;
[_currentLOD,_faceIndices] call _selectByFaceIndices
};
_removeFaces =
{
private["_currentLOD","_sel","_faceSel"];
_currentLOD = _this @ 0;
_sel = _this @ 1;
//create new selection with just faces
_faceSel = newSelection _currentLOD;
_faceSel selectFaces (getSelectedFaces _sel);
deleteSelected _faceSel;
};
_copySelection =
{
private["_currentLOD","_sel","_pointsIndices","_faceIndices","_pointsCopy","_pointFlagsCopy","_pi","_facesCopy","_face","_vertices","_uv","_flags","_tex","_mat","_faceData"];
_currentLOD = _this @ 0;
_sel = _this @ 1;
_pointsIndices = getSelectedPoints _sel;
_faceIndices = getSelectedFaces _sel;
_pointsCopy = [];
_pointFlagsCopy = [];
for "_i" from 0 to ((count _pointsIndices) - 1) do
{
_pi = _pointsIndices @ _i;
_pointsCopy set [count _pointsCopy,_currentLOD getPoint _pi];
_pointFlagsCopy set [count _pointFlagsCopy,_currentLOD getPointFlags [_pi,[0,31]]];
};
_facesCopy = [];
for "_i" from 0 to ((count _faceIndices) - 1) do
{
_face = _currentLOD face (_faceIndices @ _i);
_vertices = getVertices _face;
_uv = getUVSet _face;
_flags = _face getFaceFlags [0,31];
_tex = getTexture _face;
_mat = getMaterial _face;
_faceData = [_vertices,_tex,_mat,_uv,_flags];
_facesCopy set [count _facesCopy,_faceData];
};
[_pointsCopy,_pointFlagsCopy,_facesCopy]
};
_pasteACopy =
{
private["_currentLOD","_copyData","_points","_pointFlags","_pi","_faces","_face","_faceData","_faceIndex"];
_currentLOD = _this @ 0;
_copyData = _this @ 1;
_points = _copyData @ 0;
_pointFlags = _copyData @ 1;
_faces = _copyData @ 2;
for "_i" from 0 to ((count _points) - 1) do
{
_pi = obj addPoint (_points @ _i);
_currentLOD setPointFlags [_pi,[0,31],_pointFlags @ _i];
};
for "_i" from 0 to ((count _faces) - 1) do
{
_faceData = _faces @ _i;
_faceIndex = addFace _currentLOD;
_face = _currentLOD face _faceIndex;
_face setVertices (_faceData @ 0);
_face setTexture (_faceData @ 1);
_face setMaterial (_faceData @ 2);
_face setUVSet (_faceData @ 3);
_face setFaceFlags [[0,31],_faceData @ 4];
};
};
_removeIsolatedPoints =
{
private["_currentLOD","_usedPoints","_isolatedPoints","_face","_verts","_sel"];
_currentLOD = _this;
//find all "used" (in faces) points
_usedPoints = [];
_isolatedPoints = [];
_currentLOD forEachFace [{true},
{
_faceIndex = _x;
_face = _currentLOD face _faceIndex;
_verts = getVertices _face;
{
if (!(_x in _usedPoints)) then {_usedPoints set [count _usedPoints,_x]};
} forEach _verts;
}];
_currentLOD forEachPoint [{!(_x in _usedPoints)},
{
_isolatedPoints set [count _isolatedPoints,_x];
}];
//_currentLOD callRuntime "isolatedpts";
_sel = newSelection _currentLOD;
_sel selectPoints _isolatedPoints;
deleteSelected _sel;
};
_optimizeByTex =
{
private["_currentLOD","_texName","_texIsMat","_sel","_copyData"];
_currentLOD = _this @ 0;
_texName = _this @ 1;
_texIsMat = _this @ 2;
_sel = [_currentLOD,_texName,_texIsMat] call _selectionByTexture;
_copyData = [_currentLOD,_sel] call _copySelection;
[_currentLOD,_sel] call _removeFaces;
[_currentLOD,_copyData] call _pasteACopy;
//_currentLOD call _removeIsolatedPoints;
};
_optimizeLOD =
{
private["_currentLOD","_texList","_matList"];
_currentLOD = _this;
_texList = (_currentLOD callRuntime "texlist");
_matList = (_currentLOD callRuntime "matlist");
{
[_currentLOD,_x,false] call _optimizeByTex;
} forEach _texList;
{
[_currentLOD,_x,true] call _optimizeByTex;
} forEach _matList;
_currentLOD call _removeIsolatedPoints;
};
// main
_sourcePath = this @ 0;
_targetPath = _sourcePath;
_p3d = newLODObject;
_p3d loadP3D _sourcePath;
_LODs = getObjects _p3d;
_resolutions = getResolutions _p3d;
for "_i" from 0 to ((count _p3d) - 1) do
{
private["_currentLOD","_currentResolution"];
_currentLOD = _LODs @ _i;
_currentResolution = _resolutions @ _i;
if (IS_LOD_RESOLUTION(_currentResolution)) then
{
_currentLOD call _optimizeLOD;
};
};
save (_p3d as _targetPath);