From 0b9985b826f1575a847c3bd41eeee1e1ea64ffe9 Mon Sep 17 00:00:00 2001 From: Kai Salmen Date: Sun, 29 Jan 2017 14:26:59 +0100 Subject: [PATCH] #9756 MeshCreator is now private to OBJLoader2 --- examples/js/loaders/OBJLoader2.js | 394 ++++++++++++++-------------- examples/js/loaders/WWOBJLoader2.js | 4 +- 2 files changed, 198 insertions(+), 200 deletions(-) diff --git a/examples/js/loaders/OBJLoader2.js b/examples/js/loaders/OBJLoader2.js index 45fef16a126f6f..85cbf291c6056f 100644 --- a/examples/js/loaders/OBJLoader2.js +++ b/examples/js/loaders/OBJLoader2.js @@ -6,7 +6,7 @@ 'use strict'; if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} } -THREE.OBJLoader2.version = '1.0.1'; +THREE.OBJLoader2.version = '1.0.2'; /** * Use this class to load OBJ data from files or to parse OBJ data from arraybuffer or text @@ -22,7 +22,7 @@ THREE.OBJLoader2 = (function () { this.path = ''; this.fileLoader = new THREE.FileLoader( this.manager ); - this.meshCreator = new THREE.OBJLoader2.MeshCreator(); + this.meshCreator = new MeshCreator(); this.parser = new THREE.OBJLoader2.Parser( this.meshCreator ); this.validated = false; @@ -74,7 +74,7 @@ THREE.OBJLoader2 = (function () { * Use this convenient method to load an OBJ file at the given URL. Per default the fileLoader uses an arraybuffer * @memberOf THREE.OBJLoader2 * - * @param {string} URL of the file to load + * @param {string} url URL of the file to load * @param {callback} onLoad Called after loading was successfully completed * @param {callback} onProgress Called to report progress of loading * @param {callback} onError Called after an error occurred during loading @@ -148,7 +148,7 @@ THREE.OBJLoader2 = (function () { if ( this.validated ) return; this.fileLoader = ( this.fileLoader == null ) ? new THREE.FileLoader( this.manager ) : this.fileLoader; - this.setPath( null ); + this.setPath(); this.parser._validate(); this.meshCreator._validate(); @@ -167,10 +167,199 @@ THREE.OBJLoader2 = (function () { return sceneGraphBaseNode; }; - return OBJLoader2; -})(); + /** + * MeshCreator is used to transform THREE.OBJLoader2.RawObjectDescriptions to THREE.Mesh + * + * @class + */ + var MeshCreator = (function () { + + function MeshCreator() { + this.sceneGraphBaseNode = null; + this.materials = null; + this.debug = false; + this.globalObjectCount = 1; + + this.validated = false; + } + + MeshCreator.prototype._setSceneGraphBaseNode = function ( sceneGraphBaseNode ) { + this.sceneGraphBaseNode = ( sceneGraphBaseNode == null ) ? ( this.sceneGraphBaseNode == null ? new THREE.Group() : this.sceneGraphBaseNode ) : sceneGraphBaseNode; + }; + + MeshCreator.prototype._setMaterials = function ( materials ) { + this.materials = ( materials == null ) ? ( this.materials == null ? { materials: [] } : this.materials ) : materials; + }; + + MeshCreator.prototype._setDebug = function ( debug ) { + this.debug = ( debug == null ) ? this.debug : debug; + }; + + MeshCreator.prototype._validate = function () { + if ( this.validated ) return; + + this._setSceneGraphBaseNode( null ); + this._setMaterials( null ); + this._setDebug( null ); + this.globalObjectCount = 1; + }; + + MeshCreator.prototype._finalize = function () { + this.sceneGraphBaseNode = null; + this.materials = null; + this.validated = false; + }; + + /** + * This is an internal function, but due to its importance to Parser it is documented. + * RawObjectDescriptions are transformed to THREE.Mesh. + * It is ensured that rawObjectDescriptions only contain objects with vertices (no need to check). + * This method shall be overridden by the web worker implementation + * + * @param {THREE.OBJLoader2.RawObjectDescription[]} rawObjectDescriptions Array of descriptive information and data (vertices, normals, uvs) about the parsed object(s) + * @param {number} inputObjectCount Number of objects already retrieved from OBJ + * @param {number} absoluteVertexCount Sum of all vertices of all rawObjectDescriptions + * @param {number} absoluteNormalCount Sum of all normals of all rawObjectDescriptions + * @param {number} absoluteUvCount Sum of all uvs of all rawObjectDescriptions + */ + MeshCreator.prototype._buildMesh = function ( rawObjectDescriptions, inputObjectCount, absoluteVertexCount, absoluteNormalCount, absoluteUvCount ) { + + if ( this.debug ) console.log( 'MeshCreator.buildRawMeshData:\nInput object no.: ' + inputObjectCount ); + + var bufferGeometry = new THREE.BufferGeometry(); + var vertexBA = new THREE.BufferAttribute( new Float32Array( absoluteVertexCount ), 3 ); + bufferGeometry.addAttribute( 'position', vertexBA ); + + var normalBA; + if ( absoluteNormalCount > 0 ) { + + normalBA = new THREE.BufferAttribute( new Float32Array( absoluteNormalCount ), 3 ); + bufferGeometry.addAttribute( 'normal', normalBA ); + + } + var uvBA; + if ( absoluteUvCount > 0 ) { + + uvBA = new THREE.BufferAttribute( new Float32Array( absoluteUvCount ), 2 ); + bufferGeometry.addAttribute( 'uv', uvBA ); + + } + + if ( this.debug ) console.log( 'Creating Multi-Material for object no.: ' + this.globalObjectCount ); + + var rawObjectDescription; + var material; + var materialName; + var createMultiMaterial = rawObjectDescriptions.length > 1; + var materials = []; + var materialIndex = 0; + var materialIndexMapping = []; + var selectedMaterialIndex; + + var vertexBAOffset = 0; + var vertexGroupOffset = 0; + var vertexLength; + var normalOffset = 0; + var uvOffset = 0; + + for ( var oodIndex in rawObjectDescriptions ) { + rawObjectDescription = rawObjectDescriptions[ oodIndex ]; + + materialName = rawObjectDescription.materialName; + material = this.materials[ materialName ]; + if ( ! material ) { + + material = this.materials[ 'defaultMaterial' ]; + if ( ! material ) { + + material = new THREE.MeshStandardMaterial( { color: 0xDCF1FF} ); + material.name = 'defaultMaterial'; + this.materials[ 'defaultMaterial' ] = material; + } + console.warn( 'object_group "' + rawObjectDescription.objectName + '_' + rawObjectDescription.groupName + '" was defined without material! Assigning "defaultMaterial".' ); + + } + // clone material in case flat shading is needed due to smoothingGroup 0 + if ( rawObjectDescription.smoothingGroup === 0 ) { + + materialName = material.name + '_flat'; + var materialClone = this.materials[ materialName ]; + if ( ! materialClone ) { + + materialClone = material.clone(); + materialClone.name = materialName; + materialClone.shading = THREE.FlatShading; + this.materials[ materialName ] = name; + + } + + } + + vertexLength = rawObjectDescription.vertices.length; + if ( createMultiMaterial ) { + // re-use material if already used before. Reduces materials array size and eliminates duplicates + selectedMaterialIndex = materialIndexMapping[ materialName ]; + if ( ! selectedMaterialIndex ) { + + selectedMaterialIndex = materialIndex; + materialIndexMapping[ materialName ] = materialIndex; + materials.push( material ); + materialIndex++; + + } + + bufferGeometry.addGroup( vertexGroupOffset, vertexLength / 3, selectedMaterialIndex ); + vertexGroupOffset += vertexLength / 3; + } + + vertexBA.set( rawObjectDescription.vertices, vertexBAOffset ); + vertexBAOffset += vertexLength; + + if ( normalBA ) { + + normalBA.set( rawObjectDescription.normals, normalOffset ); + normalOffset += rawObjectDescription.normals.length; + + } + if ( uvBA ) { + + uvBA.set( rawObjectDescription.uvs, uvOffset ); + uvOffset += rawObjectDescription.uvs.length; + + } + if ( this.debug ) this._printReport( rawObjectDescription, selectedMaterialIndex ); + + } + if ( ! normalBA ) bufferGeometry.computeVertexNormals(); + + if ( createMultiMaterial ) material = new THREE.MultiMaterial( materials ); + var mesh = new THREE.Mesh( bufferGeometry, material ); + this.sceneGraphBaseNode.add( mesh ); + + this.globalObjectCount++; + }; + + MeshCreator.prototype._printReport = function ( rawObjectDescription, selectedMaterialIndex ) { + console.log( + ' Output Object no.: ' + this.globalObjectCount + + '\n objectName: ' + rawObjectDescription.objectName + + '\n groupName: ' + rawObjectDescription.groupName + + '\n materialName: ' + rawObjectDescription.materialName + + '\n materialIndex: ' + selectedMaterialIndex + + '\n smoothingGroup: ' + rawObjectDescription.smoothingGroup + + '\n #vertices: ' + rawObjectDescription.vertices.length / 3 + + '\n #uvs: ' + rawObjectDescription.uvs.length / 2 + + '\n #normals: ' + rawObjectDescription.normals.length / 3 + ); + }; + + return MeshCreator; + })(); + + return OBJLoader2; +})(); if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} } @@ -737,7 +926,7 @@ THREE.OBJLoader2._RawObject = (function () { if ( index > 0 ) { if ( debug ) this._createReport( inputObjectCount, true ); - meshCreator.buildMesh( + meshCreator._buildMesh( this.rawObjectDescriptions, inputObjectCount, absoluteVertexCount, @@ -803,194 +992,3 @@ THREE.OBJLoader2.RawObjectDescription = (function () { return RawObjectDescription; })(); - -/** - * MeshCreator is used to transform THREE.OBJLoader2.RawObjectDescriptions to THREE.Mesh - * - * @class - */ -THREE.OBJLoader2.MeshCreator = (function () { - - function MeshCreator() { - this.sceneGraphBaseNode = null; - this.materials = null; - this.debug = false; - this.globalObjectCount = 1; - - this.validated = false; - } - - MeshCreator.prototype._setSceneGraphBaseNode = function ( sceneGraphBaseNode ) { - this.sceneGraphBaseNode = ( sceneGraphBaseNode == null ) ? ( this.sceneGraphBaseNode == null ? new THREE.Group() : this.sceneGraphBaseNode ) : sceneGraphBaseNode; - }; - - MeshCreator.prototype._setMaterials = function ( materials ) { - this.materials = ( materials == null ) ? ( this.materials == null ? { materials: [] } : this.materials ) : materials; - }; - - MeshCreator.prototype._setDebug = function ( debug ) { - this.debug = ( debug == null ) ? this.debug : debug; - }; - - MeshCreator.prototype._validate = function () { - if ( this.validated ) return; - - this._setSceneGraphBaseNode( null ); - this._setMaterials( null ); - this._setDebug( null ); - this.globalObjectCount = 1; - }; - - MeshCreator.prototype._finalize = function () { - this.sceneGraphBaseNode = null; - this.materials = null; - this.validated = false; - }; - - /** - * RawObjectDescriptions are transformed to THREE.Mesh. - * It is ensured that rawObjectDescriptions only contain objects with vertices (no need to check). - * This method shall be overridden by the web worker implementation - * @memberOf THREE.OBJLoader2.MeshCreator - * - * @param {THREE.OBJLoader2.RawObjectDescription[]} rawObjectDescriptions Array of descriptive information and data (vertices, normals, uvs) about the parsed object(s) - * @param {number} inputObjectCount Number of objects already retrieved from OBJ - * @param {number} absoluteVertexCount Sum of all vertices of all rawObjectDescriptions - * @param {number} absoluteNormalCount Sum of all normals of all rawObjectDescriptions - * @param {number} absoluteUvCount Sum of all uvs of all rawObjectDescriptions - */ - MeshCreator.prototype.buildMesh = function ( rawObjectDescriptions, inputObjectCount, absoluteVertexCount, absoluteNormalCount, absoluteUvCount ) { - - if ( this.debug ) console.log( 'MeshCreator.buildRawMeshData:\nInput object no.: ' + inputObjectCount ); - - var bufferGeometry = new THREE.BufferGeometry(); - var vertexBA = new THREE.BufferAttribute( new Float32Array( absoluteVertexCount ), 3 ); - bufferGeometry.addAttribute( 'position', vertexBA ); - - var normalBA; - if ( absoluteNormalCount > 0 ) { - - normalBA = new THREE.BufferAttribute( new Float32Array( absoluteNormalCount ), 3 ); - bufferGeometry.addAttribute( 'normal', normalBA ); - - } - var uvBA; - if ( absoluteUvCount > 0 ) { - - uvBA = new THREE.BufferAttribute( new Float32Array( absoluteUvCount ), 2 ); - bufferGeometry.addAttribute( 'uv', uvBA ); - - } - - if ( this.debug ) console.log( 'Creating Multi-Material for object no.: ' + this.globalObjectCount ); - - var rawObjectDescription; - var material; - var materialName; - var createMultiMaterial = ( rawObjectDescriptions.length > 1 ) ? true : false; - var materials = []; - var materialIndex = 0; - var materialIndexMapping = []; - var selectedMaterialIndex; - - var vertexBAOffset = 0; - var vertexGroupOffset = 0; - var vertexLength; - var normalOffset = 0; - var uvOffset = 0; - - for ( var oodIndex in rawObjectDescriptions ) { - rawObjectDescription = rawObjectDescriptions[ oodIndex ]; - - materialName = rawObjectDescription.materialName; - material = this.materials[ materialName ]; - if ( ! material ) { - - material = this.materials[ 'defaultMaterial' ]; - if ( ! material ) { - - material = new THREE.MeshStandardMaterial( { color: 0xDCF1FF} ); - material.name = 'defaultMaterial'; - this.materials[ 'defaultMaterial' ] = material; - - } - console.warn( 'object_group "' + rawObjectDescription.objectName + '_' + rawObjectDescription.groupName + '" was defined without material! Assigning "defaultMaterial".' ); - - } - // clone material in case flat shading is needed due to smoothingGroup 0 - if ( rawObjectDescription.smoothingGroup === 0 ) { - - materialName = material.name + '_flat'; - var materialClone = this.materials[ materialName ]; - if ( ! materialClone ) { - - materialClone = material.clone(); - materialClone.name = materialName; - materialClone.shading = THREE.FlatShading; - this.materials[ materialName ] = name; - - } - - } - - vertexLength = rawObjectDescription.vertices.length; - if ( createMultiMaterial ) { - - // re-use material if already used before. Reduces materials array size and eliminates duplicates - selectedMaterialIndex = materialIndexMapping[ materialName ]; - if ( ! selectedMaterialIndex ) { - - selectedMaterialIndex = materialIndex; - materialIndexMapping[ materialName ] = materialIndex; - materials.push( material ); - materialIndex++; - - } - - bufferGeometry.addGroup( vertexGroupOffset, vertexLength / 3, selectedMaterialIndex ); - vertexGroupOffset += vertexLength / 3; - } - - vertexBA.set( rawObjectDescription.vertices, vertexBAOffset ); - vertexBAOffset += vertexLength; - - if ( normalBA ) { - - normalBA.set( rawObjectDescription.normals, normalOffset ); - normalOffset += rawObjectDescription.normals.length; - - } - if ( uvBA ) { - - uvBA.set( rawObjectDescription.uvs, uvOffset ); - uvOffset += rawObjectDescription.uvs.length; - - } - if ( this.debug ) this._printReport( rawObjectDescription, selectedMaterialIndex ); - - } - if ( ! normalBA ) bufferGeometry.computeVertexNormals(); - - if ( createMultiMaterial ) material = new THREE.MultiMaterial( materials ); - var mesh = new THREE.Mesh( bufferGeometry, material ); - this.sceneGraphBaseNode.add( mesh ); - - this.globalObjectCount++; - }; - - MeshCreator.prototype._printReport = function ( rawObjectDescription, selectedMaterialIndex ) { - console.log( - ' Output Object no.: ' + this.globalObjectCount + - '\n objectName: ' + rawObjectDescription.objectName + - '\n groupName: ' + rawObjectDescription.groupName + - '\n materialName: ' + rawObjectDescription.materialName + - '\n materialIndex: ' + selectedMaterialIndex + - '\n smoothingGroup: ' + rawObjectDescription.smoothingGroup + - '\n #vertices: ' + rawObjectDescription.vertices.length / 3 + - '\n #uvs: ' + rawObjectDescription.uvs.length / 2 + - '\n #normals: ' + rawObjectDescription.normals.length / 3 - ); - }; - - return MeshCreator; -})(); \ No newline at end of file diff --git a/examples/js/loaders/WWOBJLoader2.js b/examples/js/loaders/WWOBJLoader2.js index c139c2a7cc9826..9e3dae46ec9c0b 100644 --- a/examples/js/loaders/WWOBJLoader2.js +++ b/examples/js/loaders/WWOBJLoader2.js @@ -6,7 +6,7 @@ 'use strict'; if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} } -THREE.OBJLoader2.version = '1.0.1'; +THREE.OBJLoader2.version = '1.0.2'; /** * OBJ data will be loaded by dynamically created web worker. @@ -640,7 +640,7 @@ THREE.OBJLoader2.WWOBJLoader2 = (function () { * @param absoluteNormalCount * @param absoluteUvCount */ - MeshCreator.prototype.buildMesh = function ( rawObjectDescriptions, inputObjectCount, absoluteVertexCount, absoluteNormalCount, absoluteUvCount ) { + MeshCreator.prototype._buildMesh = function ( rawObjectDescriptions, inputObjectCount, absoluteVertexCount, absoluteNormalCount, absoluteUvCount ) { if ( this.debug ) console.log( 'OBJLoader.buildMesh:\nInput object no.: ' + inputObjectCount ); var vertexFa = new Float32Array( absoluteVertexCount );