Handle buffer cache by importers instead of the main importer object.
This commit is contained in:
parent
becbe31a9e
commit
0c394f9aa8
4
source/external/three.model.loader.js
vendored
4
source/external/three.model.loader.js
vendored
@ -53,8 +53,8 @@ OV.ThreeModelLoader = class
|
||||
obj.OnModelImported (importResult);
|
||||
ready ();
|
||||
},
|
||||
error : function (importerError) {
|
||||
obj.callbacks.onLoadError (importerError);
|
||||
error : function (importError) {
|
||||
obj.callbacks.onLoadError (importError);
|
||||
obj.inProgress = false;
|
||||
ready ();
|
||||
}
|
||||
|
||||
@ -179,14 +179,14 @@ OV.FileList = class
|
||||
}
|
||||
};
|
||||
|
||||
OV.ImporterErrorCode =
|
||||
OV.ImportErrorCode =
|
||||
{
|
||||
NoImportableFile : 1,
|
||||
ImportFailed : 2,
|
||||
UnknownError : 3
|
||||
};
|
||||
|
||||
OV.ImporterError = class
|
||||
OV.ImportError = class
|
||||
{
|
||||
constructor (code, message)
|
||||
{
|
||||
@ -207,58 +207,6 @@ OV.ImportResult = class
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
OV.FileBufferCache = class
|
||||
{
|
||||
constructor (fileList, missingFiles, importResult)
|
||||
{
|
||||
this.fileList = fileList;
|
||||
this.missingFiles = missingFiles;
|
||||
this.importResult = importResult;
|
||||
this.fileNameToBuffer = {};
|
||||
this.textureNameToBuffer = {};
|
||||
}
|
||||
|
||||
GetFileBuffer (filePath)
|
||||
{
|
||||
return this.GetCommonBuffer (filePath, this.fileNameToBuffer, false);
|
||||
}
|
||||
|
||||
GetTextureBuffer (filePath)
|
||||
{
|
||||
return this.GetCommonBuffer (filePath, this.textureNameToBuffer, true);
|
||||
}
|
||||
|
||||
GetCommonBuffer (filePath, nameToBuffer, withUrl)
|
||||
{
|
||||
let fileName = OV.GetFileName (filePath);
|
||||
let fileBuffer = nameToBuffer[fileName];
|
||||
if (fileBuffer === undefined) {
|
||||
let file = this.fileList.FindFileByPath (filePath);
|
||||
if (file === null || file.content === null) {
|
||||
this.importResult.missingFiles.push (fileName);
|
||||
this.missingFiles.push (fileName);
|
||||
fileBuffer = null;
|
||||
} else {
|
||||
this.importResult.usedFiles.push (fileName);
|
||||
if (withUrl) {
|
||||
fileBuffer = {
|
||||
url : OV.CreateObjectUrl (file.content),
|
||||
buffer : file.content
|
||||
};
|
||||
} else {
|
||||
fileBuffer = {
|
||||
url : null,
|
||||
buffer : file.content
|
||||
};
|
||||
}
|
||||
}
|
||||
nameToBuffer[fileName] = fileBuffer;
|
||||
}
|
||||
return fileBuffer;
|
||||
}
|
||||
};
|
||||
|
||||
OV.Importer = class
|
||||
{
|
||||
constructor ()
|
||||
@ -289,7 +237,7 @@ OV.Importer = class
|
||||
{
|
||||
let mainFile = this.fileList.GetMainFile ();
|
||||
if (mainFile === null || mainFile.file === null || mainFile.file.content === null) {
|
||||
callbacks.error (new OV.ImporterError (OV.ImporterErrorCode.NoImportableFile, null));
|
||||
callbacks.error (new OV.ImportError (OV.ImportErrorCode.NoImportableFile, null));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -297,25 +245,32 @@ OV.Importer = class
|
||||
result.mainFile = mainFile.file.name;
|
||||
result.usedFiles.push (mainFile.file.name);
|
||||
|
||||
let obj = this;
|
||||
let importer = mainFile.importer;
|
||||
let fileBufferCache = new OV.FileBufferCache (this.fileList, this.missingFiles, result);
|
||||
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 (filePath) {
|
||||
return fileBufferCache.GetFileBuffer (filePath);
|
||||
},
|
||||
getTextureBuffer : function (filePath) {
|
||||
return fileBufferCache.GetTextureBuffer (filePath);
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
if (importer.IsError ()) {
|
||||
let message = importer.GetMessage ();
|
||||
callbacks.error (new OV.ImporterError (OV.ImporterErrorCode.ImportFailed, message));
|
||||
callbacks.error (new OV.ImportError (OV.ImportErrorCode.ImportFailed, message));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -325,6 +280,21 @@ OV.Importer = class
|
||||
callbacks.success (result);
|
||||
}
|
||||
|
||||
RevokeReferences (model)
|
||||
{
|
||||
if (model === null) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < model.MaterialCount (); i++) {
|
||||
let material = model.GetMaterial (i);
|
||||
material.EnumerateTextureMaps (function (texture) {
|
||||
if (texture.url !== null) {
|
||||
OV.RevokeObjectUrl (texture.url);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
LoadFiles (fileList, fileSource, onReady)
|
||||
{
|
||||
let newFileList = new OV.FileList (this.importers);
|
||||
@ -370,19 +340,4 @@ OV.Importer = class
|
||||
{
|
||||
return this.fileList.IsOnlySource (source);
|
||||
}
|
||||
|
||||
DisposeModel (model)
|
||||
{
|
||||
if (model === null) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < model.MaterialCount (); i++) {
|
||||
let material = model.GetMaterial (i);
|
||||
material.EnumerateTextureMaps (function (texture) {
|
||||
if (texture.url !== null) {
|
||||
OV.RevokeObjectUrl (texture.url);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -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.callbacks.getTextureBuffer (textureName);
|
||||
let textureBuffer = obj.GetTextureBuffer (textureName);
|
||||
texture.name = textureName;
|
||||
if (textureBuffer !== null) {
|
||||
texture.url = textureBuffer.url;
|
||||
|
||||
@ -1,18 +1,61 @@
|
||||
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;
|
||||
@ -31,6 +74,16 @@ 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 ()
|
||||
{
|
||||
|
||||
|
||||
@ -405,9 +405,9 @@ OV.ImporterGltf = class extends OV.ImporterBase
|
||||
if (base64Buffer !== null) {
|
||||
buffer = base64Buffer.buffer;
|
||||
} else {
|
||||
let fileBuffer = this.callbacks.getFileBuffer (gltfBuffer.uri);
|
||||
let fileBuffer = this.GetFileBuffer (gltfBuffer.uri);
|
||||
if (fileBuffer !== null) {
|
||||
buffer = fileBuffer.buffer;
|
||||
buffer = fileBuffer;
|
||||
}
|
||||
}
|
||||
if (buffer === null) {
|
||||
@ -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.callbacks.getTextureBuffer (gltfImage.uri);
|
||||
let textureBuffer = this.GetTextureBuffer (gltfImage.uri);
|
||||
textureParams.name = gltfImage.uri;
|
||||
if (textureBuffer !== null) {
|
||||
textureParams.url = textureBuffer.url;
|
||||
|
||||
@ -166,11 +166,11 @@ OV.ImporterObj = class extends OV.ImporterBase
|
||||
);
|
||||
}
|
||||
|
||||
function CreateTexture (keyword, line, callbacks)
|
||||
function CreateTexture (importer, keyword, line)
|
||||
{
|
||||
let texture = new OV.TextureMap ();
|
||||
let textureName = OV.NameFromLine (line, keyword.length, '#');
|
||||
let textureBuffer = callbacks.getTextureBuffer (textureName);
|
||||
let textureBuffer = importer.GetTextureBuffer (textureName);
|
||||
texture.name = textureName;
|
||||
if (textureBuffer !== null) {
|
||||
texture.url = textureBuffer.url;
|
||||
@ -208,9 +208,9 @@ OV.ImporterObj = class extends OV.ImporterBase
|
||||
return true;
|
||||
}
|
||||
let fileName = OV.NameFromLine (line, keyword.length, '#');
|
||||
let fileBuffer = this.callbacks.getFileBuffer (fileName);
|
||||
if (fileBuffer !== null && fileBuffer.buffer !== null) {
|
||||
OV.ReadLines (fileBuffer.buffer, function (line) {
|
||||
let fileBuffer = this.GetFileBuffer (fileName);
|
||||
if (fileBuffer !== null) {
|
||||
OV.ReadLines (fileBuffer, function (line) {
|
||||
if (!obj.IsError ()) {
|
||||
obj.ProcessLine (line);
|
||||
}
|
||||
@ -221,19 +221,19 @@ OV.ImporterObj = class extends OV.ImporterBase
|
||||
if (this.currentMaterial === null || parameters.length === 0) {
|
||||
return true;
|
||||
}
|
||||
this.currentMaterial.diffuseMap = CreateTexture (keyword, line, this.callbacks);
|
||||
this.currentMaterial.diffuseMap = CreateTexture (this, keyword, line);
|
||||
return true;
|
||||
} else if (keyword === 'map_ks') {
|
||||
if (this.currentMaterial === null || parameters.length === 0) {
|
||||
return true;
|
||||
}
|
||||
this.currentMaterial.specularMap = CreateTexture (keyword, line, this.callbacks);
|
||||
this.currentMaterial.specularMap = CreateTexture (this, keyword, line);
|
||||
return true;
|
||||
} else if (keyword === 'map_bump' || keyword === 'bump') {
|
||||
if (this.currentMaterial === null || parameters.length === 0) {
|
||||
return true;
|
||||
}
|
||||
this.currentMaterial.bumpMap = CreateTexture (keyword, line, this.callbacks);
|
||||
this.currentMaterial.bumpMap = CreateTexture (this, keyword, line);
|
||||
return true;
|
||||
} else if (keyword === 'ka') {
|
||||
if (this.currentMaterial === null || parameters.length < 3) {
|
||||
|
||||
@ -42,7 +42,7 @@ OV.Init3DViewerElements = function ()
|
||||
onTextureLoaded : function () {
|
||||
viewer.Render ();
|
||||
},
|
||||
onLoadError : function (importerError) {
|
||||
onLoadError : function (importError) {
|
||||
progressDiv.innerHTML = 'Unknown error.';
|
||||
},
|
||||
});
|
||||
|
||||
15
test/test.js
15
test/test.js
@ -1,20 +1,7 @@
|
||||
var fs = require ('fs');
|
||||
process.chdir (__dirname);
|
||||
|
||||
global.atob = function (base64String) {
|
||||
return Buffer.from (base64String, 'base64').toString('binary');
|
||||
};
|
||||
|
||||
global.Blob = function () {
|
||||
|
||||
};
|
||||
|
||||
global.URL = {
|
||||
createObjectURL : function () {
|
||||
return 'ObjectUrl';
|
||||
}
|
||||
}
|
||||
|
||||
require ('./utils/globals.js');
|
||||
require ('./utils/importall.js');
|
||||
var testDirName = './tests/';
|
||||
var files = fs.readdirSync (testDirName, { withFileTypes: true });
|
||||
|
||||
1
test/testfiles/3ds/wrong.3ds
Normal file
1
test/testfiles/3ds/wrong.3ds
Normal file
@ -0,0 +1 @@
|
||||
invalid 3ds file
|
||||
11
test/testfiles/obj/two_materials_same_texture.mtl
Normal file
11
test/testfiles/obj/two_materials_same_texture.mtl
Normal file
@ -0,0 +1,11 @@
|
||||
newmtl Material1
|
||||
Ka 0.000000 0.000000 0.000000
|
||||
Kd 1.000000 1.000000 1.000000
|
||||
Ks 0.000000 0.000000 0.000000
|
||||
map_Kd ./texture.png
|
||||
|
||||
newmtl Material2
|
||||
Ka 0.000000 0.000000 0.000000
|
||||
Kd 1.000000 1.000000 1.000000
|
||||
Ks 0.000000 0.000000 0.000000
|
||||
map_Kd texture.png
|
||||
17
test/testfiles/obj/two_materials_same_texture.obj
Normal file
17
test/testfiles/obj/two_materials_same_texture.obj
Normal file
@ -0,0 +1,17 @@
|
||||
mtllib two_materials_same_texture.mtl
|
||||
|
||||
v 0.0 0.0 0.0
|
||||
v 1.0 0.0 0.0
|
||||
v 1.0 1.0 0.0
|
||||
v 0.0 1.0 0.0
|
||||
vn 0.0 0.0 1.0
|
||||
vt 0.0 0.0
|
||||
vt 1.0 0.0
|
||||
vt 1.0 1.0
|
||||
vt 0.0 1.0
|
||||
|
||||
o Mesh
|
||||
usemtl Material1
|
||||
f 1/1/1 2/2/1 3/3/1
|
||||
usemtl Material2
|
||||
f 1/1/1 3/3/1 4/4/1
|
||||
1
test/testfiles/wrong.ext
Normal file
1
test/testfiles/wrong.ext
Normal file
@ -0,0 +1 @@
|
||||
file with not importable extension
|
||||
@ -315,15 +315,9 @@ describe ('Exporter', function () {
|
||||
return new OV.Material ();
|
||||
},
|
||||
getFileBuffer (filePath) {
|
||||
if (filePath == 'model.bin') {
|
||||
return {
|
||||
url : null,
|
||||
buffer : binFile.GetContent ()
|
||||
};
|
||||
}
|
||||
return null;
|
||||
},
|
||||
getTextureBuffer (filePath) {
|
||||
if (filePath == 'model.bin') {
|
||||
return binFile.GetContent ();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
182
test/tests/importer_test.js
Normal file
182
test/tests/importer_test.js
Normal file
@ -0,0 +1,182 @@
|
||||
var assert = require ('assert');
|
||||
var path = require ('path');
|
||||
|
||||
function ImportFilesWithImporter (importer, files, callbacks)
|
||||
{
|
||||
importer.LoadFilesFromFileObjects (files, function () {
|
||||
importer.Import ({
|
||||
success : function (importResult) {
|
||||
callbacks.success (importer, importResult);
|
||||
},
|
||||
error : function (importError) {
|
||||
callbacks.error (importer, importError);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function ImportFiles (files, callbacks)
|
||||
{
|
||||
let importer = new OV.Importer ();
|
||||
ImportFilesWithImporter (importer, files, callbacks);
|
||||
}
|
||||
|
||||
describe ('Importer Test', function () {
|
||||
it ('Empty File List', function () {
|
||||
let files = [];
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert.fail ();
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.strictEqual (importError.code, OV.ImportErrorCode.NoImportableFile);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it ('Not existing file', function () {
|
||||
let files = [
|
||||
new FileObject ('obj', 'missing.obj')
|
||||
];
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert.fail ();
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.strictEqual (importError.code, OV.ImportErrorCode.NoImportableFile);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it ('Not imprtable file', function () {
|
||||
let files = [
|
||||
new FileObject ('', 'wrong.ext')
|
||||
];
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert.fail ();
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.strictEqual (importError.code, OV.ImportErrorCode.NoImportableFile);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it ('Wrong file', function () {
|
||||
let files = [
|
||||
new FileObject ('3ds', 'wrong.3ds')
|
||||
];
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert.fail ();
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.strictEqual (importError.code, OV.ImportErrorCode.ImportFailed);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it ('Single file', function () {
|
||||
let files = [
|
||||
new FileObject ('obj', 'single_triangle.obj')
|
||||
];
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['single_triangle.obj']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, []);
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it ('Missing files', function () {
|
||||
let files = [];
|
||||
files.push (new FileObject ('obj', 'cube_with_materials.obj'));
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['cube_with_materials.obj']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, ['cube_with_materials.mtl']);
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
files.push (new FileObject ('obj', 'cube_with_materials.mtl'));
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['cube_with_materials.obj', 'cube_with_materials.mtl']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, ['cube_texture.png']);
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
files.push (new FileObject ('obj', 'cube_texture.png'));
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['cube_with_materials.obj', 'cube_with_materials.mtl', 'cube_texture.png']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, []);
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it ('Missing texture multiple times', function () {
|
||||
let files = [
|
||||
new FileObject ('obj', 'two_materials_same_texture.obj'),
|
||||
new FileObject ('obj', 'two_materials_same_texture.mtl'),
|
||||
];
|
||||
ImportFiles (files, {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['two_materials_same_texture.obj', 'two_materials_same_texture.mtl']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, ['texture.png']);
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it ('Append Missing files', function () {
|
||||
let theImporter = new OV.Importer ();
|
||||
ImportFilesWithImporter (theImporter, [new FileObject ('obj', 'cube_with_materials.obj')], {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['cube_with_materials.obj']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, ['cube_with_materials.mtl']);
|
||||
ImportFilesWithImporter (theImporter, [new FileObject ('obj', 'cube_with_materials.mtl')], {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['cube_with_materials.obj', 'cube_with_materials.mtl']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, ['cube_texture.png']);
|
||||
ImportFilesWithImporter (theImporter, [new FileObject ('obj', 'cube_texture.png')], {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['cube_with_materials.obj', 'cube_with_materials.mtl', 'cube_texture.png']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, []);
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -402,7 +402,40 @@ describe ('Obj Importer', function() {
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it ('two_materials_same_texture.obj', function () {
|
||||
var model = testFiles.ImportObjFile ('two_materials_same_texture.obj');
|
||||
assert.strictEqual (model.GetMaterial (0).diffuseMap.url, model.GetMaterial (1).diffuseMap.url);
|
||||
assert.strictEqual (model.GetMaterial (0).diffuseMap.buffer.byteLength, model.GetMaterial (1).diffuseMap.buffer.byteLength);
|
||||
assert (OV.CheckModel (model));
|
||||
assert.deepStrictEqual (testUtils.ModelToObject (model), {
|
||||
name : '',
|
||||
materials : [
|
||||
{ name : 'Material1' },
|
||||
{ name : 'Material2' }
|
||||
],
|
||||
meshes : [
|
||||
{
|
||||
name : 'Mesh',
|
||||
triangles : [
|
||||
{
|
||||
vertices : [0, 0, 0, 1, 0, 0, 1, 1, 0],
|
||||
normals : [0, 0, 1, 0, 0, 1, 0, 0, 1],
|
||||
uvs : [0, 0, 1, 0, 1, 1],
|
||||
mat : 0
|
||||
},
|
||||
{
|
||||
vertices : [0, 0, 0, 1, 1, 0, 0, 1, 0],
|
||||
normals : [0, 0, 1, 0, 0, 1, 0, 0, 1],
|
||||
uvs : [0, 0, 1, 1, 0, 1],
|
||||
mat : 1
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
it ('cube_with_materials.obj', function () {
|
||||
var model = testFiles.ImportObjFile ('cube_with_materials.obj');
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
var assert = require ('assert');
|
||||
var testFiles = require ('../utils/testfiles.js');
|
||||
var testUtils = require ('../utils/testutils.js');
|
||||
|
||||
describe ('IO Test', function () {
|
||||
it ('Binary Reader', function () {
|
||||
let buffer = testFiles.GetArrayBufferFileContent ('bin', 'binary_content.bin');
|
||||
let buffer = testUtils.GetArrayBufferFileContent ('bin', 'binary_content.bin');
|
||||
let reader = new OV.BinaryReader (buffer, true);
|
||||
|
||||
assert.strictEqual (reader.GetByteLength (), 166);
|
||||
|
||||
60
test/utils/globals.js
Normal file
60
test/utils/globals.js
Normal file
@ -0,0 +1,60 @@
|
||||
var path = require ('path');
|
||||
var testUtils = require ('./testutils.js');
|
||||
|
||||
global.atob = function (base64String) {
|
||||
return Buffer.from (base64String, 'base64').toString('binary');
|
||||
};
|
||||
|
||||
global.Blob = function () {
|
||||
|
||||
};
|
||||
|
||||
var objectUrlCounter = 0;
|
||||
global.URL = {
|
||||
createObjectURL : function () {
|
||||
objectUrlCounter += 1;
|
||||
return 'ObjectUrl:' + objectUrlCounter.toString ();
|
||||
}
|
||||
};
|
||||
|
||||
global.FileObject = function (folderName, fileName)
|
||||
{
|
||||
this.folderName = folderName;
|
||||
this.fileName = fileName;
|
||||
this.name = path.join (folderName, fileName);
|
||||
};
|
||||
|
||||
global.FileReader = class
|
||||
{
|
||||
static DONE = 2;
|
||||
|
||||
readAsText (fileObject)
|
||||
{
|
||||
let content = testUtils.GetTextFileContent (fileObject.folderName, fileObject.fileName);
|
||||
if (content !== null) {
|
||||
this.onloadend ({
|
||||
target : {
|
||||
readyState : FileReader.DONE,
|
||||
result : content
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.onerror ();
|
||||
}
|
||||
}
|
||||
|
||||
readAsArrayBuffer (fileObject)
|
||||
{
|
||||
let content = testUtils.GetArrayBufferFileContent (fileObject.folderName, fileObject.fileName);
|
||||
if (content !== null) {
|
||||
this.onloadend ({
|
||||
target : {
|
||||
readyState : FileReader.DONE,
|
||||
result : content
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.onerror ();
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -1,28 +1,7 @@
|
||||
var path = require ('path');
|
||||
var fs = require ('fs');
|
||||
var testUtils = require ('./testutils.js')
|
||||
var testUtils = require ('./testutils.js');
|
||||
|
||||
module.exports =
|
||||
{
|
||||
GetTextFileContent : function (folder, fileName)
|
||||
{
|
||||
var testFilePath = path.join ('testfiles', folder, fileName);
|
||||
return fs.readFileSync (testFilePath).toString ();
|
||||
},
|
||||
|
||||
GetArrayBufferFileContent : function (folder, fileName)
|
||||
{
|
||||
var testFilePath = path.join ('testfiles', folder, fileName);
|
||||
var buffer = fs.readFileSync (testFilePath);
|
||||
var arrayBuffer = new ArrayBuffer (buffer.length);
|
||||
var uint8Array = new Uint8Array (arrayBuffer);
|
||||
var i;
|
||||
for (i = 0; i < buffer.length; ++i) {
|
||||
uint8Array[i] = buffer[i];
|
||||
}
|
||||
return arrayBuffer
|
||||
},
|
||||
|
||||
ImportObjFile : function (fileName)
|
||||
{
|
||||
var importer = new OV.ImporterObj ();
|
||||
@ -66,7 +45,7 @@ module.exports =
|
||||
|
||||
ImportFile : function (importer, format, folder, fileName)
|
||||
{
|
||||
function GetFileBuffer (filePath, importer, obj)
|
||||
function GetFileBuffer (filePath, importer)
|
||||
{
|
||||
let extension = OV.GetFileExtension (filePath);
|
||||
let knownFormats = importer.GetKnownFileFormats ();
|
||||
@ -76,22 +55,19 @@ module.exports =
|
||||
}
|
||||
let fileContent = null;
|
||||
if (format == OV.FileFormat.Text) {
|
||||
fileContent = obj.GetTextFileContent (folder, filePath);
|
||||
fileContent = testUtils.GetTextFileContent (folder, filePath);
|
||||
} else if (format == OV.FileFormat.Binary) {
|
||||
fileContent = obj.GetArrayBufferFileContent (folder, filePath);
|
||||
fileContent = testUtils.GetArrayBufferFileContent (folder, filePath);
|
||||
}
|
||||
return {
|
||||
url : null,
|
||||
buffer : fileContent
|
||||
};
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
var obj = this;
|
||||
var content = null;
|
||||
if (format == OV.FileFormat.Text) {
|
||||
content = this.GetTextFileContent (folder, fileName);
|
||||
content = testUtils.GetTextFileContent (folder, fileName);
|
||||
} else if (format == OV.FileFormat.Binary) {
|
||||
content = this.GetArrayBufferFileContent (folder, fileName);
|
||||
content = testUtils.GetArrayBufferFileContent (folder, fileName);
|
||||
}
|
||||
var extension = OV.GetFileExtension (fileName);
|
||||
importer.Import (content, extension, {
|
||||
@ -100,10 +76,7 @@ module.exports =
|
||||
return material;
|
||||
},
|
||||
getFileBuffer : function (filePath) {
|
||||
return GetFileBuffer (filePath, importer, obj);
|
||||
},
|
||||
getTextureBuffer : function (filePath) {
|
||||
return GetFileBuffer (filePath, importer, obj);
|
||||
return GetFileBuffer (filePath, importer);
|
||||
}
|
||||
});
|
||||
let model = importer.GetModel ();
|
||||
|
||||
@ -1,5 +1,30 @@
|
||||
var fs = require ('fs');
|
||||
var path = require ('path');
|
||||
|
||||
module.exports =
|
||||
{
|
||||
GetTextFileContent : function (folder, fileName)
|
||||
{
|
||||
var testFilePath = path.join ('testfiles', folder, fileName);
|
||||
if (!fs.existsSync (testFilePath)) {
|
||||
return null;
|
||||
}
|
||||
return fs.readFileSync (testFilePath).toString ();
|
||||
},
|
||||
|
||||
GetArrayBufferFileContent : function (folder, fileName)
|
||||
{
|
||||
var testFilePath = path.join ('testfiles', folder, fileName);
|
||||
var buffer = fs.readFileSync (testFilePath);
|
||||
var arrayBuffer = new ArrayBuffer (buffer.length);
|
||||
var uint8Array = new Uint8Array (arrayBuffer);
|
||||
var i;
|
||||
for (i = 0; i < buffer.length; ++i) {
|
||||
uint8Array[i] = buffer[i];
|
||||
}
|
||||
return arrayBuffer
|
||||
},
|
||||
|
||||
ModelToObject : function (model)
|
||||
{
|
||||
var obj = {
|
||||
|
||||
@ -1,24 +1,24 @@
|
||||
OV.InitModelLoader = function (modelLoader, callbacks)
|
||||
{
|
||||
function OpenErrorDialog (importerError)
|
||||
function OpenErrorDialog (importError)
|
||||
{
|
||||
if (importerError.code === OV.ImporterErrorCode.NoImportableFile) {
|
||||
if (importError.code === OV.ImportErrorCode.NoImportableFile) {
|
||||
return OV.ShowMessageDialog (
|
||||
'Something went wrong',
|
||||
'No importable file found. You can open obj, 3ds, stl, ply, gltf, glb and off files.',
|
||||
importerError.message
|
||||
importError.message
|
||||
);
|
||||
} else if (importerError.code === OV.ImporterErrorCode.ImportFailed) {
|
||||
} else if (importError.code === OV.ImportErrorCode.ImportFailed) {
|
||||
return OV.ShowMessageDialog (
|
||||
'Something went wrong',
|
||||
'Failed to import model.',
|
||||
importerError.message
|
||||
importError.message
|
||||
);
|
||||
} else {
|
||||
return OV.ShowMessageDialog (
|
||||
'Something went wrong',
|
||||
'Unknown error.',
|
||||
importerError.message
|
||||
importError.message
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -53,9 +53,9 @@ OV.InitModelLoader = function (modelLoader, callbacks)
|
||||
onTextureLoaded : function () {
|
||||
callbacks.onRender ();
|
||||
},
|
||||
onLoadError : function (importerError) {
|
||||
onLoadError : function (importError) {
|
||||
progressDialog.Hide ();
|
||||
errorDialog = OpenErrorDialog (importerError);
|
||||
errorDialog = OpenErrorDialog (importError);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -73,7 +73,7 @@ OV.Website = class
|
||||
}
|
||||
if (this.model !== null) {
|
||||
let importer = this.modelLoader.GetImporter ();
|
||||
importer.DisposeModel (this.model);
|
||||
importer.RevokeReferences (this.model);
|
||||
}
|
||||
this.model = null;
|
||||
this.parameters.introDiv.hide ();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user