유니티 엔진 (Unity Engine)

[Unity] 유니티로 Texture 를 복사하거나 Shader로 변형하는 방법

원소랑 2024. 3. 14. 18:27
728x90

 

Graphics.CopyTexture()는 텍스처 간의 간단하고 효율적인 데이터 전송
효율적인 데이터 전송

Texture.isReadable 값이 false 인 경우, CopyTexture 가 텍스처를 복사하는 가장 빠른 메소드.
아래 3가지 조건이 두 텍스처간에 동일해야 함.
- 텍스처 포멧
- 크기
- RenderTexture.antiAliasing 값

 

https://docs.unity3d.com/ScriptReference/Graphics.CopyTexture.html

 

Unity - Scripting API: Graphics.CopyTexture

This method copies pixel data from one texture to another on the GPU. If you set Texture.isReadable to true for both src and dst textures, the method also copies pixel data on the CPU. If you set Texture.isReadable to false, CopyTexture is one of the faste

docs.unity3d.com

 



Graphics.Blit()은 복사하는 동안 데이터에 셰이더를 적용하거나 텍스처 데이터를 크기에 맞게 조정. 는 GPU 텍스처 간 고성능 복사 및 후처리 효과 생성을 위한 API로, 렌더 파이프라인 및 색공간에 따른 주의사항이 있음.


소스 텍스처를 대상 렌더 텍스처에 전체 화면으로 그리기 위해 셰이더를 사용.
후처리 효과를 만들기 위해 커스텀 셰이더를 사용할 수 있습니다.
동일한 렌더 텍스처를 소스와 대상으로 지정하면 예기치 않은 동작이 발생할 수 있습니다. 대신 더블 버퍼링을 사용하세요.
선형 색공간에서는 GL.sRGBWrite를 설정해야 합니다.


내장 렌더 파이프라인에서는 Camera.main.targetTexture를 사용합니다.
URP 및 HDRP에서는 RenderPipelineManager.endContextRendering 콜백 내에서 호출해야 합니다.

https://docs.unity3d.com/ScriptReference/Graphics.Blit.html

 

Unity - Scripting API: Graphics.Blit

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

가우시안 블러 셰이더를 Graphcs.Blit() 함수로 1회 적용한 결과로 Texture2D 를 얻는 샘플 코드

using UnityEngine;

public class GaussianBlur : MonoBehaviour
{
    public Shader gaussianBlurShader;
    public Texture2D sourceTexture;
    public float blurRadius = 1f;
    public bool applyBlur = false;

    private Material blurMaterial;
    private RenderTexture tempRenderTexture;
    private Texture2D blurredTexture;

    void Start()
    {
        blurMaterial = new Material(gaussianBlurShader);
        tempRenderTexture = new RenderTexture(sourceTexture.width, sourceTexture.height, 0);
        blurredTexture = new Texture2D(sourceTexture.width, sourceTexture.height, TextureFormat.RGBA32, false);
    }

    void Update()
    {
        if (applyBlur)
        {
            ApplyGaussianBlur();
            applyBlur = false;
        }
    }

    void ApplyGaussianBlur()
    {
        // 소스 텍스처를 임시 렌더 텍스처에 복사
        Graphics.Blit(sourceTexture, tempRenderTexture);

        // 블러 셰이더 적용
        blurMaterial.SetFloat("_BlurRadius", blurRadius);
        Graphics.Blit(tempRenderTexture, tempRenderTexture, blurMaterial);

        // 렌더 텍스처에서 Texture2D로 복사
        RenderTexture.active = tempRenderTexture;
        blurredTexture.ReadPixels(new Rect(0, 0, tempRenderTexture.width, tempRenderTexture.height), 0, 0);
        blurredTexture.Apply();
        RenderTexture.active = null;

        // 임시 렌더 텍스처 해제
        RenderTexture.ReleaseTemporary(tempRenderTexture);
    }

    void OnDestroy()
    {
        DestroyImmediate(blurMaterial);
        Destroy(blurredTexture);
    }
}

 


게임 개발에 필수적인 내용을 담는 명서들을 소개합니다.

 

<유니티 교과서 개정6판>(유니티 최신 버전)
https://link.coupang.com/a/be3P0t

 

유니티 교과서 개정6판

COUPANG

www.coupang.com

 

<대마왕의 유니티 URP 셰이더 그래프 스타트업>

https://link.coupang.com/a/bs8qyC

 

대마왕의 유니티 URP 셰이더 그래프 스타트업

COUPANG

www.coupang.com


<리얼-타임 렌더링(REAL-TIME RENDERING) 4/e>
https://link.coupang.com/a/8VWas

 

리얼-타임 렌더링 4/e

COUPANG

www.coupang.com

 

<이득우의 게임 수학:39가지 예제로 배운다! 메타버스를 구성하는 게임 수학의 모든 것>
https://link.coupang.com/a/9BqLd

 

이득우의 게임 수학:39가지 예제로 배운다! 메타버스를 구성하는 게임 수학의 모든 것

COUPANG

www.coupang.com

 

유니티 에셋 스토어 링크
https://assetstore.unity.com?aid=1011lvz7h

 

에셋스토어

여러분의 작업에 필요한 베스트 에셋을 찾아보세요. 유니티 에셋스토어가 2D, 3D 모델, SDK, 템플릿, 툴 등 여러분의 콘텐츠 제작에 날개를 달아줄 다양한 에셋을 제공합니다.

assetstore.unity.com

(링크를 통해 도서/에셋 구입시 일정액의 수수료를 지급받습니다.)


 

728x90
반응형