From bc74ed9b044f383daa233336ddb968d685b737ed Mon Sep 17 00:00:00 2001 From: Viktor Kovacs Date: Thu, 15 Apr 2021 14:53:33 +0200 Subject: [PATCH] Replace tabs with spaces. --- source/export/exporterobj.js | 198 +++--- source/import/importer.js | 632 ++++++++--------- source/import/importer3ds.js | 1244 ++++++++++++++++----------------- source/import/importerbase.js | 26 +- source/import/importergltf.js | 84 +-- source/import/importerobj.js | 734 +++++++++---------- source/import/importeroff.js | 68 +- source/import/importerply.js | 38 +- source/import/importerstl.js | 62 +- 9 files changed, 1543 insertions(+), 1543 deletions(-) diff --git a/source/export/exporterobj.js b/source/export/exporterobj.js index 51bea6d..6473f64 100644 --- a/source/export/exporterobj.js +++ b/source/export/exporterobj.js @@ -1,115 +1,115 @@ OV.ExporterObj = class extends OV.ExporterBase { - constructor () - { - super (); - } + constructor () + { + super (); + } CanExport (format, extension) { return format === OV.FileFormat.Text && extension === 'obj'; } - ExportContent (model, format, files) - { - function WriteTexture (mtlWriter, keyword, texture, files) - { - if (texture === null || !texture.IsValid ()) { - return; - } - let fileName = OV.GetFileName (texture.name); - mtlWriter.WriteArrayLine ([keyword, fileName]); + ExportContent (model, format, files) + { + function WriteTexture (mtlWriter, keyword, texture, files) + { + if (texture === null || !texture.IsValid ()) { + return; + } + let fileName = OV.GetFileName (texture.name); + mtlWriter.WriteArrayLine ([keyword, fileName]); - let fileIndex = files.findIndex (function (file) { - return file.GetName () === fileName; - }); - if (fileIndex === -1) { - let textureFile = new OV.ExportedFile (fileName); - textureFile.SetContent (texture.buffer); - files.push (textureFile); - } - } + let fileIndex = files.findIndex (function (file) { + return file.GetName () === fileName; + }); + if (fileIndex === -1) { + let textureFile = new OV.ExportedFile (fileName); + textureFile.SetContent (texture.buffer); + files.push (textureFile); + } + } - let mtlFile = new OV.ExportedFile ('model.mtl'); - let objFile = new OV.ExportedFile ('model.obj'); + let mtlFile = new OV.ExportedFile ('model.mtl'); + let objFile = new OV.ExportedFile ('model.obj'); - files.push (mtlFile); - files.push (objFile); + files.push (mtlFile); + files.push (objFile); - let mtlWriter = new OV.TextWriter (); - mtlWriter.WriteLine (this.GetHeaderText ()); - for (let materialIndex = 0; materialIndex < model.MaterialCount (); materialIndex++) { - let material = model.GetMaterial (materialIndex); - mtlWriter.WriteArrayLine (['newmtl', this.GetExportedMaterialName (material.name)]); - mtlWriter.WriteArrayLine (['Ka', material.ambient.r / 255.0, material.ambient.g / 255.0, material.ambient.b / 255.0]); - mtlWriter.WriteArrayLine (['Kd', material.diffuse.r / 255.0, material.diffuse.g / 255.0, material.diffuse.b / 255.0]); - mtlWriter.WriteArrayLine (['Ks', material.specular.r / 255.0, material.specular.g / 255.0, material.specular.b / 255.0]); - mtlWriter.WriteArrayLine (['Ns', material.shininess * 100.0]); - mtlWriter.WriteArrayLine (['d', material.opacity]); - WriteTexture (mtlWriter, 'map_Kd', material.diffuseMap, files); - WriteTexture (mtlWriter, 'map_Ks', material.specularMap, files); - WriteTexture (mtlWriter, 'bump', material.bumpMap, files); - } - mtlFile.SetContent (mtlWriter.GetText ()); + let mtlWriter = new OV.TextWriter (); + mtlWriter.WriteLine (this.GetHeaderText ()); + for (let materialIndex = 0; materialIndex < model.MaterialCount (); materialIndex++) { + let material = model.GetMaterial (materialIndex); + mtlWriter.WriteArrayLine (['newmtl', this.GetExportedMaterialName (material.name)]); + mtlWriter.WriteArrayLine (['Ka', material.ambient.r / 255.0, material.ambient.g / 255.0, material.ambient.b / 255.0]); + mtlWriter.WriteArrayLine (['Kd', material.diffuse.r / 255.0, material.diffuse.g / 255.0, material.diffuse.b / 255.0]); + mtlWriter.WriteArrayLine (['Ks', material.specular.r / 255.0, material.specular.g / 255.0, material.specular.b / 255.0]); + mtlWriter.WriteArrayLine (['Ns', material.shininess * 100.0]); + mtlWriter.WriteArrayLine (['d', material.opacity]); + WriteTexture (mtlWriter, 'map_Kd', material.diffuseMap, files); + WriteTexture (mtlWriter, 'map_Ks', material.specularMap, files); + WriteTexture (mtlWriter, 'bump', material.bumpMap, files); + } + mtlFile.SetContent (mtlWriter.GetText ()); - let objWriter = new OV.TextWriter (); - objWriter.WriteLine (this.GetHeaderText ()); - objWriter.WriteArrayLine (['mtllib', mtlFile.GetName ()]); - let vertexOffset = 0; - let normalOffset = 0; - let uvOffset = 0; - let usedMaterialName = null; - for (let meshIndex = 0; meshIndex < model.MeshCount (); meshIndex++) { - let mesh = model.GetMesh (meshIndex); - objWriter.WriteArrayLine (['g', this.GetExportedMeshName (mesh.GetName ())]); - for (let vertexIndex = 0; vertexIndex < mesh.VertexCount (); vertexIndex++) { - let vertex = mesh.GetVertex (vertexIndex); - objWriter.WriteArrayLine (['v', vertex.x, vertex.y, vertex.z]); - } - for (let normalIndex = 0; normalIndex < mesh.NormalCount (); normalIndex++) { - let normal = mesh.GetNormal (normalIndex); - objWriter.WriteArrayLine (['vn', normal.x, normal.y, normal.z]); - } - for (let textureUVIndex = 0; textureUVIndex < mesh.TextureUVCount (); textureUVIndex++) { - let uv = mesh.GetTextureUV (textureUVIndex); - objWriter.WriteArrayLine (['vt', uv.x, uv.y]); - } - for (let triangleIndex = 0; triangleIndex < mesh.TriangleCount (); triangleIndex++) { - let triangle = mesh.GetTriangle (triangleIndex); - let v0 = triangle.v0 + vertexOffset + 1; - let v1 = triangle.v1 + vertexOffset + 1; - let v2 = triangle.v2 + vertexOffset + 1; - let n0 = triangle.n0 + normalOffset + 1; - let n1 = triangle.n1 + normalOffset + 1; - let n2 = triangle.n2 + normalOffset + 1; - if (triangle.mat !== null) { - let material = model.GetMaterial (triangle.mat); - let materialName = this.GetExportedMaterialName (material.name); - if (materialName !== usedMaterialName) { - objWriter.WriteArrayLine (['usemtl', materialName]); - usedMaterialName = materialName; - } - } - let u0 = ''; - let u1 = ''; - let u2 = ''; - if (triangle.HasTextureUVs ()) { - u0 = triangle.u0 + uvOffset + 1; - u1 = triangle.u1 + uvOffset + 1; - u2 = triangle.u2 + uvOffset + 1; - } - objWriter.WriteArrayLine (['f', [v0, u0, n0].join ('/'), [v1, u1, n1].join ('/'), [v2, u2, n2].join ('/')]); - } - vertexOffset += mesh.VertexCount (); - normalOffset += mesh.NormalCount (); - uvOffset += mesh.TextureUVCount (); - } + let objWriter = new OV.TextWriter (); + objWriter.WriteLine (this.GetHeaderText ()); + objWriter.WriteArrayLine (['mtllib', mtlFile.GetName ()]); + let vertexOffset = 0; + let normalOffset = 0; + let uvOffset = 0; + let usedMaterialName = null; + for (let meshIndex = 0; meshIndex < model.MeshCount (); meshIndex++) { + let mesh = model.GetMesh (meshIndex); + objWriter.WriteArrayLine (['g', this.GetExportedMeshName (mesh.GetName ())]); + for (let vertexIndex = 0; vertexIndex < mesh.VertexCount (); vertexIndex++) { + let vertex = mesh.GetVertex (vertexIndex); + objWriter.WriteArrayLine (['v', vertex.x, vertex.y, vertex.z]); + } + for (let normalIndex = 0; normalIndex < mesh.NormalCount (); normalIndex++) { + let normal = mesh.GetNormal (normalIndex); + objWriter.WriteArrayLine (['vn', normal.x, normal.y, normal.z]); + } + for (let textureUVIndex = 0; textureUVIndex < mesh.TextureUVCount (); textureUVIndex++) { + let uv = mesh.GetTextureUV (textureUVIndex); + objWriter.WriteArrayLine (['vt', uv.x, uv.y]); + } + for (let triangleIndex = 0; triangleIndex < mesh.TriangleCount (); triangleIndex++) { + let triangle = mesh.GetTriangle (triangleIndex); + let v0 = triangle.v0 + vertexOffset + 1; + let v1 = triangle.v1 + vertexOffset + 1; + let v2 = triangle.v2 + vertexOffset + 1; + let n0 = triangle.n0 + normalOffset + 1; + let n1 = triangle.n1 + normalOffset + 1; + let n2 = triangle.n2 + normalOffset + 1; + if (triangle.mat !== null) { + let material = model.GetMaterial (triangle.mat); + let materialName = this.GetExportedMaterialName (material.name); + if (materialName !== usedMaterialName) { + objWriter.WriteArrayLine (['usemtl', materialName]); + usedMaterialName = materialName; + } + } + let u0 = ''; + let u1 = ''; + let u2 = ''; + if (triangle.HasTextureUVs ()) { + u0 = triangle.u0 + uvOffset + 1; + u1 = triangle.u1 + uvOffset + 1; + u2 = triangle.u2 + uvOffset + 1; + } + objWriter.WriteArrayLine (['f', [v0, u0, n0].join ('/'), [v1, u1, n1].join ('/'), [v2, u2, n2].join ('/')]); + } + vertexOffset += mesh.VertexCount (); + normalOffset += mesh.NormalCount (); + uvOffset += mesh.TextureUVCount (); + } - objFile.SetContent (objWriter.GetText ()); - } + objFile.SetContent (objWriter.GetText ()); + } - GetHeaderText () - { - return '# exported by https://3dviewer.net'; - } + GetHeaderText () + { + return '# exported by https://3dviewer.net'; + } }; diff --git a/source/import/importer.js b/source/import/importer.js index d4681e3..ec15b34 100644 --- a/source/import/importer.js +++ b/source/import/importer.js @@ -1,218 +1,218 @@ OV.File = class { - constructor (file, source) - { - this.source = source; - if (source === OV.FileSource.Url) { - this.fileUrl = file; - this.fileObject = null; - this.name = OV.GetFileName (file); - this.extension = OV.GetFileExtension (file); - } else if (source === OV.FileSource.File) { - this.fileUrl = null; - this.fileObject = file; - this.name = OV.GetFileName (file.name); - this.extension = OV.GetFileExtension (file.name); - } - this.content = null; - } + constructor (file, source) + { + this.source = source; + if (source === OV.FileSource.Url) { + this.fileUrl = file; + this.fileObject = null; + this.name = OV.GetFileName (file); + this.extension = OV.GetFileExtension (file); + } else if (source === OV.FileSource.File) { + this.fileUrl = null; + this.fileObject = file; + this.name = OV.GetFileName (file.name); + this.extension = OV.GetFileExtension (file.name); + } + this.content = null; + } }; OV.FileList = class { - constructor (importers) - { - this.files = []; - this.importers = importers; - } + constructor (importers) + { + this.files = []; + this.importers = importers; + } - FillFromFileUrls (fileList) - { - this.Fill (fileList, OV.FileSource.Url); - } + FillFromFileUrls (fileList) + { + this.Fill (fileList, OV.FileSource.Url); + } - FillFromFileObjects (fileList) - { - this.Fill (fileList, OV.FileSource.File); - } + FillFromFileObjects (fileList) + { + this.Fill (fileList, OV.FileSource.File); + } - ExtendFromFileList (files) - { - for (let i = 0; i < files.length; i++) { - let file = files[i]; - if (!this.ContainsFileByPath (file.name)) { - this.files.push (file); - } - } - } + ExtendFromFileList (files) + { + for (let i = 0; i < files.length; i++) { + let file = files[i]; + if (!this.ContainsFileByPath (file.name)) { + this.files.push (file); + } + } + } - GetFiles () - { - return this.files; - } + GetFiles () + { + return this.files; + } - GetContent (onReady) - { - let obj = this; - let taskRunner = new OV.TaskRunner (); - taskRunner.Run (this.files.length, { - runTask : function (index, complete) { - obj.GetFileContent (obj.files[index], complete); - }, - onReady : onReady - }); - } + GetContent (onReady) + { + let obj = this; + let taskRunner = new OV.TaskRunner (); + taskRunner.Run (this.files.length, { + runTask : function (index, complete) { + obj.GetFileContent (obj.files[index], complete); + }, + onReady : onReady + }); + } - ContainsFileByPath (filePath) - { - return this.FindFileByPath (filePath) !== null; - } + ContainsFileByPath (filePath) + { + return this.FindFileByPath (filePath) !== null; + } - FindFileByPath (filePath) - { - let fileName = OV.GetFileName (filePath).toLowerCase (); - for (let fileIndex = 0; fileIndex < this.files.length; fileIndex++) { - let file = this.files[fileIndex]; - if (file.name.toLowerCase () === fileName) { - return file; - } - } - return null; - } + FindFileByPath (filePath) + { + let fileName = OV.GetFileName (filePath).toLowerCase (); + for (let fileIndex = 0; fileIndex < this.files.length; fileIndex++) { + let file = this.files[fileIndex]; + if (file.name.toLowerCase () === fileName) { + return file; + } + } + return null; + } - HasMainFile () - { - return this.GetMainFile () !== null; - } + HasMainFile () + { + return this.GetMainFile () !== null; + } - GetMainFile () - { - for (let fileIndex = 0; fileIndex < this.files.length; fileIndex++) { - let file = this.files[fileIndex]; - let importer = this.FindImporter (file); - if (importer !== null) { - return { - file : file, - importer : importer - }; - } - } - return null; - } + GetMainFile () + { + for (let fileIndex = 0; fileIndex < this.files.length; fileIndex++) { + let file = this.files[fileIndex]; + let importer = this.FindImporter (file); + if (importer !== null) { + return { + file : file, + importer : importer + }; + } + } + return null; + } - IsOnlySource (source) - { - if (this.files.length === 0) { - return false; - } - for (let i = 0; i < this.files.length; i++) { - let file = this.files[i]; - if (file.source !== source) { - return false; - } - } - return true; - } + IsOnlySource (source) + { + if (this.files.length === 0) { + return false; + } + for (let i = 0; i < this.files.length; i++) { + let file = this.files[i]; + if (file.source !== source) { + return false; + } + } + return true; + } - Fill (fileList, fileSource) - { - this.files = []; - for (let fileIndex = 0; fileIndex < fileList.length; fileIndex++) { - let fileObject = fileList[fileIndex]; - let file = new OV.File (fileObject, fileSource); - this.AddFile (file); - } - } + Fill (fileList, fileSource) + { + this.files = []; + for (let fileIndex = 0; fileIndex < fileList.length; fileIndex++) { + let fileObject = fileList[fileIndex]; + let file = new OV.File (fileObject, fileSource); + this.AddFile (file); + } + } - AddFile (file) - { - this.files.push (file); - } - - GetFileContent (file, complete) - { - let callbacks = { - success : function (content) { - file.content = content; - }, - error : function () { - - }, - complete : function () { - complete (); - } - }; - if (file.content !== null) { - complete (); - return; - } - let format = this.GetFileFormat (file); - if (file.source === OV.FileSource.Url) { - OV.RequestUrl (file.fileUrl, format, callbacks); - } else if (file.source === OV.FileSource.File) { - OV.ReadFile (file.fileObject, format, callbacks); - } - } - - GetFileFormat (file) - { - for (let importerIndex = 0; importerIndex < this.importers.length; importerIndex++) { - let importer = this.importers[importerIndex]; - let extension = file.extension.toLowerCase (); - let knownFormats = importer.GetKnownFileFormats (); - if (knownFormats[extension] !== undefined) { - return knownFormats[extension]; - } - } - return OV.FileFormat.Binary; - } + AddFile (file) + { + this.files.push (file); + } + + GetFileContent (file, complete) + { + let callbacks = { + success : function (content) { + file.content = content; + }, + error : function () { + + }, + complete : function () { + complete (); + } + }; + if (file.content !== null) { + complete (); + return; + } + let format = this.GetFileFormat (file); + if (file.source === OV.FileSource.Url) { + OV.RequestUrl (file.fileUrl, format, callbacks); + } else if (file.source === OV.FileSource.File) { + OV.ReadFile (file.fileObject, format, callbacks); + } + } + + GetFileFormat (file) + { + for (let importerIndex = 0; importerIndex < this.importers.length; importerIndex++) { + let importer = this.importers[importerIndex]; + let extension = file.extension.toLowerCase (); + let knownFormats = importer.GetKnownFileFormats (); + if (knownFormats[extension] !== undefined) { + return knownFormats[extension]; + } + } + return OV.FileFormat.Binary; + } - FindImporter (file) - { - for (let importerIndex = 0; importerIndex < this.importers.length; importerIndex++) { - let importer = this.importers[importerIndex]; - if (importer.CanImportExtension (file.extension.toLowerCase ())) { - return importer; - } - } - return null; - } + FindImporter (file) + { + for (let importerIndex = 0; importerIndex < this.importers.length; importerIndex++) { + let importer = this.importers[importerIndex]; + if (importer.CanImportExtension (file.extension.toLowerCase ())) { + return importer; + } + } + return null; + } }; OV.ImportSettings = class { - constructor () - { - this.defaultColor = new OV.Color (200, 200, 200); - } + constructor () + { + this.defaultColor = new OV.Color (200, 200, 200); + } }; OV.ImportErrorCode = { - NoImportableFile : 1, - ImportFailed : 2, - UnknownError : 3 + NoImportableFile : 1, + ImportFailed : 2, + UnknownError : 3 }; OV.ImportError = class { - constructor (code, message) - { - this.code = code; - this.message = message; - } + constructor (code, message) + { + this.code = code; + this.message = message; + } }; OV.ImportResult = class { - constructor () - { - this.model = null; - this.mainFile = null; - this.upVector = null; - this.usedFiles = null; - this.missingFiles = null; - } + constructor () + { + this.model = null; + this.mainFile = null; + this.upVector = null; + this.usedFiles = null; + this.missingFiles = null; + } }; OV.ImportBuffers = class @@ -257,149 +257,149 @@ OV.ImportBuffers = class OV.Importer = class { - constructor () - { - this.importers = [ - new OV.ImporterObj (), - new OV.ImporterStl (), - new OV.ImporterOff (), - new OV.ImporterPly (), - new OV.Importer3ds (), - new OV.ImporterGltf () - ]; - this.fileList = new OV.FileList (this.importers); - this.model = null; - this.usedFiles = []; - this.missingFiles = []; - } - - LoadFilesFromUrls (fileList, onReady) - { - this.LoadFiles (fileList, OV.FileSource.Url, onReady); - } + constructor () + { + this.importers = [ + new OV.ImporterObj (), + new OV.ImporterStl (), + new OV.ImporterOff (), + new OV.ImporterPly (), + new OV.Importer3ds (), + new OV.ImporterGltf () + ]; + this.fileList = new OV.FileList (this.importers); + this.model = null; + this.usedFiles = []; + this.missingFiles = []; + } + + LoadFilesFromUrls (fileList, onReady) + { + this.LoadFiles (fileList, OV.FileSource.Url, onReady); + } - LoadFilesFromFileObjects (fileList, onReady) - { - this.LoadFiles (fileList, OV.FileSource.File, onReady); - } + LoadFilesFromFileObjects (fileList, onReady) + { + this.LoadFiles (fileList, OV.FileSource.File, onReady); + } - Import (settings, callbacks) - { - let mainFile = this.fileList.GetMainFile (); - if (mainFile === null || mainFile.file === null || mainFile.file.content === null) { - callbacks.onError (new OV.ImportError (OV.ImportErrorCode.NoImportableFile, null)); - return; - } + Import (settings, callbacks) + { + let mainFile = this.fileList.GetMainFile (); + if (mainFile === null || mainFile.file === null || mainFile.file.content === null) { + callbacks.onError (new OV.ImportError (OV.ImportErrorCode.NoImportableFile, null)); + return; + } - this.RevokeModelUrls (); - this.model = null; - this.usedFiles = []; - this.missingFiles = []; - this.usedFiles.push (mainFile.file.name); + this.RevokeModelUrls (); + this.model = null; + this.usedFiles = []; + this.missingFiles = []; + this.usedFiles.push (mainFile.file.name); - let obj = this; - let importer = mainFile.importer; - let buffers = new OV.ImportBuffers (function (fileName) { - let fileBuffer = null; - let file = obj.fileList.FindFileByPath (fileName); - if (file === null || file.content === null) { - obj.missingFiles.push (fileName); - fileBuffer = null; - } else { - fileBuffer = file.content; - obj.usedFiles.push (fileName); - } - return fileBuffer; - }); + let obj = this; + let importer = mainFile.importer; + let buffers = new OV.ImportBuffers (function (fileName) { + let fileBuffer = null; + let file = obj.fileList.FindFileByPath (fileName); + if (file === null || file.content === null) { + obj.missingFiles.push (fileName); + fileBuffer = null; + } else { + fileBuffer = file.content; + obj.usedFiles.push (fileName); + } + return fileBuffer; + }); - importer.Import (mainFile.file.content, mainFile.file.extension, { - getDefaultMaterial : function () { - let material = new OV.Material (); - material.diffuse = settings.defaultColor; - return material; - }, - getFileBuffer : function (filePath) { - return buffers.GetFileBuffer (filePath); - }, - getTextureBuffer : function (filePath) { - return buffers.GetTextureBuffer (filePath); - }, - onSuccess : function () { - obj.model = importer.GetModel (); - obj.model.SetName (mainFile.file.name); - - let result = new OV.ImportResult (); - result.mainFile = mainFile.file.name; - result.model = obj.model; - result.usedFiles = obj.usedFiles; - result.missingFiles = obj.missingFiles; - result.upVector = importer.GetUpDirection (); - callbacks.onSuccess (result); - }, - onError : function () { - let message = importer.GetMessage (); - callbacks.onError (new OV.ImportError (OV.ImportErrorCode.ImportFailed, message)); - } - }); - } + importer.Import (mainFile.file.content, mainFile.file.extension, { + getDefaultMaterial : function () { + let material = new OV.Material (); + material.diffuse = settings.defaultColor; + return material; + }, + getFileBuffer : function (filePath) { + return buffers.GetFileBuffer (filePath); + }, + getTextureBuffer : function (filePath) { + return buffers.GetTextureBuffer (filePath); + }, + onSuccess : function () { + obj.model = importer.GetModel (); + obj.model.SetName (mainFile.file.name); + + let result = new OV.ImportResult (); + result.mainFile = mainFile.file.name; + result.model = obj.model; + result.usedFiles = obj.usedFiles; + result.missingFiles = obj.missingFiles; + result.upVector = importer.GetUpDirection (); + callbacks.onSuccess (result); + }, + onError : function () { + let message = importer.GetMessage (); + callbacks.onError (new OV.ImportError (OV.ImportErrorCode.ImportFailed, message)); + } + }); + } - LoadFiles (fileList, fileSource, onReady) - { - let newFileList = new OV.FileList (this.importers); - if (fileSource === OV.FileSource.Url) { - newFileList.FillFromFileUrls (fileList); - } else if (fileSource === OV.FileSource.File) { - newFileList.FillFromFileObjects (fileList); - } - let reset = false; - if (newFileList.HasMainFile ()) { - reset = true; - } else { - let foundMissingFile = false; - for (let i = 0; i < this.missingFiles.length; i++) { - let missingFile = this.missingFiles[i]; - if (newFileList.ContainsFileByPath (missingFile)) { - foundMissingFile = true; - } - } - if (!foundMissingFile) { - reset = true; - } else { - let newFiles = newFileList.GetFiles (); - this.fileList.ExtendFromFileList (newFiles); - reset = false; - } - } - if (reset) { - this.fileList = newFileList; - } - this.fileList.GetContent (function () { - onReady (); - }); - } + LoadFiles (fileList, fileSource, onReady) + { + let newFileList = new OV.FileList (this.importers); + if (fileSource === OV.FileSource.Url) { + newFileList.FillFromFileUrls (fileList); + } else if (fileSource === OV.FileSource.File) { + newFileList.FillFromFileObjects (fileList); + } + let reset = false; + if (newFileList.HasMainFile ()) { + reset = true; + } else { + let foundMissingFile = false; + for (let i = 0; i < this.missingFiles.length; i++) { + let missingFile = this.missingFiles[i]; + if (newFileList.ContainsFileByPath (missingFile)) { + foundMissingFile = true; + } + } + if (!foundMissingFile) { + reset = true; + } else { + let newFiles = newFileList.GetFiles (); + this.fileList.ExtendFromFileList (newFiles); + reset = false; + } + } + if (reset) { + this.fileList = newFileList; + } + this.fileList.GetContent (function () { + onReady (); + }); + } - GetFileList () - { - return this.fileList; - } + GetFileList () + { + return this.fileList; + } - IsOnlyFileSource (source) - { - return this.fileList.IsOnlySource (source); - } + IsOnlyFileSource (source) + { + return this.fileList.IsOnlySource (source); + } - RevokeModelUrls () - { - if (this.model === null) { - return; - } - for (let i = 0; i < this.model.MaterialCount (); i++) { - let material = this.model.GetMaterial (i); - material.EnumerateTextureMaps (function (texture) { - if (texture.url !== null) { - OV.RevokeObjectUrl (texture.url); - } - }); - } - } + RevokeModelUrls () + { + if (this.model === null) { + return; + } + for (let i = 0; i < this.model.MaterialCount (); i++) { + let material = this.model.GetMaterial (i); + material.EnumerateTextureMaps (function (texture) { + if (texture.url !== null) { + OV.RevokeObjectUrl (texture.url); + } + }); + } + } }; diff --git a/source/import/importer3ds.js b/source/import/importer3ds.js index 0a7b65f..04e58e9 100644 --- a/source/import/importer3ds.js +++ b/source/import/importer3ds.js @@ -1,683 +1,683 @@ OV.CHUNK3DS = { - MAIN3DS : 0x4D4D, - EDIT3DS : 0x3D3D, - EDIT_MATERIAL : 0xAFFF, - MAT_NAME : 0xA000, - MAT_AMBIENT : 0xA010, - MAT_DIFFUSE : 0xA020, - MAT_SPECULAR : 0xA030, - MAT_SHININESS : 0xA040, - MAT_SHININESS_STRENGTH : 0xA041, - MAT_TRANSPARENCY : 0xA050, - MAT_COLOR_F : 0x0010, - MAT_COLOR : 0x0011, - MAT_LIN_COLOR : 0x0012, - MAT_LIN_COLOR_F : 0x0013, - MAT_TEXMAP : 0xA200, - MAT_TEXMAP_NAME : 0xA300, - MAT_TEXMAP_UOFFSET : 0xA358, - MAT_TEXMAP_VOFFSET : 0xA35A, - MAT_TEXMAP_USCALE : 0xA354, - MAT_TEXMAP_VSCALE : 0xA356, - MAT_TEXMAP_ROTATION : 0xA35C, - PERCENTAGE : 0x0030, - PERCENTAGE_F : 0x0031, - EDIT_OBJECT : 0x4000, - OBJ_TRIMESH : 0x4100, - OBJ_LIGHT : 0x4600, - OBJ_CAMERA : 0x4700, - TRI_VERTEX : 0x4110, - TRI_TEXVERTEX : 0x4140, - TRI_FACE : 0x4120, - TRI_TRANSFORMATION : 0x4160, - TRI_MATERIAL : 0x4130, - TRI_SMOOTH : 0x4150, - KF3DS : 0xB000, - OBJECT_NODE : 0xB002, - OBJECT_HIERARCHY : 0xB010, - OBJECT_INSTANCE_NAME : 0xB011, - OBJECT_PIVOT : 0xB013, - OBJECT_POSITION : 0xB020, - OBJECT_ROTATION : 0xB021, - OBJECT_SCALE : 0xB022, - OBJECT_ID : 0xB030 + MAIN3DS : 0x4D4D, + EDIT3DS : 0x3D3D, + EDIT_MATERIAL : 0xAFFF, + MAT_NAME : 0xA000, + MAT_AMBIENT : 0xA010, + MAT_DIFFUSE : 0xA020, + MAT_SPECULAR : 0xA030, + MAT_SHININESS : 0xA040, + MAT_SHININESS_STRENGTH : 0xA041, + MAT_TRANSPARENCY : 0xA050, + MAT_COLOR_F : 0x0010, + MAT_COLOR : 0x0011, + MAT_LIN_COLOR : 0x0012, + MAT_LIN_COLOR_F : 0x0013, + MAT_TEXMAP : 0xA200, + MAT_TEXMAP_NAME : 0xA300, + MAT_TEXMAP_UOFFSET : 0xA358, + MAT_TEXMAP_VOFFSET : 0xA35A, + MAT_TEXMAP_USCALE : 0xA354, + MAT_TEXMAP_VSCALE : 0xA356, + MAT_TEXMAP_ROTATION : 0xA35C, + PERCENTAGE : 0x0030, + PERCENTAGE_F : 0x0031, + EDIT_OBJECT : 0x4000, + OBJ_TRIMESH : 0x4100, + OBJ_LIGHT : 0x4600, + OBJ_CAMERA : 0x4700, + TRI_VERTEX : 0x4110, + TRI_TEXVERTEX : 0x4140, + TRI_FACE : 0x4120, + TRI_TRANSFORMATION : 0x4160, + TRI_MATERIAL : 0x4130, + TRI_SMOOTH : 0x4150, + KF3DS : 0xB000, + OBJECT_NODE : 0xB002, + OBJECT_HIERARCHY : 0xB010, + OBJECT_INSTANCE_NAME : 0xB011, + OBJECT_PIVOT : 0xB013, + OBJECT_POSITION : 0xB020, + OBJECT_ROTATION : 0xB021, + OBJECT_SCALE : 0xB022, + OBJECT_ID : 0xB030 }; OV.Importer3ds = class extends OV.ImporterBase { - constructor () - { - super (); + constructor () + { + super (); - this.materialNameToIndex = null; - this.meshNameToIndex = null; - - this.meshTransformations = null; - this.defaultMaterialIndex = null; - } - - ResetState () - { - this.materialNameToIndex = {}; - this.meshNameToIndex = {}; + this.materialNameToIndex = null; + this.meshNameToIndex = null; + + this.meshTransformations = null; + this.defaultMaterialIndex = null; + } + + ResetState () + { + this.materialNameToIndex = {}; + this.meshNameToIndex = {}; - this.meshTransformations = []; - this.defaultMaterialIndex = null; - } + this.meshTransformations = []; + this.defaultMaterialIndex = null; + } - CanImportExtension (extension) - { - return extension === '3ds'; - } + CanImportExtension (extension) + { + return extension === '3ds'; + } - GetKnownFileFormats () - { - return { - '3ds' : OV.FileFormat.Binary - }; - } - + GetKnownFileFormats () + { + return { + '3ds' : OV.FileFormat.Binary + }; + } + GetUpDirection () { return OV.Direction.Z; } - ImportContent (fileContent, onFinish) - { + ImportContent (fileContent, onFinish) + { this.ProcessBinary (fileContent); - onFinish (); - } - - ProcessBinary (fileContent) - { - let obj = this; - let reader = new OV.BinaryReader (fileContent, true); - let endByte = reader.GetByteLength (); - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.MAIN3DS) { - obj.ReadMainChunk (reader, chunkLength); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); + onFinish (); } - ReadMainChunk (reader, length) - { - let obj = this; - let endByte = this.GetChunkEnd (reader, length); - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.EDIT3DS) { - obj.ReadEditorChunk (reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.KF3DS) { - obj.ReadKeyFrameChunk (reader, chunkLength); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); - } + ProcessBinary (fileContent) + { + let obj = this; + let reader = new OV.BinaryReader (fileContent, true); + let endByte = reader.GetByteLength (); + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.MAIN3DS) { + obj.ReadMainChunk (reader, chunkLength); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); + } - ReadEditorChunk (reader, length) - { - let obj = this; - let endByte = this.GetChunkEnd (reader, length); - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.EDIT_MATERIAL) { - obj.ReadMaterialChunk (reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.EDIT_OBJECT) { - obj.ReadObjectChunk (reader, chunkLength); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); - } + ReadMainChunk (reader, length) + { + let obj = this; + let endByte = this.GetChunkEnd (reader, length); + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.EDIT3DS) { + obj.ReadEditorChunk (reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.KF3DS) { + obj.ReadKeyFrameChunk (reader, chunkLength); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); + } - ReadMaterialChunk (reader, length) - { - let obj = this; - let material = new OV.Material (); - let endByte = this.GetChunkEnd (reader, length); - let shininess = null; - let shininessStrength = null; - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.MAT_NAME) { - material.name = obj.ReadName (reader); - } else if (chunkId === OV.CHUNK3DS.MAT_AMBIENT) { - material.ambient = obj.ReadColorChunk (reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.MAT_DIFFUSE) { - material.diffuse = obj.ReadColorChunk (reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.MAT_SPECULAR) { - material.specular = obj.ReadColorChunk (reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.MAT_SHININESS) { - shininess = obj.ReadPercentageChunk (reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.MAT_SHININESS_STRENGTH) { - shininessStrength = obj.ReadPercentageChunk (reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.MAT_TRANSPARENCY) { - material.opacity = 1.0 - obj.ReadPercentageChunk (reader, chunkLength); - material.transparent = OV.IsLower (material.opacity, 1.0); - } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP) { - material.diffuseMap = obj.ReadTextureMapChunk (reader, chunkLength); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); + ReadEditorChunk (reader, length) + { + let obj = this; + let endByte = this.GetChunkEnd (reader, length); + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.EDIT_MATERIAL) { + obj.ReadMaterialChunk (reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.EDIT_OBJECT) { + obj.ReadObjectChunk (reader, chunkLength); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); + } - if (shininess !== null || shininessStrength !== null) { - material.shininess = shininess * shininessStrength; - } - let materialIndex = this.model.AddMaterial (material); - this.materialNameToIndex[material.name] = materialIndex; - } + ReadMaterialChunk (reader, length) + { + let obj = this; + let material = new OV.Material (); + let endByte = this.GetChunkEnd (reader, length); + let shininess = null; + let shininessStrength = null; + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.MAT_NAME) { + material.name = obj.ReadName (reader); + } else if (chunkId === OV.CHUNK3DS.MAT_AMBIENT) { + material.ambient = obj.ReadColorChunk (reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.MAT_DIFFUSE) { + material.diffuse = obj.ReadColorChunk (reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.MAT_SPECULAR) { + material.specular = obj.ReadColorChunk (reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.MAT_SHININESS) { + shininess = obj.ReadPercentageChunk (reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.MAT_SHININESS_STRENGTH) { + shininessStrength = obj.ReadPercentageChunk (reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.MAT_TRANSPARENCY) { + material.opacity = 1.0 - obj.ReadPercentageChunk (reader, chunkLength); + material.transparent = OV.IsLower (material.opacity, 1.0); + } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP) { + material.diffuseMap = obj.ReadTextureMapChunk (reader, chunkLength); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); - ReadTextureMapChunk (reader, length) - { - let obj = this; - let texture = new OV.TextureMap (); - let endByte = this.GetChunkEnd (reader, length); - 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); - texture.name = textureName; - if (textureBuffer !== null) { - texture.url = textureBuffer.url; - texture.buffer = textureBuffer.buffer; - } - } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_UOFFSET) { - texture.offset.x = reader.ReadFloat32 (); - } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_VOFFSET) { - texture.offset.y = reader.ReadFloat32 (); - } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_USCALE) { - texture.scale.x = reader.ReadFloat32 (); - } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_VSCALE) { - texture.scale.y = reader.ReadFloat32 (); - } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_ROTATION) { - texture.rotation = reader.ReadFloat32 () * OV.DegRad; - } else { - obj.SkipChunk (reader, chunkLength); - } - }); - return texture; - } + if (shininess !== null || shininessStrength !== null) { + material.shininess = shininess * shininessStrength; + } + let materialIndex = this.model.AddMaterial (material); + this.materialNameToIndex[material.name] = materialIndex; + } - ReadColorChunk (reader, length) - { - let obj = this; - let color = new OV.Color (0, 0, 0); - let endByte = this.GetChunkEnd (reader, length); - let hasLinColor = false; - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.MAT_COLOR) { - if (!hasLinColor) { - color.r = reader.ReadUnsignedCharacter8 (); - color.g = reader.ReadUnsignedCharacter8 (); - color.b = reader.ReadUnsignedCharacter8 (); - } - } else if (chunkId === OV.CHUNK3DS.MAT_LIN_COLOR) { - color.r = reader.ReadUnsignedCharacter8 (); - color.g = reader.ReadUnsignedCharacter8 (); - color.b = reader.ReadUnsignedCharacter8 (); - hasLinColor = true; - } else if (chunkId === OV.CHUNK3DS.MAT_COLOR_F) { - if (!hasLinColor) { - color.r = parseInt (reader.ReadFloat32 () * 255.0, 10); - color.g = parseInt (reader.ReadFloat32 () * 255.0, 10); - color.b = parseInt (reader.ReadFloat32 () * 255.0, 10); - } - } else if (chunkId === OV.CHUNK3DS.MAT_LIN_COLOR_F) { - color.r = parseInt (reader.ReadFloat32 () * 255.0, 10); - color.g = parseInt (reader.ReadFloat32 () * 255.0, 10); - color.b = parseInt (reader.ReadFloat32 () * 255.0, 10); - hasLinColor = true; - } else { - obj.SkipChunk (reader, chunkLength); - } - }); - return color; - } + ReadTextureMapChunk (reader, length) + { + let obj = this; + let texture = new OV.TextureMap (); + let endByte = this.GetChunkEnd (reader, length); + 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); + texture.name = textureName; + if (textureBuffer !== null) { + texture.url = textureBuffer.url; + texture.buffer = textureBuffer.buffer; + } + } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_UOFFSET) { + texture.offset.x = reader.ReadFloat32 (); + } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_VOFFSET) { + texture.offset.y = reader.ReadFloat32 (); + } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_USCALE) { + texture.scale.x = reader.ReadFloat32 (); + } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_VSCALE) { + texture.scale.y = reader.ReadFloat32 (); + } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_ROTATION) { + texture.rotation = reader.ReadFloat32 () * OV.DegRad; + } else { + obj.SkipChunk (reader, chunkLength); + } + }); + return texture; + } - ReadPercentageChunk (reader, length) - { - let obj = this; - let percentage = 0.0; - let endByte = this.GetChunkEnd (reader, length); - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.PERCENTAGE) { - percentage = reader.ReadUnsignedInteger16 () / 100.0; - } else if (chunkId === OV.CHUNK3DS.PERCENTAGE_F) { - percentage = reader.ReadFloat32 (); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); - return percentage; - } + ReadColorChunk (reader, length) + { + let obj = this; + let color = new OV.Color (0, 0, 0); + let endByte = this.GetChunkEnd (reader, length); + let hasLinColor = false; + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.MAT_COLOR) { + if (!hasLinColor) { + color.r = reader.ReadUnsignedCharacter8 (); + color.g = reader.ReadUnsignedCharacter8 (); + color.b = reader.ReadUnsignedCharacter8 (); + } + } else if (chunkId === OV.CHUNK3DS.MAT_LIN_COLOR) { + color.r = reader.ReadUnsignedCharacter8 (); + color.g = reader.ReadUnsignedCharacter8 (); + color.b = reader.ReadUnsignedCharacter8 (); + hasLinColor = true; + } else if (chunkId === OV.CHUNK3DS.MAT_COLOR_F) { + if (!hasLinColor) { + color.r = parseInt (reader.ReadFloat32 () * 255.0, 10); + color.g = parseInt (reader.ReadFloat32 () * 255.0, 10); + color.b = parseInt (reader.ReadFloat32 () * 255.0, 10); + } + } else if (chunkId === OV.CHUNK3DS.MAT_LIN_COLOR_F) { + color.r = parseInt (reader.ReadFloat32 () * 255.0, 10); + color.g = parseInt (reader.ReadFloat32 () * 255.0, 10); + color.b = parseInt (reader.ReadFloat32 () * 255.0, 10); + hasLinColor = true; + } else { + obj.SkipChunk (reader, chunkLength); + } + }); + return color; + } - ReadObjectChunk (reader, length) - { - let obj = this; - let endByte = this.GetChunkEnd (reader, length); - let objectName = this.ReadName (reader); - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.OBJ_TRIMESH) { - obj.ReadMeshChunk (reader, chunkLength, objectName); - } else if (chunkId === OV.CHUNK3DS.OBJ_LIGHT) { - obj.SkipChunk (reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.OBJ_CAMERA) { - obj.SkipChunk (reader, chunkLength); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); - } + ReadPercentageChunk (reader, length) + { + let obj = this; + let percentage = 0.0; + let endByte = this.GetChunkEnd (reader, length); + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.PERCENTAGE) { + percentage = reader.ReadUnsignedInteger16 () / 100.0; + } else if (chunkId === OV.CHUNK3DS.PERCENTAGE_F) { + percentage = reader.ReadFloat32 (); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); + return percentage; + } - ReadMeshChunk (reader, length, objectName) - { - let obj = this; + ReadObjectChunk (reader, length) + { + let obj = this; + let endByte = this.GetChunkEnd (reader, length); + let objectName = this.ReadName (reader); + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.OBJ_TRIMESH) { + obj.ReadMeshChunk (reader, chunkLength, objectName); + } else if (chunkId === OV.CHUNK3DS.OBJ_LIGHT) { + obj.SkipChunk (reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.OBJ_CAMERA) { + obj.SkipChunk (reader, chunkLength); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); + } - let mesh = new OV.Mesh (); - mesh.SetName (objectName); + ReadMeshChunk (reader, length, objectName) + { + let obj = this; - let endByte = this.GetChunkEnd (reader, length); - let transformation = null; - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.TRI_VERTEX) { - obj.ReadVerticesChunk (mesh, reader); - } else if (chunkId === OV.CHUNK3DS.TRI_TEXVERTEX) { - obj.ReadTextureVerticesChunk (mesh, reader); - } else if (chunkId === OV.CHUNK3DS.TRI_FACE) { - obj.ReadFacesChunk (mesh, reader, chunkLength); - } else if (chunkId === OV.CHUNK3DS.TRI_TRANSFORMATION) { - transformation = obj.ReadTransformationChunk (reader); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); + let mesh = new OV.Mesh (); + mesh.SetName (objectName); - if (mesh.VertexCount () === mesh.TextureUVCount ()) { - for (let i = 0; i < mesh.TriangleCount (); i++) { - let triangle = mesh.GetTriangle (i); - triangle.SetTextureUVs ( - triangle.v0, - triangle.v1, - triangle.v2 - ); - } - } + let endByte = this.GetChunkEnd (reader, length); + let transformation = null; + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.TRI_VERTEX) { + obj.ReadVerticesChunk (mesh, reader); + } else if (chunkId === OV.CHUNK3DS.TRI_TEXVERTEX) { + obj.ReadTextureVerticesChunk (mesh, reader); + } else if (chunkId === OV.CHUNK3DS.TRI_FACE) { + obj.ReadFacesChunk (mesh, reader, chunkLength); + } else if (chunkId === OV.CHUNK3DS.TRI_TRANSFORMATION) { + transformation = obj.ReadTransformationChunk (reader); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); - let meshIndex = this.model.AddMesh (mesh); - this.meshNameToIndex[mesh.GetName ()] = meshIndex; - this.meshTransformations.push (new OV.Matrix (transformation)); - } + if (mesh.VertexCount () === mesh.TextureUVCount ()) { + for (let i = 0; i < mesh.TriangleCount (); i++) { + let triangle = mesh.GetTriangle (i); + triangle.SetTextureUVs ( + triangle.v0, + triangle.v1, + triangle.v2 + ); + } + } - ReadVerticesChunk (mesh, reader) - { - let vertexCount = reader.ReadUnsignedInteger16 (); - for (let i = 0; i < vertexCount; i++) { - let x = reader.ReadFloat32 (); - let y = reader.ReadFloat32 (); - let z = reader.ReadFloat32 (); - mesh.AddVertex (new OV.Coord3D (x, y, z)); - } - } + let meshIndex = this.model.AddMesh (mesh); + this.meshNameToIndex[mesh.GetName ()] = meshIndex; + this.meshTransformations.push (new OV.Matrix (transformation)); + } - ReadTextureVerticesChunk (mesh, reader) - { - let texVertexCount = reader.ReadUnsignedInteger16 (); - for (let i = 0; i < texVertexCount; i++) { - let x = reader.ReadFloat32 (); - let y = reader.ReadFloat32 (); - mesh.AddTextureUV (new OV.Coord2D (x, y)); - } - } + ReadVerticesChunk (mesh, reader) + { + let vertexCount = reader.ReadUnsignedInteger16 (); + for (let i = 0; i < vertexCount; i++) { + let x = reader.ReadFloat32 (); + let y = reader.ReadFloat32 (); + let z = reader.ReadFloat32 (); + mesh.AddVertex (new OV.Coord3D (x, y, z)); + } + } - ReadFacesChunk (mesh, reader, length) - { - let endByte = this.GetChunkEnd (reader, length); - let faceCount = reader.ReadUnsignedInteger16 (); - for (let i = 0; i < faceCount; i++) { - let v0 = reader.ReadUnsignedInteger16 (); - let v1 = reader.ReadUnsignedInteger16 (); - let v2 = reader.ReadUnsignedInteger16 (); - reader.ReadUnsignedInteger16 (); // flags - mesh.AddTriangle (new OV.Triangle (v0, v1, v2)); - } + ReadTextureVerticesChunk (mesh, reader) + { + let texVertexCount = reader.ReadUnsignedInteger16 (); + for (let i = 0; i < texVertexCount; i++) { + let x = reader.ReadFloat32 (); + let y = reader.ReadFloat32 (); + mesh.AddTextureUV (new OV.Coord2D (x, y)); + } + } - let obj = this; - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.TRI_MATERIAL) { - obj.ReadFaceMaterialsChunk (mesh, reader); - } else if (chunkId === OV.CHUNK3DS.TRI_SMOOTH) { - obj.ReadFaceSmoothingGroupsChunk (mesh, faceCount, reader); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); - } + ReadFacesChunk (mesh, reader, length) + { + let endByte = this.GetChunkEnd (reader, length); + let faceCount = reader.ReadUnsignedInteger16 (); + for (let i = 0; i < faceCount; i++) { + let v0 = reader.ReadUnsignedInteger16 (); + let v1 = reader.ReadUnsignedInteger16 (); + let v2 = reader.ReadUnsignedInteger16 (); + reader.ReadUnsignedInteger16 (); // flags + mesh.AddTriangle (new OV.Triangle (v0, v1, v2)); + } - ReadFaceMaterialsChunk (mesh, reader) - { - let materialName = this.ReadName (reader); - let materialIndex = this.materialNameToIndex[materialName]; - let faceCount = reader.ReadUnsignedInteger16 (); - for (let i = 0; i < faceCount; i++) { - let faceIndex = reader.ReadUnsignedInteger16 (); - let triangle = mesh.GetTriangle (faceIndex); - if (materialIndex !== undefined) { - triangle.mat = materialIndex; - } - } - } - - ReadFaceSmoothingGroupsChunk (mesh, faceCount, reader) - { - for (let i = 0; i < faceCount; i++) { - let smoothingGroup = reader.ReadUnsignedInteger32 (); - let triangle = mesh.GetTriangle (i); - triangle.curve = smoothingGroup; - } - } + let obj = this; + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.TRI_MATERIAL) { + obj.ReadFaceMaterialsChunk (mesh, reader); + } else if (chunkId === OV.CHUNK3DS.TRI_SMOOTH) { + obj.ReadFaceSmoothingGroupsChunk (mesh, faceCount, reader); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); + } - ReadTransformationChunk (reader) - { - let matrix = []; - for (let i = 0; i < 4; i++) { - for (let j = 0; j < 3; j++) { - matrix.push (reader.ReadFloat32 ()); - } - if (i < 3) { - matrix.push (0); - } else { - matrix.push (1); - } - } - return matrix; - } + ReadFaceMaterialsChunk (mesh, reader) + { + let materialName = this.ReadName (reader); + let materialIndex = this.materialNameToIndex[materialName]; + let faceCount = reader.ReadUnsignedInteger16 (); + for (let i = 0; i < faceCount; i++) { + let faceIndex = reader.ReadUnsignedInteger16 (); + let triangle = mesh.GetTriangle (faceIndex); + if (materialIndex !== undefined) { + triangle.mat = materialIndex; + } + } + } + + ReadFaceSmoothingGroupsChunk (mesh, faceCount, reader) + { + for (let i = 0; i < faceCount; i++) { + let smoothingGroup = reader.ReadUnsignedInteger32 (); + let triangle = mesh.GetTriangle (i); + triangle.curve = smoothingGroup; + } + } - ReadKeyFrameChunk (reader, length) - { - let nodeHierarchy = { - nodes : [], - idToIndex : {}, - meshIndexToNodes : {} - }; + ReadTransformationChunk (reader) + { + let matrix = []; + for (let i = 0; i < 4; i++) { + for (let j = 0; j < 3; j++) { + matrix.push (reader.ReadFloat32 ()); + } + if (i < 3) { + matrix.push (0); + } else { + matrix.push (1); + } + } + return matrix; + } - let endByte = this.GetChunkEnd (reader, length); - let obj = this; - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.OBJECT_NODE) { - obj.ReadObjectNodeChunk (nodeHierarchy, reader, chunkLength); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); + ReadKeyFrameChunk (reader, length) + { + let nodeHierarchy = { + nodes : [], + idToIndex : {}, + meshIndexToNodes : {} + }; - this.ApplyModelTransformations (nodeHierarchy); - } + let endByte = this.GetChunkEnd (reader, length); + let obj = this; + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.OBJECT_NODE) { + obj.ReadObjectNodeChunk (nodeHierarchy, reader, chunkLength); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); - ApplyModelTransformations (nodeHierarchy) - { - function GetNodeTransformation (nodeHierarchy, node) - { - function GetNodePosition (node) - { - if (node.positions.length === 0) { - return [0.0, 0.0, 0.0]; - } - return node.positions[0]; - } - - function GetNodeRotation (node) - { - function GetQuaternionFromAxisAndAngle (rotation) - { - let result = [0.0, 0.0, 0.0, 1.0]; - let length = Math.sqrt (rotation[0] * rotation[0] + rotation[1] * rotation[1] + rotation[2] * rotation[2]); - if (length > 0.0) { - let omega = rotation[3] * -0.5; - let si = Math.sin (omega) / length; - result = [si * rotation[0], si * rotation[1], si * rotation[2], Math.cos (omega)]; - } - return result; - } + this.ApplyModelTransformations (nodeHierarchy); + } - if (node.rotations.length === 0) { - return [0.0, 0.0, 0.0, 1.0]; - } - - let rotation = node.rotations[0]; - return GetQuaternionFromAxisAndAngle (rotation); - } + ApplyModelTransformations (nodeHierarchy) + { + function GetNodeTransformation (nodeHierarchy, node) + { + function GetNodePosition (node) + { + if (node.positions.length === 0) { + return [0.0, 0.0, 0.0]; + } + return node.positions[0]; + } + + function GetNodeRotation (node) + { + function GetQuaternionFromAxisAndAngle (rotation) + { + let result = [0.0, 0.0, 0.0, 1.0]; + let length = Math.sqrt (rotation[0] * rotation[0] + rotation[1] * rotation[1] + rotation[2] * rotation[2]); + if (length > 0.0) { + let omega = rotation[3] * -0.5; + let si = Math.sin (omega) / length; + result = [si * rotation[0], si * rotation[1], si * rotation[2], Math.cos (omega)]; + } + return result; + } - function GetNodeScale (node) - { - if (node.scales.length === 0) { - return [1.0, 1.0, 1.0]; - } - return node.scales[0]; - } - - if (node.matrix !== null) { - return node.matrix; - } - - let matrix = new OV.Matrix (); - matrix.ComposeTRS ( - GetNodePosition (node), - GetNodeRotation (node), - GetNodeScale (node) - ); + if (node.rotations.length === 0) { + return [0.0, 0.0, 0.0, 1.0]; + } + + let rotation = node.rotations[0]; + return GetQuaternionFromAxisAndAngle (rotation); + } - if (node.userId !== 65535) { - let parentIndex = nodeHierarchy.idToIndex[node.userId]; - if (parentIndex !== undefined) { - let parentNode = nodeHierarchy.nodes[parentIndex]; - let parentMatrix = GetNodeTransformation (nodeHierarchy, parentNode); - matrix = matrix.MultiplyMatrix (parentMatrix); - } - } - - node.matrix = matrix; - return matrix; - } + function GetNodeScale (node) + { + if (node.scales.length === 0) { + return [1.0, 1.0, 1.0]; + } + return node.scales[0]; + } + + if (node.matrix !== null) { + return node.matrix; + } + + let matrix = new OV.Matrix (); + matrix.ComposeTRS ( + GetNodePosition (node), + GetNodeRotation (node), + GetNodeScale (node) + ); - function ApplyMeshTransformation (model, currentMeshIndex, meshMatrix, nodeHierarchy, node) - { - function GetNodePivotPoint (node) - { - if (node === null) { - return [0.0, 0.0, 0.0]; - } - return node.pivot; - } + if (node.userId !== 65535) { + let parentIndex = nodeHierarchy.idToIndex[node.userId]; + if (parentIndex !== undefined) { + let parentNode = nodeHierarchy.nodes[parentIndex]; + let parentMatrix = GetNodeTransformation (nodeHierarchy, parentNode); + matrix = matrix.MultiplyMatrix (parentMatrix); + } + } + + node.matrix = matrix; + return matrix; + } - if (!meshMatrix.IsValid ()) { - return; - } + function ApplyMeshTransformation (model, currentMeshIndex, meshMatrix, nodeHierarchy, node) + { + function GetNodePivotPoint (node) + { + if (node === null) { + return [0.0, 0.0, 0.0]; + } + return node.pivot; + } - let nodeMatrix = meshMatrix; - if (node !== null) { - nodeMatrix = GetNodeTransformation (nodeHierarchy, node); - } + if (!meshMatrix.IsValid ()) { + return; + } - let determinant = meshMatrix.Determinant (); - if (OV.IsNegative (determinant)) { - // Mirror by x coordinates - let scaleMatrix = new OV.Matrix ().CreateScale (-1.0, 1.0, 1.0); - meshMatrix = scaleMatrix.MultiplyMatrix (meshMatrix); - } + let nodeMatrix = meshMatrix; + if (node !== null) { + nodeMatrix = GetNodeTransformation (nodeHierarchy, node); + } - let invMeshMatrix = meshMatrix.Invert (); - if (invMeshMatrix === null) { - return; - } - - let pivotPoint = GetNodePivotPoint (node); - let pivotMatrix = new OV.Matrix ().CreateTranslation (-pivotPoint[0], -pivotPoint[1], -pivotPoint[2]); + let determinant = meshMatrix.Determinant (); + if (OV.IsNegative (determinant)) { + // Mirror by x coordinates + let scaleMatrix = new OV.Matrix ().CreateScale (-1.0, 1.0, 1.0); + meshMatrix = scaleMatrix.MultiplyMatrix (meshMatrix); + } - let matrix = nodeMatrix.Clone (); - matrix = pivotMatrix.MultiplyMatrix (matrix); - matrix = invMeshMatrix.MultiplyMatrix (matrix); + let invMeshMatrix = meshMatrix.Invert (); + if (invMeshMatrix === null) { + return; + } + + let pivotPoint = GetNodePivotPoint (node); + let pivotMatrix = new OV.Matrix ().CreateTranslation (-pivotPoint[0], -pivotPoint[1], -pivotPoint[2]); - let transformation = new OV.Transformation (matrix); - let mesh = model.GetMesh (currentMeshIndex); - OV.TransformMesh (mesh, transformation); - } + let matrix = nodeMatrix.Clone (); + matrix = pivotMatrix.MultiplyMatrix (matrix); + matrix = invMeshMatrix.MultiplyMatrix (matrix); - function AddDuplicatedMesh (model, meshIndex, toIndex) - { - let mesh = model.GetMesh (meshIndex); - let clonedMesh = OV.CloneMesh (mesh); - let clonedMeshIndex = model.AddMeshToIndex (clonedMesh, toIndex); - return clonedMeshIndex; - } + let transformation = new OV.Transformation (matrix); + let mesh = model.GetMesh (currentMeshIndex); + OV.TransformMesh (mesh, transformation); + } - let newToOldMeshIndexOffset = 0; - for (let meshIndex = 0; meshIndex < this.model.MeshCount (); meshIndex++) { - let currentMeshIndex = meshIndex; - let originalMeshIndex = currentMeshIndex - newToOldMeshIndexOffset; - let meshTransformation = this.meshTransformations[originalMeshIndex]; - let meshNodes = nodeHierarchy.meshIndexToNodes[originalMeshIndex]; - if (meshNodes === undefined) { - ApplyMeshTransformation (this.model, currentMeshIndex, meshTransformation, nodeHierarchy, null); - } else { - for (let nodeIndex = 0; nodeIndex < meshNodes.length; nodeIndex++) { - let currentNode = meshNodes[nodeIndex]; - let transformedMeshIndex = currentMeshIndex; - if (nodeIndex > 0) { - transformedMeshIndex = AddDuplicatedMesh (this.model, currentMeshIndex, currentMeshIndex + nodeIndex); - newToOldMeshIndexOffset += 1; - meshIndex += 1; - } - ApplyMeshTransformation (this.model, transformedMeshIndex, meshTransformation, nodeHierarchy, currentNode); - } - } - } - } + function AddDuplicatedMesh (model, meshIndex, toIndex) + { + let mesh = model.GetMesh (meshIndex); + let clonedMesh = OV.CloneMesh (mesh); + let clonedMeshIndex = model.AddMeshToIndex (clonedMesh, toIndex); + return clonedMeshIndex; + } - ReadObjectNodeChunk (nodeHierarchy, reader, length) - { - function ReadTrackVector (obj, reader, type) - { - let result = []; - reader.Skip (10); - - let keyNum = reader.ReadInteger32 (); - for (let i = 0; i < keyNum; i++) { - reader.ReadInteger32 (); - let flags = reader.ReadUnsignedInteger16 (); - if (flags !== 0) { - reader.ReadFloat32 (); - } - - let current = null; - if (type === OV.CHUNK3DS.OBJECT_ROTATION) { - let tmp = reader.ReadFloat32 (); - current = obj.ReadVector (reader); - current[3] = tmp; - } else { - current = obj.ReadVector (reader); - } - result.push (current); - } + let newToOldMeshIndexOffset = 0; + for (let meshIndex = 0; meshIndex < this.model.MeshCount (); meshIndex++) { + let currentMeshIndex = meshIndex; + let originalMeshIndex = currentMeshIndex - newToOldMeshIndexOffset; + let meshTransformation = this.meshTransformations[originalMeshIndex]; + let meshNodes = nodeHierarchy.meshIndexToNodes[originalMeshIndex]; + if (meshNodes === undefined) { + ApplyMeshTransformation (this.model, currentMeshIndex, meshTransformation, nodeHierarchy, null); + } else { + for (let nodeIndex = 0; nodeIndex < meshNodes.length; nodeIndex++) { + let currentNode = meshNodes[nodeIndex]; + let transformedMeshIndex = currentMeshIndex; + if (nodeIndex > 0) { + transformedMeshIndex = AddDuplicatedMesh (this.model, currentMeshIndex, currentMeshIndex + nodeIndex); + newToOldMeshIndexOffset += 1; + meshIndex += 1; + } + ApplyMeshTransformation (this.model, transformedMeshIndex, meshTransformation, nodeHierarchy, currentNode); + } + } + } + } - return result; - } - - let objectNode = { - name : '', - instanceName : '', - nodeId : -1, - flags : -1, - userId : -1, - pivot : [0.0, 0.0, 0.0], - positions : [], - rotations : [], - scales : [], - matrix : null - }; - - let obj = this; - let endByte = this.GetChunkEnd (reader, length); - this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { - if (chunkId === OV.CHUNK3DS.OBJECT_HIERARCHY) { - objectNode.name = obj.ReadName (reader); - objectNode.flags = reader.ReadUnsignedInteger32 (); - objectNode.userId = reader.ReadUnsignedInteger16 (); - } else if (chunkId === OV.CHUNK3DS.OBJECT_INSTANCE_NAME) { - objectNode.instanceName = obj.ReadName (reader); - } else if (chunkId === OV.CHUNK3DS.OBJECT_PIVOT) { - objectNode.pivot = obj.ReadVector (reader); - } else if (chunkId === OV.CHUNK3DS.OBJECT_POSITION) { - objectNode.positions = ReadTrackVector (obj, reader, OV.CHUNK3DS.OBJECT_POSITION); - } else if (chunkId === OV.CHUNK3DS.OBJECT_ROTATION) { - objectNode.rotations = ReadTrackVector (obj, reader, OV.CHUNK3DS.OBJECT_ROTATION); - } else if (chunkId === OV.CHUNK3DS.OBJECT_SCALE) { - objectNode.scales = ReadTrackVector (obj, reader, OV.CHUNK3DS.OBJECT_SCALE); - } else if (chunkId === OV.CHUNK3DS.OBJECT_ID) { - objectNode.nodeId = reader.ReadUnsignedInteger16 (); - } else { - obj.SkipChunk (reader, chunkLength); - } - }); + ReadObjectNodeChunk (nodeHierarchy, reader, length) + { + function ReadTrackVector (obj, reader, type) + { + let result = []; + reader.Skip (10); + + let keyNum = reader.ReadInteger32 (); + for (let i = 0; i < keyNum; i++) { + reader.ReadInteger32 (); + let flags = reader.ReadUnsignedInteger16 (); + if (flags !== 0) { + reader.ReadFloat32 (); + } + + let current = null; + if (type === OV.CHUNK3DS.OBJECT_ROTATION) { + let tmp = reader.ReadFloat32 (); + current = obj.ReadVector (reader); + current[3] = tmp; + } else { + current = obj.ReadVector (reader); + } + result.push (current); + } - let nodeIndex = nodeHierarchy.nodes.length; - nodeHierarchy.nodes.push (objectNode); - nodeHierarchy.idToIndex[objectNode.nodeId] = nodeIndex; + return result; + } + + let objectNode = { + name : '', + instanceName : '', + nodeId : -1, + flags : -1, + userId : -1, + pivot : [0.0, 0.0, 0.0], + positions : [], + rotations : [], + scales : [], + matrix : null + }; + + let obj = this; + let endByte = this.GetChunkEnd (reader, length); + this.ReadChunks (reader, endByte, function (chunkId, chunkLength) { + if (chunkId === OV.CHUNK3DS.OBJECT_HIERARCHY) { + objectNode.name = obj.ReadName (reader); + objectNode.flags = reader.ReadUnsignedInteger32 (); + objectNode.userId = reader.ReadUnsignedInteger16 (); + } else if (chunkId === OV.CHUNK3DS.OBJECT_INSTANCE_NAME) { + objectNode.instanceName = obj.ReadName (reader); + } else if (chunkId === OV.CHUNK3DS.OBJECT_PIVOT) { + objectNode.pivot = obj.ReadVector (reader); + } else if (chunkId === OV.CHUNK3DS.OBJECT_POSITION) { + objectNode.positions = ReadTrackVector (obj, reader, OV.CHUNK3DS.OBJECT_POSITION); + } else if (chunkId === OV.CHUNK3DS.OBJECT_ROTATION) { + objectNode.rotations = ReadTrackVector (obj, reader, OV.CHUNK3DS.OBJECT_ROTATION); + } else if (chunkId === OV.CHUNK3DS.OBJECT_SCALE) { + objectNode.scales = ReadTrackVector (obj, reader, OV.CHUNK3DS.OBJECT_SCALE); + } else if (chunkId === OV.CHUNK3DS.OBJECT_ID) { + objectNode.nodeId = reader.ReadUnsignedInteger16 (); + } else { + obj.SkipChunk (reader, chunkLength); + } + }); - let meshIndex = this.meshNameToIndex[objectNode.name]; - if (meshIndex !== undefined) { - let meshNodes = nodeHierarchy.meshIndexToNodes[meshIndex]; - if (meshNodes === undefined) { - nodeHierarchy.meshIndexToNodes[meshIndex] = []; - } - nodeHierarchy.meshIndexToNodes[meshIndex].push (objectNode); - } - } + let nodeIndex = nodeHierarchy.nodes.length; + nodeHierarchy.nodes.push (objectNode); + nodeHierarchy.idToIndex[objectNode.nodeId] = nodeIndex; - ReadName (reader) - { - let name = ''; - let char = 0; - let count = 0; - while (count < 64) { - char = reader.ReadCharacter8 (); - if (char === 0) { - break; - } - name = name + String.fromCharCode (char); - count = count + 1; - } - return name; - } + let meshIndex = this.meshNameToIndex[objectNode.name]; + if (meshIndex !== undefined) { + let meshNodes = nodeHierarchy.meshIndexToNodes[meshIndex]; + if (meshNodes === undefined) { + nodeHierarchy.meshIndexToNodes[meshIndex] = []; + } + nodeHierarchy.meshIndexToNodes[meshIndex].push (objectNode); + } + } - ReadVector (reader) - { - let result = [ - reader.ReadFloat32 (), - reader.ReadFloat32 (), - reader.ReadFloat32 () - ]; - return result; - } + ReadName (reader) + { + let name = ''; + let char = 0; + let count = 0; + while (count < 64) { + char = reader.ReadCharacter8 (); + if (char === 0) { + break; + } + name = name + String.fromCharCode (char); + count = count + 1; + } + return name; + } - ReadChunks (reader, endByte, onChunk) - { - while (reader.GetPosition () <= endByte - 6) { - let chunkId = reader.ReadUnsignedInteger16 (); - let chunkLength = reader.ReadUnsignedInteger32 (); - onChunk (chunkId, chunkLength); - } - } - - GetChunkEnd (reader, length) - { - return reader.GetPosition () + length - 6; - } + ReadVector (reader) + { + let result = [ + reader.ReadFloat32 (), + reader.ReadFloat32 (), + reader.ReadFloat32 () + ]; + return result; + } - SkipChunk (reader, length) - { - reader.Skip (length - 6); - } + ReadChunks (reader, endByte, onChunk) + { + while (reader.GetPosition () <= endByte - 6) { + let chunkId = reader.ReadUnsignedInteger16 (); + let chunkLength = reader.ReadUnsignedInteger32 (); + onChunk (chunkId, chunkLength); + } + } + + GetChunkEnd (reader, length) + { + return reader.GetPosition () + length - 6; + } + + SkipChunk (reader, length) + { + reader.Skip (length - 6); + } }; diff --git a/source/import/importerbase.js b/source/import/importerbase.js index 37475ae..39756a1 100644 --- a/source/import/importerbase.js +++ b/source/import/importerbase.js @@ -8,9 +8,9 @@ OV.ImporterBase = class this.error = null; this.message = null; } - - Import (content, extension, callbacks) - { + + Import (content, extension, callbacks) + { this.extension = extension; this.callbacks = callbacks; this.model = new OV.Model (); @@ -18,11 +18,11 @@ OV.ImporterBase = class this.message = null; let obj = this; - obj.ResetState (); + obj.ResetState (); obj.ImportContent (content, function () { obj.CreateResult (callbacks); }); - } + } CreateResult (callbacks) { @@ -46,15 +46,15 @@ OV.ImporterBase = class } - CanImportExtension (extension) - { - return false; - } + CanImportExtension (extension) + { + return false; + } - GetKnownFileFormats () - { - return {}; - } + GetKnownFileFormats () + { + return {}; + } GetUpDirection () { diff --git a/source/import/importergltf.js b/source/import/importergltf.js index ab4d07a..e2ed638 100644 --- a/source/import/importergltf.js +++ b/source/import/importergltf.js @@ -1,40 +1,40 @@ OV.GltfComponentType = { - BYTE : 5120, - UNSIGNED_BYTE : 5121, - SHORT : 5122, - UNSIGNED_SHORT : 5123, - UNSIGNED_INT : 5125, - FLOAT : 5126 + BYTE : 5120, + UNSIGNED_BYTE : 5121, + SHORT : 5122, + UNSIGNED_SHORT : 5123, + UNSIGNED_INT : 5125, + FLOAT : 5126 }; OV.GltfDataType = { - SCALAR : 0, - VEC2 : 1, - VEC3 : 2, - VEC4 : 3, - MAT2 : 4, - MAT3 : 5, + SCALAR : 0, + VEC2 : 1, + VEC3 : 2, + VEC4 : 3, + MAT2 : 4, + MAT3 : 5, MAT4 : 6 }; OV.GltfRenderMode = { - POINTS : 0, - LINES : 1, - LINE_LOOP : 2, - LINE_STRIP : 3, - TRIANGLES : 4, - TRIANGLE_STRIP : 5, + POINTS : 0, + LINES : 1, + LINE_LOOP : 2, + LINE_STRIP : 3, + TRIANGLES : 4, + TRIANGLE_STRIP : 5, TRIANGLE_FAN : 6 }; OV.GltfConstants = { - GLTF_STRING : 0x46546C67, - JSON_CHUNK_TYPE : 0x4E4F534A, - BINARY_CHUNK_TYPE : 0x004E4942 + GLTF_STRING : 0x46546C67, + JSON_CHUNK_TYPE : 0x4E4F534A, + BINARY_CHUNK_TYPE : 0x004E4942 }; OV.GltfNodeTree = class @@ -345,50 +345,50 @@ OV.GltfExtensions = class OV.ImporterGltf = class extends OV.ImporterBase { - constructor () - { - super (); + constructor () + { + super (); this.bufferContents = null; this.gltfExtensions = null; this.imageIndexToTextureParams = null; - } - - ResetState () - { + } + + ResetState () + { this.bufferContents = []; this.gltfExtensions = new OV.GltfExtensions (); this.imageIndexToTextureParams = {}; - } + } - CanImportExtension (extension) - { - return extension === 'gltf' || extension === 'glb'; - } + CanImportExtension (extension) + { + return extension === 'gltf' || extension === 'glb'; + } - GetKnownFileFormats () - { - return { - 'gltf' : OV.FileFormat.Text, + GetKnownFileFormats () + { + return { + 'gltf' : OV.FileFormat.Text, 'glb' : OV.FileFormat.Binary, 'bin' : OV.FileFormat.Binary - }; - } + }; + } GetUpDirection () { return OV.Direction.Y; } - ImportContent (fileContent, onFinish) - { + ImportContent (fileContent, onFinish) + { if (this.extension === 'gltf') { this.ProcessGltf (fileContent); } else if (this.extension === 'glb') { this.ProcessBinaryGltf (fileContent); } onFinish (); - } + } ProcessGltf (fileContent) { diff --git a/source/import/importerobj.js b/source/import/importerobj.js index 5dadb7a..12a65b5 100644 --- a/source/import/importerobj.js +++ b/source/import/importerobj.js @@ -1,388 +1,388 @@ OV.ImporterObj = class extends OV.ImporterBase { - constructor () - { - super (); + constructor () + { + super (); - this.globalVertices = null; - this.globalNormals = null; - this.globalUvs = null; - - this.currentMesh = null; - this.currentMeshData = null; - this.currentMaterial = null; - this.currentMaterialIndex = null; - - this.meshNameToMeshData = null; - this.materialNameToIndex = null; - } - - ResetState () - { - this.globalVertices = []; - this.globalNormals = []; - this.globalUvs = []; + this.globalVertices = null; + this.globalNormals = null; + this.globalUvs = null; + + this.currentMesh = null; + this.currentMeshData = null; + this.currentMaterial = null; + this.currentMaterialIndex = null; + + this.meshNameToMeshData = null; + this.materialNameToIndex = null; + } + + ResetState () + { + this.globalVertices = []; + this.globalNormals = []; + this.globalUvs = []; - this.currentMesh = null; - this.currentMeshData = null; - this.currentMaterial = null; - this.currentMaterialIndex = null; + this.currentMesh = null; + this.currentMeshData = null; + this.currentMaterial = null; + this.currentMaterialIndex = null; - this.meshNameToMeshData = {}; - this.materialNameToIndex = {}; - } + this.meshNameToMeshData = {}; + this.materialNameToIndex = {}; + } - CanImportExtension (extension) - { - return extension === 'obj'; - } - - GetKnownFileFormats () - { - return { - 'obj' : OV.FileFormat.Text, - 'mtl' : OV.FileFormat.Text - }; - } + CanImportExtension (extension) + { + return extension === 'obj'; + } + + GetKnownFileFormats () + { + return { + 'obj' : OV.FileFormat.Text, + 'mtl' : OV.FileFormat.Text + }; + } - GetUpDirection () + GetUpDirection () { return OV.Direction.Y; } - - ImportContent (fileContent, onFinish) - { - let obj = this; - OV.ReadLines (fileContent, function (line) { - if (!obj.IsError ()) { - obj.ProcessLine (line); - } - }); - onFinish (); - } - - ProcessLine (line) - { - if (line[0] === '#') { - return; - } + + ImportContent (fileContent, onFinish) + { + let obj = this; + OV.ReadLines (fileContent, function (line) { + if (!obj.IsError ()) { + obj.ProcessLine (line); + } + }); + onFinish (); + } + + ProcessLine (line) + { + if (line[0] === '#') { + return; + } - let parameters = OV.ParametersFromLine (line, '#'); - if (parameters.length === 0) { - return; - } - - let keyword = parameters[0].toLowerCase (); - parameters.shift (); + let parameters = OV.ParametersFromLine (line, '#'); + if (parameters.length === 0) { + return; + } + + let keyword = parameters[0].toLowerCase (); + parameters.shift (); - if (this.ProcessMeshParameter (keyword, parameters, line)) { - return; - } + if (this.ProcessMeshParameter (keyword, parameters, line)) { + return; + } - if (this.ProcessMaterialParameter (keyword, parameters, line)) { - return; - } - } - - AddNewMesh (name) - { - let meshData = this.meshNameToMeshData[name]; - if (meshData === undefined) { - let mesh = new OV.Mesh (); - if (name !== null) { - mesh.SetName (name); - } - this.model.AddMesh (mesh); - meshData = { - mesh : mesh, - data : { - globalToCurrentVertices : {}, - globalToCurrentNormals : {}, - globalToCurrentUvs : {} - } - }; - this.meshNameToMeshData[name] = meshData; - } - this.currentMesh = meshData.mesh; - this.currentMeshData = meshData.data; - } + if (this.ProcessMaterialParameter (keyword, parameters, line)) { + return; + } + } + + AddNewMesh (name) + { + let meshData = this.meshNameToMeshData[name]; + if (meshData === undefined) { + let mesh = new OV.Mesh (); + if (name !== null) { + mesh.SetName (name); + } + this.model.AddMesh (mesh); + meshData = { + mesh : mesh, + data : { + globalToCurrentVertices : {}, + globalToCurrentNormals : {}, + globalToCurrentUvs : {} + } + }; + this.meshNameToMeshData[name] = meshData; + } + this.currentMesh = meshData.mesh; + this.currentMeshData = meshData.data; + } - ProcessMeshParameter (keyword, parameters, line) - { - if (keyword === 'g' || keyword === 'o') { - if (parameters.length === 0) { - return true; - } - let name = OV.NameFromLine (line, keyword.length, '#'); - this.AddNewMesh (name); - return true; - } else if (keyword === 'v') { - if (parameters.length < 3) { - return true; - } - this.globalVertices.push (new OV.Coord3D ( - parseFloat (parameters[0]), - parseFloat (parameters[1]), - parseFloat (parameters[2]) - )); - return true; - } else if (keyword === 'vn') { - if (parameters.length < 3) { - return true; - } - this.globalNormals.push (new OV.Coord3D ( - parseFloat (parameters[0]), - parseFloat (parameters[1]), - parseFloat (parameters[2]) - )); - return true; - } else if (keyword === 'vt') { - if (parameters.length < 2) { - return true; - } - this.globalUvs.push (new OV.Coord2D ( - parseFloat (parameters[0]), - parseFloat (parameters[1]) - )); - return true; - } else if (keyword === 'f') { - if (parameters.length < 3) { - return true; - } - this.ProcessFace (parameters); - return true; - } + ProcessMeshParameter (keyword, parameters, line) + { + if (keyword === 'g' || keyword === 'o') { + if (parameters.length === 0) { + return true; + } + let name = OV.NameFromLine (line, keyword.length, '#'); + this.AddNewMesh (name); + return true; + } else if (keyword === 'v') { + if (parameters.length < 3) { + return true; + } + this.globalVertices.push (new OV.Coord3D ( + parseFloat (parameters[0]), + parseFloat (parameters[1]), + parseFloat (parameters[2]) + )); + return true; + } else if (keyword === 'vn') { + if (parameters.length < 3) { + return true; + } + this.globalNormals.push (new OV.Coord3D ( + parseFloat (parameters[0]), + parseFloat (parameters[1]), + parseFloat (parameters[2]) + )); + return true; + } else if (keyword === 'vt') { + if (parameters.length < 2) { + return true; + } + this.globalUvs.push (new OV.Coord2D ( + parseFloat (parameters[0]), + parseFloat (parameters[1]) + )); + return true; + } else if (keyword === 'f') { + if (parameters.length < 3) { + return true; + } + this.ProcessFace (parameters); + return true; + } - return false; - } + return false; + } - ProcessMaterialParameter (keyword, parameters, line) - { - function CreateColor (parameters) - { - return new OV.Color ( - parseInt (parseFloat (parameters[0] * 255.0), 10), - parseInt (parseFloat (parameters[1] * 255.0), 10), - parseInt (parseFloat (parameters[2] * 255.0), 10) - ); - } + ProcessMaterialParameter (keyword, parameters, line) + { + function CreateColor (parameters) + { + return new OV.Color ( + parseInt (parseFloat (parameters[0] * 255.0), 10), + parseInt (parseFloat (parameters[1] * 255.0), 10), + parseInt (parseFloat (parameters[2] * 255.0), 10) + ); + } - function CreateTexture (keyword, line, callbacks) - { - let texture = new OV.TextureMap (); - let textureName = OV.NameFromLine (line, keyword.length, '#'); - let textureBuffer = callbacks.getTextureBuffer (textureName); - texture.name = textureName; - if (textureBuffer !== null) { - texture.url = textureBuffer.url; - texture.buffer = textureBuffer.buffer; - } - return texture; - } + function CreateTexture (keyword, line, callbacks) + { + let texture = new OV.TextureMap (); + let textureName = OV.NameFromLine (line, keyword.length, '#'); + let textureBuffer = callbacks.getTextureBuffer (textureName); + texture.name = textureName; + if (textureBuffer !== null) { + texture.url = textureBuffer.url; + texture.buffer = textureBuffer.buffer; + } + return texture; + } - let obj = this; - if (keyword === 'newmtl') { - if (parameters.length === 0) { - return true; - } - - let material = new OV.Material (); - let materialName = OV.NameFromLine (line, keyword.length, '#'); - let materialIndex = this.model.AddMaterial (material); - material.name = materialName; - this.currentMaterial = material; - this.materialNameToIndex[materialName] = materialIndex; - return true; - } else if (keyword === 'usemtl') { - if (parameters.length === 0) { - return true; - } - - let materialName = OV.NameFromLine (line, keyword.length, '#'); - let materialIndex = this.materialNameToIndex[materialName]; - if (materialIndex !== undefined) { - this.currentMaterialIndex = materialIndex; - } - return true; - } else if (keyword === 'mtllib') { - if (parameters.length === 0) { - return true; - } - let fileName = OV.NameFromLine (line, keyword.length, '#'); - let fileBuffer = this.callbacks.getFileBuffer (fileName); - if (fileBuffer !== null) { - OV.ReadLines (fileBuffer, function (line) { - if (!obj.IsError ()) { - obj.ProcessLine (line); - } - }); - } - return true; - } else if (keyword === 'map_kd') { - if (this.currentMaterial === null || parameters.length === 0) { - return true; - } - 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 (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 (keyword, line, this.callbacks); - return true; - } else if (keyword === 'ka') { - if (this.currentMaterial === null || parameters.length < 3) { - return true; - } - this.currentMaterial.ambient = CreateColor (parameters); - return true; - } else if (keyword === 'kd') { - if (this.currentMaterial === null || parameters.length < 3) { - return true; - } - this.currentMaterial.diffuse = CreateColor (parameters); - return true; - } else if (keyword === 'ks') { - if (this.currentMaterial === null || parameters.length < 3) { - return true; - } - this.currentMaterial.specular = CreateColor (parameters); - return true; - } else if (keyword === 'ns') { - if (this.currentMaterial === null || parameters.length < 1) { - return true; - } - this.currentMaterial.shininess = parseFloat (parameters[0]) / 100.0; - return true; - } else if (keyword === 'tr') { - if (this.currentMaterial === null || parameters.length < 1) { - return true; - } - this.currentMaterial.opacity = 1.0 - parseFloat (parameters[0]); - this.currentMaterial.transparent = OV.IsLower (this.currentMaterial.opacity, 1.0); - return true; - } else if (keyword === 'd') { - if (this.currentMaterial === null || parameters.length < 1) { - return true; - } - this.currentMaterial.opacity = parseFloat (parameters[0]); - this.currentMaterial.transparent = OV.IsLower (this.currentMaterial.opacity, 1.0); - return true; - } + let obj = this; + if (keyword === 'newmtl') { + if (parameters.length === 0) { + return true; + } + + let material = new OV.Material (); + let materialName = OV.NameFromLine (line, keyword.length, '#'); + let materialIndex = this.model.AddMaterial (material); + material.name = materialName; + this.currentMaterial = material; + this.materialNameToIndex[materialName] = materialIndex; + return true; + } else if (keyword === 'usemtl') { + if (parameters.length === 0) { + return true; + } + + let materialName = OV.NameFromLine (line, keyword.length, '#'); + let materialIndex = this.materialNameToIndex[materialName]; + if (materialIndex !== undefined) { + this.currentMaterialIndex = materialIndex; + } + return true; + } else if (keyword === 'mtllib') { + if (parameters.length === 0) { + return true; + } + let fileName = OV.NameFromLine (line, keyword.length, '#'); + let fileBuffer = this.callbacks.getFileBuffer (fileName); + if (fileBuffer !== null) { + OV.ReadLines (fileBuffer, function (line) { + if (!obj.IsError ()) { + obj.ProcessLine (line); + } + }); + } + return true; + } else if (keyword === 'map_kd') { + if (this.currentMaterial === null || parameters.length === 0) { + return true; + } + 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 (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 (keyword, line, this.callbacks); + return true; + } else if (keyword === 'ka') { + if (this.currentMaterial === null || parameters.length < 3) { + return true; + } + this.currentMaterial.ambient = CreateColor (parameters); + return true; + } else if (keyword === 'kd') { + if (this.currentMaterial === null || parameters.length < 3) { + return true; + } + this.currentMaterial.diffuse = CreateColor (parameters); + return true; + } else if (keyword === 'ks') { + if (this.currentMaterial === null || parameters.length < 3) { + return true; + } + this.currentMaterial.specular = CreateColor (parameters); + return true; + } else if (keyword === 'ns') { + if (this.currentMaterial === null || parameters.length < 1) { + return true; + } + this.currentMaterial.shininess = parseFloat (parameters[0]) / 100.0; + return true; + } else if (keyword === 'tr') { + if (this.currentMaterial === null || parameters.length < 1) { + return true; + } + this.currentMaterial.opacity = 1.0 - parseFloat (parameters[0]); + this.currentMaterial.transparent = OV.IsLower (this.currentMaterial.opacity, 1.0); + return true; + } else if (keyword === 'd') { + if (this.currentMaterial === null || parameters.length < 1) { + return true; + } + this.currentMaterial.opacity = parseFloat (parameters[0]); + this.currentMaterial.transparent = OV.IsLower (this.currentMaterial.opacity, 1.0); + return true; + } - return false; - } + return false; + } - ProcessFace (parameters) - { - function GetRelativeIndex (index, count) - { - if (index > 0) { - return index - 1; - } else { - return count + index; - } - } - - function GetLocalIndex (globalValueArray, globalToCurrentIndices, globalIndex, valueAdderFunc) - { - if (isNaN (globalIndex) || globalIndex < 0 || globalIndex >= globalValueArray.length) { - return null; - } - let result = globalToCurrentIndices[globalIndex]; - if (result === undefined) { - let globalValue = globalValueArray[globalIndex]; - if (globalValue === undefined) { - return null; - } - result = valueAdderFunc (globalValue); - globalToCurrentIndices[globalIndex] = result; - } - return result; - } - - function GetLocalVertexIndex (obj, mesh, globalIndex) - { - return GetLocalIndex (obj.globalVertices, obj.currentMeshData.globalToCurrentVertices, globalIndex, function (val) { - return mesh.AddVertex (new OV.Coord3D (val.x, val.y, val.z)); - }); - } - - function GetLocalNormalIndex (obj, mesh, globalIndex) - { - return GetLocalIndex (obj.globalNormals, obj.currentMeshData.globalToCurrentNormals, globalIndex, function (val) { - return mesh.AddNormal (new OV.Coord3D (val.x, val.y, val.z)); - }); - } - - function GetLocalUVIndex (obj, mesh, globalIndex) - { - return GetLocalIndex (obj.globalUvs, obj.currentMeshData.globalToCurrentUvs, globalIndex, function (val) { - return mesh.AddTextureUV (new OV.Coord2D (val.x, val.y)); - }); - } - - let vertices = []; - let normals = []; - let uvs = []; - - for (let i = 0; i < parameters.length; i++) { - let vertexParams = parameters[i].split ('/'); - vertices.push (GetRelativeIndex (parseInt (vertexParams[0], 10), this.globalVertices.length)); - if (vertexParams.length > 1 && vertexParams[1].length > 0) { - uvs.push (GetRelativeIndex (parseInt (vertexParams[1], 10), this.globalUvs.length)); - } - if (vertexParams.length > 2 && vertexParams[2].length > 0) { - normals.push (GetRelativeIndex (parseInt (vertexParams[2], 10), this.globalNormals.length)); - } - } - - if (this.currentMesh === null) { - this.AddNewMesh (''); - } - - for (let i = 0; i < vertices.length - 2; i++) { - let v0 = GetLocalVertexIndex (this, this.currentMesh, vertices[0]); - let v1 = GetLocalVertexIndex (this, this.currentMesh, vertices[i + 1]); - let v2 = GetLocalVertexIndex (this, this.currentMesh, vertices[i + 2]); - if (v0 === null || v1 === null || v2 === null) { - this.SetError (); - this.SetMessage ('Invalid vertex index.'); - break; - } - let triangle = new OV.Triangle (v0, v1, v2); - if (normals.length === vertices.length) { - let n0 = GetLocalNormalIndex (this, this.currentMesh, normals[0]); - let n1 = GetLocalNormalIndex (this, this.currentMesh, normals[i + 1]); - let n2 = GetLocalNormalIndex (this, this.currentMesh, normals[i + 2]); - if (n0 === null || n1 === null || n2 === null) { - this.SetError (); - this.SetMessage ('Invalid normal index.'); - break; - } - triangle.SetNormals (n0, n1, n2); - } - if (uvs.length === vertices.length) { - let u0 = GetLocalUVIndex (this, this.currentMesh, uvs[0]); - let u1 = GetLocalUVIndex (this, this.currentMesh, uvs[i + 1]); - let u2 = GetLocalUVIndex (this, this.currentMesh, uvs[i + 2]); - if (u0 === null || u1 === null || u2 === null) { - this.SetError (); - this.SetMessage ('Invalid uv index.'); - break; - } - triangle.SetTextureUVs (u0, u1, u2); - } - if (this.currentMaterialIndex !== null) { - triangle.mat = this.currentMaterialIndex; - } - this.currentMesh.AddTriangle (triangle); - } - } + ProcessFace (parameters) + { + function GetRelativeIndex (index, count) + { + if (index > 0) { + return index - 1; + } else { + return count + index; + } + } + + function GetLocalIndex (globalValueArray, globalToCurrentIndices, globalIndex, valueAdderFunc) + { + if (isNaN (globalIndex) || globalIndex < 0 || globalIndex >= globalValueArray.length) { + return null; + } + let result = globalToCurrentIndices[globalIndex]; + if (result === undefined) { + let globalValue = globalValueArray[globalIndex]; + if (globalValue === undefined) { + return null; + } + result = valueAdderFunc (globalValue); + globalToCurrentIndices[globalIndex] = result; + } + return result; + } + + function GetLocalVertexIndex (obj, mesh, globalIndex) + { + return GetLocalIndex (obj.globalVertices, obj.currentMeshData.globalToCurrentVertices, globalIndex, function (val) { + return mesh.AddVertex (new OV.Coord3D (val.x, val.y, val.z)); + }); + } + + function GetLocalNormalIndex (obj, mesh, globalIndex) + { + return GetLocalIndex (obj.globalNormals, obj.currentMeshData.globalToCurrentNormals, globalIndex, function (val) { + return mesh.AddNormal (new OV.Coord3D (val.x, val.y, val.z)); + }); + } + + function GetLocalUVIndex (obj, mesh, globalIndex) + { + return GetLocalIndex (obj.globalUvs, obj.currentMeshData.globalToCurrentUvs, globalIndex, function (val) { + return mesh.AddTextureUV (new OV.Coord2D (val.x, val.y)); + }); + } + + let vertices = []; + let normals = []; + let uvs = []; + + for (let i = 0; i < parameters.length; i++) { + let vertexParams = parameters[i].split ('/'); + vertices.push (GetRelativeIndex (parseInt (vertexParams[0], 10), this.globalVertices.length)); + if (vertexParams.length > 1 && vertexParams[1].length > 0) { + uvs.push (GetRelativeIndex (parseInt (vertexParams[1], 10), this.globalUvs.length)); + } + if (vertexParams.length > 2 && vertexParams[2].length > 0) { + normals.push (GetRelativeIndex (parseInt (vertexParams[2], 10), this.globalNormals.length)); + } + } + + if (this.currentMesh === null) { + this.AddNewMesh (''); + } + + for (let i = 0; i < vertices.length - 2; i++) { + let v0 = GetLocalVertexIndex (this, this.currentMesh, vertices[0]); + let v1 = GetLocalVertexIndex (this, this.currentMesh, vertices[i + 1]); + let v2 = GetLocalVertexIndex (this, this.currentMesh, vertices[i + 2]); + if (v0 === null || v1 === null || v2 === null) { + this.SetError (); + this.SetMessage ('Invalid vertex index.'); + break; + } + let triangle = new OV.Triangle (v0, v1, v2); + if (normals.length === vertices.length) { + let n0 = GetLocalNormalIndex (this, this.currentMesh, normals[0]); + let n1 = GetLocalNormalIndex (this, this.currentMesh, normals[i + 1]); + let n2 = GetLocalNormalIndex (this, this.currentMesh, normals[i + 2]); + if (n0 === null || n1 === null || n2 === null) { + this.SetError (); + this.SetMessage ('Invalid normal index.'); + break; + } + triangle.SetNormals (n0, n1, n2); + } + if (uvs.length === vertices.length) { + let u0 = GetLocalUVIndex (this, this.currentMesh, uvs[0]); + let u1 = GetLocalUVIndex (this, this.currentMesh, uvs[i + 1]); + let u2 = GetLocalUVIndex (this, this.currentMesh, uvs[i + 2]); + if (u0 === null || u1 === null || u2 === null) { + this.SetError (); + this.SetMessage ('Invalid uv index.'); + break; + } + triangle.SetTextureUVs (u0, u1, u2); + } + if (this.currentMaterialIndex !== null) { + triangle.mat = this.currentMaterialIndex; + } + this.currentMesh.AddTriangle (triangle); + } + } }; diff --git a/source/import/importeroff.js b/source/import/importeroff.js index f0dc2fb..2cbadb9 100644 --- a/source/import/importeroff.js +++ b/source/import/importeroff.js @@ -7,9 +7,9 @@ OV.ImporterOff = class extends OV.ImporterBase this.mesh = null; this.status = null; } - - ResetState () - { + + ResetState () + { this.mesh = new OV.Mesh (); this.model.AddMesh (this.mesh); this.status = { @@ -18,47 +18,47 @@ OV.ImporterOff = class extends OV.ImporterBase foundVertex : 0, foundFace : 0 }; - } + } - CanImportExtension (extension) - { - return extension === 'off'; - } + CanImportExtension (extension) + { + return extension === 'off'; + } GetKnownFileFormats () - { - return { - 'off' : OV.FileFormat.Text - }; - } - + { + return { + 'off' : OV.FileFormat.Text + }; + } + GetUpDirection () { return OV.Direction.Y; } - ImportContent (fileContent, onFinish) - { + ImportContent (fileContent, onFinish) + { let obj = this; - OV.ReadLines (fileContent, function (line) { - if (!obj.IsError ()) { - obj.ProcessLine (line); - } - }); + OV.ReadLines (fileContent, function (line) { + if (!obj.IsError ()) { + obj.ProcessLine (line); + } + }); onFinish (); - } - - ProcessLine (line) - { - if (line[0] === '#') { - return; - } + } + + ProcessLine (line) + { + if (line[0] === '#') { + return; + } - let parameters = OV.ParametersFromLine (line, '#'); - if (parameters.length === 0) { - return; - } - + let parameters = OV.ParametersFromLine (line, '#'); + if (parameters.length === 0) { + return; + } + if (parameters[0] === 'OFF') { return; } @@ -100,5 +100,5 @@ OV.ImporterOff = class extends OV.ImporterBase } return; } - } + } }; diff --git a/source/import/importerply.js b/source/import/importerply.js index 0a1f7c5..6fb9333 100644 --- a/source/import/importerply.js +++ b/source/import/importerply.js @@ -92,32 +92,32 @@ OV.ImporterPly = class extends OV.ImporterBase this.model = null; this.mesh = null; } - - ResetState () - { + + ResetState () + { this.mesh = new OV.Mesh (); this.model.AddMesh (this.mesh); - } + } - CanImportExtension (extension) - { - return extension === 'ply'; - } + CanImportExtension (extension) + { + return extension === 'ply'; + } GetKnownFileFormats () - { - return { - 'ply' : OV.FileFormat.Binary - }; - } - + { + return { + 'ply' : OV.FileFormat.Binary + }; + } + GetUpDirection () { return OV.Direction.Y; } - ImportContent (fileContent, onFinish) - { + ImportContent (fileContent, onFinish) + { let headerString = this.GetHeaderContent (fileContent); let header = this.ReadHeader (headerString); if (header.Check ()) { @@ -133,7 +133,7 @@ OV.ImporterPly = class extends OV.ImporterBase this.SetMessage ('Invalid header information.'); } onFinish (); - } + } GetHeaderContent (fileContent) { @@ -180,7 +180,7 @@ OV.ImporterPly = class extends OV.ImporterBase header.AddSingleFormat (parameters[1], parameters[2]); } } - }); + }); return header; } @@ -231,7 +231,7 @@ OV.ImporterPly = class extends OV.ImporterBase } return; } - }); + }); } ReadBinaryContent (header, fileContent, headerLength) diff --git a/source/import/importerstl.js b/source/import/importerstl.js index 89241f2..bea4d72 100644 --- a/source/import/importerstl.js +++ b/source/import/importerstl.js @@ -6,33 +6,33 @@ OV.ImporterStl = class extends OV.ImporterBase this.mesh = null; this.triangle = null; } - - ResetState () - { + + ResetState () + { this.mesh = new OV.Mesh (); this.model.AddMesh (this.mesh); this.triangle = null; - } + } - CanImportExtension (extension) - { - return extension === 'stl'; - } + CanImportExtension (extension) + { + return extension === 'stl'; + } - GetKnownFileFormats () - { - return { - 'stl' : OV.FileFormat.Binary - }; - } - + GetKnownFileFormats () + { + return { + 'stl' : OV.FileFormat.Binary + }; + } + GetUpDirection () { return OV.Direction.Z; } - ImportContent (fileContent, onFinish) - { + ImportContent (fileContent, onFinish) + { if (this.IsBinaryStlFile (fileContent)) { this.ProcessBinary (fileContent); } else { @@ -45,10 +45,10 @@ OV.ImporterStl = class extends OV.ImporterBase }); } onFinish (); - } + } - IsBinaryStlFile (fileContent) - { + IsBinaryStlFile (fileContent) + { let byteLength = fileContent.byteLength; if (byteLength < 84) { return false; @@ -65,17 +65,17 @@ OV.ImporterStl = class extends OV.ImporterBase return true; } - ProcessLine (line) - { - if (line[0] === '#') { - return; - } + ProcessLine (line) + { + if (line[0] === '#') { + return; + } - let parameters = OV.ParametersFromLine (line, '#'); - if (parameters.length === 0) { - return; - } - + let parameters = OV.ParametersFromLine (line, '#'); + if (parameters.length === 0) { + return; + } + let keyword = parameters[0]; if (keyword === 'solid') { if (parameters.length > 1) { @@ -133,7 +133,7 @@ OV.ImporterStl = class extends OV.ImporterBase } ProcessBinary (fileContent) - { + { function ReadVector (reader) { let coord = new OV.Coord3D ();