UnityContrib / framework

Unity Contribution Framework
MIT License
8 stars 2 forks source link

Image tools? #8

Open robintheilade opened 9 years ago

robintheilade commented 9 years ago

I wrote a scaling script for TGA textures that come with the Untiy african trees asset. I was not able to scale the TGA textures using paint.net or gimp without them failing in Unity. This script scales the textures and saves them as png and could be part of an image tool box in this framework. Textures must be read/write enabled and is currently scaled according to their image size setting and not their actual size.

using System.IO;
using System.Linq;
using UnityEngine;

public class ScaleTga : MonoBehaviour
{
    public Texture2D texture;
    public float scale = 0.25f;

    void Start()
    {
        if (this.texture == null)
        {
            return;
        }

        var srcPixels = this.texture.GetPixels();
        var destPixels = new Color[(int)(srcPixels.Length * this.scale)];
        var destTexture = new Texture2D((int)(this.texture.width * this.scale), (int)(this.texture.height * this.scale));

        for (var y = 0.0f; y < this.texture.height; y += 1.0f / this.scale)
        {
            for (var x = 0.0f; x < this.texture.width; x += 1.0f / this.scale)
            {
                var pixel = srcPixels[(int)(y * this.texture.height + x)];
                destPixels[(int)(y * this.scale * destTexture.height + x * this.scale)] = pixel;
            }
        }

        destTexture.SetPixels(destPixels.ToArray());
        using (var stream = File.OpenWrite(@"C:\git\github.com\UnityContrib\framework\Unity\Assets\Unity Technologies\AfricanTrees\Textures\output.png"))
        {
            var bytes = destTexture.EncodeToPNG();
            stream.Write(bytes, 0, bytes.Length);
        }
    }
}

Needs a lot of polishing before it can be deemed ready for the framework.

robintheilade commented 9 years ago

Port and include texture painter? http://arongranberg.com/unity/unitypaint/

How to credit properly?