void Generate() { if (destination == null) { destination = new Texture2D(source.width, source.height, TextureFormat.ARGB32, false); destination.hideFlags = HideFlags.HideAndDontSave; } string path = AssetDatabase.GetAssetPath(source); TextureImporter importer = TextureImporter.GetAtPath(path) as TextureImporter; if (importer == null) { Debug.LogError("Cannot work with built-in textures."); return; } if (importer.crunchedCompression) { Debug.LogError("You have to disable crunch compression while generating the SDF texture."); return; } bool isReadble = importer.isReadable; TextureImporterCompression compression = importer.textureCompression; bool uncompressed = compression == TextureImporterCompression.Uncompressed; if (!isReadble || !uncompressed) { importer.isReadable = true; importer.textureCompression = TextureImporterCompression.Uncompressed; AssetDatabase.ImportAsset(path); } SDFTextureGenerator.Generate( source, destination, insideDistance, outsideDistance, postProcessDistance, rgbFillMode); if (!isReadble || !uncompressed) { importer.isReadable = isReadble; importer.textureCompression = compression; AssetDatabase.ImportAsset(path); } destination.Apply(); allowSave = true; }
void CreateSDFTexture() { if (generatedTex2D == null || generatedTex2D.width != originalTex2D.width || generatedTex2D.height != originalTex2D.height) { generatedTex2D = new Texture2D(originalTex2D.width, originalTex2D.height, GeneratedTextureFormat, false); } var sourcePath = AssetDatabase.GetAssetPath(originalTex2D); var texImporter = TextureImporter.GetAtPath(sourcePath) as TextureImporter; if (texImporter == null) { // This happens if the texture is built-in to unity and not an asset Debug.LogWarning("Couldn't locate the image. Try using an asset from inside your project."); return; } var compression = texImporter.textureCompression; bool uncompressed = compression == TextureImporterCompression.Uncompressed; bool readable = texImporter.isReadable; // mark as uncompressed and readable if necessary if (!readable || !uncompressed) { texImporter.isReadable = true; texImporter.textureCompression = TextureImporterCompression.Uncompressed; AssetDatabase.ImportAsset(sourcePath); } SDFTextureGenerator.Generate(originalTex2D, generatedTex2D, distanceInner, distanceOuter, distancePostProcess, rgbFillMode); // restore texture settings. if (!readable || !uncompressed) { texImporter.isReadable = readable; texImporter.textureCompression = compression; AssetDatabase.ImportAsset(sourcePath); } generatedTex2D.Apply(); }
private void Generate() { if (destination == null) { destination = new Texture2D(source.width, source.height, TextureFormat.ARGB32, false); destination.hideFlags = HideFlags.HideAndDontSave; } string path = AssetDatabase.GetAssetPath(source); TextureImporter importer = TextureImporter.GetAtPath(path) as TextureImporter; if (importer == null) { Debug.LogError("Cannot work with built-in textures."); return; } bool isReadble = importer.isReadable; TextureImporterFormat format = importer.textureFormat; bool correctFormat = format == TextureImporterFormat.Alpha8 || format == TextureImporterFormat.ARGB32 || format == TextureImporterFormat.RGBA32 || format == TextureImporterFormat.AutomaticTruecolor; if (!isReadble || !correctFormat) { importer.isReadable = true; importer.textureFormat = TextureImporterFormat.ARGB32; AssetDatabase.ImportAsset(path); } SDFTextureGenerator.Generate( source, destination, insideDistance, outsideDistance, postProcessDistance, rgbFillMode); if (!isReadble || !correctFormat) { importer.isReadable = isReadble; importer.textureFormat = format; AssetDatabase.ImportAsset(path); } destination.Apply(); allowSave = true; }
void Awake() { _generator = new SDFTextureGenerator(); }