Outline on Texture2D
I'm creating a minimap, but I want to add a little outline effect, just like in this answer: http://answers.unity3d.com/questions/1036262/shaders-offset-texture-coordinates-by-a-single-pix.html
But without shaders, because this texture will be saved as a png file, then use it as a sprite inside a UI Image.
This is what I have done: Looks good, but how can I add a simple 1 pixel outline inside the texture?
For extra details, here is my script:
using UnityEngine;
using System.Collections;
using System.IO;
[RequireComponent(typeof(Camera))]
public class MiniMap : MonoBehaviour {
private Camera cam;
void Awake () {
cam = GetComponent<Camera> ();
CreateMiniMap ();
Destroy (gameObject);
}
void CreateMiniMap () {
RenderTexture rt = new RenderTexture (512, 512, 16, RenderTextureFormat.ARGB32); //Create a render texture
Texture2D minimap = new Texture2D (rt.width, rt.height, TextureFormat.ARGB32, false);
cam.targetTexture = rt;
cam.Render ();
RenderTexture.active = rt;
minimap.ReadPixels (new Rect (0, 0, rt.width, rt.height), 0, 0); //Render texture to Texture2D
minimap.Apply ();
RemoveColor (cam.backgroundColor, Color.clear, minimap); //Removes the background of the Texture2D
FillImage (Color.black, minimap);
cam.targetTexture = null;
RenderTexture.active = null;
DestroyImmediate (rt);
if (!Directory.Exists (Application.dataPath + "/MiniMaps")) {
Directory.CreateDirectory(Application.dataPath + "/MiniMaps");
} //Creates a directory to save the minimap
File.WriteAllBytes (Application.dataPath + "/MiniMaps/" + GameManager.map.name + "_Minimap.png", minimap.EncodeToPNG ());
} //Save as a PNG file
void RemoveColor (Color color, Color newColor, Texture2D tex) {
Color[] pixels = tex.GetPixels (0, 0, tex.width, tex.height, 0);
for (int i = 0; i < pixels.Length; i++) {
if (pixels[i] == color)
pixels[i] = newColor;
}
tex.SetPixels (0, 0, tex.width, tex.height, pixels, 0);
tex.Apply ();
}
void FillImage (Color fillColor, Texture2D tex) {
Color[] pixels = tex.GetPixels (0, 0, tex.width, tex.height, 0);
for (int i = 0; i < pixels.Length; i++) {
if (pixels[i] != Color.clear)
pixels[i] = fillColor;
}
tex.SetPixels (0, 0, tex.width, tex.height, pixels, 0);
tex.Apply ();
}
}
Answer by newfreeknowledge · Sep 29, 2018 at 05:43 PM
I solved a similar problem by drawing the "outline image" in a different color before drawing the image I want on top of it:
var outline: int = 3; //how many pixels of outline
var rect = new Rect(x,y,a,b);
var rect_outline = new Rect(x-outline, y-outline, a+2*outline, b+2*outline);
GUI.DrawTexture(rect_outline, image_outline, ScaleMode.StretchToFill);
GUI.DrawTexture(rect, image, ScaleMode.StretchToFill);
image_outline is a copy of image, but its pixels are filled with the desired outline color. So you do something like:
image_outline = Instantiate(image) as Texture2D;
FillImage (Color.white, image_outline);
I know the response is late, but I only noticed the question today. Maybe it can help someone.
Your answer
Follow this Question
Related Questions
How to check the Completion scenario for my drawing? 1 Answer
Changing dinamically a texture 0 Answers
SetPixels edge pixels 0 Answers
Hide part of image with a certain color 0 Answers