본문 바로가기

TIL

23.10.26

Unity : 3D to 2D Image

렌더 텍스쳐(Render Texture)

화면에 렌더링되는 이미지 데이터를 저장하는 데 사용되는 특수한 종류의 텍스처입니다. 주로 카메라의 뷰 포트에서 렌더링된 2D 또는 3D 씬을 캡처하거나 다른 텍스처에 렌더링하는데 사용됩니다. 

 

사용 방법

  1. 프로젝트에 렌더 텍스처를 생성하고 해당 렌더 텍스처를 카메라의 대상으로 설정합니다.
  2. 렌더 텍스처를 렌더링하는 카메라에는 해당 3D객체가 화면에 나타나도록 배치합니다.
  3. 스크립트를 이용하여 렌더 텍스처를 이미지로 저장합니다.
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);
    }
}

'TIL' 카테고리의 다른 글

23.10.30  (0) 2023.11.01
23.10.27  (0) 2023.10.30
23.10.25  (0) 2023.10.25
23.10.24  (0) 2023.10.24
23.10.23  (0) 2023.10.23