add Potree (version 1.8)

This commit is contained in:
Tim Wundenberg
2021-08-04 21:30:59 +02:00
parent 04de194255
commit a90fcc336e
1693 changed files with 740830 additions and 0 deletions

View File

@@ -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 };

View File

@@ -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
};