-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureMod.cs
More file actions
31 lines (23 loc) · 1.24 KB
/
TextureMod.cs
File metadata and controls
31 lines (23 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using UnityEngine;
public static class TextureMod
{
private static readonly Material ModMaterial = new Material(Shader.Find("Hidden/TextureMod"));
public static void Modify(ref Texture2D texture, bool greyscale, bool flip_h, bool flip_v)
{
int w = texture.width; int h = texture.height;
var tempRenderTexture = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32);
ModMaterial.SetTexture("_MainTex", texture);
ModMaterial.SetInt("_Grayscale", greyscale ? 1 : 0);
ModMaterial.SetInt("_FlipHorizontal", flip_h ? 1 : 0);
ModMaterial.SetInt("_FlipVertical", flip_v ? 1 : 0);
Graphics.Blit(texture, tempRenderTexture, ModMaterial);
RenderTexture.active = tempRenderTexture;
texture.ReadPixels(new Rect(0, 0, w, h), 0, 0);
texture.Apply();
RenderTexture.active = null;
RenderTexture.ReleaseTemporary(tempRenderTexture);
}
public static void ConvertGreyScale(ref Texture2D texture) => Modify(ref texture, true, false, false);
public static void FlipHorizontally(ref Texture2D texture) => Modify(ref texture, false, true, false);
public static void FlipVertically(ref Texture2D texture) => Modify(ref texture, false, false, true);
}