private GLTFTextureInfo ExportTexture(BabylonTexture babylonTexture, GLTF gltf, string name, Func <string> writeImageFunc) { if (babylonTexture == null) { return(null); } if (name == null) { name = babylonTexture.name; } //Check for texture optimisation if (CheckIfImageIsRegistered(name)) { var TextureComponent = GetRegisteredTexture(name); return(TextureComponent); } RaiseMessage("GLTFExporter.Texture | Export texture named: " + name, 2); string validImageFormat = writeImageFunc.Invoke(); if (validImageFormat == null) { return(null); } name = Path.ChangeExtension(name, validImageFormat); // -------------------------- // -------- Sampler --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create sampler", 3); GLTFSampler gltfSampler = new GLTFSampler(); gltfSampler.index = gltf.SamplersList.Count; gltf.SamplersList.Add(gltfSampler); // --- Retreive info from babylon texture --- // Mag and min filters GLTFSampler.TextureMagFilter?magFilter; GLTFSampler.TextureMinFilter?minFilter; getSamplingParameters(babylonTexture.samplingMode, out magFilter, out minFilter); gltfSampler.magFilter = magFilter; gltfSampler.minFilter = minFilter; // WrapS and wrapT gltfSampler.wrapS = getWrapMode(babylonTexture.wrapU); gltfSampler.wrapT = getWrapMode(babylonTexture.wrapV); // -------------------------- // --------- Image ---------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create image", 3); GLTFImage gltfImage = new GLTFImage { uri = name }; gltfImage.index = gltf.ImagesList.Count; gltf.ImagesList.Add(gltfImage); switch (validImageFormat) { case "jpg": gltfImage.FileExtension = "jpeg"; break; case "png": gltfImage.FileExtension = "png"; break; } // -------------------------- // -------- Texture --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create texture", 3); var gltfTexture = new GLTFTexture { name = name, sampler = gltfSampler.index, source = gltfImage.index }; gltfTexture.index = gltf.TexturesList.Count; gltf.TexturesList.Add(gltfTexture); // -------------------------- // ------ TextureInfo ------- // -------------------------- var gltfTextureInfo = new GLTFTextureInfo { index = gltfTexture.index, texCoord = babylonTexture.coordinatesIndex }; // Add texture extension if (babylonTexture.uOffset != 0f || babylonTexture.vOffset != 0f || babylonTexture.uScale != 1f || babylonTexture.vScale != 1f || babylonTexture.wAng != 0f) { AddTextureTransformExtension(ref gltf, ref gltfTextureInfo, babylonTexture); } // Add the texture in the dictionary RegisterTexture(gltfTextureInfo, name); return(gltfTextureInfo); }
private GLTFTextureInfo ExportTexture(BabylonTexture babylonTexture, GLTF gltf, string name, Func <string> writeImageFunc) { if (babylonTexture == null) { return(null); } if (name == null) { name = babylonTexture.name; } RaiseMessage("GLTFExporter.Texture | Export texture named: " + name, 2); if (glTFTextureInfoMap.ContainsKey(babylonTexture.Id)) { return(glTFTextureInfoMap[babylonTexture.Id]); } else { string validImageFormat = writeImageFunc.Invoke(); if (validImageFormat == null) { return(null); } name = Path.ChangeExtension(name, validImageFormat); // -------------------------- // -------- Sampler --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create sampler", 3); GLTFSampler gltfSampler = new GLTFSampler(); gltfSampler.index = gltf.SamplersList.Count; // --- Retrieve info from babylon texture --- // Mag and min filters GLTFSampler.TextureMagFilter?magFilter; GLTFSampler.TextureMinFilter?minFilter; getSamplingParameters(babylonTexture.samplingMode, out magFilter, out minFilter); gltfSampler.magFilter = magFilter; gltfSampler.minFilter = minFilter; // WrapS and wrapT gltfSampler.wrapS = getWrapMode(babylonTexture.wrapU); gltfSampler.wrapT = getWrapMode(babylonTexture.wrapV); var matchingSampler = gltf.SamplersList.FirstOrDefault(sampler => sampler.wrapS == gltfSampler.wrapS && sampler.wrapT == gltfSampler.wrapT && sampler.magFilter == gltfSampler.magFilter && sampler.minFilter == gltfSampler.minFilter); if (matchingSampler != null) { gltfSampler = matchingSampler; } else { gltf.SamplersList.Add(gltfSampler); } // -------------------------- // --------- Image ---------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create image", 3); GLTFImage gltfImage = null; if (glTFImageMap.ContainsKey(name)) { gltfImage = glTFImageMap[name]; } else { string textureUri = name; if (!string.IsNullOrWhiteSpace(relativeTextureFolder)) { textureUri = relativeTextureFolder + "/" + name; } gltfImage = new GLTFImage { uri = textureUri }; gltfImage.index = gltf.ImagesList.Count; gltf.ImagesList.Add(gltfImage); glTFImageMap.Add(name, gltfImage); switch (validImageFormat) { case "jpg": gltfImage.FileExtension = "jpeg"; break; case "png": gltfImage.FileExtension = "png"; break; } } // -------------------------- // -------- Texture --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create texture", 3); var gltfTexture = new GLTFTexture { name = name, sampler = gltfSampler.index, source = gltfImage.index }; gltfTexture.index = gltf.TexturesList.Count; gltf.TexturesList.Add(gltfTexture); // -------------------------- // ------ TextureInfo ------- // -------------------------- var gltfTextureInfo = new GLTFTextureInfo { index = gltfTexture.index, texCoord = babylonTexture.coordinatesIndex }; if (!(babylonTexture.uOffset == 0) || !(babylonTexture.vOffset == 0) || !(babylonTexture.uScale == 1) || !(babylonTexture.vScale == -1) || !(babylonTexture.wAng == 0)) { // Add texture extension if enabled in the export settings if (exportParameters.enableKHRTextureTransform) { AddTextureTransformExtension(ref gltf, ref gltfTextureInfo, babylonTexture); } else { RaiseWarning("GLTFExporter.Texture | KHR_texture_transform is not enabled, so the texture may look incorrect at runtime!", 3); } } var textureID = name + TextureTransformID(gltfTextureInfo); // Check for texture optimization. This is done here after the texture transform has been potentially applied to the texture extension if (CheckIfImageIsRegistered(textureID)) { var textureComponent = GetRegisteredTexture(textureID); return(textureComponent); } // Add the texture in the dictionary RegisterTexture(gltfTextureInfo, textureID); glTFTextureInfoMap[babylonTexture.Id] = gltfTextureInfo; return(gltfTextureInfo); } }
private GLTFTextureInfo ExportTexture(BabylonTexture babylonTexture, GLTF gltf, string name, Func<string> writeImageFunc) { if (babylonTexture == null) { return null; } if (name == null) { name = babylonTexture.name; } RaiseMessage("GLTFExporter.Texture | Export texture named: " + name, 2); string validImageFormat = writeImageFunc.Invoke(); if (validImageFormat == null) { return null; } name = Path.ChangeExtension(name, validImageFormat); // -------------------------- // -------- Sampler --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create sampler", 3); GLTFSampler gltfSampler = new GLTFSampler(); gltfSampler.index = gltf.SamplersList.Count; gltf.SamplersList.Add(gltfSampler); // --- Retreive info from babylon texture --- // Mag and min filters GLTFSampler.TextureMagFilter? magFilter; GLTFSampler.TextureMinFilter? minFilter; getSamplingParameters(babylonTexture.samplingMode, out magFilter, out minFilter); gltfSampler.magFilter = magFilter; gltfSampler.minFilter = minFilter; // WrapS and wrapT gltfSampler.wrapS = getWrapMode(babylonTexture.wrapU); gltfSampler.wrapT = getWrapMode(babylonTexture.wrapV); // -------------------------- // --------- Image ---------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create image", 3); GLTFImage gltfImage = new GLTFImage { uri = name }; gltfImage.index = gltf.ImagesList.Count; gltf.ImagesList.Add(gltfImage); switch (validImageFormat) { case "jpg": gltfImage.FileExtension = "jpeg"; break; case "png": gltfImage.FileExtension = "png"; break; } // -------------------------- // -------- Texture --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create texture", 3); var gltfTexture = new GLTFTexture { name = name, sampler = gltfSampler.index, source = gltfImage.index }; gltfTexture.index = gltf.TexturesList.Count; gltf.TexturesList.Add(gltfTexture); // -------------------------- // ------ TextureInfo ------- // -------------------------- var gltfTextureInfo = new GLTFTextureInfo { index = gltfTexture.index, texCoord = babylonTexture.coordinatesIndex }; return gltfTextureInfo; }
private GLTFTextureInfo ExportTexture(BabylonTexture babylonTexture, GLTF gltf, string name) { if (babylonTexture == null) { return(null); } if (name == null) { name = babylonTexture.name; } logger.RaiseMessage("GLTFExporter.Texture | Export texture named: " + name, 2); if (glTFTextureInfoMap.ContainsKey(babylonTexture.Id)) { return(glTFTextureInfoMap[babylonTexture.Id]); } else { var sourcePath = babylonTexture.originalPath; if (babylonTexture.bitmap != null) { sourcePath = Path.Combine(gltf.OutputFolder, name); } if (sourcePath == null || sourcePath == "") { logger.RaiseWarning("Texture path is missing.", 3); return(null); } var validImageFormat = TextureUtilities.GetValidImageFormat(Path.GetExtension(sourcePath)); if (validImageFormat == null) { // Image format is not supported by the exporter logger.RaiseWarning(string.Format("Format of texture {0} is not supported by the exporter. Consider using a standard image format like jpg or png.", Path.GetFileName(sourcePath)), 3); return(null); } var destPath = Path.Combine(gltf.OutputFolder, name); destPath = Path.ChangeExtension(destPath, validImageFormat); name = Path.ChangeExtension(name, validImageFormat); // -------------------------- // -------- Sampler --------- // -------------------------- logger.RaiseMessage("GLTFExporter.Texture | create sampler", 3); GLTFSampler gltfSampler = new GLTFSampler(); gltfSampler.index = gltf.SamplersList.Count; // --- Retrieve info from babylon texture --- // Mag and min filters GLTFSampler.TextureMagFilter?magFilter; GLTFSampler.TextureMinFilter?minFilter; getSamplingParameters(babylonTexture.samplingMode, out magFilter, out minFilter); gltfSampler.magFilter = magFilter; gltfSampler.minFilter = minFilter; // WrapS and wrapT gltfSampler.wrapS = getWrapMode(babylonTexture.wrapU); gltfSampler.wrapT = getWrapMode(babylonTexture.wrapV); var matchingSampler = gltf.SamplersList.FirstOrDefault(sampler => sampler.wrapS == gltfSampler.wrapS && sampler.wrapT == gltfSampler.wrapT && sampler.magFilter == gltfSampler.magFilter && sampler.minFilter == gltfSampler.minFilter); if (matchingSampler != null) { gltfSampler = matchingSampler; } else { gltf.SamplersList.Add(gltfSampler); } // -------------------------- // --------- Image ---------- // -------------------------- logger.RaiseMessage("GLTFExporter.Texture | create image", 3); GLTFImage gltfImage = null; if (glTFImageMap.ContainsKey(name)) { gltfImage = glTFImageMap[name]; } else { string textureUri = name; if (!string.IsNullOrWhiteSpace(exportParameters.textureFolder)) { textureUri = PathUtilities.GetRelativePath(exportParameters.outputPath, exportParameters.textureFolder); textureUri = Path.Combine(textureUri, name); } gltfImage = new GLTFImage { uri = textureUri }; gltfImage.index = gltf.ImagesList.Count; gltf.ImagesList.Add(gltfImage); if (exportParameters.outputFormat == "glb") { var imageBufferView = WriteImageToGltfBuffer(gltf, gltfImage, sourcePath, babylonTexture.bitmap); gltfImage.uri = null; gltfImage.bufferView = imageBufferView.index; gltfImage.mimeType = "image/" + gltfImage.FileExtension; } else { if (exportParameters.writeTextures) { if (babylonTexture.bitmap != null) { // We may have modified this texture image, copy the buffer contents to disk var extension = Path.GetExtension(name).ToLower(); var imageFormat = extension == ".jpg" ? System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png; logger.RaiseMessage($"GLTFExporter.Texture | write image '{name}' to '{destPath}'", 3); TextureUtilities.SaveBitmap(babylonTexture.bitmap, destPath, imageFormat, exportParameters.txtQuality, logger); } else { // Copy texture from source to output TextureUtilities.CopyTexture(sourcePath, destPath, exportParameters.txtQuality, logger); } } } glTFImageMap.Add(name, gltfImage); switch (validImageFormat) { case "jpg": gltfImage.FileExtension = "jpeg"; break; case "png": gltfImage.FileExtension = "png"; break; } } // -------------------------- // -------- Texture --------- // -------------------------- logger.RaiseMessage("GLTFExporter.Texture | create texture", 3); var gltfTexture = new GLTFTexture { name = name, sampler = gltfSampler.index, source = gltfImage.index }; gltfTexture.index = gltf.TexturesList.Count; if (!CheckIfImageIsRegistered(name)) { gltf.TexturesList.Add(gltfTexture); } else { gltfTexture = gltf.TexturesList[GetRegisteredTexture(gltfTexture.name).index]; } // -------------------------- // ------ TextureInfo ------- // -------------------------- var gltfTextureInfo = new GLTFTextureInfo { index = gltfTexture.index, texCoord = babylonTexture.coordinatesIndex }; if (!(babylonTexture.uOffset == 0) || !(babylonTexture.vOffset == 0) || !(babylonTexture.uScale == 1) || !(babylonTexture.vScale == 1) || !(babylonTexture.wAng == 0)) { // Add texture extension if enabled in the export settings if (exportParameters.enableKHRTextureTransform) { AddTextureTransformExtension(ref gltf, ref gltfTextureInfo, babylonTexture); } else { logger.RaiseWarning("GLTFExporter.Texture | KHR_texture_transform is not enabled, so the texture may look incorrect at runtime!", 3); logger.RaiseWarning("GLTFExporter.Texture | KHR_texture_transform is not enabled, so the texture may look incorrect at runtime!", 3); } } var textureID = name + TextureTransformID(gltfTextureInfo); // Check for texture optimization. This is done here after the texture transform has been potentially applied to the texture extension if (CheckIfImageIsRegistered(textureID)) { var textureComponent = GetRegisteredTexture(textureID); return(textureComponent); } // Add the texture in the dictionary RegisterTexture(gltfTextureInfo, textureID); glTFTextureInfoMap[babylonTexture.Id] = gltfTextureInfo; return(gltfTextureInfo); } }
private GLTFTextureInfo ExportTexture(BabylonTexture babylonTexture, GLTF gltf, string name = null) { if (babylonTexture == null) { return(null); } if (name == null) { name = babylonTexture.name; } RaiseMessage("GLTFExporter.Texture | Export texture named: " + name, 1); // -------------------------- // -------- Sampler --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create sampler", 2); GLTFSampler gltfSampler = new GLTFSampler(); gltfSampler.index = gltf.SamplersList.Count; gltf.SamplersList.Add(gltfSampler); // --- Retreive info from babylon texture --- // Mag and min filters GLTFSampler.TextureMagFilter?magFilter; GLTFSampler.TextureMinFilter?minFilter; getSamplingParameters(babylonTexture.samplingMode, out magFilter, out minFilter); gltfSampler.magFilter = magFilter; gltfSampler.minFilter = minFilter; // WrapS and wrapT gltfSampler.wrapS = getWrapMode(babylonTexture.wrapU); gltfSampler.wrapT = getWrapMode(babylonTexture.wrapV); // -------------------------- // --------- Image ---------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create image", 2); GLTFImage gltfImage = new GLTFImage { uri = name }; gltfImage.index = gltf.ImagesList.Count; gltf.ImagesList.Add(gltfImage); switch (Path.GetExtension(name)) { case ".jpg": gltfImage.FileExtension = "jpeg"; break; case ".png": gltfImage.FileExtension = "png"; break; } // -------------------------- // -------- Texture --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create texture", 2); var gltfTexture = new GLTFTexture { name = name, sampler = gltfSampler.index, source = gltfImage.index }; gltfTexture.index = gltf.TexturesList.Count; gltf.TexturesList.Add(gltfTexture); // -------------------------- // ------ TextureInfo ------- // -------------------------- var gltfTextureInfo = new GLTFTextureInfo { index = gltfTexture.index }; // TODO - Animations return(gltfTextureInfo); }
// todo undo #if !WINDOWS_UWP IEnumerator Start() { var fullPath0 = Application.streamingAssetsPath + Path.DirectorySeparatorChar + asset0Path; ILoader loader0 = new FileLoader(URIHelper.GetDirectoryName(fullPath0)); var fullPath1 = Application.streamingAssetsPath + Path.DirectorySeparatorChar + asset1Path; ILoader loader1 = new FileLoader(URIHelper.GetDirectoryName(fullPath1)); yield return(loader0.LoadStream(Path.GetFileName(asset0Path))); var asset0Stream = loader0.LoadedStream; GLTFRoot asset0Root = null; GLTFParser.ParseJson(asset0Stream, ref asset0Root); yield return(loader1.LoadStream(Path.GetFileName(asset1Path))); var asset1Stream = loader1.LoadedStream; GLTFRoot asset1Root = null; GLTFParser.ParseJson(asset0Stream, ref asset1Root); string newPath = "../../" + URIHelper.GetDirectoryName(asset0Path); int previousBufferCount = asset1Root.Buffers.Count; int previousImageCount = asset1Root.Images.Count; int previousSceneCounter = asset1Root.Scenes.Count; GLTFHelpers.MergeGLTF(asset1Root, asset0Root); for (int i = previousBufferCount; i < asset1Root.Buffers.Count; ++i) { GLTF.Schema.GLTFBuffer buffer = asset1Root.Buffers[i]; if (!URIHelper.IsBase64Uri(buffer.Uri)) { buffer.Uri = newPath + buffer.Uri; } } for (int i = previousImageCount; i < asset1Root.Images.Count; ++i) { GLTFImage image = asset1Root.Images[i]; if (!URIHelper.IsBase64Uri(image.Uri)) { image.Uri = newPath + image.Uri; } } foreach (NodeId node in asset1Root.Scenes[asset0Root.Scene.Id + previousSceneCounter].Nodes) { node.Value.Translation.X += 5f; asset1Root.Scene.Value.Nodes.Add(node); } GLTFSceneImporter importer = new GLTFSceneImporter( asset1Root, loader1 ); importer.MaximumLod = MaximumLod; importer.isMultithreaded = Multithreaded; yield return(importer.LoadScene(-1)); }
// todo undo #if !WINDOWS_UWP private async Task Start() { var fullPath0 = Application.streamingAssetsPath + Path.DirectorySeparatorChar + asset0Path; ILoader loader0 = new FileLoader(URIHelper.GetDirectoryName(fullPath0)); var fullPath1 = Application.streamingAssetsPath + Path.DirectorySeparatorChar + asset1Path; ILoader loader1 = new FileLoader(URIHelper.GetDirectoryName(fullPath1)); await loader0.LoadStream(Path.GetFileName(asset0Path)); var asset0Stream = loader0.LoadedStream; GLTFRoot asset0Root; GLTFParser.ParseJson(asset0Stream, out asset0Root); await loader1.LoadStream(Path.GetFileName(asset1Path)); var asset1Stream = loader1.LoadedStream; GLTFRoot asset1Root; GLTFParser.ParseJson(asset1Stream, out asset1Root); string newPath = "../../" + URIHelper.GetDirectoryName(asset0Path); int previousBufferCount = asset1Root.Buffers.Count; int previousImageCount = asset1Root.Images.Count; int previousSceneCounter = asset1Root.Scenes.Count; GLTFHelpers.MergeGLTF(asset1Root, asset0Root); for (int i = previousBufferCount; i < asset1Root.Buffers.Count; ++i) { GLTF.Schema.GLTFBuffer buffer = asset1Root.Buffers[i]; if (!URIHelper.IsBase64Uri(buffer.Uri)) { buffer.Uri = newPath + buffer.Uri; } } for (int i = previousImageCount; i < asset1Root.Images.Count; ++i) { GLTFImage image = asset1Root.Images[i]; if (!URIHelper.IsBase64Uri(image.Uri)) { image.Uri = newPath + image.Uri; } } foreach (NodeId node in asset1Root.Scenes[asset0Root.Scene.Id + previousSceneCounter].Nodes) { node.Value.Translation.X += 5f; asset1Root.Scene.Value.Nodes.Add(node); } GLTFSceneImporter importer = new GLTFSceneImporter( asset1Root, null, new ImportOptions { ExternalDataLoader = loader1, AsyncCoroutineHelper = gameObject.AddComponent <AsyncCoroutineHelper>() }); importer.MaximumLod = MaximumLod; importer.IsMultithreaded = Multithreaded; await importer.LoadSceneAsync(-1); }
private GLTFTextureInfo ExportTexture(BabylonTexture babylonTexture, GLTF gltf) { if (babylonTexture == null) { return(null); } RaiseMessage("GLTFExporter.Texture | ExportTexture babylonTexture.name=" + babylonTexture.name, 1); // -------------------------- // -------- Sampler --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create sampler", 1); GLTFSampler gltfSampler = new GLTFSampler(); gltfSampler.index = gltf.SamplersList.Count; gltf.SamplersList.Add(gltfSampler); // --- Retreive info from babylon texture --- // Mag and min filters GLTFSampler.TextureMagFilter?magFilter; GLTFSampler.TextureMinFilter?minFilter; getSamplingParameters(babylonTexture.samplingMode, out magFilter, out minFilter); gltfSampler.magFilter = magFilter; gltfSampler.minFilter = minFilter; // WrapS and wrapT gltfSampler.wrapS = getWrapMode(babylonTexture.wrapU); gltfSampler.wrapT = getWrapMode(babylonTexture.wrapV); // -------------------------- // --------- Image ---------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create image", 1); GLTFImage gltfImage = new GLTFImage { uri = babylonTexture.name }; gltfImage.index = gltf.ImagesList.Count; gltf.ImagesList.Add(gltfImage); // -------------------------- // -------- Texture --------- // -------------------------- RaiseMessage("GLTFExporter.Texture | create texture", 1); var gltfTexture = new GLTFTexture { name = babylonTexture.name, sampler = gltfSampler.index, source = gltfImage.index }; gltfTexture.index = gltf.TexturesList.Count; gltf.TexturesList.Add(gltfTexture); // -------------------------- // ------ TextureInfo ------- // -------------------------- var gltfTextureInfo = new GLTFTextureInfo { index = gltfTexture.index }; // TODO - Animations //// Copy image to output //var absolutePath = texture.Map.FullFilePath; //try //{ // if (File.Exists(absolutePath)) // { // if (CopyTexturesToOutput) // { // RaiseMessage("GLTFExporter.Texture | copy image src path = "+ absolutePath + " and dest path = "+ Path.Combine(gltf.OutputPath, gltfTexture.name)); // File.Copy(absolutePath, Path.Combine(gltf.OutputPath, gltfTexture.name), true); // } // } // else // { // RaiseWarning(string.Format("Texture {0} not found.", gltfTexture.name), 2); // } //} //catch //{ // // silently fails //} return(gltfTextureInfo); }