Skip to content

Commit d706faa

Browse files
committed
#13156 WorkerSupport.buildSingelton allows to use constructors from functions list (fixed singleton typo along)
Builder defines the default materials and renamed 'vertexColorMaterial' to 'defaultVertexColorMaterial' for consistency. Fixed Builder.updateMaterials clone instructions
1 parent 6fd6885 commit d706faa

File tree

4 files changed

+93
-69
lines changed

4 files changed

+93
-69
lines changed

docs/examples/loaders/LoaderSupport.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ <h2>Methods</h2>
158158

159159
<h3>[method:null validate] ( [page:Function functionCodeBuilder], Array of [page:String libLocations], [page:String libPath], [page:LoaderSupport.WorkerRunnerRefImpl runnerImpl] )</h3>
160160
<div>
161-
[page:Function functionCodeBuilder] - Function that is invoked with funcBuildObject and funcBuildSingelton that allows stringification of objects and singletons.<br>
161+
[page:Function functionCodeBuilder] - Function that is invoked with funcBuildObject and funcBuildSingleton that allows stringification of objects and singletons.<br>
162162
Array of [page:String libLocations] - URL of libraries that shall be added to worker code relative to libPath.<br>
163163
[page:String libPath] - Base path used for loading libraries.<br>
164164
[page:LoaderSupport.WorkerRunnerRefImpl runnerImpl] - The default worker parser wrapper implementation (communication and execution). An extended class could be passed here.

