Unity : 3D to 2D Image
렌더 텍스쳐(Render Texture)
화면에 렌더링되는 이미지 데이터를 저장하는 데 사용되는 특수한 종류의 텍스처입니다. 주로 카메라의 뷰 포트에서 렌더링된 2D 또는 3D 씬을 캡처하거나 다른 텍스처에 렌더링하는데 사용됩니다.
사용 방법
- 프로젝트에 렌더 텍스처를 생성하고 해당 렌더 텍스처를 카메라의 대상으로 설정합니다.
- 렌더 텍스처를 렌더링하는 카메라에는 해당 3D객체가 화면에 나타나도록 배치합니다.
- 스크립트를 이용하여 렌더 텍스처를 이미지로 저장합니다.
using UnityEngine;
public class RenderTextureToImage : MonoBehaviour
{
public RenderTexture renderTexture;
public string imagePath = "RenderTextures/";
// 예를 들어, 렌더 텍스처를 이미지로 저장하려면 다음과 같이 호출합니다.
public void SaveRenderTextureToImage()
{
Texture2D texture = new Texture2D(renderTexture.width, renderTexture.height);
RenderTexture.active = renderTexture;
texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture.Apply();
byte[] bytes = texture.EncodeToPNG();
System.IO.File.WriteAllBytes(imagePath + "rendered_image.png", bytes);
}
}