-
Notifications
You must be signed in to change notification settings - Fork 41
/
render.js
executable file
·418 lines (385 loc) · 16.6 KB
/
render.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
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
#!/usr/bin/env node
var async = require('async');
var fs = require('fs');
var shell = require('shelljs');
var STK = require('./stk-ssc');
var cmd = require('./ssc-parseargs');
var render_helper = require('./ssc-render-helper');
var THREE = global.THREE;
var _ = STK.util;
cmd
.version('0.0.1')
.description('Renders asset by id')
.option('--id <id>', 'Scene or model id [default: e251dc99c5b4a9127af78305d7f7113c]', 'e251dc99c5b4a9127af78305d7f7113c')
.option('--ids_file <file>', 'File with model ids')
.option('--source <source>', 'Scene or model source [default: 3dw]', '3dw')
.option('--format <format>', 'Asset format')
.option('--output_dir <dir>', 'Base directory for output files', '.')
.option('--output_suffix <suffix>', 'Suffix to use in output png (sceneId.suffix.png)')
.option('--use_subdir [flag]','Put output into subdirectory per id [false]', STK.util.cmd.parseBoolean, false)
.optionGroups(['config_file', 'scene', 'render_options', 'view', 'render_views', 'color_by', 'transform3d', 'norm_geo', 'asset_cache'])
.option('--skip_existing', 'Skip rendering existing images [false]')
.option('--voxels <voxel-type>', 'Type of voxels to use [default: none]', 'none')
.option('--extra <extra>', 'Additional stuff to render [wall|navmap]',
/*/^(wall|navmap)$/, */ STK.util.cmd.collect, [])
.option('--level <level>', 'Scene level to render', STK.util.cmd.parseInt)
.option('--room <room>', 'Room id to render [null]')
.option('--show_ceiling [flag]', 'Whether to show ceiling or not', STK.util.cmd.parseBoolean, false)
.option('--hide_nonparent_arch [flag]', 'Whether to hide arch nodes that are not support parents', STK.util.cmd.parseBoolean, false)
.option('--hide_empty_regions [flag]', 'Whether to hide empty regions', STK.util.cmd.parseBoolean, false)
.option('--auto_align [flag]', 'Whether to auto align asset', STK.util.cmd.parseBoolean, false)
.option('--seed <num>', 'Random seed to use', STK.util.cmd.parseInt, 12345678)
.option('--repeat <num>', 'Number of times to repeat rendering of scene (for stress testing)', STK.util.cmd.parseInt, 1)
.option('--heapdump <num>', 'Number of times to dump the heap (for memory debugging)', STK.util.cmd.parseInt, 0)
.option('--material_type <material_type>')
.option('--material_side <material_side>')
.option('--support_articulated [flag]', 'Whether to parse articulated object', STK.util.cmd.parseBoolean)
.option('--use_search_controller [flag]', 'Whether to lookup asset information online', STK.util.cmd.parseBoolean, false)
.parse(process.argv);
var msg = cmd.checkImageSize(cmd);
if (msg) {
console.error(msg);
process.exit(-1);
}
var useSearchController = cmd.use_search_controller;
// Parse arguments and initialize globals
STK.Constants.setVirtualUnit(1); // set to meters
cmd.material_type = cmd.material_type || 'phong';
if (cmd.material_type) {
STK.materials.Materials.setDefaultMaterialType(cmd.material_type, cmd.material_type);
}
if (cmd.material_side) {
STK.materials.Materials.DefaultMaterialSide = STK.materials.Materials.getMaterialSide(cmd.material_side, STK.materials.Materials.DefaultMaterialSide);
}
var rendererOptions = cmd.getRendererOptions(cmd);
var renderer = new STK.PNGRenderer(rendererOptions);
STK.assets.AssetManager.enableCompressedLoading(renderer.renderer);
var assetManager = new STK.assets.AssetManager({
autoAlignModels: cmd.auto_align,
autoScaleModels: false,
assetCacheSize: cmd.assetCacheSize,
enableLights: cmd.use_lights,
defaultLightState: cmd.use_lights,
supportArticulated: cmd.support_articulated, mergeFixedParts: false,
searchController: useSearchController? new STK.search.BasicSearchController() : null
});
var ids = cmd.id ? [cmd.id] : ['e251dc99c5b4a9127af78305d7f7113c'];
var assetFiles = (cmd.assets != null)? [cmd.assets] : [];
STK.assets.registerAssetGroupsSync({ assetSources: [cmd.source], assetFiles: assetFiles });
if (cmd.format) {
STK.assets.AssetGroups.setDefaultFormat(cmd.format);
}
var assetGroup = assetManager.getAssetGroup(cmd.source);
if (!assetGroup) {
console.log('Unrecognized asset source ' + cmd.source);
return;
}
var supportedAssetTypes = ['scene', 'model', 'scan', 'arch'];
if (supportedAssetTypes.indexOf(assetGroup.type) < 0) {
console.log('Unsupported asset type ' + assetGroup.type);
return;
}
if (cmd.ids_file) {
var data = fs.readFileSync(cmd.ids_file, 'utf8');
ids = data.split('\n').map(function(x) { return x.trim(); }).filter(function(x) { return x.length; });
ids = STK.util.shuffle(ids);
} else if (cmd.id === 'all') {
ids = assetGroup.assetDb.assetInfos.map(function(info) { return info.id; });
ids = STK.util.shuffle(ids);
}
if (cmd.repeat > 1) {
ids = _.flatten(_.times(cmd.repeat, _.constant(ids)));
}
var sceneDefaults = { includeCeiling: true, defaultMaterialType: STK.materials.Materials.DefaultMaterialType, preload: cmd.extra };
if (cmd.scene) {
sceneDefaults = _.merge(sceneDefaults, cmd.scene);
}
sceneDefaults.emptyRoom = cmd.empty_room;
sceneDefaults.archOnly = cmd.arch_only;
var cameraConfig = _.defaults(Object.create(null), cmd.camera || {}, {
type: 'perspective',
fov: 50,
near: 0.1*STK.Constants.metersToVirtualUnit,
far: 400*STK.Constants.metersToVirtualUnit
});
main();
var rng = new STK.math.RNG();
rng.seed(cmd.seed);
var mainCache = {};
function processIds(assetsDb) {
var memcheckOpts = { heapdump: { limit: cmd.heapdump } };
async.forEachOfSeries(ids, function (id, index, callback) {
console.log('Processing ' + id);
STK.util.clearCache();
STK.util.checkMemory('Processing ' + id + ' index=' + index, memcheckOpts);
// Create THREE scene
var scene = new THREE.Scene();
var camera = STK.gfx.Camera.fromJson(cameraConfig, cmd.width, cmd.height);
scene.add(camera);
render_helper.addLights(scene, camera, cmd);
var cameraControls = new STK.controls.CameraControls({
camera: camera,
container: renderer.canvas,
controlType: 'none',
cameraPositionStrategy: 'positionByCentroid' //'positionByCentroid'
});
var outputDir = cmd.output_dir;
if (cmd.use_subdir) {
outputDir = outputDir + '/' + id;
if (cmd.skip_existing && shell.test('-d', outputDir)) {
console.warn('Skipping existing output at: ' + outputDir);
setTimeout(function () {
callback();
});
return;
}
}
var basename = outputDir + '/' + id;
shell.mkdir('-p', outputDir);
var fullId = cmd.source + '.' + id;
var metadata = assetsDb? assetsDb.getAssetInfo(fullId) : null;
var wrappedCallback = function() {
STK.geo.Object3DUtil.dispose(scene);
callback();
};
// Scenes
if (assetGroup.type === STK.Constants.assetTypeScene || assetGroup.type === STK.Constants.assetTypeArch) {
var floor = cmd.level;
var room = cmd.room;
if (floor != null) {
basename += '_' + floor;
if (room != null) {
basename += '_' + room;
}
}
var sceneOpts = {fullId: fullId, floor: floor, room: room};
if (cmd.assetInfo && (cmd.assetInfo.source == null || cmd.assetInfo.source === cmd.source)) {
sceneOpts = _.defaults(sceneOpts, cmd.assetInfo);
}
sceneOpts = _.defaults(sceneOpts, sceneDefaults);
assetManager.loadAssetAsScene(sceneOpts, function (err, sceneState) {
if (err) {
console.error('Error loading ' + fullId, err);
callback(err, null);
return;
}
//console.log(sceneState);
sceneState.compactify(); // Make sure that there are no missing models
render_helper.setShadows(sceneState, cmd.use_shadows);
render_helper.setVisible(sceneState, cmd);
scene.add(sceneState.fullScene);
var sceneBBox = STK.geo.Object3DUtil.getBoundingBox(sceneState.fullScene);
var bbdims = sceneBBox.dimensions();
console.log('Loaded ' + sceneState.getFullID() +
' bbdims: [' + bbdims.x + ',' + bbdims.y + ',' + bbdims.z + ']');
var bboxes = [];
var transformInfo = STK.geo.Object3DUtil.applyTransforms(sceneState.fullScene, {
assetName: sceneState.getFullID() ,
hasTransforms: cmd.auto_align || cmd.auto_scale,
normalizeSize: cmd.normalize_size,
normalizeSizeTo: cmd.normalize_size_to,
center: cmd.center,
bboxes: bboxes,
debug: true
});
var suffix = cmd.output_suffix || cmd.color_by;
var outbasename = suffix? (basename + '.' + suffix) : basename;
if (cmd.encode_index) {
outbasename = outbasename + '.encoded';
}
for (var i = 0; i < cmd.extra.length; i++) {
var extra = cmd.extra[i];
var extraInfo = sceneState.info[extra];
if (extraInfo) {
if (extraInfo.assetType === 'wall') {
if (extraInfo.data) {
var walls = extraInfo.data;
STK.scene.SceneUtil.visualizeWallLines(sceneState, walls);
} else {
console.warn('No wall for scene ' + fullId);
}
} else if (extraInfo.assetType === 'navmap') {
var collisionProcessor = STK.sim.CollisionProcessorFactory.createCollisionProcessor({mode: 'navgrid'});
if (extraInfo.data) {
var navscene = new STK.nav.NavScene({
sceneState: sceneState,
tileOverlap: 0.25,
baseTileHeight: collisionProcessor.traversableFloorHeight * STK.Constants.metersToVirtualUnit,
isValid: function (position) {
return collisionProcessor.isPositionInsideScene(sceneState, position);
}
});
sceneState.navscene = navscene;
//navscene.visualizeTileWeight();
navscene.visualizeTraversable(new THREE.Color('orange'));
} else {
console.warn('No navmap for scene ' + fullId);
}
} else {
console.warn('Unsupported extra ' + extra);
}
} else {
console.warn('No info for extra ' + extra);
}
}
var cmdOpts = _.defaults(_.pick(cmd, render_helper.render.cmdFields));
if (cmdOpts.view == undefined && cmdOpts.view_index == undefined) {
cmdOpts.view_index = 0;
}
render_helper.convertViews(sceneState, cmdOpts);
render_helper.updateViewOptsForTargetObjects(sceneState, cmdOpts, cameraControls, renderer);
if (cmdOpts.save_view_log) {
renderer.viewlogFilename = outbasename + '.views.jsonl';
shell.rm(renderer.viewlogFilename);
} else {
renderer.viewlogFilename = null;
}
function doRender(outbasename, cb) {
var renderOpts = {
cameraControls: cameraControls,
targetBBox: cmdOpts.targetBBox || sceneBBox,
basename: outbasename,
angleStep: cmd.turntable_step,
framerate: cmd.framerate,
tilt: cmd.tilt,
skipVideo: cmd.skip_video,
logdata: _.defaults({ assetType: assetGroup.type, toWorld: sceneState.scene.matrixWorld.toArray() }, sceneOpts),
callback: cb
};
if (cmd.render_turntable) {
// farther near for scenes to avoid z-fighting
camera.near = 4 * STK.Constants.metersToVirtualUnit;
}
render_helper.render(scene, renderer, renderOpts, cmdOpts, cmd.checkImageSize);
}
function onDrained() {
if (cmd.retexture) {
var count = 0;
async.whilst(function() {
return count < cmd.retexture;
}, function(cb) {
count++;
// reseed and retexture
var name = outbasename + '.' + count;
var retextureOpts = _.get(cmd, 'scene.retexture') || {};
var newseed = (count === 1 && retextureOpts.seed)? retextureOpts.seed : rng.randBits(31);
console.log('retexturing with seed ' + newseed + ' for ' + name);
rng.seed(newseed);
render_helper.retexture({
waitTextures: true,
cache: mainCache,
sceneState: sceneState,
assetManager: assetManager,
rng: rng,
retexture: retextureOpts
}, function(err, res) {
if (err) {
console.warn('Error retexturing scene: ', err);
}
doRender(name, cb);
});
}, function(err, n) {
wrappedCallback();
});
} else {
doRender(outbasename, wrappedCallback);
}
}
function waitImages() {
render_helper.getEnvMap(renderer, cmd.envmap).then(( { envMap } ) => {
scene.environment = envMap;
STK.util.waitImagesLoaded(onDrained);
});
}
render_helper.colorScene(scene, sceneState, cmd, basename, (err, res) => {
if (res) {
cmdOpts.pixel_index = res.index;
}
waitImages();
});
});
} else if (assetGroup.type === STK.Constants.assetTypeModel || assetGroup.type === STK.Constants.assetTypeScan) {
assetManager.clearCache();
assetManager.getModelInstance(cmd.source, fullId, function (mInst) {
// Ensure is normal geometry (for some reason, BufferGeometry not working with ssc)
STK.geo.Object3DUtil.normalizeGeometry(mInst.object3D, {
assetName: fullId,
toGeometry: cmd.to_geometry,
toNonindexed: cmd.to_nonindexed
});
var sceneState = new STK.scene.SceneState(null, mInst.model.info);
sceneState.addObject(mInst, cmd.auto_align);
scene.add(sceneState.fullScene);
var sceneBBox = STK.geo.Object3DUtil.getBoundingBox(mInst.object3D);
var bbdims = sceneBBox.dimensions();
console.log('Loaded ' + sceneState.getFullID() +
' bbdims: [' + bbdims.x + ',' + bbdims.y + ',' + bbdims.z + ']');
var bboxes = [];
var transformInfo = STK.geo.Object3DUtil.applyTransforms(sceneState.fullScene, {
assetName: fullId,
hasTransforms: cmd.auto_align || cmd.auto_scale,
normalizeSize: cmd.normalize_size,
normalizeSizeTo: cmd.normalize_size_to,
center: cmd.center,
bboxes: bboxes,
debug: true
});
var suffix = cmd.output_suffix || cmd.color_by;
var outbasename = suffix? (basename + '.' + suffix) : basename;
if (cmd.encode_index) {
outbasename = outbasename + '.encoded';
}
var cmdOpts = _.defaults(_.pick(cmd, render_helper.render.cmdFields));
render_helper.convertViews(sceneState, cmdOpts);
render_helper.updateViewOptsForTargetObjects(sceneState, cmdOpts, cameraControls, renderer);
if (cmdOpts.save_view_log) {
renderer.viewlogFilename = outbasename + '.views.jsonl';
shell.rm(renderer.viewlogFilename);
} else {
renderer.viewlogFilename = null;
}
var renderOpts = {
cameraControls: cameraControls,
targetBBox: cmdOpts.targetBBox || sceneBBox,
basename: outbasename,
angleStep: cmd.turntable_step,
framerate: cmd.framerate,
tilt: cmd.tilt,
skipVideo: cmd.skip_video,
logdata: { fullId: fullId, assetType: assetGroup.type, toWorld: mInst.getObject3D('Model').matrixWorld.toArray() },
callback: wrappedCallback
};
function onDrained() {
if (cmd.voxels && cmd.voxels !== 'none') {
mInst.voxels = new STK.model.ModelInstanceVoxels({voxelsField: cmd.voxels});
mInst.voxels.init(mInst);
mInst.voxels.loadVoxels(function (v) {
STK.geo.Object3DUtil.setVisible(mInst.object3D, false);
scene.add(v.getVoxelNode());
render_helper.render(scene, renderer, renderOpts, cmdOpts, cmd.checkImageSize);
});
} else {
render_helper.render(scene, renderer, renderOpts, cmdOpts, cmd.checkImageSize);
}
}
function waitImages() {
render_helper.getEnvMap(renderer, cmd.envmap).then(( { envMap } ) => {
scene.environment = envMap;
STK.util.waitImagesLoaded(onDrained);
});
}
render_helper.colorScene(scene, sceneState, cmd, basename, waitImages);
},
function (error) {
console.error('Error loading ' + fullId, error);
callback(error, null);
},
metadata);
}
}, function (err, results) {
console.log('DONE');
});
}
function main() {
processIds();
}