이 페이지의 예제는 전체 화면 blit을 수행하는 커스텀 렌더러 기능을 생성하는 방법을 설명합니다.
참고: Unity는 더 이상 렌더 그래프 API를 사용하지 않는 렌더링 경로를 개발하거나 개선하지 않습니다. 이제 새로운 그래픽스 기능을 개발할 때는 렌더 그래프 API를 사용하십시오. 이 페이지의 지침을 사용하려면 URP Graphics 설정(Project Settings > Graphics)에서 Compatibility Mode (Render Graph Disabled)를 활성화하십시오.
이 예제는 다음 솔루션을 구현합니다.
커스텀 렌더러 기능은 커스텀 렌더 패스를 호출합니다.
렌더 패스가 불투명 텍스처를 현재 렌더러의 카메라 컬러 타겟으로 blit을 수행합니다. 렌더 패스는 커맨드 버퍼를 사용하여 양쪽 눈에 대한 전체 화면 메시를 그립니다.
이 예제에는 렌더링의 GPU 측을 수행하는 셰이더가 포함되어 있습니다. 이 셰이더는__ XR__XR은 VR(가상 현실), AR(증강 현실)과 MR(혼합 현실) 애플리케이션을 모두 포함하는 포괄적인 용어입니다. 이러한 형태의 인터랙티브 애플리케이션을 지원하는 기기를 XR 기기라고 합니다. 자세한 정보
See in Glossary 샘플러 매크로를 사용하여 컬러 버퍼를 샘플링합니다.
이 예제를 진행하려면 다음 조건을 충족해야 합니다.
이 예제의 단계를 수행하려면 다음 게임 오브젝트가 포함된 새로운 씬을 생성하십시오.
큐브를 생성합니다. 큐브가 메인 카메라에서 분명하게 보여야 합니다.
이제 이 예제의 단계를 따르는 데 필요한 씬이 준비되었습니다.
이 섹션에서는 예시 씬 및 게임 오브젝트 만들기 섹션에 설명된 대로 씬을 생성했다고 가정합니다.
다음 단계에 따라 커스텀 렌더 패스를 사용하여 커스텀 렌더러 기능을 만듭니다.
새 C# 스크립트를 생성합니다. ColorBlitRendererFeature.cs라고 명명합니다. 이 스크립트는 커스텀 렌더러 기능을 구현합니다.
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
internal class ColorBlitRendererFeature : ScriptableRendererFeature
{
public Shader shader;
[Range(0f, 1f)] public float intensity = 1f;
Material m_Material;
ColorBlitPass m_RenderPass;
// Use this method to add one or more Render Passes into the rendering sequence of the renderer with the EnqueuePass method.
public override void AddRenderPasses(ScriptableRenderer renderer,
ref RenderingData renderingData)
{
if (m_Material == null)
return;
if (renderingData.cameraData.cameraType != CameraType.Game)
return;
m_RenderPass.SetIntensity(intensity);
renderer.EnqueuePass(m_RenderPass);
}
// Use the Create method to initialize the resources. This method only runs in the first frame.
// Use the Dispose method to clean up the resources before the renderer feature is disabled.
public override void Create()
{
m_Material = CoreUtils.CreateEngineMaterial(shader);
m_RenderPass = new ColorBlitPass(m_Material);
}
protected override void Dispose(bool disposing)
{
CoreUtils.Destroy(m_Material);
}
}
새 C# 스크립트를 생성합니다. ColorBlitPass.cs라고 명명합니다. 이 스크립트는 커스텀__ blit__‘bit block transfer’의 약어입니다. blit 동작은 데이터 블록을 메모리의 한 곳에서 다른 곳으로 전송하는 과정입니다.
See in Glossary 드로우 콜을 수행하는 커스텀 렌더 패스를 구현합니다.
이 렌더 패스는 AddBlitPass를 사용해 blit 작업을 수행합니다.
참고: 이 메서드는 URP XR 통합과 호환성 문제가 있으므로 URP XR 프로젝트에서
cmd.Blit메서드를 사용하지 마십시오.cmd.Blit을 사용하면 XR 셰이더 키워드를 암시적으로 활성화 또는 비활성화하여 XR SPI 렌더링을 중단시킵니다.
using UnityEngine;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule.Util;
public class ColorBlitPass : ScriptableRenderPass
{
private const string k_PassName = "ColorBlitPass";
private Material m_Material;
private float m_Intensity;
private static readonly int k_IntensityID = Shader.PropertyToID("_Intensity");
public ColorBlitPass(Material mat)
{
m_Material = mat;
renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
}
public void SetIntensity(float intensity)
{
m_Intensity = intensity;
}
// Use the RecordRenderGraph method to configure the input and output parameters for the AddBlitPass method and execute the AddBlitPass method.
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
{
var resourceData = frameData.Get<UniversalResourceData>();
// The following line ensures that the render pass doesn't blit
// from the back buffer.
if (resourceData.isActiveTargetBackBuffer)
{
Debug.LogError($"Skipping render pass. ColorBlitRendererFeature requires an intermediate ColorTexture, we can't use the BackBuffer as a texture input.");
return;
}
var source = resourceData.activeColorTexture;
// Define the texture descriptor for creating the destination render graph texture.
var destinationDesc = renderGraph.GetTextureDesc(source);
destinationDesc.name = $"CameraColor-{k_PassName}";
destinationDesc.clearBuffer = false;
destinationDesc.depthBufferBits = 0;
// Create the texture.
TextureHandle destination = renderGraph.CreateTexture(destinationDesc);
// The AddBlitPass method adds the render graph pass that blits from the source to the destination texture.
RenderGraphUtils.BlitMaterialParameters para = new(source, destination, m_Material, 0);
para.material.SetFloat(k_IntensityID, m_Intensity);
renderGraph.AddBlitPass(para, passName: k_PassName);
// Use the destination texture as the camera texture to avoid the extra blit from the destination texture back to the camera texture.
resourceData.cameraColor = destination;
}
}
blit 동작을 수행하는 셰이더를 생성합니다. 셰이더 파일 ColorBlit.shader를 호출합니다. 버텍스 함수가 전체 화면 사각형 위치를 출력합니다. 프래그먼트 함수가 컬러 버퍼를 샘플링하여 렌더 타겟에 color * float4(0, _Intensity, 0, 1) 값을 반환합니다.
Shader "ColorBlit"
{
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline"}
ZWrite Off Cull Off
Pass
{
Name "ColorBlitPass"
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
#pragma vertex Vert
#pragma fragment Frag
float _Intensity;
float4 Frag(Varyings input) : SV_Target
{
// This function handles the different ways XR platforms handle texture arrays.
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
// Sample the texture using the SAMPLE_TEXTURE2D_X_LOD function
float2 uv = input.texcoord.xy;
half4 color = SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_LinearRepeat, uv, _BlitMipLevel);
// Modify the sampled color
return half4(0, _Intensity, 0, 1) * color;
}
ENDHLSL
}
}
}
유니버설 렌더러 에셋에 ColorBlitRendererFeature를 추가합니다.
렌더러 기능을 추가하는 방법은 렌더러 기능을 렌더러에 추가하는 방법 페이지를 참조하십시오.
이 예제에서는 Intensity 프로퍼티를 1.5로 설정합니다.
Unity에 다음 뷰가 표시됩니다.
참고: 예제를 XR에서 시각화하려면 프로젝트가 XR SDK를 사용하도록 설정하십시오. 프로젝트에 MockHMD XR 플러그인을 추가합니다. Render Mode 프로퍼티는 Single Pass Instanced로 설정하십시오.
예제가 완료되었습니다.
호환성 모드에서 blit 작업을 수행하는 자세한 방법은 URP 14(Unity 2022) 기술 자료의 텍스처 사용하기 섹션을 참고하십시오.