55 lines
1.4 KiB
C#
55 lines
1.4 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.IO;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEditor;
|
||
|
|
|
||
|
|
[CustomEditor(typeof(RenderTextureSaveToPNG))]
|
||
|
|
public class RenderTextureSaveToPNGEditor : Editor
|
||
|
|
{
|
||
|
|
RenderTextureSaveToPNG inst;
|
||
|
|
|
||
|
|
private void OnEnable()
|
||
|
|
{
|
||
|
|
inst = (RenderTextureSaveToPNG)target;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void OnInspectorGUI()
|
||
|
|
{
|
||
|
|
base.OnInspectorGUI();
|
||
|
|
|
||
|
|
//
|
||
|
|
if(GUILayout.Button("RenderTextureSaveToPNG", GUILayout.Height(50)))
|
||
|
|
{
|
||
|
|
SaveRenderToPng();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Texture2D SaveRenderToPng()
|
||
|
|
{
|
||
|
|
int width = inst.inputTex.width;
|
||
|
|
int height = inst.inputTex.height;
|
||
|
|
Texture2D tex2d = new Texture2D(width, height, TextureFormat.ARGB32, false);
|
||
|
|
RenderTexture.active = inst.inputTex;
|
||
|
|
tex2d.ReadPixels(new Rect(0, 0, width, height), 0, 0);
|
||
|
|
|
||
|
|
for(int y = 0; y < tex2d.height; ++y)
|
||
|
|
{
|
||
|
|
for (int x = 0; x < tex2d.width; ++x)
|
||
|
|
{
|
||
|
|
if(tex2d.GetPixel(x, y).grayscale > inst.maskSrength)
|
||
|
|
{
|
||
|
|
tex2d.SetPixel(x, y, Color.white);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
tex2d.Apply();
|
||
|
|
|
||
|
|
File.WriteAllBytes(Application.dataPath + "/Materials/" + inst.inputTex.name + ".png", tex2d.EncodeToPNG());
|
||
|
|
AssetDatabase.Refresh();
|
||
|
|
|
||
|
|
return tex2d;
|
||
|
|
}
|
||
|
|
}
|