Files
XRLib/Assets/Scripts/UVC/Extention/RenderTextureEx.cs

121 lines
4.3 KiB
C#
Raw Normal View History

2025-06-04 23:10:11 +09:00
using UnityEngine;
2025-06-10 20:16:35 +09:00
using UVC.Log;
2025-06-04 23:10:11 +09:00
namespace UVC.Extension
{
/// <summary>
/// RenderTexture Ȯ<><C8AE> <20>޼<EFBFBD><DEBC><20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD> Ŭ<><C5AC><EFBFBD><EFBFBD><EFBFBD>Դϴ<D4B4>.
/// </summary>
public static class RenderTextureEx
{
/// <summary>
/// RenderTexture<72><65> Texture2D<32><44> <20><>ȯ<EFBFBD>մϴ<D5B4>.
/// </summary>
/// <param name="renderTexture"><3E><>ȯ<EFBFBD><C8AF> RenderTexture <20><>ü</param>
/// <returns><3E><>ȯ<EFBFBD><C8AF> Texture2D <20><>ü</returns>
/// <example>
/// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>:
/// <code>
/// // RenderTexture <20><><EFBFBD><EFBFBD>
/// RenderTexture renderTexture = new RenderTexture(256, 256, 24);
///
/// // ī<>޶󿡼<DEB6> renderTexture<72><65> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// Camera.main.targetTexture = renderTexture;
/// Camera.main.Render();
///
/// // RenderTexture<72><65> Texture2D<32><44> <20><>ȯ
/// Texture2D texture2D = renderTexture.ToTexture2D();
///
/// // <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD>
/// Camera.main.targetTexture = null;
/// Camera.main.ResetAspect();
/// </code>
/// </example>
public static Texture2D ToTexture2D(this RenderTexture renderTexture)
{
Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new UnityEngine.Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
return texture2D;
}
/// <summary>
/// RenderTexture<72><65> <20><><EFBFBD><EFBFBD>Ʈ <20><EFBFBD><E8BFAD> <20><>ȯ<EFBFBD>մϴ<D5B4>.
/// </summary>
/// <param name="renderTexture"><3E><>ȯ<EFBFBD><C8AF> RenderTexture <20><>ü</param>
/// <param name="format"><3E>̹<EFBFBD><CCB9><EFBFBD> <20><><EFBFBD><EFBFBD> (<28><EFBFBD><E2BABB>: PNG)</param>
/// <returns><3E><><EFBFBD><EFBFBD>Ʈ <20>迭</returns>
/// <example>
/// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>:
/// <code>
/// RenderTexture rt = new RenderTexture(256, 256, 24);
/// byte[] bytes = rt.ToBytes();
/// File.WriteAllBytes("screenshot.png", bytes);
/// </code>
/// </example>
public static byte[] ToBytes(this RenderTexture renderTexture, TextureFormat format = TextureFormat.RGBA32)
{
Texture2D texture2D = renderTexture.ToTexture2D();
byte[] bytes = texture2D.EncodeToPNG();
Object.Destroy(texture2D);
return bytes;
}
/// <summary>
/// RenderTexture<72><65> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ο<EFBFBD> PNG <20><><EFBFBD>Ϸ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>մϴ<D5B4>.
/// </summary>
/// <param name="renderTexture"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> RenderTexture <20><>ü</param>
/// <param name="filePath"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD></param>
/// <returns><3E><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD></returns>
/// <example>
/// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>:
/// <code>
/// RenderTexture rt = new RenderTexture(256, 256, 24);
/// Camera.main.targetTexture = rt;
/// Camera.main.Render();
/// bool success = rt.SaveToPNG("Assets/Screenshots/screenshot.png");
/// Camera.main.targetTexture = null;
/// </code>
/// </example>
public static bool SaveToPNG(this RenderTexture renderTexture, string filePath)
{
try
{
byte[] bytes = renderTexture.ToBytes();
System.IO.File.WriteAllBytes(filePath, bytes);
return true;
}
catch (System.Exception e)
{
2025-06-10 20:16:35 +09:00
ULog.Error($"RenderTexture<72><65> PNG<4E><47> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><DFBD>ϴ<EFBFBD>: {e.Message}", e);
2025-06-04 23:10:11 +09:00
return false;
}
}
/// <summary>
/// RenderTexture<72><65> ũ<><20><><EFBFBD><EFBFBD><EFBFBD>մϴ<D5B4>.
/// </summary>
/// <param name="renderTexture"><3E><><EFBFBD><EFBFBD> RenderTexture</param>
/// <param name="width"><3E><> <20>ʺ<EFBFBD></param>
/// <param name="height"><3E><> <20><><EFBFBD><EFBFBD></param>
/// <returns>ũ<><20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ο<EFBFBD> RenderTexture</returns>
/// <example>
/// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>:
/// <code>
/// RenderTexture originalRT = new RenderTexture(1024, 1024, 24);
/// RenderTexture resizedRT = originalRT.Resize(512, 512);
/// // <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD>
/// resizedRT.Release();
/// originalRT.Release();
/// </code>
/// </example>
public static RenderTexture Resize(this RenderTexture renderTexture, int width, int height)
{
RenderTexture resized = new RenderTexture(width, height, renderTexture.depth);
Graphics.Blit(renderTexture, resized);
return resized;
}
}
}