// Create different names based on the format // Opaque and Transparent textures are exported differently. public static string GetNameFromObject(Object o, IMAGETYPE format) { string transparencyStyle = ( format == IMAGETYPE.RGBA_OPAQUE) ? "OPAQUE" : "TRANSPARENT"; // Don't use the object ID, so that we can deterministically produce // the same filenames when exporting the same asset again. return("texture_" + transparencyStyle + "_" + GlTF_Writer.GetNameFromObject(o, false)); }
private void addTexturePixels(ref Texture2D texture, ref Color[] colors, IMAGETYPE outputChannel, IMAGETYPE inputChannel = IMAGETYPE.R) { int height = texture.height; int width = texture.width; Color[] inputColors = new Color[texture.width * texture.height]; if (!texture || !getPixelsFromTexture(ref texture, out inputColors)) { return; } if (height * width != colors.Length) { Debug.Log("Issue with texture dimensions"); return; } if (inputChannel != IMAGETYPE.R && inputChannel != IMAGETYPE.A) { Debug.Log("Incorrect input channel (only 'R' and 'A' supported)"); } for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { int index = i * width + j; int newIndex = (height - i - 1) * width + j; Color c = outputChannel == IMAGETYPE.RGB ? inputColors[newIndex] : colors[index]; float inputValue = inputChannel == IMAGETYPE.R ? inputColors[newIndex].r : inputColors[newIndex].a; if (outputChannel == IMAGETYPE.R) { c.r = inputValue; } else if (outputChannel == IMAGETYPE.G) { c.g = inputValue; } else if (outputChannel == IMAGETYPE.B) { c.b = inputValue; } else if (outputChannel == IMAGETYPE.G_INVERT) { c.g = 1.0f - inputValue; } colors[index] = c; } } }
// Get or create texture object, image and sampler private int processTexture(Texture2D t, IMAGETYPE format) { var texName = GlTF_Texture.GetNameFromObject(t); if (AssetDatabase.GetAssetPath(t).Length == 0) { Debug.LogWarning("Texture " + t.name + " cannot be found in assets"); return(-1); } if (!GlTF_Writer.textureNames.Contains(texName)) { string assetPath = AssetDatabase.GetAssetPath(t); // Create texture GlTF_Texture texture = new GlTF_Texture(); texture.name = texName; // Export image GlTF_Image img = new GlTF_Image(); img.name = GlTF_Image.GetNameFromObject(t); img.uri = convertTexture(ref t, assetPath, savedPath, format); texture.source = GlTF_Writer.imageNames.Count; GlTF_Writer.imageNames.Add(img.name); GlTF_Writer.images.Add(img); // Add sampler GlTF_Sampler sampler; var samplerName = GlTF_Sampler.GetNameFromObject(t); if (!GlTF_Writer.samplerNames.Contains(samplerName)) { sampler = new GlTF_Sampler(t); sampler.name = samplerName; GlTF_Writer.samplers.Add(sampler); GlTF_Writer.samplerNames.Add(samplerName); } GlTF_Writer.textures.Add(texture); GlTF_Writer.textureNames.Add(texName); } return(GlTF_Writer.textureNames.IndexOf(texName)); }
// Flip all images on Y and public string convertTexture(ref Texture2D inputTexture, string pathInProject, string exportDirectory, IMAGETYPE format) { int height = inputTexture.height; int width = inputTexture.width; Color[] textureColors = new Color[inputTexture.height * inputTexture.width]; if (!getPixelsFromTexture(ref inputTexture, out textureColors)) { Debug.Log("Failed to convert texture " + inputTexture.name + " (unsupported type or format)"); return(""); } Color[] newTextureColors = new Color[inputTexture.height * inputTexture.width]; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { newTextureColors[i * width + j] = textureColors[(height - i - 1) * width + j]; if (format == IMAGETYPE.RGBA_OPAQUE) { newTextureColors[i * width + j].a = 1.0f; } } } Texture2D newtex = new Texture2D(inputTexture.width, inputTexture.height); newtex.SetPixels(newTextureColors); newtex.Apply(); string pathInArchive = Path.GetDirectoryName(pathInProject); string exportDir = Path.Combine(exportDirectory, pathInArchive); if (!Directory.Exists(exportDir)) { Directory.CreateDirectory(exportDir); } string outputFilename = Path.GetFileNameWithoutExtension(pathInProject) + (format == IMAGETYPE.RGBA ? ".png" : ".jpg"); string exportPath = exportDir + "/" + outputFilename; // relative path inside the .zip string pathInGltfFile = pathInArchive + "/" + outputFilename; File.WriteAllBytes(exportPath, (format == IMAGETYPE.RGBA ? newtex.EncodeToPNG() : newtex.EncodeToJPG(format == IMAGETYPE.NORMAL_MAP ? 95 : jpgQuality))); if (!GlTF_Writer.exportedFiles.ContainsKey(exportPath)) { GlTF_Writer.exportedFiles.Add(exportPath, pathInArchive); } else { Debug.LogError("Texture '" + inputTexture + "' already exists"); } return(pathInGltfFile); }