examples/js/loaders/LoaderSupport.js

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,41 @@ THREE.LoaderSupport.Builder = (function () {
468468
this.logger.logInfo( 'Using THREE.LoaderSupport.Builder version: ' + LOADER_BUILDER_VERSION );
469469
this.callbacks = new THREE.LoaderSupport.Callbacks();
470470
this.materials = [];
471+
this._createDefaultMaterials();
471472
}
472473

474+
Builder.prototype._createDefaultMaterials = function () {
475+
var defaultMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
476+
defaultMaterial.name = 'defaultMaterial';
477+
478+
var defaultVertexColorMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
479+
defaultVertexColorMaterial.name = 'defaultVertexColorMaterial';
480+
defaultVertexColorMaterial.vertexColors = THREE.VertexColors;
481+
482+
var defaultLineMaterial = new THREE.LineBasicMaterial();
483+
defaultLineMaterial.name = 'defaultLineMaterial';
484+
485+
var defaultPointMaterial = new THREE.PointsMaterial( { size: 1, sizeAttenuation: false } );
486+
defaultPointMaterial.name = 'defaultPointMaterial';
487+
488+
var runtimeMaterials = {};
489+
runtimeMaterials[ defaultMaterial.name ] = defaultMaterial;
490+
runtimeMaterials[ defaultVertexColorMaterial.name ] = defaultVertexColorMaterial;
491+
runtimeMaterials[ defaultLineMaterial.name ] = defaultLineMaterial;
492+
runtimeMaterials[ defaultPointMaterial.name ] = defaultPointMaterial;
493+
494+
this.updateMaterials(
495+
{
496+
cmd: 'materialData',
497+
materials: {
498+
materialCloneInstructions: null,
499+
serializedMaterials: null,
500+
runtimeMaterials: runtimeMaterials
501+
}
502+
}
503+
);
504+
};
505+
473506
/**
474507
* Set materials loaded by any supplier of an Array of {@link THREE.Material}.
475508
* @memberOf THREE.LoaderSupport.Builder
@@ -614,6 +647,7 @@ THREE.LoaderSupport.Builder = (function () {
614647
}
615648
if ( useOrgMesh ) {
616649

650+
if ( meshPayload.computeBoundingSphere ) bufferGeometry.computeBoundingSphere();
617651
if ( geometryType === 0 ) {
618652

619653
mesh = new THREE.Mesh( bufferGeometry, material );
@@ -682,19 +716,27 @@ THREE.LoaderSupport.Builder = (function () {
682716

683717
var materialNameOrg = materialCloneInstructions.materialNameOrg;
684718
var materialOrg = this.materials[ materialNameOrg ];
685-
material = materialOrg.clone();
686719

687-
materialName = materialCloneInstructions.materialName;
688-
material.name = materialName;
720+
if ( Validator.isValid( materialNameOrg ) ) {
689721

690-
var materialProperties = materialCloneInstructions.materialProperties;
691-
for ( var key in materialProperties ) {
722+
material = materialOrg.clone();
692723

693-
if ( material.hasOwnProperty( key ) && materialProperties.hasOwnProperty( key ) ) material[ key ] = materialProperties[ key ];
724+
materialName = materialCloneInstructions.materialName;
725+
material.name = materialName;
694726

695-
}
696-
this.materials[ materialName ] = material;
727+
var materialProperties = materialCloneInstructions.materialProperties;
728+
for ( var key in materialProperties ) {
697729

730+
if ( material.hasOwnProperty( key ) && materialProperties.hasOwnProperty( key ) ) material[ key ] = materialProperties[ key ];
731+
732+
}
733+
this.materials[ materialName ] = material;
734+
735+
} else {
736+
737+
this.logger.logWarn( 'Requested material "' + materialNameOrg + '" is not available!' );
738+
739+
}
698740
}
699741

700742
var materials = materialPayload.materials.serializedMaterials;
@@ -783,42 +825,9 @@ THREE.LoaderSupport.LoaderBase = (function () {
783825

784826
this.loaderRootNode = new THREE.Group();
785827
this.builder = new THREE.LoaderSupport.Builder( this.logger );
786-
this._createDefaultMaterials();
787828
this.callbacks = new THREE.LoaderSupport.Callbacks();
788829
};
789830

790-
LoaderBase.prototype._createDefaultMaterials = function () {
791-
var defaultMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
792-
defaultMaterial.name = 'defaultMaterial';
793-
794-
var vertexColorMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
795-
vertexColorMaterial.name = 'vertexColorMaterial';
796-
vertexColorMaterial.vertexColors = THREE.VertexColors;
797-
798-
var defaultLineMaterial = new THREE.LineBasicMaterial();
799-
defaultLineMaterial.name = 'defaultLineMaterial';
800-
801-
var defaultPointMaterial = new THREE.PointsMaterial( { size: 10, sizeAttenuation: false } );
802-
defaultPointMaterial.name = 'defaultPointMaterial';
803-
804-
var runtimeMaterials = {};
805-
runtimeMaterials[ defaultMaterial.name ] = defaultMaterial;
806-
runtimeMaterials[ vertexColorMaterial.name ] = vertexColorMaterial;
807-
runtimeMaterials[ defaultLineMaterial.name ] = defaultLineMaterial;
808-
runtimeMaterials[ defaultPointMaterial.name ] = defaultPointMaterial;
809-
810-
this.builder.updateMaterials(
811-
{
812-
cmd: 'materialData',
813-
materials: {
814-
materialCloneInstructions: null,
815-
serializedMaterials: null,
816-
runtimeMaterials: runtimeMaterials
817-
}
818-
}
819-
);
820-
};
821-
822831
LoaderBase.prototype._applyPrepData = function ( prepData ) {
823832
if ( Validator.isValid( prepData ) ) {
824833

@@ -1203,7 +1212,7 @@ THREE.LoaderSupport.WorkerSupport = (function () {
12031212
* Validate the status of worker code and the derived worker.
12041213
* @memberOf THREE.LoaderSupport.WorkerSupport
12051214
*
1206-
* @param {Function} functionCodeBuilder Function that is invoked with funcBuildObject and funcBuildSingelton that allows stringification of objects and singletons.
1215+
* @param {Function} functionCodeBuilder Function that is invoked with funcBuildObject and funcBuildSingleton that allows stringification of objects and singletons.
12071216
* @param {String} parserName Name of the Parser object
12081217
* @param {String[]} libLocations URL of libraries that shall be added to worker code relative to libPath
12091218
* @param {String} libPath Base path used for loading libraries
@@ -1226,9 +1235,9 @@ THREE.LoaderSupport.WorkerSupport = (function () {
12261235

12271236
}
12281237

1229-
var userWorkerCode = functionCodeBuilder( buildObject, buildSingelton );
1238+
var userWorkerCode = functionCodeBuilder( buildObject, buildSingleton );
12301239
userWorkerCode += 'var Parser = '+ parserName + ';\n\n';
1231-
userWorkerCode += buildSingelton( runnerImpl.name, runnerImpl );
1240+
userWorkerCode += buildSingleton( runnerImpl.name, runnerImpl );
12321241
userWorkerCode += 'new ' + runnerImpl.name + '();\n\n';
12331242

12341243
var scope = this;
@@ -1238,7 +1247,7 @@ THREE.LoaderSupport.WorkerSupport = (function () {
12381247
var loadAllLibraries = function ( path, locations ) {
12391248
if ( locations.length === 0 ) {
12401249

1241-
scope.loaderWorker.initWorker( libsContent + userWorkerCode, scope.logger, runnerImpl.name );
1250+
scope.loaderWorker.initWorker( libsContent + userWorkerCode, runnerImpl.name );
12421251
scope.logger.logTimeEnd( 'buildWebWorkerCode' );
12431252

12441253
} else {
@@ -1260,7 +1269,7 @@ THREE.LoaderSupport.WorkerSupport = (function () {
12601269

12611270
} else {
12621271

1263-
this.loaderWorker.initWorker( userWorkerCode, this.logger, runnerImpl.name );
1272+
this.loaderWorker.initWorker( userWorkerCode, runnerImpl.name );
12641273
this.logger.logTimeEnd( 'buildWebWorkerCode' );
12651274

12661275
}
@@ -1329,26 +1338,41 @@ THREE.LoaderSupport.WorkerSupport = (function () {
13291338
return objectString;
13301339
};
13311340

1332-
var buildSingelton = function ( fullName, object ) {
1333-
var objectString = fullName + ' = (function () {\n\n';
1334-
objectString += '\t' + object.prototype.constructor.toString() + '\n\n';
1341+
var buildSingleton = function ( fullName, object, internalName ) {
1342+
var objectString = '';
1343+
var objectName = ( Validator.isValid( internalName ) ) ? internalName : object.name;
13351344

1336-
var funcString;
1337-
var objectPart;
1345+
var funcString, objectPart, constructorString;
13381346
for ( var name in object.prototype ) {
13391347

13401348
objectPart = object.prototype[ name ];
1341-
if ( typeof objectPart === 'function' ) {
1349+
if ( name === 'constructor' ) {
13421350

13431351
funcString = objectPart.toString();
1344-
objectString += '\t' + object.name + '.prototype.' + name + ' = ' + funcString + ';\n\n';
1352+
funcString = funcString.replace( 'function', '' );
1353+
constructorString = '\tfunction ' + objectName + funcString + ';\n\n';
1354+
1355+
} else if ( typeof objectPart === 'function' ) {
1356+
1357+
funcString = objectPart.toString();
1358+
objectString += '\t' + objectName + '.prototype.' + name + ' = ' + funcString + ';\n\n';
13451359

13461360
}
13471361

13481362
}
1349-
objectString += '\treturn ' + object.name + ';\n';
1363+
objectString += '\treturn ' + objectName + ';\n';
13501364
objectString += '})();\n\n';
1365+
if ( ! Validator.isValid( constructorString ) ) {
1366+
1367+
constructorString = fullName + ' = (function () {\n\n';
1368+
constructorString += '\t' + object.prototype.constructor.toString() + '\n\n';
1369+
objectString = constructorString + objectString;
13511370

1371+
} else {
1372+
1373+
objectString = fullName + ' = (function () {\n\n' + constructorString + objectString;
1374+
1375+
}
13521376
return objectString;
13531377
};
13541378

examples/js/loaders/OBJLoader2.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,16 @@ THREE.OBJLoader2 = (function () {
249249
};
250250

251251
this.workerSupport = Validator.verifyInput( this.workerSupport, new THREE.LoaderSupport.WorkerSupport( this.logger ) );
252-
var buildCode = function ( funcBuildObject, funcBuildSingelton ) {
252+
var buildCode = function ( funcBuildObject, funcBuildSingleton ) {
253253
var workerCode = '';
254254
workerCode += '/**\n';
255255
workerCode += ' * This code was constructed by OBJLoader2 buildCode.\n';
256256
workerCode += ' */\n\n';
257257
workerCode += 'THREE = { LoaderSupport: {} };\n\n';
258258
workerCode += funcBuildObject( 'THREE.LoaderSupport.Validator', Validator );
259-
workerCode += funcBuildSingelton( 'THREE.LoaderSupport.ConsoleLogger', THREE.LoaderSupport.ConsoleLogger );
260-
workerCode += funcBuildSingelton( 'THREE.LoaderSupport.LoaderBase', THREE.LoaderSupport.LoaderBase );
261-
workerCode += funcBuildSingelton( 'Parser', Parser );
259+
workerCode += funcBuildSingleton( 'THREE.LoaderSupport.ConsoleLogger', THREE.LoaderSupport.ConsoleLogger );
260+
workerCode += funcBuildSingleton( 'THREE.LoaderSupport.LoaderBase', THREE.LoaderSupport.LoaderBase );
261+
workerCode += funcBuildSingleton( 'Parser', Parser );
262262

263263
return workerCode;
264264
};
@@ -1037,7 +1037,7 @@ THREE.OBJLoader2 = (function () {
10371037
// both original and derived names do not lead to an existing material => need to use a default material
10381038
if ( ! THREE.LoaderSupport.Validator.isValid( materialOrg ) && ! THREE.LoaderSupport.Validator.isValid( material ) ) {
10391039

1040-
var defaultMaterialName = haveVertexColors ? 'vertexColorMaterial' : 'defaultMaterial';
1040+
var defaultMaterialName = haveVertexColors ? 'defaultVertexColorMaterial' : 'defaultMaterial';
10411041
materialOrg = this.materials[ defaultMaterialName ];
10421042
this.logger.logWarn( 'object_group "' + meshOutputGroup.objectName + '_' +
10431043
meshOutputGroup.groupName + '" was defined with unresolvable material "' +

examples/webgl_loader_obj2_meshspray.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@
134134
scope.logger.logTimeEnd( 'MeshSpray' );
135135
};
136136

137-
var buildCode = function ( funcBuildObject, funcBuildSingelton ) {
137+
var buildCode = function ( funcBuildObject, funcBuildSingleton ) {
138138
var workerCode = '';
139139
workerCode += '/**\n';
140140
workerCode += ' * This code was constructed by MeshSpray buildCode.\n';
141141
workerCode += ' */\n\n';
142142
workerCode += 'THREE.LoaderSupport = {};\n\n';
143143
workerCode += funcBuildObject( 'THREE.LoaderSupport.Validator', THREE.LoaderSupport.Validator );
144-
workerCode += funcBuildSingelton( 'THREE.LoaderSupport.ConsoleLogger', THREE.LoaderSupport.ConsoleLogger );
145-
workerCode += funcBuildSingelton( 'Parser', Parser );
144+
workerCode += funcBuildSingleton( 'THREE.LoaderSupport.ConsoleLogger', THREE.LoaderSupport.ConsoleLogger );
145+
workerCode += funcBuildSingleton( 'Parser', Parser );
146146

147147
return workerCode;
148148
};
@@ -261,16 +261,16 @@
261261
*
262262
* This is not the most effective way, but outlining possibilities
263263
*/
264-
var materialName = 'vertexColorMaterial_double';
265-
var vertexColorMaterialJson = this.serializedMaterials[ 'vertexColorMaterial' ];
264+
var materialName = 'defaultVertexColorMaterial_double';
265+
var defaultVertexColorMaterialJson = this.serializedMaterials[ 'defaultVertexColorMaterial' ];
266266
var loader = new THREE.MaterialLoader();
267267

268-
var vertexColorMaterialDouble = loader.parse( vertexColorMaterialJson );
269-
vertexColorMaterialDouble.name = materialName;
270-
vertexColorMaterialDouble.side = THREE.DoubleSide;
268+
var defaultVertexColorMaterialDouble = loader.parse( defaultVertexColorMaterialJson );
269+
defaultVertexColorMaterialDouble.name = materialName;
270+
defaultVertexColorMaterialDouble.side = THREE.DoubleSide;
271271

272272
var newSerializedMaterials = {};
273-
newSerializedMaterials[ materialName ] = vertexColorMaterialDouble.toJSON();
273+
newSerializedMaterials[ materialName ] = defaultVertexColorMaterialDouble.toJSON();
274274
var payload = {
275275
cmd: 'materialData',
276276
materials: {

0 commit comments

Comments
 (0)