add Potree (version 1.8)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Development repository: https://github.com/kaisalmen/WWOBJLoader
|
||||
*/
|
||||
|
||||
import { MTLLoader } from '../../../../jsm/loaders/MTLLoader.js';
|
||||
|
||||
|
||||
const MtlObjBridge = {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param processResult
|
||||
* @param assetLoader
|
||||
*/
|
||||
link: function ( processResult, assetLoader ) {
|
||||
|
||||
if ( typeof assetLoader.addMaterials === 'function' ) {
|
||||
|
||||
assetLoader.addMaterials( this.addMaterialsFromMtlLoader( processResult ), true );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the array instance of {@link MTLLoader.MaterialCreator}.
|
||||
*
|
||||
* @param Instance of {@link MTLLoader.MaterialCreator}
|
||||
*/
|
||||
addMaterialsFromMtlLoader: function ( materialCreator ) {
|
||||
|
||||
let newMaterials = {};
|
||||
|
||||
if ( materialCreator instanceof MTLLoader.MaterialCreator ) {
|
||||
|
||||
materialCreator.preload();
|
||||
newMaterials = materialCreator.materials;
|
||||
|
||||
}
|
||||
|
||||
return newMaterials;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export { MtlObjBridge };
|
||||
@@ -0,0 +1,276 @@
|
||||
/**
|
||||
* Development repository: https://github.com/kaisalmen/WWOBJLoader
|
||||
*/
|
||||
|
||||
import {
|
||||
LineBasicMaterial,
|
||||
MaterialLoader,
|
||||
MeshStandardMaterial,
|
||||
PointsMaterial
|
||||
} from '../../../../../build/three.module.js';
|
||||
|
||||
|
||||
const MaterialHandler = function () {
|
||||
|
||||
this.logging = {
|
||||
enabled: false,
|
||||
debug: false
|
||||
};
|
||||
|
||||
this.callbacks = {
|
||||
onLoadMaterials: null
|
||||
};
|
||||
this.materials = {};
|
||||
|
||||
};
|
||||
|
||||
MaterialHandler.prototype = {
|
||||
|
||||
constructor: MaterialHandler,
|
||||
|
||||
/**
|
||||
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
|
||||
*
|
||||
* @param {boolean} enabled True or false.
|
||||
* @param {boolean} debug True or false.
|
||||
*/
|
||||
setLogging: function ( enabled, debug ) {
|
||||
|
||||
this.logging.enabled = enabled === true;
|
||||
this.logging.debug = debug === true;
|
||||
|
||||
},
|
||||
|
||||
_setCallbacks: function ( onLoadMaterials ) {
|
||||
|
||||
if ( onLoadMaterials !== undefined && onLoadMaterials !== null && onLoadMaterials instanceof Function ) {
|
||||
|
||||
this.callbacks.onLoadMaterials = onLoadMaterials;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates default materials and adds them to the materials object.
|
||||
*
|
||||
* @param overrideExisting boolean Override existing material
|
||||
*/
|
||||
createDefaultMaterials: function ( overrideExisting ) {
|
||||
|
||||
const defaultMaterial = new MeshStandardMaterial( { color: 0xDCF1FF } );
|
||||
defaultMaterial.name = 'defaultMaterial';
|
||||
|
||||
const defaultVertexColorMaterial = new MeshStandardMaterial( { color: 0xDCF1FF } );
|
||||
defaultVertexColorMaterial.name = 'defaultVertexColorMaterial';
|
||||
defaultVertexColorMaterial.vertexColors = true;
|
||||
|
||||
const defaultLineMaterial = new LineBasicMaterial();
|
||||
defaultLineMaterial.name = 'defaultLineMaterial';
|
||||
|
||||
const defaultPointMaterial = new PointsMaterial( { size: 0.1 } );
|
||||
defaultPointMaterial.name = 'defaultPointMaterial';
|
||||
|
||||
const runtimeMaterials = {};
|
||||
runtimeMaterials[ defaultMaterial.name ] = defaultMaterial;
|
||||
runtimeMaterials[ defaultVertexColorMaterial.name ] = defaultVertexColorMaterial;
|
||||
runtimeMaterials[ defaultLineMaterial.name ] = defaultLineMaterial;
|
||||
runtimeMaterials[ defaultPointMaterial.name ] = defaultPointMaterial;
|
||||
|
||||
this.addMaterials( runtimeMaterials, overrideExisting );
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates the materials with contained material objects (sync) or from alteration instructions (async).
|
||||
*
|
||||
* @param {Object} materialPayload Material update instructions
|
||||
* @returns {Object} Map of {@link Material}
|
||||
*/
|
||||
addPayloadMaterials: function ( materialPayload ) {
|
||||
|
||||
let material, materialName;
|
||||
const materialCloneInstructions = materialPayload.materials.materialCloneInstructions;
|
||||
let newMaterials = {};
|
||||
|
||||
if ( materialCloneInstructions !== undefined && materialCloneInstructions !== null ) {
|
||||
|
||||
let materialNameOrg = materialCloneInstructions.materialNameOrg;
|
||||
materialNameOrg = ( materialNameOrg !== undefined && materialNameOrg !== null ) ? materialNameOrg : '';
|
||||
const materialOrg = this.materials[ materialNameOrg ];
|
||||
if ( materialOrg ) {
|
||||
|
||||
material = materialOrg.clone();
|
||||
|
||||
materialName = materialCloneInstructions.materialName;
|
||||
material.name = materialName;
|
||||
|
||||
Object.assign( material, materialCloneInstructions.materialProperties );
|
||||
|
||||
this.materials[ materialName ] = material;
|
||||
newMaterials[ materialName ] = material;
|
||||
|
||||
} else {
|
||||
|
||||
if ( this.logging.enabled ) {
|
||||
|
||||
console.info( 'Requested material "' + materialNameOrg + '" is not available!' );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let materials = materialPayload.materials.serializedMaterials;
|
||||
|
||||
if ( materials !== undefined && materials !== null && Object.keys( materials ).length > 0 ) {
|
||||
|
||||
const loader = new MaterialLoader();
|
||||
let materialJson;
|
||||
|
||||
for ( materialName in materials ) {
|
||||
|
||||
materialJson = materials[ materialName ];
|
||||
|
||||
if ( materialJson !== undefined && materialJson !== null ) {
|
||||
|
||||
material = loader.parse( materialJson );
|
||||
|
||||
if ( this.logging.enabled ) {
|
||||
|
||||
console.info( 'De-serialized material with name "' + materialName + '" will be added.' );
|
||||
|
||||
}
|
||||
|
||||
this.materials[ materialName ] = material;
|
||||
newMaterials[ materialName ] = material;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
materials = materialPayload.materials.runtimeMaterials;
|
||||
newMaterials = this.addMaterials( materials, true, newMaterials );
|
||||
|
||||
return newMaterials;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Set materials loaded by any supplier of an Array of {@link Material}.
|
||||
*
|
||||
* @param materials Object with named {@link Material}
|
||||
* @param overrideExisting boolean Override existing material
|
||||
* @param newMaterials [Object] with named {@link Material}
|
||||
*/
|
||||
addMaterials: function ( materials, overrideExisting, newMaterials ) {
|
||||
|
||||
if ( newMaterials === undefined || newMaterials === null ) {
|
||||
|
||||
newMaterials = {};
|
||||
|
||||
}
|
||||
|
||||
if ( materials !== undefined && materials !== null && Object.keys( materials ).length > 0 ) {
|
||||
|
||||
let material;
|
||||
let existingMaterial;
|
||||
let add;
|
||||
|
||||
for ( const materialName in materials ) {
|
||||
|
||||
material = materials[ materialName ];
|
||||
add = overrideExisting === true;
|
||||
|
||||
if ( ! add ) {
|
||||
|
||||
existingMaterial = this.materials[ materialName ];
|
||||
add = ( existingMaterial === null || existingMaterial === undefined );
|
||||
|
||||
}
|
||||
|
||||
if ( add ) {
|
||||
|
||||
this.materials[ materialName ] = material;
|
||||
newMaterials[ materialName ] = material;
|
||||
|
||||
}
|
||||
|
||||
if ( this.logging.enabled && this.logging.debug ) {
|
||||
|
||||
console.info( 'Material with name "' + materialName + '" was added.' );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( this.callbacks.onLoadMaterials ) {
|
||||
|
||||
this.callbacks.onLoadMaterials( newMaterials );
|
||||
|
||||
}
|
||||
|
||||
return newMaterials;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the mapping object of material name and corresponding material.
|
||||
*
|
||||
* @returns {Object} Map of {@link Material}
|
||||
*/
|
||||
getMaterials: function () {
|
||||
|
||||
return this.materials;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} materialName
|
||||
* @returns {Material}
|
||||
*/
|
||||
getMaterial: function ( materialName ) {
|
||||
|
||||
return this.materials[ materialName ];
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the mapping object of material name and corresponding jsonified material.
|
||||
*
|
||||
* @returns {Object} Map of Materials in JSON representation
|
||||
*/
|
||||
getMaterialsJSON: function () {
|
||||
|
||||
const materialsJSON = {};
|
||||
let material;
|
||||
|
||||
for ( const materialName in this.materials ) {
|
||||
|
||||
material = this.materials[ materialName ];
|
||||
materialsJSON[ materialName ] = material.toJSON();
|
||||
|
||||
}
|
||||
|
||||
return materialsJSON;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes all materials
|
||||
*/
|
||||
clearMaterials: function () {
|
||||
|
||||
this.materials = {};
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export { MaterialHandler };
|
||||
@@ -0,0 +1,313 @@
|
||||
/**
|
||||
* Development repository: https://github.com/kaisalmen/WWOBJLoader
|
||||
*/
|
||||
|
||||
import {
|
||||
BufferAttribute,
|
||||
BufferGeometry,
|
||||
LineSegments,
|
||||
Mesh,
|
||||
Points
|
||||
} from '../../../../../build/three.module.js';
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {MaterialHandler} materialHandler
|
||||
* @constructor
|
||||
*/
|
||||
const MeshReceiver = function ( materialHandler ) {
|
||||
|
||||
this.logging = {
|
||||
enabled: false,
|
||||
debug: false
|
||||
};
|
||||
|
||||
this.callbacks = {
|
||||
onProgress: null,
|
||||
onMeshAlter: null
|
||||
};
|
||||
this.materialHandler = materialHandler;
|
||||
|
||||
};
|
||||
|
||||
MeshReceiver.prototype = {
|
||||
|
||||
constructor: MeshReceiver,
|
||||
|
||||
/**
|
||||
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
|
||||
*
|
||||
* @param {boolean} enabled True or false.
|
||||
* @param {boolean} debug True or false.
|
||||
*/
|
||||
setLogging: function ( enabled, debug ) {
|
||||
|
||||
this.logging.enabled = enabled === true;
|
||||
this.logging.debug = debug === true;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Function} onProgress
|
||||
* @param {Function} onMeshAlter
|
||||
* @private
|
||||
*/
|
||||
_setCallbacks: function ( onProgress, onMeshAlter ) {
|
||||
|
||||
if ( onProgress !== null && onProgress !== undefined && onProgress instanceof Function ) {
|
||||
|
||||
this.callbacks.onProgress = onProgress;
|
||||
|
||||
}
|
||||
|
||||
if ( onMeshAlter !== null && onMeshAlter !== undefined && onMeshAlter instanceof Function ) {
|
||||
|
||||
this.callbacks.onMeshAlter = onMeshAlter;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Builds one or multiple meshes from the data described in the payload (buffers, params, material info).
|
||||
*
|
||||
* @param {Object} meshPayload Raw mesh description (buffers, params, materials) used to build one to many meshes.
|
||||
* @returns {Mesh[]} mesh Array of {@link Mesh}
|
||||
*/
|
||||
buildMeshes: function ( meshPayload ) {
|
||||
|
||||
const meshName = meshPayload.params.meshName;
|
||||
const buffers = meshPayload.buffers;
|
||||
|
||||
const bufferGeometry = new BufferGeometry();
|
||||
if ( buffers.vertices !== undefined && buffers.vertices !== null ) {
|
||||
|
||||
bufferGeometry.setAttribute( 'position', new BufferAttribute( new Float32Array( buffers.vertices ), 3 ) );
|
||||
|
||||
}
|
||||
|
||||
if ( buffers.indices !== undefined && buffers.indices !== null ) {
|
||||
|
||||
bufferGeometry.setIndex( new BufferAttribute( new Uint32Array( buffers.indices ), 1 ) );
|
||||
|
||||
}
|
||||
|
||||
if ( buffers.colors !== undefined && buffers.colors !== null ) {
|
||||
|
||||
bufferGeometry.setAttribute( 'color', new BufferAttribute( new Float32Array( buffers.colors ), 3 ) );
|
||||
|
||||
}
|
||||
|
||||
if ( buffers.normals !== undefined && buffers.normals !== null ) {
|
||||
|
||||
bufferGeometry.setAttribute( 'normal', new BufferAttribute( new Float32Array( buffers.normals ), 3 ) );
|
||||
|
||||
} else {
|
||||
|
||||
bufferGeometry.computeVertexNormals();
|
||||
|
||||
}
|
||||
|
||||
if ( buffers.uvs !== undefined && buffers.uvs !== null ) {
|
||||
|
||||
bufferGeometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( buffers.uvs ), 2 ) );
|
||||
|
||||
}
|
||||
|
||||
if ( buffers.skinIndex !== undefined && buffers.skinIndex !== null ) {
|
||||
|
||||
bufferGeometry.setAttribute( 'skinIndex', new BufferAttribute( new Uint16Array( buffers.skinIndex ), 4 ) );
|
||||
|
||||
}
|
||||
|
||||
if ( buffers.skinWeight !== undefined && buffers.skinWeight !== null ) {
|
||||
|
||||
bufferGeometry.setAttribute( 'skinWeight', new BufferAttribute( new Float32Array( buffers.skinWeight ), 4 ) );
|
||||
|
||||
}
|
||||
|
||||
let material, materialName, key;
|
||||
const materialNames = meshPayload.materials.materialNames;
|
||||
const createMultiMaterial = meshPayload.materials.multiMaterial;
|
||||
const multiMaterials = [];
|
||||
|
||||
for ( key in materialNames ) {
|
||||
|
||||
materialName = materialNames[ key ];
|
||||
material = this.materialHandler.getMaterial( materialName );
|
||||
if ( createMultiMaterial ) multiMaterials.push( material );
|
||||
|
||||
}
|
||||
|
||||
if ( createMultiMaterial ) {
|
||||
|
||||
material = multiMaterials;
|
||||
const materialGroups = meshPayload.materials.materialGroups;
|
||||
let materialGroup;
|
||||
for ( key in materialGroups ) {
|
||||
|
||||
materialGroup = materialGroups[ key ];
|
||||
bufferGeometry.addGroup( materialGroup.start, materialGroup.count, materialGroup.index );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const meshes = [];
|
||||
let mesh;
|
||||
let callbackOnMeshAlterResult;
|
||||
let useOrgMesh = true;
|
||||
const geometryType = meshPayload.geometryType === null ? 0 : meshPayload.geometryType;
|
||||
|
||||
if ( this.callbacks.onMeshAlter ) {
|
||||
|
||||
callbackOnMeshAlterResult = this.callbacks.onMeshAlter(
|
||||
{
|
||||
detail: {
|
||||
meshName: meshName,
|
||||
bufferGeometry: bufferGeometry,
|
||||
material: material,
|
||||
geometryType: geometryType
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
// here LoadedMeshUserOverride is required to be provided by the callback used to alter the results
|
||||
if ( callbackOnMeshAlterResult ) {
|
||||
|
||||
if ( callbackOnMeshAlterResult.isDisregardMesh() ) {
|
||||
|
||||
useOrgMesh = false;
|
||||
|
||||
} else if ( callbackOnMeshAlterResult.providesAlteredMeshes() ) {
|
||||
|
||||
for ( const i in callbackOnMeshAlterResult.meshes ) {
|
||||
|
||||
meshes.push( callbackOnMeshAlterResult.meshes[ i ] );
|
||||
|
||||
}
|
||||
|
||||
useOrgMesh = false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( useOrgMesh ) {
|
||||
|
||||
if ( meshPayload.computeBoundingSphere ) bufferGeometry.computeBoundingSphere();
|
||||
if ( geometryType === 0 ) {
|
||||
|
||||
mesh = new Mesh( bufferGeometry, material );
|
||||
|
||||
} else if ( geometryType === 1 ) {
|
||||
|
||||
mesh = new LineSegments( bufferGeometry, material );
|
||||
|
||||
} else {
|
||||
|
||||
mesh = new Points( bufferGeometry, material );
|
||||
|
||||
}
|
||||
|
||||
mesh.name = meshName;
|
||||
meshes.push( mesh );
|
||||
|
||||
}
|
||||
|
||||
let progressMessage = meshPayload.params.meshName;
|
||||
if ( meshes.length > 0 ) {
|
||||
|
||||
const meshNames = [];
|
||||
for ( const 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 ) + '%)';
|
||||
|
||||
}
|
||||
|
||||
if ( this.callbacks.onProgress ) {
|
||||
|
||||
this.callbacks.onProgress( 'progress', progressMessage, meshPayload.progress.numericalValue );
|
||||
|
||||
}
|
||||
|
||||
return meshes;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
const LoadedMeshUserOverride = function ( disregardMesh, alteredMesh ) {
|
||||
|
||||
this.disregardMesh = disregardMesh === true;
|
||||
this.alteredMesh = alteredMesh === true;
|
||||
this.meshes = [];
|
||||
|
||||
};
|
||||
|
||||
|
||||
LoadedMeshUserOverride.prototype = {
|
||||
|
||||
constructor: LoadedMeshUserOverride,
|
||||
|
||||
/**
|
||||
* Add a mesh created within callback.
|
||||
*
|
||||
* @param {Mesh} mesh
|
||||
*/
|
||||
addMesh: function ( mesh ) {
|
||||
|
||||
this.meshes.push( mesh );
|
||||
this.alteredMesh = true;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Answers if mesh shall be disregarded completely.
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isDisregardMesh: function () {
|
||||
|
||||
return this.disregardMesh;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Answers if new mesh(es) were created.
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
providesAlteredMeshes: function () {
|
||||
|
||||
return this.alteredMesh;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
MeshReceiver,
|
||||
LoadedMeshUserOverride
|
||||
};
|
||||
@@ -0,0 +1,264 @@
|
||||
/**
|
||||
* Development repository: https://github.com/kaisalmen/WWOBJLoader
|
||||
*/
|
||||
|
||||
const CodeSerializer = {
|
||||
|
||||
/**
|
||||
* Serialize an object with specific prototype definition.
|
||||
*
|
||||
* @param {Object} targetPrototype The object that should be serialized
|
||||
* @param {Object} targetPrototypeInstance An instance of the oriobject that should be serialized
|
||||
* @param {String} [basePrototypeName] Name of the prototype
|
||||
* @param {Object} [overrideFunctions} Array of {@Link CodeSerializationInstruction} allows to replace or remove function with provided content
|
||||
*
|
||||
* @returns {String}
|
||||
*/
|
||||
serializeClass: function ( targetPrototype, targetPrototypeInstance, basePrototypeName, overrideFunctions ) {
|
||||
|
||||
let objectPart, constructorString, i, funcInstructions, funcTemp;
|
||||
const fullObjectName = targetPrototypeInstance.constructor.name;
|
||||
const prototypeFunctions = [];
|
||||
const objectProperties = [];
|
||||
const objectFunctions = [];
|
||||
const isExtended = ( basePrototypeName !== null && basePrototypeName !== undefined );
|
||||
|
||||
if ( ! Array.isArray( overrideFunctions ) ) overrideFunctions = [];
|
||||
|
||||
for ( const name in targetPrototype.prototype ) {
|
||||
|
||||
objectPart = targetPrototype.prototype[ name ];
|
||||
funcInstructions = new CodeSerializationInstruction( name, fullObjectName + '.prototype.' + name );
|
||||
funcInstructions.setCode( objectPart.toString() );
|
||||
|
||||
if ( name === 'constructor' ) {
|
||||
|
||||
if ( ! funcInstructions.isRemoveCode() ) {
|
||||
|
||||
constructorString = fullObjectName + ' = ' + funcInstructions.getCode() + ';\n\n';
|
||||
|
||||
}
|
||||
|
||||
} else if ( typeof objectPart === 'function' ) {
|
||||
|
||||
funcTemp = overrideFunctions[ name ];
|
||||
|
||||
if ( funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName() ) {
|
||||
|
||||
funcInstructions = funcTemp;
|
||||
|
||||
}
|
||||
|
||||
if ( ! funcInstructions.isRemoveCode() ) {
|
||||
|
||||
if ( isExtended ) {
|
||||
|
||||
prototypeFunctions.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n' );
|
||||
|
||||
} else {
|
||||
|
||||
prototypeFunctions.push( '\t' + funcInstructions.getName() + ': ' + funcInstructions.getCode() + ',\n\n' );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for ( const name in targetPrototype ) {
|
||||
|
||||
objectPart = targetPrototype[ name ];
|
||||
funcInstructions = new CodeSerializationInstruction( name, fullObjectName + '.' + name );
|
||||
|
||||
if ( typeof objectPart === 'function' ) {
|
||||
|
||||
funcTemp = overrideFunctions[ name ];
|
||||
if ( funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName() ) {
|
||||
|
||||
funcInstructions = funcTemp;
|
||||
|
||||
} else {
|
||||
|
||||
funcInstructions.setCode( objectPart.toString() );
|
||||
|
||||
}
|
||||
|
||||
if ( ! funcInstructions.isRemoveCode() ) {
|
||||
|
||||
objectFunctions.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n' );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if ( typeof ( objectPart ) === 'string' || objectPart instanceof String ) {
|
||||
|
||||
funcInstructions.setCode( '\"' + objectPart.toString() + '\"' );
|
||||
|
||||
} else if ( typeof objectPart === 'object' ) {
|
||||
|
||||
console.log( 'Omitting object "' + funcInstructions.getName() + '" and replace it with empty object.' );
|
||||
funcInstructions.setCode( '{}' );
|
||||
|
||||
} else {
|
||||
|
||||
funcInstructions.setCode( objectPart );
|
||||
|
||||
}
|
||||
|
||||
if ( ! funcInstructions.isRemoveCode() ) {
|
||||
|
||||
objectProperties.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n' );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let objectString = constructorString + '\n\n';
|
||||
|
||||
if ( isExtended ) {
|
||||
|
||||
objectString += fullObjectName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
|
||||
|
||||
}
|
||||
|
||||
objectString += fullObjectName + '.prototype.constructor = ' + fullObjectName + ';\n';
|
||||
objectString += '\n\n';
|
||||
|
||||
for ( i = 0; i < objectProperties.length; i ++ ) {
|
||||
|
||||
objectString += objectProperties[ i ];
|
||||
|
||||
}
|
||||
|
||||
objectString += '\n\n';
|
||||
|
||||
for ( i = 0; i < objectFunctions.length; i ++ ) {
|
||||
|
||||
objectString += objectFunctions[ i ];
|
||||
|
||||
}
|
||||
|
||||
objectString += '\n\n';
|
||||
|
||||
if ( isExtended ) {
|
||||
|
||||
for ( i = 0; i < prototypeFunctions.length; i ++ ) {
|
||||
|
||||
objectString += prototypeFunctions[ i ];
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
objectString += fullObjectName + '.prototype = {\n\n';
|
||||
for ( i = 0; i < prototypeFunctions.length; i ++ ) {
|
||||
|
||||
objectString += prototypeFunctions[ i ];
|
||||
|
||||
}
|
||||
|
||||
objectString += '\n};';
|
||||
|
||||
}
|
||||
|
||||
objectString += '\n\n';
|
||||
|
||||
return objectString;
|
||||
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Allows to define instructions to override or remove
|
||||
* @param {String} name Usually the name of a function
|
||||
* @param {String} fullName The name plus full object description
|
||||
* @constructor
|
||||
*/
|
||||
const CodeSerializationInstruction = function ( name, fullName ) {
|
||||
|
||||
this.name = name;
|
||||
this.fullName = fullName;
|
||||
this.code = null;
|
||||
this.removeCode = false;
|
||||
|
||||
};
|
||||
|
||||
CodeSerializationInstruction.prototype = {
|
||||
|
||||
constructor: CodeSerializationInstruction,
|
||||
|
||||
/**
|
||||
* Returns the name of the function
|
||||
* @return {String}
|
||||
*/
|
||||
getName: function () {
|
||||
|
||||
return this.name;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the full name of the function
|
||||
* @return {String}
|
||||
*/
|
||||
getFullName: function () {
|
||||
|
||||
return this.fullName;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the string containing the serialized function
|
||||
* @param {String} code
|
||||
* @return {CodeSerializationInstruction}
|
||||
*/
|
||||
setCode: function ( code ) {
|
||||
|
||||
this.code = code;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the serialized function code
|
||||
* @return {String}
|
||||
*/
|
||||
getCode: function () {
|
||||
|
||||
return this.code;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Set if function should be removed
|
||||
* @param {boolean} removeCode
|
||||
* @return {CodeSerializationInstruction}
|
||||
*/
|
||||
setRemoveCode: function ( removeCode ) {
|
||||
|
||||
this.removeCode = removeCode;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* If function should be completely removed
|
||||
* @return {boolean}
|
||||
*/
|
||||
isRemoveCode: function () {
|
||||
|
||||
return this.removeCode;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export {
|
||||
CodeSerializer,
|
||||
CodeSerializationInstruction
|
||||
};
|
||||
@@ -0,0 +1,584 @@
|
||||
/**
|
||||
* Development repository: https://github.com/kaisalmen/WWOBJLoader
|
||||
*/
|
||||
|
||||
/**
|
||||
* These instructions are used by {WorkerExecutionSupport} to build code for the web worker or to assign code
|
||||
*
|
||||
* @param {boolean} supportsStandardWorker
|
||||
* @param {boolean} supportsJsmWorker
|
||||
* @constructor
|
||||
*/
|
||||
const CodeBuilderInstructions = function ( supportsStandardWorker, supportsJsmWorker, preferJsmWorker ) {
|
||||
|
||||
this.supportsStandardWorker = supportsStandardWorker;
|
||||
this.supportsJsmWorker = supportsJsmWorker;
|
||||
this.preferJsmWorker = preferJsmWorker;
|
||||
this.startCode = '';
|
||||
this.codeFragments = [];
|
||||
this.importStatements = [];
|
||||
|
||||
this.jsmWorkerUrl = null;
|
||||
this.defaultGeometryType = 0;
|
||||
|
||||
};
|
||||
|
||||
CodeBuilderInstructions.prototype = {
|
||||
|
||||
constructor: CodeBuilderInstructions,
|
||||
|
||||
isSupportsStandardWorker: function () {
|
||||
|
||||
return this.supportsStandardWorker;
|
||||
|
||||
},
|
||||
|
||||
isSupportsJsmWorker: function () {
|
||||
|
||||
return this.supportsJsmWorker;
|
||||
|
||||
},
|
||||
|
||||
isPreferJsmWorker: function () {
|
||||
|
||||
return this.preferJsmWorker;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the full path to the module that contains the worker code.
|
||||
*
|
||||
* @param {String} jsmWorkerUrl
|
||||
*/
|
||||
setJsmWorkerUrl: function ( jsmWorkerUrl ) {
|
||||
|
||||
if ( jsmWorkerUrl !== undefined && jsmWorkerUrl !== null ) {
|
||||
|
||||
this.jsmWorkerUrl = jsmWorkerUrl;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add code that is contained in addition to fragments and libraries
|
||||
* @param {String} startCode
|
||||
*/
|
||||
addStartCode: function ( startCode ) {
|
||||
|
||||
this.startCode = startCode;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add code fragment that is included in the provided order
|
||||
* @param {String} code
|
||||
*/
|
||||
addCodeFragment: function ( code ) {
|
||||
|
||||
this.codeFragments.push( code );
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add full path to a library that is contained at the start of the worker via "importScripts"
|
||||
* @param {String} libraryPath
|
||||
*/
|
||||
addLibraryImport: function ( libraryPath ) {
|
||||
|
||||
const libraryUrl = new URL( libraryPath, window.location.href ).href;
|
||||
const code = 'importScripts( "' + libraryUrl + '" );';
|
||||
this.importStatements.push( code );
|
||||
|
||||
},
|
||||
|
||||
getImportStatements: function () {
|
||||
|
||||
return this.importStatements;
|
||||
|
||||
},
|
||||
|
||||
getCodeFragments: function () {
|
||||
|
||||
return this.codeFragments;
|
||||
|
||||
},
|
||||
|
||||
getStartCode: function () {
|
||||
|
||||
return this.startCode;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
const WorkerExecutionSupport = function () {
|
||||
|
||||
// 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._reset();
|
||||
|
||||
};
|
||||
|
||||
WorkerExecutionSupport.WORKER_SUPPORT_VERSION = '3.2.0';
|
||||
console.info( 'Using WorkerSupport version: ' + WorkerExecutionSupport.WORKER_SUPPORT_VERSION );
|
||||
|
||||
|
||||
WorkerExecutionSupport.prototype = {
|
||||
|
||||
constructor: WorkerExecutionSupport,
|
||||
|
||||
_reset: function () {
|
||||
|
||||
this.logging = {
|
||||
enabled: false,
|
||||
debug: false
|
||||
};
|
||||
|
||||
const scope = this;
|
||||
const scopeTerminate = function ( ) {
|
||||
|
||||
scope._terminate();
|
||||
|
||||
};
|
||||
|
||||
this.worker = {
|
||||
native: null,
|
||||
jsmWorker: false,
|
||||
logging: true,
|
||||
workerRunner: {
|
||||
name: 'WorkerRunner',
|
||||
usesMeshDisassembler: false,
|
||||
defaultGeometryType: 0
|
||||
},
|
||||
terminateWorkerOnLoad: true,
|
||||
forceWorkerDataCopy: false,
|
||||
started: false,
|
||||
queuedMessage: null,
|
||||
callbacks: {
|
||||
onAssetAvailable: null,
|
||||
onLoad: null,
|
||||
terminate: scopeTerminate
|
||||
}
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
|
||||
*
|
||||
* @param {boolean} enabled True or false.
|
||||
* @param {boolean} debug True or false.
|
||||
*/
|
||||
setLogging: function ( enabled, debug ) {
|
||||
|
||||
this.logging.enabled = enabled === true;
|
||||
this.logging.debug = debug === true;
|
||||
this.worker.logging = enabled === true;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Forces all ArrayBuffers to be transferred to worker to be copied.
|
||||
*
|
||||
* @param {boolean} forceWorkerDataCopy True or false.
|
||||
*/
|
||||
setForceWorkerDataCopy: function ( forceWorkerDataCopy ) {
|
||||
|
||||
this.worker.forceWorkerDataCopy = forceWorkerDataCopy === true;
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Request termination of worker once parser is finished.
|
||||
*
|
||||
* @param {boolean} terminateWorkerOnLoad True or false.
|
||||
*/
|
||||
setTerminateWorkerOnLoad: function ( terminateWorkerOnLoad ) {
|
||||
|
||||
this.worker.terminateWorkerOnLoad = terminateWorkerOnLoad === true;
|
||||
if ( this.worker.terminateWorkerOnLoad && this.isWorkerLoaded( this.worker.jsmWorker ) &&
|
||||
this.worker.queuedMessage === null && this.worker.started ) {
|
||||
|
||||
if ( this.logging.enabled ) {
|
||||
|
||||
console.info( 'Worker is terminated immediately as it is not running!' );
|
||||
|
||||
}
|
||||
|
||||
this._terminate();
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Update all callbacks.
|
||||
*
|
||||
* @param {Function} onAssetAvailable The function for processing the data, e.g. {@link MeshReceiver}.
|
||||
* @param {Function} [onLoad] The function that is called when parsing is complete.
|
||||
*/
|
||||
updateCallbacks: function ( onAssetAvailable, onLoad ) {
|
||||
|
||||
if ( onAssetAvailable !== undefined && onAssetAvailable !== null ) {
|
||||
|
||||
this.worker.callbacks.onAssetAvailable = onAssetAvailable;
|
||||
|
||||
}
|
||||
|
||||
if ( onLoad !== undefined && onLoad !== null ) {
|
||||
|
||||
this.worker.callbacks.onLoad = onLoad;
|
||||
|
||||
}
|
||||
|
||||
this._verifyCallbacks();
|
||||
|
||||
},
|
||||
|
||||
_verifyCallbacks: function () {
|
||||
|
||||
if ( this.worker.callbacks.onAssetAvailable === undefined || this.worker.callbacks.onAssetAvailable === null ) {
|
||||
|
||||
throw 'Unable to run as no "onAssetAvailable" callback is set.';
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Builds the worker code according the provided Instructions.
|
||||
* If jsm worker code shall be built, then function may fall back to standard if lag is set
|
||||
*
|
||||
* @param {CodeBuilderInstructions} codeBuilderInstructions
|
||||
*/
|
||||
buildWorker: function ( codeBuilderInstructions ) {
|
||||
|
||||
let jsmSuccess = false;
|
||||
|
||||
if ( codeBuilderInstructions.isSupportsJsmWorker() && codeBuilderInstructions.isPreferJsmWorker() ) {
|
||||
|
||||
jsmSuccess = this._buildWorkerJsm( codeBuilderInstructions );
|
||||
|
||||
}
|
||||
|
||||
if ( ! jsmSuccess && codeBuilderInstructions.isSupportsStandardWorker() ) {
|
||||
|
||||
this._buildWorkerStandard( codeBuilderInstructions );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {CodeBuilderInstructions} codeBuilderInstructions
|
||||
* @return {boolean} Whether loading of jsm worker was successful
|
||||
* @private
|
||||
*/
|
||||
_buildWorkerJsm: function ( codeBuilderInstructions ) {
|
||||
|
||||
let jsmSuccess = true;
|
||||
const timeLabel = 'buildWorkerJsm';
|
||||
const workerAvailable = this._buildWorkerCheckPreconditions( true, timeLabel );
|
||||
if ( ! workerAvailable ) {
|
||||
|
||||
try {
|
||||
|
||||
const worker = new Worker( codeBuilderInstructions.jsmWorkerUrl.href, { type: 'module' } );
|
||||
this._configureWorkerCommunication( worker, true, codeBuilderInstructions.defaultGeometryType, timeLabel );
|
||||
|
||||
} catch ( e ) {
|
||||
|
||||
jsmSuccess = false;
|
||||
// Chrome throws this exception, but Firefox currently does not complain, but can't execute the worker afterwards
|
||||
if ( e instanceof TypeError || e instanceof SyntaxError ) {
|
||||
|
||||
console.error( 'Modules are not supported in workers.' );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return jsmSuccess;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Validate the status of worker code and the derived worker and specify functions that should be build when new raw mesh data becomes available and when the parser is finished.
|
||||
*
|
||||
* @param {CodeBuilderIns} buildWorkerCode The function that is invoked to create the worker code of the parser.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {CodeBuilderInstructions} codeBuilderInstructions
|
||||
* @private
|
||||
*/
|
||||
_buildWorkerStandard: function ( codeBuilderInstructions ) {
|
||||
|
||||
const timeLabel = 'buildWorkerStandard';
|
||||
const workerAvailable = this._buildWorkerCheckPreconditions( false, timeLabel );
|
||||
if ( ! workerAvailable ) {
|
||||
|
||||
let concatenateCode = '';
|
||||
codeBuilderInstructions.getImportStatements().forEach( function ( element ) {
|
||||
|
||||
concatenateCode += element + '\n';
|
||||
|
||||
} );
|
||||
concatenateCode += '\n';
|
||||
codeBuilderInstructions.getCodeFragments().forEach( function ( element ) {
|
||||
|
||||
concatenateCode += element + '\n';
|
||||
|
||||
} );
|
||||
concatenateCode += '\n';
|
||||
concatenateCode += codeBuilderInstructions.getStartCode();
|
||||
|
||||
const blob = new Blob( [ concatenateCode ], { type: 'application/javascript' } );
|
||||
const worker = new Worker( window.URL.createObjectURL( blob ) );
|
||||
|
||||
this._configureWorkerCommunication( worker, false, codeBuilderInstructions.defaultGeometryType, timeLabel );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_buildWorkerCheckPreconditions: function ( requireJsmWorker, timeLabel ) {
|
||||
|
||||
let workerAvailable = false;
|
||||
if ( this.isWorkerLoaded( requireJsmWorker ) ) {
|
||||
|
||||
workerAvailable = true;
|
||||
|
||||
} else {
|
||||
|
||||
if ( this.logging.enabled ) {
|
||||
|
||||
console.info( 'WorkerExecutionSupport: Building ' + ( requireJsmWorker ? 'jsm' : 'standard' ) + ' worker code...' );
|
||||
console.time( timeLabel );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return workerAvailable;
|
||||
|
||||
},
|
||||
|
||||
_configureWorkerCommunication: function ( worker, haveJsmWorker, defaultGeometryType, timeLabel ) {
|
||||
|
||||
this.worker.native = worker;
|
||||
this.worker.jsmWorker = haveJsmWorker;
|
||||
|
||||
const scope = this;
|
||||
const scopedReceiveWorkerMessage = function ( event ) {
|
||||
|
||||
scope._receiveWorkerMessage( event );
|
||||
|
||||
};
|
||||
|
||||
this.worker.native.onmessage = scopedReceiveWorkerMessage;
|
||||
this.worker.native.onerror = scopedReceiveWorkerMessage;
|
||||
if ( defaultGeometryType !== undefined && defaultGeometryType !== null ) {
|
||||
|
||||
this.worker.workerRunner.defaultGeometryType = defaultGeometryType;
|
||||
|
||||
}
|
||||
|
||||
if ( this.logging.enabled ) {
|
||||
|
||||
console.timeEnd( timeLabel );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns if Worker code is available and complies with expectation.
|
||||
* @param {boolean} requireJsmWorker
|
||||
* @return {boolean|*}
|
||||
*/
|
||||
isWorkerLoaded: function ( requireJsmWorker ) {
|
||||
|
||||
return this.worker.native !== null &&
|
||||
( ( requireJsmWorker && this.worker.jsmWorker ) || ( ! requireJsmWorker && ! this.worker.jsmWorker ) );
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Executed in worker scope
|
||||
*/
|
||||
_receiveWorkerMessage: function ( event ) {
|
||||
|
||||
// fast-fail in case of error
|
||||
if ( event.type === 'error' ) {
|
||||
|
||||
console.error( event );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
const payload = event.data;
|
||||
const workerRunnerName = this.worker.workerRunner.name;
|
||||
switch ( payload.cmd ) {
|
||||
|
||||
case 'assetAvailable':
|
||||
this.worker.callbacks.onAssetAvailable( payload );
|
||||
break;
|
||||
|
||||
case 'completeOverall':
|
||||
this.worker.queuedMessage = null;
|
||||
this.worker.started = false;
|
||||
if ( this.worker.callbacks.onLoad !== null ) {
|
||||
|
||||
this.worker.callbacks.onLoad( payload.msg );
|
||||
|
||||
}
|
||||
|
||||
if ( this.worker.terminateWorkerOnLoad ) {
|
||||
|
||||
if ( this.worker.logging.enabled ) {
|
||||
|
||||
console.info( 'WorkerSupport [' + workerRunnerName + ']: Run is complete. Terminating application on request!' );
|
||||
|
||||
}
|
||||
|
||||
this.worker.callbacks.terminate();
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'error':
|
||||
console.error( 'WorkerSupport [' + workerRunnerName + ']: Reported error: ' + payload.msg );
|
||||
this.worker.queuedMessage = null;
|
||||
this.worker.started = false;
|
||||
if ( this.worker.callbacks.onLoad !== null ) {
|
||||
|
||||
this.worker.callbacks.onLoad( payload.msg );
|
||||
|
||||
}
|
||||
|
||||
if ( this.worker.terminateWorkerOnLoad ) {
|
||||
|
||||
if ( this.worker.logging.enabled ) {
|
||||
|
||||
console.info( 'WorkerSupport [' + workerRunnerName + ']: Run reported error. Terminating application on request!' );
|
||||
|
||||
}
|
||||
|
||||
this.worker.callbacks.terminate();
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
console.error( 'WorkerSupport [' + workerRunnerName + ']: Received unknown command: ' + payload.cmd );
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Runs the parser with the provided configuration.
|
||||
*
|
||||
* @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
|
||||
*/
|
||||
executeParallel: function ( payload, transferables ) {
|
||||
|
||||
payload.cmd = 'parse';
|
||||
payload.usesMeshDisassembler = this.worker.workerRunner.usesMeshDisassembler;
|
||||
payload.defaultGeometryType = this.worker.workerRunner.defaultGeometryType;
|
||||
if ( ! this._verifyWorkerIsAvailable( payload, transferables ) ) return;
|
||||
|
||||
this._postMessage();
|
||||
|
||||
},
|
||||
|
||||
_verifyWorkerIsAvailable: function ( payload, transferables ) {
|
||||
|
||||
this._verifyCallbacks();
|
||||
let ready = true;
|
||||
if ( this.worker.queuedMessage !== null ) {
|
||||
|
||||
console.warn( 'Already processing message. Rejecting new run instruction' );
|
||||
ready = false;
|
||||
|
||||
} else {
|
||||
|
||||
this.worker.queuedMessage = {
|
||||
payload: payload,
|
||||
transferables: ( transferables === undefined || transferables === null ) ? [] : transferables
|
||||
};
|
||||
this.worker.started = true;
|
||||
|
||||
}
|
||||
|
||||
return ready;
|
||||
|
||||
},
|
||||
|
||||
_postMessage: function () {
|
||||
|
||||
if ( this.worker.queuedMessage !== null ) {
|
||||
|
||||
if ( this.worker.queuedMessage.payload.data.input instanceof ArrayBuffer ) {
|
||||
|
||||
let transferables = [];
|
||||
if ( this.worker.forceWorkerDataCopy ) {
|
||||
|
||||
transferables.push( this.worker.queuedMessage.payload.data.input.slice( 0 ) );
|
||||
|
||||
} else {
|
||||
|
||||
transferables.push( this.worker.queuedMessage.payload.data.input );
|
||||
|
||||
}
|
||||
|
||||
if ( this.worker.queuedMessage.transferables.length > 0 ) {
|
||||
|
||||
transferables = transferables.concat( this.worker.queuedMessage.transferables );
|
||||
|
||||
}
|
||||
|
||||
this.worker.native.postMessage( this.worker.queuedMessage.payload, transferables );
|
||||
|
||||
} else {
|
||||
|
||||
this.worker.native.postMessage( this.worker.queuedMessage.payload );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_terminate: function () {
|
||||
|
||||
this.worker.native.terminate();
|
||||
this._reset();
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
CodeBuilderInstructions,
|
||||
WorkerExecutionSupport
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Development repository: https://github.com/kaisalmen/WWOBJLoader
|
||||
*/
|
||||
|
||||
import { OBJLoader2Parser } from '../../OBJLoader2Parser.js';
|
||||
|
||||
import {
|
||||
WorkerRunner,
|
||||
DefaultWorkerPayloadHandler
|
||||
} from './WorkerRunner.js';
|
||||
|
||||
new WorkerRunner( new DefaultWorkerPayloadHandler( new OBJLoader2Parser() ) );
|
||||
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* Development repository: https://github.com/kaisalmen/WWOBJLoader
|
||||
*/
|
||||
|
||||
const ObjectManipulator = function () {
|
||||
};
|
||||
|
||||
ObjectManipulator.prototype = {
|
||||
|
||||
constructor: ObjectManipulator,
|
||||
|
||||
/**
|
||||
* Applies values from parameter object via set functions or via direct assignment.
|
||||
*
|
||||
* @param {Object} objToAlter The objToAlter instance
|
||||
* @param {Object} params The parameter object
|
||||
* @param {boolean} forceCreation Force the creation of a property
|
||||
*/
|
||||
applyProperties: function ( objToAlter, params, forceCreation ) {
|
||||
|
||||
// fast-fail
|
||||
if ( objToAlter === undefined || objToAlter === null || params === undefined || params === null ) return;
|
||||
|
||||
let property, funcName, values;
|
||||
for ( property in params ) {
|
||||
|
||||
funcName = 'set' + property.substring( 0, 1 ).toLocaleUpperCase() + property.substring( 1 );
|
||||
values = params[ property ];
|
||||
|
||||
if ( typeof objToAlter[ funcName ] === 'function' ) {
|
||||
|
||||
objToAlter[ funcName ]( values );
|
||||
|
||||
} else if ( objToAlter.hasOwnProperty( property ) || forceCreation ) {
|
||||
|
||||
objToAlter[ property ] = values;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
const DefaultWorkerPayloadHandler = function ( parser ) {
|
||||
|
||||
this.parser = parser;
|
||||
this.logging = {
|
||||
enabled: false,
|
||||
debug: false
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
DefaultWorkerPayloadHandler.prototype = {
|
||||
|
||||
constructor: DefaultWorkerPayloadHandler,
|
||||
|
||||
handlePayload: function ( payload ) {
|
||||
|
||||
if ( payload.logging ) {
|
||||
|
||||
this.logging.enabled = payload.logging.enabled === true;
|
||||
this.logging.debug = payload.logging.debug === true;
|
||||
|
||||
}
|
||||
|
||||
if ( payload.cmd === 'parse' ) {
|
||||
|
||||
const scope = this;
|
||||
const callbacks = {
|
||||
callbackOnAssetAvailable: function ( payload ) {
|
||||
|
||||
self.postMessage( payload );
|
||||
|
||||
},
|
||||
callbackOnProgress: function ( text ) {
|
||||
|
||||
if ( scope.logging.enabled && scope.logging.debug ) console.debug( 'WorkerRunner: progress: ' + text );
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
const parser = this.parser;
|
||||
if ( typeof parser[ 'setLogging' ] === 'function' ) {
|
||||
|
||||
parser.setLogging( this.logging.enabled, this.logging.debug );
|
||||
|
||||
}
|
||||
|
||||
const objectManipulator = new ObjectManipulator();
|
||||
objectManipulator.applyProperties( parser, payload.params, false );
|
||||
objectManipulator.applyProperties( parser, callbacks, false );
|
||||
|
||||
const arraybuffer = payload.data.input;
|
||||
let executeFunctionName = 'execute';
|
||||
if ( typeof parser.getParseFunctionName === 'function' ) executeFunctionName = parser.getParseFunctionName();
|
||||
if ( payload.usesMeshDisassembler ) {
|
||||
|
||||
// TODO: Allow to plug and use generic MeshDisassembler
|
||||
|
||||
} else {
|
||||
|
||||
parser[ executeFunctionName ]( arraybuffer, payload.data.options );
|
||||
|
||||
}
|
||||
|
||||
if ( this.logging.enabled ) console.log( 'WorkerRunner: Run complete!' );
|
||||
|
||||
self.postMessage( {
|
||||
cmd: 'completeOverall',
|
||||
msg: 'WorkerRunner completed run.'
|
||||
} );
|
||||
|
||||
} else {
|
||||
|
||||
console.error( 'WorkerRunner: Received unknown command: ' + payload.cmd );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Default implementation of the WorkerRunner responsible for creation and configuration of the parser within the worker.
|
||||
* @constructor
|
||||
*/
|
||||
const WorkerRunner = function ( payloadHandler ) {
|
||||
|
||||
this.payloadHandler = payloadHandler;
|
||||
|
||||
const scope = this;
|
||||
const scopedRunner = function ( event ) {
|
||||
|
||||
scope.processMessage( event.data );
|
||||
|
||||
};
|
||||
|
||||
self.addEventListener( 'message', scopedRunner, false );
|
||||
|
||||
};
|
||||
|
||||
WorkerRunner.prototype = {
|
||||
|
||||
constructor: WorkerRunner,
|
||||
|
||||
/**
|
||||
* Configures the Parser implementation according the supplied configuration object.
|
||||
*
|
||||
* @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
|
||||
*/
|
||||
processMessage: function ( payload ) {
|
||||
|
||||
this.payloadHandler.handlePayload( payload );
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export {
|
||||
WorkerRunner,
|
||||
DefaultWorkerPayloadHandler,
|
||||
ObjectManipulator
|
||||
};
|
||||
Reference in New Issue
Block a user