From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/XEditor/Render2Texture.cs | 84 +++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Client/Assets/Scripts/XEditor/Render2Texture.cs (limited to 'Client/Assets/Scripts/XEditor/Render2Texture.cs') diff --git a/Client/Assets/Scripts/XEditor/Render2Texture.cs b/Client/Assets/Scripts/XEditor/Render2Texture.cs new file mode 100644 index 00000000..f4afd1bc --- /dev/null +++ b/Client/Assets/Scripts/XEditor/Render2Texture.cs @@ -0,0 +1,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("Assets/Editor/EditorRes/Render2Tex.prefab"); + if(prefab!=null) + { + GameObject render2Tex = GameObject.Instantiate(prefab); + Render2Texture r2t = render2Tex.GetComponent(); + 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("Assets/Editor/EditorRes/Render2Tex.prefab"); + if (prefab != null) + { + GameObject render2Tex = GameObject.Instantiate(prefab); + Render2Texture r2t = render2Tex.GetComponent(); + if (r2t != null) + { + tex = r2t.Render(tex0, tex1, width, height, tile, offset, shaderName); + } + GameObject.DestroyImmediate(render2Tex); + } + return tex; + } +} +#endif \ No newline at end of file -- cgit v1.1-26-g67d0