-
Notifications
You must be signed in to change notification settings - Fork 224
/
FurTextureGenerator.cs
92 lines (83 loc) · 2.63 KB
/
FurTextureGenerator.cs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class FurTextureGenerator : MonoBehaviour
{
[Header("Press Play, then change parameters in material.")]
public Shader FurTextureGeneratorShader;
public int Resolution = 1024;
private Material _Material;
private RenderTexture _RenderTexture, _Target;
void RenderToTexture (RenderTexture destination, Material mat, int pass)
{
RenderTexture.active = destination;
GL.PushMatrix();
GL.LoadOrtho();
GL.invertCulling = true;
mat.SetPass(pass);
GL.Begin(GL.QUADS);
GL.MultiTexCoord2(0, 0.0f, 0.0f);
GL.Vertex3(0.0f, 0.0f, 0.0f);
GL.MultiTexCoord2(0, 1.0f, 0.0f);
GL.Vertex3(1.0f, 0.0f, 0.0f);
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 0.0f);
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, 0.0f);
GL.End();
GL.invertCulling = false;
GL.PopMatrix();
}
void Start()
{
if (FurTextureGeneratorShader == null) FurTextureGeneratorShader = Shader.Find("Fur Texture Generator");
_Material = new Material(FurTextureGeneratorShader);
_RenderTexture = new RenderTexture(Resolution, Resolution, 0, RenderTextureFormat.ARGB32);
_Target = new RenderTexture(Resolution, Resolution, 0, RenderTextureFormat.ARGB32);
GetComponent<Renderer>().material = _Material;
}
void Export(RenderTexture renderTexture)
{
RenderTexture currentTexture = RenderTexture.active;
RenderTexture.active = renderTexture;
Texture2D texture = new Texture2D(Resolution, Resolution, TextureFormat.ARGB32, false);
texture.ReadPixels( new Rect(0, 0, Resolution, Resolution), 0, 0);
RenderTexture.active = currentTexture;
byte[] bytes = texture.EncodeToPNG();
string fileName = "Fur" + Random.Range(0, 1e9f).ToString("F0") + ".png";
string path = System.IO.Path.Combine(Application.dataPath, fileName);
System.IO.File.WriteAllBytes(path, bytes);
System.Diagnostics.Process.Start(path);
}
void Update()
{
RenderToTexture(_RenderTexture, _Material, 0);
_Material.SetTexture("_RenderTexture", _RenderTexture);
RenderToTexture(_Target, _Material, 1);
}
void OnDestroy()
{
Destroy(_Material);
_RenderTexture.Release();
_Target.Release();
}
public void ExportNow()
{
Export(_Target);
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(FurTextureGenerator))]
public class FurTextureGeneratorEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
FurTextureGenerator ftg = (FurTextureGenerator)target;
if(GUILayout.Button("Export To PNG")) ftg.ExportNow();
}
}
#endif