diff --git a/source/import/importer.js b/source/import/importer.js index 0f65848..50c5d3d 100644 --- a/source/import/importer.js +++ b/source/import/importer.js @@ -222,6 +222,46 @@ OV.ImportResult = class } }; +OV.ImporterBuffers = class +{ + constructor (getBufferCallback) + { + this.getBufferCallback = getBufferCallback; + this.fileBuffers = {}; + this.textureBuffers = {}; + } + + GetFileBuffer (filePath) + { + let fileName = OV.GetFileName (filePath); + let buffer = this.fileBuffers[fileName]; + if (buffer === undefined) { + buffer = this.getBufferCallback (fileName); + this.fileBuffers[fileName] = buffer; + } + return buffer; + } + + GetTextureBuffer (filePath) + { + let fileName = OV.GetFileName (filePath); + let buffer = this.textureBuffers[fileName]; + if (buffer === undefined) { + let texBuffer = this.getBufferCallback (fileName); + if (texBuffer !== null) { + buffer = { + url : OV.CreateObjectUrl (texBuffer), + buffer : texBuffer + }; + } else { + buffer = null; + } + this.textureBuffers[fileName] = buffer; + } + return buffer; + } +}; + OV.Importer = class { constructor () @@ -262,24 +302,31 @@ OV.Importer = class let obj = this; let importer = mainFile.importer; + let buffers = new OV.ImporterBuffers (function (fileName) { + let fileBuffer = null; + let file = obj.fileList.FindFileByPath (fileName); + if (file === null || file.content === null) { + result.missingFiles.push (fileName); + obj.missingFiles.push (fileName); + fileBuffer = null; + } else { + result.usedFiles.push (fileName); + fileBuffer = file.content; + } + return fileBuffer; + }); + importer.Import (mainFile.file.content, mainFile.file.extension, { getDefaultMaterial : function () { let material = new OV.Material (); material.diffuse = new OV.Color (200, 200, 200); return material; }, - getFileBuffer : function (fileName) { - let fileBuffer = null; - let file = obj.fileList.FindFileByPath (fileName); - if (file === null || file.content === null) { - result.missingFiles.push (fileName); - obj.missingFiles.push (fileName); - fileBuffer = null; - } else { - result.usedFiles.push (fileName); - fileBuffer = file.content; - } - return fileBuffer; + getFileBuffer : function (filePath) { + return buffers.GetFileBuffer (filePath); + }, + getTextureBuffer : function (filePath) { + return buffers.GetTextureBuffer (filePath); } }); diff --git a/source/import/importer3ds.js b/source/import/importer3ds.js index 5973d3d..2cb9e92 100644 --- a/source/import/importer3ds.js +++ b/source/import/importer3ds.js @@ -177,7 +177,7 @@ OV.Importer3ds = class extends OV.ImporterBase this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_NAME) { let textureName = obj.ReadName (reader); - let textureBuffer = obj.GetTextureBuffer (textureName); + let textureBuffer = obj.callbacks.getTextureBuffer (textureName); texture.name = textureName; if (textureBuffer !== null) { texture.url = textureBuffer.url; diff --git a/source/import/importerbase.js b/source/import/importerbase.js index 42fc726..ae9e584 100644 --- a/source/import/importerbase.js +++ b/source/import/importerbase.js @@ -1,61 +1,18 @@ -OV.ImporterBuffers = class -{ - constructor (getBufferCallback) - { - this.getBufferCallback = getBufferCallback; - this.fileBuffers = {}; - this.textureBuffers = {}; - } - - GetFileBuffer (filePath) - { - let fileName = OV.GetFileName (filePath); - let buffer = this.fileBuffers[fileName]; - if (buffer === undefined) { - buffer = this.getBufferCallback (fileName); - this.fileBuffers[fileName] = buffer; - } - return buffer; - } - - GetTextureBuffer (filePath) - { - let fileName = OV.GetFileName (filePath); - let buffer = this.textureBuffers[fileName]; - if (buffer === undefined) { - let texBuffer = this.getBufferCallback (fileName); - if (texBuffer !== null) { - buffer = { - url : OV.CreateObjectUrl (texBuffer), - buffer : texBuffer - }; - } else { - buffer = null; - } - this.textureBuffers[fileName] = buffer; - } - return buffer; - } -}; - OV.ImporterBase = class { constructor () { this.extension = null; this.callbacks = null; - this.buffers = null; this.model = null; this.error = null; this.message = null; - this.buffers = null; } Import (content, extension, callbacks) { this.extension = extension; this.callbacks = callbacks; - this.buffers = new OV.ImporterBuffers (this.callbacks.getFileBuffer); this.model = new OV.Model (); this.error = false; this.message = null; @@ -74,16 +31,6 @@ OV.ImporterBase = class OV.FinalizeModel (this.model, this.callbacks.getDefaultMaterial); } - GetFileBuffer (filePath) - { - return this.buffers.GetFileBuffer (filePath); - } - - GetTextureBuffer (filePath) - { - return this.buffers.GetTextureBuffer (filePath); - } - ResetState () { diff --git a/source/import/importergltf.js b/source/import/importergltf.js index 6095dee..57cab21 100644 --- a/source/import/importergltf.js +++ b/source/import/importergltf.js @@ -405,7 +405,7 @@ OV.ImporterGltf = class extends OV.ImporterBase if (base64Buffer !== null) { buffer = base64Buffer.buffer; } else { - let fileBuffer = this.GetFileBuffer (gltfBuffer.uri); + let fileBuffer = this.callbacks.getFileBuffer (gltfBuffer.uri); if (fileBuffer !== null) { buffer = fileBuffer; } @@ -636,7 +636,7 @@ OV.ImporterGltf = class extends OV.ImporterBase textureParams.url = OV.CreateObjectUrlWithMimeType (base64Buffer.buffer, base64Buffer.mimeType); textureParams.buffer = base64Buffer.buffer; } else { - let textureBuffer = this.GetTextureBuffer (gltfImage.uri); + let textureBuffer = this.callbacks.getTextureBuffer (gltfImage.uri); textureParams.name = gltfImage.uri; if (textureBuffer !== null) { textureParams.url = textureBuffer.url; diff --git a/source/import/importerobj.js b/source/import/importerobj.js index e081c20..b2e011f 100644 --- a/source/import/importerobj.js +++ b/source/import/importerobj.js @@ -166,11 +166,11 @@ OV.ImporterObj = class extends OV.ImporterBase ); } - function CreateTexture (importer, keyword, line) + function CreateTexture (keyword, line, callbacks) { let texture = new OV.TextureMap (); let textureName = OV.NameFromLine (line, keyword.length, '#'); - let textureBuffer = importer.GetTextureBuffer (textureName); + let textureBuffer = callbacks.getTextureBuffer (textureName); texture.name = textureName; if (textureBuffer !== null) { texture.url = textureBuffer.url; @@ -208,7 +208,7 @@ OV.ImporterObj = class extends OV.ImporterBase return true; } let fileName = OV.NameFromLine (line, keyword.length, '#'); - let fileBuffer = this.GetFileBuffer (fileName); + let fileBuffer = this.callbacks.getFileBuffer (fileName); if (fileBuffer !== null) { OV.ReadLines (fileBuffer, function (line) { if (!obj.IsError ()) { @@ -221,19 +221,19 @@ OV.ImporterObj = class extends OV.ImporterBase if (this.currentMaterial === null || parameters.length === 0) { return true; } - this.currentMaterial.diffuseMap = CreateTexture (this, keyword, line); + this.currentMaterial.diffuseMap = CreateTexture (keyword, line, this.callbacks); return true; } else if (keyword === 'map_ks') { if (this.currentMaterial === null || parameters.length === 0) { return true; } - this.currentMaterial.specularMap = CreateTexture (this, keyword, line); + this.currentMaterial.specularMap = CreateTexture (keyword, line, this.callbacks); return true; } else if (keyword === 'map_bump' || keyword === 'bump') { if (this.currentMaterial === null || parameters.length === 0) { return true; } - this.currentMaterial.bumpMap = CreateTexture (this, keyword, line); + this.currentMaterial.bumpMap = CreateTexture (keyword, line, this.callbacks); return true; } else if (keyword === 'ka') { if (this.currentMaterial === null || parameters.length < 3) { diff --git a/test/tests/exporter_test.js b/test/tests/exporter_test.js index 1d993f7..b3d248d 100644 --- a/test/tests/exporter_test.js +++ b/test/tests/exporter_test.js @@ -319,6 +319,9 @@ describe ('Exporter', function () { return binFile.GetContent (); } return null; + }, + getTextureBuffer (filePath) { + return null; } }); @@ -350,6 +353,9 @@ describe ('Exporter', function () { }, getFileBuffer (filePath) { return null; + }, + getTextureBuffer (filePath) { + return null; } }); diff --git a/test/utils/testfiles.js b/test/utils/testfiles.js index 36272c7..0201afd 100644 --- a/test/utils/testfiles.js +++ b/test/utils/testfiles.js @@ -45,8 +45,14 @@ module.exports = ImportFile : function (importer, format, folder, fileName) { - function GetFileBuffer (filePath, importer) - { + var content = null; + if (format == OV.FileFormat.Text) { + content = testUtils.GetTextFileContent (folder, fileName); + } else if (format == OV.FileFormat.Binary) { + content = testUtils.GetArrayBufferFileContent (folder, fileName); + } + var extension = OV.GetFileExtension (fileName); + let buffers = new OV.ImporterBuffers (function (filePath) { let extension = OV.GetFileExtension (filePath); let knownFormats = importer.GetKnownFileFormats (); let format = OV.FileFormat.Binary; @@ -60,23 +66,17 @@ module.exports = fileContent = testUtils.GetArrayBufferFileContent (folder, filePath); } return fileContent; - } - - var obj = this; - var content = null; - if (format == OV.FileFormat.Text) { - content = testUtils.GetTextFileContent (folder, fileName); - } else if (format == OV.FileFormat.Binary) { - content = testUtils.GetArrayBufferFileContent (folder, fileName); - } - var extension = OV.GetFileExtension (fileName); + }); importer.Import (content, extension, { getDefaultMaterial : function () { var material = new OV.Material (); return material; }, getFileBuffer : function (filePath) { - return GetFileBuffer (filePath, importer); + return buffers.GetFileBuffer (filePath); + }, + getTextureBuffer : function (filePath) { + return buffers.GetTextureBuffer (filePath); } }); let model = importer.GetModel ();