using UnityEngine; public class TextureCombiner : MonoBehaviour { // 定义单个图片的信息结构 [System.Serializable] public struct TextureInfo { public Texture2D texture; public Vector2 position; public float rotation; public Vector2 scale; } public TextureInfo[] textureInfos; public Texture2D CombineTextures(int width, int height) { RenderTexture rt = RenderTexture.GetTemporary(width, height, 0); RenderTexture.active = rt; GL.Clear(true, true, Color.clear); GL.PushMatrix(); // 创建正交相机矩阵 Matrix4x4 orthoMatrix = Matrix4x4.Ortho(0, width, 0, height, -1, 1); GL.LoadProjectionMatrix(orthoMatrix); Material blitMaterial = new Material(Shader.Find("Unlit/Texture")); foreach (TextureInfo info in textureInfos) { GL.PushMatrix(); // 创建变换矩阵 Matrix4x4 transformMatrix = Matrix4x4.TRS( info.position, Quaternion.Euler(0, 0, info.rotation), new Vector3(info.scale.x, info.scale.y, 1) ); // 应用变换矩阵 GL.MultMatrix(transformMatrix); blitMaterial.mainTexture = info.texture; blitMaterial.SetPass(0); GL.Begin(GL.QUADS); float w = info.texture.width / 2f; float h = info.texture.height / 2f; GL.TexCoord2(0, 0); GL.Vertex3(-w, -h, 0); GL.TexCoord2(1, 0); GL.Vertex3(w, -h, 0); GL.TexCoord2(1, 1); GL.Vertex3(w, h, 0); GL.TexCoord2(0, 1); GL.Vertex3(-w, h, 0); GL.End(); GL.PopMatrix(); } GL.PopMatrix(); // 读取结果 Texture2D result = new Texture2D(width, height); result.ReadPixels(new Rect(0, 0, width, height), 0, 0); result.Apply(); RenderTexture.ReleaseTemporary(rt); RenderTexture.active = null; Destroy(blitMaterial); return result; } }