[b86468]: / v3 / js / libs / three / loaders / LoaderSupport.js

Download this file

1510 lines (1163 with data), 46.7 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
 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
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
/**
* @author Kai Salmen / https://kaisalmen.de
* Development repository: https://github.com/kaisalmen/WWOBJLoader
*/
'use strict';
if ( THREE.LoaderSupport === undefined ) { THREE.LoaderSupport = {} }
/**
* Validation functions.
* @class
*/
THREE.LoaderSupport.Validator = {
/**
* If given input is null or undefined, false is returned otherwise true.
*
* @param input Can be anything
* @returns {boolean}
*/
isValid: function( input ) {
return ( input !== null && input !== undefined );
},
/**
* If given input is null or undefined, the defaultValue is returned otherwise the given input.
*
* @param input Can be anything
* @param defaultValue Can be anything
* @returns {*}
*/
verifyInput: function( input, defaultValue ) {
return ( input === null || input === undefined ) ? defaultValue : input;
}
};
/**
* Callbacks utilized by loaders and builders.
* @class
*/
THREE.LoaderSupport.Callbacks = (function () {
var Validator = THREE.LoaderSupport.Validator;
function Callbacks() {
this.onProgress = null;
this.onMeshAlter = null;
this.onLoad = null;
this.onLoadMaterials = null;
}
/**
* Register callback function that is invoked by internal function "announceProgress" to print feedback.
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnProgress Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnProgress = function ( callbackOnProgress ) {
this.onProgress = Validator.verifyInput( callbackOnProgress, this.onProgress );
};
/**
* Register callback function that is called every time a mesh was loaded.
* Use {@link THREE.LoaderSupport.LoadedMeshUserOverride} for alteration instructions (geometry, material or disregard mesh).
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnMeshAlter Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnMeshAlter = function ( callbackOnMeshAlter ) {
this.onMeshAlter = Validator.verifyInput( callbackOnMeshAlter, this.onMeshAlter );
};
/**
* Register callback function that is called once loading of the complete OBJ file is completed.
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnLoad Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnLoad = function ( callbackOnLoad ) {
this.onLoad = Validator.verifyInput( callbackOnLoad, this.onLoad );
};
/**
* Register callback function that is called when materials have been loaded.
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnLoadMaterials Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnLoadMaterials = function ( callbackOnLoadMaterials ) {
this.onLoadMaterials = Validator.verifyInput( callbackOnLoadMaterials, this.onLoadMaterials );
};
return Callbacks;
})();
/**
* Object to return by callback onMeshAlter. Used to disregard a certain mesh or to return one to many meshes.
* @class
*
* @param {boolean} disregardMesh=false Tell implementation to completely disregard this mesh
* @param {boolean} disregardMesh=false Tell implementation that mesh(es) have been altered or added
*/
THREE.LoaderSupport.LoadedMeshUserOverride = (function () {
function LoadedMeshUserOverride( disregardMesh, alteredMesh ) {
this.disregardMesh = disregardMesh === true;
this.alteredMesh = alteredMesh === true;
this.meshes = [];
}
/**
* Add a mesh created within callback.
*
* @memberOf THREE.OBJLoader2.LoadedMeshUserOverride
*
* @param {THREE.Mesh} mesh
*/
LoadedMeshUserOverride.prototype.addMesh = function ( mesh ) {
this.meshes.push( mesh );
this.alteredMesh = true;
};
/**
* Answers if mesh shall be disregarded completely.
*
* @returns {boolean}
*/
LoadedMeshUserOverride.prototype.isDisregardMesh = function () {
return this.disregardMesh;
};
/**
* Answers if new mesh(es) were created.
*
* @returns {boolean}
*/
LoadedMeshUserOverride.prototype.providesAlteredMeshes = function () {
return this.alteredMesh;
};
return LoadedMeshUserOverride;
})();
/**
* A resource description used by {@link THREE.LoaderSupport.PrepData} and others.
* @class
*
* @param {string} url URL to the file
* @param {string} extension The file extension (type)
*/
THREE.LoaderSupport.ResourceDescriptor = (function () {
var Validator = THREE.LoaderSupport.Validator;
function ResourceDescriptor( url, extension ) {
var urlParts = url.split( '/' );
if ( urlParts.length < 2 ) {
this.path = null;
this.name = url;
this.url = url;
} else {
this.path = Validator.verifyInput( urlParts.slice( 0, urlParts.length - 1).join( '/' ) + '/', null );
this.name = urlParts[ urlParts.length - 1 ];
this.url = url;
}
this.name = Validator.verifyInput( this.name, 'Unnamed_Resource' );
this.extension = Validator.verifyInput( extension, 'default' );
this.extension = this.extension.trim();
this.content = null;
}
/**
* Set the content of this resource
* @memberOf THREE.LoaderSupport.ResourceDescriptor
*
* @param {Object} content The file content as arraybuffer or text
*/
ResourceDescriptor.prototype.setContent = function ( content ) {
this.content = Validator.verifyInput( content, null );
};
return ResourceDescriptor;
})();
/**
* Configuration instructions to be used by run method.
* @class
*/
THREE.LoaderSupport.PrepData = (function () {
var Validator = THREE.LoaderSupport.Validator;
function PrepData( modelName ) {
this.logging = {
enabled: true,
debug: false
};
this.modelName = Validator.verifyInput( modelName, '' );
this.resources = [];
this.callbacks = new THREE.LoaderSupport.Callbacks();
}
/**
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
* @memberOf THREE.LoaderSupport.PrepData
*
* @param {boolean} enabled True or false.
* @param {boolean} debug True or false.
*/
PrepData.prototype.setLogging = function ( enabled, debug ) {
this.logging.enabled = enabled === true;
this.logging.debug = debug === true;
};
/**
* Returns all callbacks as {@link THREE.LoaderSupport.Callbacks}
* @memberOf THREE.LoaderSupport.PrepData
*
* @returns {THREE.LoaderSupport.Callbacks}
*/
PrepData.prototype.getCallbacks = function () {
return this.callbacks;
};
/**
* Add a resource description.
* @memberOf THREE.LoaderSupport.PrepData
*
* @param {THREE.LoaderSupport.ResourceDescriptor} Adds a {@link THREE.LoaderSupport.ResourceDescriptor}
*/
PrepData.prototype.addResource = function ( resource ) {
this.resources.push( resource );
};
/**
* Clones this object and returns it afterwards. Callbacks and resources are not cloned deep (references!).
* @memberOf THREE.LoaderSupport.PrepData
*
* @returns {@link THREE.LoaderSupport.PrepData}
*/
PrepData.prototype.clone = function () {
var clone = new THREE.LoaderSupport.PrepData( this.modelName );
clone.logging.enabled = this.logging.enabled;
clone.logging.debug = this.logging.debug;
clone.resources = this.resources;
clone.callbacks = this.callbacks;
var property, value;
for ( property in this ) {
value = this[ property ];
if ( ! clone.hasOwnProperty( property ) && typeof this[ property ] !== 'function' ) {
clone[ property ] = value;
}
}
return clone;
};
/**
* Identify files or content of interest from an Array of {@link THREE.LoaderSupport.ResourceDescriptor}.
* @memberOf THREE.LoaderSupport.PrepData
*
* @param {THREE.LoaderSupport.ResourceDescriptor[]} resources Array of {@link THREE.LoaderSupport.ResourceDescriptor}
* @param Object fileDesc Object describing which resources are of interest (ext, type (string or UInt8Array) and ignore (boolean))
* @returns {{}} Object with each "ext" and the corresponding {@link THREE.LoaderSupport.ResourceDescriptor}
*/
PrepData.prototype.checkResourceDescriptorFiles = function ( resources, fileDesc ) {
var resource, triple, i, found;
var result = {};
for ( var index in resources ) {
resource = resources[ index ];
found = false;
if ( ! Validator.isValid( resource.name ) ) continue;
if ( Validator.isValid( resource.content ) ) {
for ( i = 0; i < fileDesc.length && !found; i++ ) {
triple = fileDesc[ i ];
if ( resource.extension.toLowerCase() === triple.ext.toLowerCase() ) {
if ( triple.ignore ) {
found = true;
} else if ( triple.type === "ArrayBuffer" ) {
// fast-fail on bad type
if ( ! ( resource.content instanceof ArrayBuffer || resource.content instanceof Uint8Array ) ) throw 'Provided content is not of type ArrayBuffer! Aborting...';
result[ triple.ext ] = resource;
found = true;
} else if ( triple.type === "String" ) {
if ( ! ( typeof( resource.content ) === 'string' || resource.content instanceof String) ) throw 'Provided content is not of type String! Aborting...';
result[ triple.ext ] = resource;
found = true;
}
}
}
if ( !found ) throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
} else {
// fast-fail on bad type
if ( ! ( typeof( resource.name ) === 'string' || resource.name instanceof String ) ) throw 'Provided file is not properly defined! Aborting...';
for ( i = 0; i < fileDesc.length && !found; i++ ) {
triple = fileDesc[ i ];
if ( resource.extension.toLowerCase() === triple.ext.toLowerCase() ) {
if ( ! triple.ignore ) result[ triple.ext ] = resource;
found = true;
}
}
if ( !found ) throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
}
}
return result;
};
return PrepData;
})();
/**
* Builds one or many THREE.Mesh from one raw set of Arraybuffers, materialGroup descriptions and further parameters.
* Supports vertex, vertexColor, normal, uv and index buffers.
* @class
*/
THREE.LoaderSupport.MeshBuilder = (function () {
var LOADER_MESH_BUILDER_VERSION = '1.2.1';
var Validator = THREE.LoaderSupport.Validator;
function MeshBuilder() {
console.info( 'Using THREE.LoaderSupport.MeshBuilder version: ' + LOADER_MESH_BUILDER_VERSION );
this.logging = {
enabled: true,
debug: false
};
this.callbacks = new THREE.LoaderSupport.Callbacks();
this.materials = [];
}
/**
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
* @memberOf THREE.LoaderSupport.MeshBuilder
*
* @param {boolean} enabled True or false.
* @param {boolean} debug True or false.
*/
MeshBuilder.prototype.setLogging = function ( enabled, debug ) {
this.logging.enabled = enabled === true;
this.logging.debug = debug === true;
};
/**
* Initializes the MeshBuilder (currently only default material initialisation).
* @memberOf THREE.LoaderSupport.MeshBuilder
*
*/
MeshBuilder.prototype.init = function () {
var defaultMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
defaultMaterial.name = 'defaultMaterial';
var defaultVertexColorMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
defaultVertexColorMaterial.name = 'defaultVertexColorMaterial';
defaultVertexColorMaterial.vertexColors = THREE.VertexColors;
var defaultLineMaterial = new THREE.LineBasicMaterial();
defaultLineMaterial.name = 'defaultLineMaterial';
var defaultPointMaterial = new THREE.PointsMaterial( { size: 1 } );
defaultPointMaterial.name = 'defaultPointMaterial';
var runtimeMaterials = {};
runtimeMaterials[ defaultMaterial.name ] = defaultMaterial;
runtimeMaterials[ defaultVertexColorMaterial.name ] = defaultVertexColorMaterial;
runtimeMaterials[ defaultLineMaterial.name ] = defaultLineMaterial;
runtimeMaterials[ defaultPointMaterial.name ] = defaultPointMaterial;
this.updateMaterials(
{
cmd: 'materialData',
materials: {
materialCloneInstructions: null,
serializedMaterials: null,
runtimeMaterials: runtimeMaterials
}
}
);
};
/**
* Set materials loaded by any supplier of an Array of {@link THREE.Material}.
* @memberOf THREE.LoaderSupport.MeshBuilder
*
* @param {THREE.Material[]} materials Array of {@link THREE.Material}
*/
MeshBuilder.prototype.setMaterials = function ( materials ) {
var payload = {
cmd: 'materialData',
materials: {
materialCloneInstructions: null,
serializedMaterials: null,
runtimeMaterials: Validator.isValid( this.callbacks.onLoadMaterials ) ? this.callbacks.onLoadMaterials( materials ) : materials
}
};
this.updateMaterials( payload );
};
MeshBuilder.prototype._setCallbacks = function ( callbacks ) {
if ( Validator.isValid( callbacks.onProgress ) ) this.callbacks.setCallbackOnProgress( callbacks.onProgress );
if ( Validator.isValid( callbacks.onMeshAlter ) ) this.callbacks.setCallbackOnMeshAlter( callbacks.onMeshAlter );
if ( Validator.isValid( callbacks.onLoad ) ) this.callbacks.setCallbackOnLoad( callbacks.onLoad );
if ( Validator.isValid( callbacks.onLoadMaterials ) ) this.callbacks.setCallbackOnLoadMaterials( callbacks.onLoadMaterials );
};
/**
* Delegates processing of the payload (mesh building or material update) to the corresponding functions (BW-compatibility).
* @memberOf THREE.LoaderSupport.MeshBuilder
*
* @param {Object} payload Raw Mesh or Material descriptions.
* @returns {THREE.Mesh[]} mesh Array of {@link THREE.Mesh} or null in case of material update
*/
MeshBuilder.prototype.processPayload = function ( payload ) {
if ( payload.cmd === 'meshData' ) {
return this.buildMeshes( payload );
} else if ( payload.cmd === 'materialData' ) {
this.updateMaterials( payload );
return null;
}
};
/**
* Builds one or multiple meshes from the data described in the payload (buffers, params, material info).
* @memberOf THREE.LoaderSupport.MeshBuilder
*
* @param {Object} meshPayload Raw mesh description (buffers, params, materials) used to build one to many meshes.
* @returns {THREE.Mesh[]} mesh Array of {@link THREE.Mesh}
*/
MeshBuilder.prototype.buildMeshes = function ( meshPayload ) {
var meshName = meshPayload.params.meshName;
var bufferGeometry = new THREE.BufferGeometry();
bufferGeometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( meshPayload.buffers.vertices ), 3 ) );
if ( Validator.isValid( meshPayload.buffers.indices ) ) {
bufferGeometry.setIndex( new THREE.BufferAttribute( new Uint32Array( meshPayload.buffers.indices ), 1 ));
}
var haveVertexColors = Validator.isValid( meshPayload.buffers.colors );
if ( haveVertexColors ) {
bufferGeometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( meshPayload.buffers.colors ), 3 ) );
}
if ( Validator.isValid( meshPayload.buffers.normals ) ) {
bufferGeometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( meshPayload.buffers.normals ), 3 ) );
} else {
bufferGeometry.computeVertexNormals();
}
if ( Validator.isValid( meshPayload.buffers.uvs ) ) {
bufferGeometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( meshPayload.buffers.uvs ), 2 ) );
}
var material, materialName, key;
var materialNames = meshPayload.materials.materialNames;
var createMultiMaterial = meshPayload.materials.multiMaterial;
var multiMaterials = [];
for ( key in materialNames ) {
materialName = materialNames[ key ];
material = this.materials[ materialName ];
if ( createMultiMaterial ) multiMaterials.push( material );
}
if ( createMultiMaterial ) {
material = multiMaterials;
var materialGroups = meshPayload.materials.materialGroups;
var materialGroup;
for ( key in materialGroups ) {
materialGroup = materialGroups[ key ];
bufferGeometry.addGroup( materialGroup.start, materialGroup.count, materialGroup.index );
}
}
var meshes = [];
var mesh;
var callbackOnMeshAlter = this.callbacks.onMeshAlter;
var callbackOnMeshAlterResult;
var useOrgMesh = true;
var geometryType = Validator.verifyInput( meshPayload.geometryType, 0 );
if ( Validator.isValid( callbackOnMeshAlter ) ) {
callbackOnMeshAlterResult = callbackOnMeshAlter(
{
detail: {
meshName: meshName,
bufferGeometry: bufferGeometry,
material: material,
geometryType: geometryType
}
}
);
if ( Validator.isValid( callbackOnMeshAlterResult ) ) {
if ( callbackOnMeshAlterResult.isDisregardMesh() ) {
useOrgMesh = false;
} else if ( callbackOnMeshAlterResult.providesAlteredMeshes() ) {
for ( var i in callbackOnMeshAlterResult.meshes ) {
meshes.push( callbackOnMeshAlterResult.meshes[ i ] );
}
useOrgMesh = false;
}
}
}
if ( useOrgMesh ) {
if ( meshPayload.computeBoundingSphere ) bufferGeometry.computeBoundingSphere();
if ( geometryType === 0 ) {
mesh = new THREE.Mesh( bufferGeometry, material );
} else if ( geometryType === 1) {
mesh = new THREE.LineSegments( bufferGeometry, material );
} else {
mesh = new THREE.Points( bufferGeometry, material );
}
mesh.name = meshName;
meshes.push( mesh );
}
var progressMessage;
if ( Validator.isValid( meshes ) && meshes.length > 0 ) {
var meshNames = [];
for ( var i in meshes ) {
mesh = meshes[ i ];
meshNames[ i ] = mesh.name;
}
progressMessage = 'Adding mesh(es) (' + meshNames.length + ': ' + meshNames + ') from input mesh: ' + meshName;
progressMessage += ' (' + ( meshPayload.progress.numericalValue * 100 ).toFixed( 2 ) + '%)';
} else {
progressMessage = 'Not adding mesh: ' + meshName;
progressMessage += ' (' + ( meshPayload.progress.numericalValue * 100 ).toFixed( 2 ) + '%)';
}
var callbackOnProgress = this.callbacks.onProgress;
if ( Validator.isValid( callbackOnProgress ) ) {
var event = new CustomEvent( 'MeshBuilderEvent', {
detail: {
type: 'progress',
modelName: meshPayload.params.meshName,
text: progressMessage,
numericalValue: meshPayload.progress.numericalValue
}
} );
callbackOnProgress( event );
}
return meshes;
};
/**
* Updates the materials with contained material objects (sync) or from alteration instructions (async).
* @memberOf THREE.LoaderSupport.MeshBuilder
*
* @param {Object} materialPayload Material update instructions
*/
MeshBuilder.prototype.updateMaterials = function ( materialPayload ) {
var material, materialName;
var materialCloneInstructions = materialPayload.materials.materialCloneInstructions;
if ( Validator.isValid( materialCloneInstructions ) ) {
var materialNameOrg = materialCloneInstructions.materialNameOrg;
var materialOrg = this.materials[ materialNameOrg ];
if ( Validator.isValid( materialNameOrg ) ) {
material = materialOrg.clone();
materialName = materialCloneInstructions.materialName;
material.name = materialName;
var materialProperties = materialCloneInstructions.materialProperties;
for ( var key in materialProperties ) {
if ( material.hasOwnProperty( key ) && materialProperties.hasOwnProperty( key ) ) material[ key ] = materialProperties[ key ];
}
this.materials[ materialName ] = material;
} else {
console.warn( 'Requested material "' + materialNameOrg + '" is not available!' );
}
}
var materials = materialPayload.materials.serializedMaterials;
if ( Validator.isValid( materials ) && Object.keys( materials ).length > 0 ) {
var loader = new THREE.MaterialLoader();
var materialJson;
for ( materialName in materials ) {
materialJson = materials[ materialName ];
if ( Validator.isValid( materialJson ) ) {
material = loader.parse( materialJson );
if ( this.logging.enabled ) console.info( 'De-serialized material with name "' + materialName + '" will be added.' );
this.materials[ materialName ] = material;
}
}
}
materials = materialPayload.materials.runtimeMaterials;
if ( Validator.isValid( materials ) && Object.keys( materials ).length > 0 ) {
for ( materialName in materials ) {
material = materials[ materialName ];
if ( this.logging.enabled ) console.info( 'Material with name "' + materialName + '" will be added.' );
this.materials[ materialName ] = material;
}
}
};
/**
* Returns the mapping object of material name and corresponding jsonified material.
*
* @returns {Object} Map of Materials in JSON representation
*/
MeshBuilder.prototype.getMaterialsJSON = function () {
var materialsJSON = {};
var material;
for ( var materialName in this.materials ) {
material = this.materials[ materialName ];
materialsJSON[ materialName ] = material.toJSON();
}
return materialsJSON;
};
/**
* Returns the mapping object of material name and corresponding material.
*
* @returns {Object} Map of {@link THREE.Material}
*/
MeshBuilder.prototype.getMaterials = function () {
return this.materials;
};
return MeshBuilder;
})();
/**
* Default implementation of the WorkerRunner responsible for creation and configuration of the parser within the worker.
*
* @class
*/
THREE.LoaderSupport.WorkerRunnerRefImpl = (function () {
function WorkerRunnerRefImpl() {
var scope = this;
var scopedRunner = function( event ) {
scope.processMessage( event.data );
};
self.addEventListener( 'message', scopedRunner, false );
}
/**
* Applies values from parameter object via set functions or via direct assignment.
* @memberOf THREE.LoaderSupport.WorkerRunnerRefImpl
*
* @param {Object} parser The parser instance
* @param {Object} params The parameter object
*/
WorkerRunnerRefImpl.prototype.applyProperties = function ( parser, params ) {
var property, funcName, values;
for ( property in params ) {
funcName = 'set' + property.substring( 0, 1 ).toLocaleUpperCase() + property.substring( 1 );
values = params[ property ];
if ( typeof parser[ funcName ] === 'function' ) {
parser[ funcName ]( values );
} else if ( parser.hasOwnProperty( property ) ) {
parser[ property ] = values;
}
}
};
/**
* Configures the Parser implementation according the supplied configuration object.
* @memberOf THREE.LoaderSupport.WorkerRunnerRefImpl
*
* @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
*/
WorkerRunnerRefImpl.prototype.processMessage = function ( payload ) {
if ( payload.cmd === 'run' ) {
var callbacks = {
callbackMeshBuilder: function ( payload ) {
self.postMessage( payload );
},
callbackProgress: function ( text ) {
if ( payload.logging.enabled && payload.logging.debug ) console.debug( 'WorkerRunner: progress: ' + text );
}
};
// Parser is expected to be named as such
var parser = new Parser();
if ( typeof parser[ 'setLogging' ] === 'function' ) parser.setLogging( payload.logging.enabled, payload.logging.debug );
this.applyProperties( parser, payload.params );
this.applyProperties( parser, payload.materials );
this.applyProperties( parser, callbacks );
parser.workerScope = self;
parser.parse( payload.data.input, payload.data.options );
if ( payload.logging.enabled ) console.log( 'WorkerRunner: Run complete!' );
callbacks.callbackMeshBuilder( {
cmd: 'complete',
msg: 'WorkerRunner completed run.'
} );
} else {
console.error( 'WorkerRunner: Received unknown command: ' + payload.cmd );
}
};
return WorkerRunnerRefImpl;
})();
/**
* This class provides means to transform existing parser code into a web worker. It defines a simple communication protocol
* which allows to configure the worker and receive raw mesh data during execution.
* @class
*/
THREE.LoaderSupport.WorkerSupport = (function () {
var WORKER_SUPPORT_VERSION = '2.2.0';
var Validator = THREE.LoaderSupport.Validator;
var LoaderWorker = (function () {
function LoaderWorker() {
this._reset();
}
LoaderWorker.prototype._reset = function () {
this.logging = {
enabled: true,
debug: false
};
this.worker = null;
this.runnerImplName = null;
this.callbacks = {
meshBuilder: null,
onLoad: null
};
this.terminateRequested = false;
this.queuedMessage = null;
this.started = false;
this.forceCopy = false;
};
LoaderWorker.prototype.setLogging = function ( enabled, debug ) {
this.logging.enabled = enabled === true;
this.logging.debug = debug === true;
};
LoaderWorker.prototype.setForceCopy = function ( forceCopy ) {
this.forceCopy = forceCopy === true;
};
LoaderWorker.prototype.initWorker = function ( code, runnerImplName ) {
this.runnerImplName = runnerImplName;
var blob = new Blob( [ code ], { type: 'application/javascript' } );
this.worker = new Worker( window.URL.createObjectURL( blob ) );
this.worker.onmessage = this._receiveWorkerMessage;
// set referemce to this, then processing in worker scope within "_receiveWorkerMessage" can access members
this.worker.runtimeRef = this;
// process stored queuedMessage
this._postMessage();
};
/**
* Executed in worker scope
*/
LoaderWorker.prototype._receiveWorkerMessage = function ( e ) {
var payload = e.data;
switch ( payload.cmd ) {
case 'meshData':
case 'materialData':
case 'imageData':
this.runtimeRef.callbacks.meshBuilder( payload );
break;
case 'complete':
this.runtimeRef.queuedMessage = null;
this.started = false;
this.runtimeRef.callbacks.onLoad( payload.msg );
if ( this.runtimeRef.terminateRequested ) {
if ( this.runtimeRef.logging.enabled ) console.info( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Run is complete. Terminating application on request!' );
this.runtimeRef._terminate();
}
break;
case 'error':
console.error( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Reported error: ' + payload.msg );
this.runtimeRef.queuedMessage = null;
this.started = false;
this.runtimeRef.callbacks.onLoad( payload.msg );
if ( this.runtimeRef.terminateRequested ) {
if ( this.runtimeRef.logging.enabled ) console.info( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Run reported error. Terminating application on request!' );
this.runtimeRef._terminate();
}
break;
default:
console.error( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Received unknown command: ' + payload.cmd );
break;
}
};
LoaderWorker.prototype.setCallbacks = function ( meshBuilder, onLoad ) {
this.callbacks.meshBuilder = Validator.verifyInput( meshBuilder, this.callbacks.meshBuilder );
this.callbacks.onLoad = Validator.verifyInput( onLoad, this.callbacks.onLoad );
};
LoaderWorker.prototype.run = function( payload ) {
if ( Validator.isValid( this.queuedMessage ) ) {
console.warn( 'Already processing message. Rejecting new run instruction' );
return;
} else {
this.queuedMessage = payload;
this.started = true;
}
if ( ! Validator.isValid( this.callbacks.meshBuilder ) ) throw 'Unable to run as no "MeshBuilder" callback is set.';
if ( ! Validator.isValid( this.callbacks.onLoad ) ) throw 'Unable to run as no "onLoad" callback is set.';
if ( payload.cmd !== 'run' ) payload.cmd = 'run';
if ( Validator.isValid( payload.logging ) ) {
payload.logging.enabled = payload.logging.enabled === true;
payload.logging.debug = payload.logging.debug === true;
} else {
payload.logging = {
enabled: true,
debug: false
}
}
this._postMessage();
};
LoaderWorker.prototype._postMessage = function () {
if ( Validator.isValid( this.queuedMessage ) && Validator.isValid( this.worker ) ) {
if ( this.queuedMessage.data.input instanceof ArrayBuffer ) {
var content;
if ( this.forceCopy ) {
content = this.queuedMessage.data.input.slice( 0 );
} else {
content = this.queuedMessage.data.input;
}
this.worker.postMessage( this.queuedMessage, [ content ] );
} else {
this.worker.postMessage( this.queuedMessage );
}
}
};
LoaderWorker.prototype.setTerminateRequested = function ( terminateRequested ) {
this.terminateRequested = terminateRequested === true;
if ( this.terminateRequested && Validator.isValid( this.worker ) && ! Validator.isValid( this.queuedMessage ) && this.started ) {
if ( this.logging.enabled ) console.info( 'Worker is terminated immediately as it is not running!' );
this._terminate();
}
};
LoaderWorker.prototype._terminate = function () {
this.worker.terminate();
this._reset();
};
return LoaderWorker;
})();
function WorkerSupport() {
console.info( 'Using THREE.LoaderSupport.WorkerSupport version: ' + WORKER_SUPPORT_VERSION );
this.logging = {
enabled: true,
debug: false
};
// check worker support first
if ( window.Worker === undefined ) throw "This browser does not support web workers!";
if ( window.Blob === undefined ) throw "This browser does not support Blob!";
if ( typeof window.URL.createObjectURL !== 'function' ) throw "This browser does not support Object creation from URL!";
this.loaderWorker = new LoaderWorker();
}
/**
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
* @memberOf THREE.LoaderSupport.WorkerSupport
*
* @param {boolean} enabled True or false.
* @param {boolean} debug True or false.
*/
WorkerSupport.prototype.setLogging = function ( enabled, debug ) {
this.logging.enabled = enabled === true;
this.logging.debug = debug === true;
this.loaderWorker.setLogging( this.logging.enabled, this.logging.debug );
};
/**
* Forces all ArrayBuffers to be transferred to worker to be copied.
* @memberOf THREE.LoaderSupport.WorkerSupport
*
* @param {boolean} forceWorkerDataCopy True or false.
*/
WorkerSupport.prototype.setForceWorkerDataCopy = function ( forceWorkerDataCopy ) {
this.loaderWorker.setForceCopy( forceWorkerDataCopy );
};
/**
* Validate the status of worker code and the derived worker.
* @memberOf THREE.LoaderSupport.WorkerSupport
*
* @param {Function} functionCodeBuilder Function that is invoked with funcBuildObject and funcBuildSingleton that allows stringification of objects and singletons.
* @param {String} parserName Name of the Parser object
* @param {String[]} libLocations URL of libraries that shall be added to worker code relative to libPath
* @param {String} libPath Base path used for loading libraries
* @param {THREE.LoaderSupport.WorkerRunnerRefImpl} runnerImpl The default worker parser wrapper implementation (communication and execution). An extended class could be passed here.
*/
WorkerSupport.prototype.validate = function ( functionCodeBuilder, parserName, libLocations, libPath, runnerImpl ) {
if ( Validator.isValid( this.loaderWorker.worker ) ) return;
if ( this.logging.enabled ) {
console.info( 'WorkerSupport: Building worker code...' );
console.time( 'buildWebWorkerCode' );
}
if ( Validator.isValid( runnerImpl ) ) {
if ( this.logging.enabled ) console.info( 'WorkerSupport: Using "' + runnerImpl.name + '" as Runner class for worker.' );
} else {
runnerImpl = THREE.LoaderSupport.WorkerRunnerRefImpl;
if ( this.logging.enabled ) console.info( 'WorkerSupport: Using DEFAULT "THREE.LoaderSupport.WorkerRunnerRefImpl" as Runner class for worker.' );
}
var userWorkerCode = functionCodeBuilder( buildObject, buildSingleton );
userWorkerCode += 'var Parser = '+ parserName + ';\n\n';
userWorkerCode += buildSingleton( runnerImpl.name, runnerImpl );
userWorkerCode += 'new ' + runnerImpl.name + '();\n\n';
var scope = this;
if ( Validator.isValid( libLocations ) && libLocations.length > 0 ) {
var libsContent = '';
var loadAllLibraries = function ( path, locations ) {
if ( locations.length === 0 ) {
scope.loaderWorker.initWorker( libsContent + userWorkerCode, runnerImpl.name );
if ( scope.logging.enabled ) console.timeEnd( 'buildWebWorkerCode' );
} else {
var loadedLib = function ( contentAsString ) {
libsContent += contentAsString;
loadAllLibraries( path, locations );
};
var fileLoader = new THREE.FileLoader();
fileLoader.setPath( path );
fileLoader.setResponseType( 'text' );
fileLoader.load( locations[ 0 ], loadedLib );
locations.shift();
}
};
loadAllLibraries( libPath, libLocations );
} else {
this.loaderWorker.initWorker( userWorkerCode, runnerImpl.name );
if ( this.logging.enabled ) console.timeEnd( 'buildWebWorkerCode' );
}
};
/**
* Specify functions that should be build when new raw mesh data becomes available and when the parser is finished.
* @memberOf THREE.LoaderSupport.WorkerSupport
*
* @param {Function} meshBuilder The mesh builder function. Default is {@link THREE.LoaderSupport.MeshBuilder}.
* @param {Function} onLoad The function that is called when parsing is complete.
*/
WorkerSupport.prototype.setCallbacks = function ( meshBuilder, onLoad ) {
this.loaderWorker.setCallbacks( meshBuilder, onLoad );
};
/**
* Runs the parser with the provided configuration.
* @memberOf THREE.LoaderSupport.WorkerSupport
*
* @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
*/
WorkerSupport.prototype.run = function ( payload ) {
this.loaderWorker.run( payload );
};
/**
* Request termination of worker once parser is finished.
* @memberOf THREE.LoaderSupport.WorkerSupport
*
* @param {boolean} terminateRequested True or false.
*/
WorkerSupport.prototype.setTerminateRequested = function ( terminateRequested ) {
this.loaderWorker.setTerminateRequested( terminateRequested );
};
var buildObject = function ( fullName, object ) {
var objectString = fullName + ' = {\n';
var part;
for ( var name in object ) {
part = object[ name ];
if ( typeof( part ) === 'string' || part instanceof String ) {
part = part.replace( '\n', '\\n' );
part = part.replace( '\r', '\\r' );
objectString += '\t' + name + ': "' + part + '",\n';
} else if ( part instanceof Array ) {
objectString += '\t' + name + ': [' + part + '],\n';
} else if ( Number.isInteger( part ) ) {
objectString += '\t' + name + ': ' + part + ',\n';
} else if ( typeof part === 'function' ) {
objectString += '\t' + name + ': ' + part + ',\n';
}
}
objectString += '}\n\n';
return objectString;
};
var buildSingleton = function ( fullName, object, internalName, basePrototypeName, ignoreFunctions ) {
var objectString = '';
var objectName = ( Validator.isValid( internalName ) ) ? internalName : object.name;
var funcString, objectPart, constructorString;
ignoreFunctions = Validator.verifyInput( ignoreFunctions, [] );
for ( var name in object.prototype ) {
objectPart = object.prototype[ name ];
if ( name === 'constructor' ) {
funcString = objectPart.toString();
funcString = funcString.replace( 'function', '' );
constructorString = '\tfunction ' + objectName + funcString + ';\n\n';
} else if ( typeof objectPart === 'function' ) {
if ( ignoreFunctions.indexOf( name ) < 0 ) {
funcString = objectPart.toString();
objectString += '\t' + objectName + '.prototype.' + name + ' = ' + funcString + ';\n\n';
}
}
}
objectString += '\treturn ' + objectName + ';\n';
objectString += '})();\n\n';
var inheritanceBlock = '';
if ( Validator.isValid( basePrototypeName ) ) {
inheritanceBlock += '\n';
inheritanceBlock += objectName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
inheritanceBlock += objectName + '.constructor = ' + objectName + ';\n';
inheritanceBlock += '\n';
}
if ( ! Validator.isValid( constructorString ) ) {
constructorString = fullName + ' = (function () {\n\n';
constructorString += inheritanceBlock + '\t' + object.prototype.constructor.toString() + '\n\n';
objectString = constructorString + objectString;
} else {
objectString = fullName + ' = (function () {\n\n' + inheritanceBlock + constructorString + objectString;
}
return objectString;
};
return WorkerSupport;
})();
/**
* Orchestrate loading of multiple OBJ files/data from an instruction queue with a configurable amount of workers (1-16).
* Workflow:
* prepareWorkers
* enqueueForRun
* processQueue
* tearDown (to force stop)
*
* @class
*
* @param {string} classDef Class definition to be used for construction
*/
THREE.LoaderSupport.WorkerDirector = (function () {
var LOADER_WORKER_DIRECTOR_VERSION = '2.2.1';
var Validator = THREE.LoaderSupport.Validator;
var MAX_WEB_WORKER = 16;
var MAX_QUEUE_SIZE = 8192;
function WorkerDirector( classDef ) {
console.info( 'Using THREE.LoaderSupport.WorkerDirector version: ' + LOADER_WORKER_DIRECTOR_VERSION );
this.logging = {
enabled: true,
debug: false
};
this.maxQueueSize = MAX_QUEUE_SIZE ;
this.maxWebWorkers = MAX_WEB_WORKER;
this.crossOrigin = null;
if ( ! Validator.isValid( classDef ) ) throw 'Provided invalid classDef: ' + classDef;
this.workerDescription = {
classDef: classDef,
globalCallbacks: {},
workerSupports: {},
forceWorkerDataCopy: true
};
this.objectsCompleted = 0;
this.instructionQueue = [];
this.instructionQueuePointer = 0;
this.callbackOnFinishedProcessing = null;
}
/**
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
* @memberOf THREE.LoaderSupport.WorkerDirector
*
* @param {boolean} enabled True or false.
* @param {boolean} debug True or false.
*/
WorkerDirector.prototype.setLogging = function ( enabled, debug ) {
this.logging.enabled = enabled === true;
this.logging.debug = debug === true;
};
/**
* Returns the maximum length of the instruction queue.
* @memberOf THREE.LoaderSupport.WorkerDirector
*
* @returns {number}
*/
WorkerDirector.prototype.getMaxQueueSize = function () {
return this.maxQueueSize;
};
/**
* Returns the maximum number of workers.
* @memberOf THREE.LoaderSupport.WorkerDirector
*
* @returns {number}
*/
WorkerDirector.prototype.getMaxWebWorkers = function () {
return this.maxWebWorkers;
};
/**
* Sets the CORS string to be used.
* @memberOf THREE.LoaderSupport.WorkerDirector
*
* @param {string} crossOrigin CORS value
*/
WorkerDirector.prototype.setCrossOrigin = function ( crossOrigin ) {
this.crossOrigin = crossOrigin;
};
/**
* Forces all ArrayBuffers to be transferred to worker to be copied.
* @memberOf THREE.LoaderSupport.WorkerDirector
*
* @param {boolean} forceWorkerDataCopy True or false.
*/
WorkerDirector.prototype.setForceWorkerDataCopy = function ( forceWorkerDataCopy ) {
this.workerDescription.forceWorkerDataCopy = forceWorkerDataCopy === true;
};
/**
* Create or destroy workers according limits. Set the name and register callbacks for dynamically created web workers.
* @memberOf THREE.LoaderSupport.WorkerDirector
*
* @param {THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks} globalCallbacks Register global callbacks used by all web workers
* @param {number} maxQueueSize Set the maximum size of the instruction queue (1-1024)
* @param {number} maxWebWorkers Set the maximum amount of workers (1-16)
*/
WorkerDirector.prototype.prepareWorkers = function ( globalCallbacks, maxQueueSize, maxWebWorkers ) {
if ( Validator.isValid( globalCallbacks ) ) this.workerDescription.globalCallbacks = globalCallbacks;
this.maxQueueSize = Math.min( maxQueueSize, MAX_QUEUE_SIZE );
this.maxWebWorkers = Math.min( maxWebWorkers, MAX_WEB_WORKER );
this.maxWebWorkers = Math.min( this.maxWebWorkers, this.maxQueueSize );
this.objectsCompleted = 0;
this.instructionQueue = [];
this.instructionQueuePointer = 0;
for ( var instanceNo = 0; instanceNo < this.maxWebWorkers; instanceNo++ ) {
var workerSupport = new THREE.LoaderSupport.WorkerSupport();
workerSupport.setLogging( this.logging.enabled, this.logging.debug );
workerSupport.setForceWorkerDataCopy( this.workerDescription.forceWorkerDataCopy );
this.workerDescription.workerSupports[ instanceNo ] = {
instanceNo: instanceNo,
inUse: false,
terminateRequested: false,
workerSupport: workerSupport,
loader: null
};
}
};
/**
* Store run instructions in internal instructionQueue.
* @memberOf THREE.LoaderSupport.WorkerDirector
*
* @param {THREE.LoaderSupport.PrepData} prepData
*/
WorkerDirector.prototype.enqueueForRun = function ( prepData ) {
if ( this.instructionQueue.length < this.maxQueueSize ) {
this.instructionQueue.push( prepData );
}
};
/**
* Returns if any workers are running.
*
* @memberOf THREE.LoaderSupport.WorkerDirector
* @returns {boolean}
*/
WorkerDirector.prototype.isRunning = function () {
var wsKeys = Object.keys( this.workerDescription.workerSupports );
return ( ( this.instructionQueue.length > 0 && this.instructionQueuePointer < this.instructionQueue.length ) || wsKeys.length > 0 );
};
/**
* Process the instructionQueue until it is depleted.
* @memberOf THREE.LoaderSupport.WorkerDirector
*/
WorkerDirector.prototype.processQueue = function () {
var prepData, supportDesc;
for ( var instanceNo in this.workerDescription.workerSupports ) {
supportDesc = this.workerDescription.workerSupports[ instanceNo ];
if ( ! supportDesc.inUse ) {
if ( this.instructionQueuePointer < this.instructionQueue.length ) {
prepData = this.instructionQueue[ this.instructionQueuePointer ];
this._kickWorkerRun( prepData, supportDesc );
this.instructionQueuePointer++;
} else {
this._deregister( supportDesc );
}
}
}
if ( ! this.isRunning() && this.callbackOnFinishedProcessing !== null ) {
this.callbackOnFinishedProcessing();
this.callbackOnFinishedProcessing = null;
}
};
WorkerDirector.prototype._kickWorkerRun = function( prepData, supportDesc ) {
supportDesc.inUse = true;
supportDesc.workerSupport.setTerminateRequested( supportDesc.terminateRequested );
if ( this.logging.enabled ) console.info( '\nAssigning next item from queue to worker (queue length: ' + this.instructionQueue.length + ')\n\n' );
var scope = this;
var prepDataCallbacks = prepData.getCallbacks();
var globalCallbacks = this.workerDescription.globalCallbacks;
var wrapperOnLoad = function ( event ) {
if ( Validator.isValid( globalCallbacks.onLoad ) ) globalCallbacks.onLoad( event );
if ( Validator.isValid( prepDataCallbacks.onLoad ) ) prepDataCallbacks.onLoad( event );
scope.objectsCompleted++;
supportDesc.inUse = false;
scope.processQueue();
};
var wrapperOnProgress = function ( event ) {
if ( Validator.isValid( globalCallbacks.onProgress ) ) globalCallbacks.onProgress( event );
if ( Validator.isValid( prepDataCallbacks.onProgress ) ) prepDataCallbacks.onProgress( event );
};
var wrapperOnMeshAlter = function ( event, override ) {
if ( Validator.isValid( globalCallbacks.onMeshAlter ) ) override = globalCallbacks.onMeshAlter( event, override );
if ( Validator.isValid( prepDataCallbacks.onMeshAlter ) ) override = globalCallbacks.onMeshAlter( event, override );
return override;
};
var wrapperOnLoadMaterials = function ( materials ) {
if ( Validator.isValid( globalCallbacks.onLoadMaterials ) ) materials = globalCallbacks.onLoadMaterials( materials );
if ( Validator.isValid( prepDataCallbacks.onLoadMaterials ) ) materials = prepDataCallbacks.onLoadMaterials( materials );
return materials;
};
supportDesc.loader = this._buildLoader( supportDesc.instanceNo );
var updatedCallbacks = new THREE.LoaderSupport.Callbacks();
updatedCallbacks.setCallbackOnLoad( wrapperOnLoad );
updatedCallbacks.setCallbackOnProgress( wrapperOnProgress );
updatedCallbacks.setCallbackOnMeshAlter( wrapperOnMeshAlter );
updatedCallbacks.setCallbackOnLoadMaterials( wrapperOnLoadMaterials );
prepData.callbacks = updatedCallbacks;
supportDesc.loader.run( prepData, supportDesc.workerSupport );
};
WorkerDirector.prototype._buildLoader = function ( instanceNo ) {
var classDef = this.workerDescription.classDef;
var loader = Object.create( classDef.prototype );
classDef.call( loader, THREE.DefaultLoadingManager );
// verify that all required functions are implemented
if ( ! loader.hasOwnProperty( 'instanceNo' ) ) throw classDef.name + ' has no property "instanceNo".';
loader.instanceNo = instanceNo;
if ( ! loader.hasOwnProperty( 'workerSupport' ) ) {
throw classDef.name + ' has no property "workerSupport".';
}
if ( typeof loader.run !== 'function' ) throw classDef.name + ' has no function "run".';
if ( ! loader.hasOwnProperty( 'callbacks' ) || ! Validator.isValid( loader.callbacks ) ) {
console.warn( classDef.name + ' has an invalid property "callbacks". Will change to "THREE.LoaderSupport.Callbacks"' );
loader.callbacks = new THREE.LoaderSupport.Callbacks();
}
return loader;
};
WorkerDirector.prototype._deregister = function ( supportDesc ) {
if ( Validator.isValid( supportDesc ) ) {
supportDesc.workerSupport.setTerminateRequested( true );
if ( this.logging.enabled ) console.info( 'Requested termination of worker #' + supportDesc.instanceNo + '.' );
var loaderCallbacks = supportDesc.loader.callbacks;
if ( Validator.isValid( loaderCallbacks.onProgress ) ) loaderCallbacks.onProgress( { detail: { text: '' } } );
delete this.workerDescription.workerSupports[ supportDesc.instanceNo ];
}
};
/**
* Terminate all workers.
* @memberOf THREE.LoaderSupport.WorkerDirector
*
* @param {callback} callbackOnFinishedProcessing Function called once all workers finished processing.
*/
WorkerDirector.prototype.tearDown = function ( callbackOnFinishedProcessing ) {
if ( this.logging.enabled ) console.info( 'WorkerDirector received the deregister call. Terminating all workers!' );
this.instructionQueuePointer = this.instructionQueue.length;
this.callbackOnFinishedProcessing = Validator.verifyInput( callbackOnFinishedProcessing, null );
for ( var name in this.workerDescription.workerSupports ) {
this.workerDescription.workerSupports[ name ].terminateRequested = true;
}
};
return WorkerDirector;
})();