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
|
#if UNITY_EDITOR
using System.IO;
using UnityEngine;
using UnityEditor;
[RequireComponent(typeof(Camera))]
public class Render2Texture : MonoBehaviour
{
public Camera cameraCache;
public MeshRenderer mr;
public Texture2D Render(Texture src, Texture tex1, int width, int height, Vector2 tile, Vector2 offset, string shaderName)
{
if (cameraCache != null && mr != null)
{
Shader shader = Shader.Find(shaderName);
Material mat = new Material(shader);
mat.SetTexture("_MainTex", src);
if (mat.HasProperty("_Tex1"))
mat.SetTexture("_Tex1", tex1);
mat.SetTextureScale("_MainTex", tile);
mat.SetTextureOffset("_MainTex", offset);
width = width <= 0 ? src.width : width;
height = height <= 0 ? src.height : height;
RenderTexture current = RenderTexture.active;
RenderTexture rt = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32);
cameraCache.targetTexture = rt;
mr.sharedMaterial = mat;
cameraCache.Render();
Texture2D des = new Texture2D(src.width, src.height);
Graphics.SetRenderTarget(rt);
des.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
des.Apply();
GameObject.DestroyImmediate(mat);
Graphics.SetRenderTarget(current);
RenderTexture.ReleaseTemporary(rt);
return des;
}
return null;
}
public static Texture2D ScaleTexture(Texture src, string despath, int width, int height, Vector2 tile, Vector2 offset, string shaderName)
{
Texture2D tex = null;
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Editor/EditorRes/Render2Tex.prefab");
if(prefab!=null)
{
GameObject render2Tex = GameObject.Instantiate<GameObject>(prefab);
Render2Texture r2t = render2Tex.GetComponent<Render2Texture>();
if (r2t != null)
{
tex = r2t.Render(src, null, width, height, tile, offset, shaderName);
if (tex != null && !string.IsNullOrEmpty(despath))
{
byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(despath, bytes);
AssetDatabase.Refresh();
}
}
GameObject.DestroyImmediate(render2Tex);
}
return tex;
}
public static Texture2D CompactTexture(Texture tex0, Texture tex1, int width, int height, Vector2 tile, Vector2 offset, string shaderName)
{
Texture2D tex = null;
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Editor/EditorRes/Render2Tex.prefab");
if (prefab != null)
{
GameObject render2Tex = GameObject.Instantiate<GameObject>(prefab);
Render2Texture r2t = render2Tex.GetComponent<Render2Texture>();
if (r2t != null)
{
tex = r2t.Render(tex0, tex1, width, height, tile, offset, shaderName);
}
GameObject.DestroyImmediate(render2Tex);
}
return tex;
}
}
#endif
|