public string TryWriteImage(GLTF gltf, string sourcePath, string textureName, ExportParameters exportParameters, ILoggingProvider logger) { 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); } // Copy texture to output var destPath = Path.Combine(gltf.OutputFolder, textureName); destPath = Path.ChangeExtension(destPath, validImageFormat); TextureUtilities.CopyTexture(sourcePath, destPath, exportParameters.txtQuality, logger); return(validImageFormat); }
private string GetGltfValidImageFormat(string extension) { return(TextureUtilities.GetValidImageFormat(extension)); }
private BabylonTexture ExportTexture(ITexmap texMap, BabylonScene babylonScene, float amount = 1.0f, bool allowCube = false, bool forceAlpha = false) { IBitmapTex texture = _getBitmapTex(texMap, false); if (texture == null) { float specialAmount; var specialTexMap = _getSpecialTexmap(texMap, out specialAmount); texture = _getBitmapTex(specialTexMap, false); amount *= specialAmount; } if (texture == null) { return(null); } var sourcePath = texture.Map.FullFilePath; if (sourcePath == null || sourcePath == "") { RaiseWarning("Texture path is missing.", 2); return(null); } RaiseMessage("Export texture named: " + Path.GetFileName(sourcePath), 2); var validImageFormat = TextureUtilities.GetValidImageFormat(Path.GetExtension(sourcePath)); if (validImageFormat == null) { // Image format is not supported by the exporter 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 textureID = texture.GetGuid().ToString(); if (textureMap.ContainsKey(textureID)) { return(textureMap[textureID]); } else { var babylonTexture = new BabylonTexture(textureID) { name = Path.GetFileNameWithoutExtension(texture.MapName) + "." + validImageFormat }; RaiseMessage($"texture id = {babylonTexture.Id}", 2); // Level babylonTexture.level = amount; // Alpha if (forceAlpha) { babylonTexture.hasAlpha = true; babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2) || (texture.AlphaSource == 3); // 'RGB intensity' or 'None (Opaque)' } else { babylonTexture.hasAlpha = (texture.AlphaSource != 3); // Not 'None (Opaque)' babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2); // 'RGB intensity' } // UVs var uvGen = _exportUV(texture.UVGen, babylonTexture); // Animations var animations = new List <BabylonAnimation>(); ExportFloatAnimation("uOffset", animations, key => new[] { uvGen.GetUOffs(key) }); ExportFloatAnimation("vOffset", animations, key => new[] { -uvGen.GetVOffs(key) }); ExportFloatAnimation("uScale", animations, key => new[] { uvGen.GetUScl(key) }); ExportFloatAnimation("vScale", animations, key => new[] { uvGen.GetVScl(key) }); ExportFloatAnimation("uAng", animations, key => new[] { uvGen.GetUAng(key) }); ExportFloatAnimation("vAng", animations, key => new[] { uvGen.GetVAng(key) }); ExportFloatAnimation("wAng", animations, key => new[] { uvGen.GetWAng(key) }); babylonTexture.animations = animations.ToArray(); // Copy texture to output if (isBabylonExported) { var destPath = Path.Combine(babylonScene.OutputPath, babylonTexture.name); TextureUtilities.CopyTexture(sourcePath, destPath, exportParameters.txtQuality, this); // Is cube _exportIsCube(Path.Combine(babylonScene.OutputPath, babylonTexture.name), babylonTexture, allowCube); } else { babylonTexture.isCube = false; } babylonTexture.originalPath = sourcePath; return(babylonTexture); } }
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); switch (validImageFormat) { case "jpg": gltfImage.FileExtension = "jpeg"; break; case "png": gltfImage.FileExtension = "png"; break; } 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); } // -------------------------- // -------- 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); } }