public static string buildImageName(Texture2D image) { string extension = GLTFTextureUtils.useJPGTexture(image) ? ".jpg": ".png"; //return image.GetInstanceID().ToString().Replace("-", "") + "_" + image.name + extension; return(image.name + extension); }
public void registerImageFromData(byte[] imageData, int imageID, string imageName = "") { Texture2D texture = new Texture2D(4, 4); texture.name = imageName; texture.LoadImage(imageData); GL.sRGBWrite = true; saveTexture(GLTFTextureUtils.flipTexture(texture), imageID); }
public Texture2D flipTexture(Texture2D texture) { if (!_flippedTextures.ContainsKey(texture)) { Texture2D flipped = GLTFTextureUtils.flipTexture(texture); _flippedTextures.Add(texture, flipped); } return(_flippedTextures[texture]); }
public Texture2D handleNormalMap(Texture2D texture) { if (!_convertedBump.ContainsKey(texture)) { Texture2D tex = GLTFTextureUtils.handleNormalMap(texture); _convertedBump.Add(texture, tex); } return(_convertedBump[texture]); }
public Texture2D packOcclusionMetalRough(Texture2D metalSmooth, Texture2D occlusion) { KeyValuePair <Texture2D, Texture2D> key = new KeyValuePair <Texture2D, Texture2D>(metalSmooth, occlusion); if (!_packedTextures.ContainsKey(key)) { Texture2D tex = GLTFTextureUtils.packOcclusionMetalRough(metalSmooth, occlusion); _packedTextures.Add(key, tex); } return(_packedTextures[key]); }
public Texture2D saveTexture(Texture2D texture, int index = -1, string imageName = "") { string basename = GLTFUtils.cleanName(texture.name + (index >= 0 ? "_" + index.ToString() : "") + ".png"); // Extension will be overridden string fullPath = Path.Combine(_importTexturesDirectory, basename); // Write texture string newAssetPath = GLTFTextureUtils.writeTextureOnDisk(texture, fullPath, true); // Reload as asset string projectPath = GLTFUtils.getPathProjectFromAbsolute(newAssetPath); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(projectPath, typeof(Texture2D)); _parsedImages.Add(tex); return(tex); }
public List <UnityEngine.Texture2D> splitMetalRoughTexture(Texture2D inputTexture, bool hasOcclusion, float metallicFactor, float roughnessFactor) { string inputTexturePath = AssetDatabase.GetAssetPath(inputTexture); if (!_assetsToRemove.Contains(inputTexturePath)) { _assetsToRemove.Add(inputTexturePath); } List <UnityEngine.Texture2D> outputs = new List <UnityEngine.Texture2D>(); #if true int width = inputTexture.width; int height = inputTexture.height; Color[] occlusion = new Color[width * height]; Color[] metalRough = new Color[width * height]; Color[] textureColors = new Color[width * height]; GLTFUtils.getPixelsFromTexture(ref inputTexture, out textureColors); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { float occ = textureColors[i * width + j].r; float rough = textureColors[i * width + j].g; float met = textureColors[i * width + j].b; occlusion[i * width + j] = new Color(occ, occ, occ, 1.0f); metalRough[i * width + j] = new Color(met * metallicFactor, met * metallicFactor, met * metallicFactor, (1.0f - rough) * roughnessFactor); } } Texture2D metalRoughTexture = new Texture2D(width, height, TextureFormat.ARGB32, true); metalRoughTexture.name = Path.GetFileNameWithoutExtension(inputTexturePath) + "_metal"; metalRoughTexture.SetPixels(metalRough); metalRoughTexture.Apply(); outputs.Add(_assetManager.saveTexture(metalRoughTexture)); if (hasOcclusion) { Texture2D occlusionTexture = new Texture2D(width, height); occlusionTexture.name = Path.GetFileNameWithoutExtension(inputTexturePath) + "_occlusion"; occlusionTexture.SetPixels(occlusion); occlusionTexture.Apply(); outputs.Add(_assetManager.saveTexture(occlusionTexture)); } // Delete original texture AssetDatabase.Refresh(); #else string inputTextureName = Path.GetFileNameWithoutExtension(inputTexturePath); string metalRoughPath = Path.Combine(_assetManager.getImportTextureDir(), inputTextureName + "_metal.png"); GLTFTextureUtils.extractMetalRough(inputTexture, metalRoughPath); outputs.Add(AssetDatabase.LoadAssetAtPath <Texture2D>(GLTFUtils.getPathProjectFromAbsolute(metalRoughPath))); if (hasOcclusion) { string occlusionPath = Path.Combine(_assetManager.getImportTextureDir(), inputTextureName + "_occlusion.png"); GLTFTextureUtils.extractOcclusion(inputTexture, occlusionPath); outputs.Add(AssetDatabase.LoadAssetAtPath <Texture2D>(GLTFUtils.getPathProjectFromAbsolute(occlusionPath))); } #endif return(outputs); }