包含此页的版本:
不含此页的版本:
本页上的示例执行全屏blit“位块传输”的简写术语。blit作是将数据块从内存中的一个位置传输到另一个位置的过程。
请参阅术语表这会将屏幕染成绿色。
注意: Unity 不再开发或改进不使用渲染图 API 的渲染路径。在开发新的图形功能时,请改用渲染图 API。要使用此页面上的说明,请在 URP 图形设置(项目设置>图形)中启用兼容模式(渲染图禁用)。
要使用这些示例,请执行以下步骤:
若要创建自定义呈现通道,请创建一个名为ColorBlitPass.cs,然后粘贴到示例自定义渲染通道部分中的代码。
注意:该示例使用Blitter应用程序接口。不要使用CommandBuffer.Blit处于兼容模式下的 API。有关更多信息,请参阅兼容模式下的 Blit。
若要创建将自定义渲染通道添加到渲染循环的可脚本渲染器功能,请创建一个名为ColorBlitRendererFeature.cs,然后粘贴 示例可脚本渲染器功能(Example Scriptable Renderer Feature) 部分中的代码。
要创建着色器在 GPU 上运行的程序。更多信息
请参阅术语表代码,为像素计算机图像中的最小单位。像素大小取决于您的屏幕分辨率。像素光照是在每个屏幕像素下计算的。更多信息
请参阅术语表green,创建一个着色器文件,然后粘贴到示例着色器部分中的代码。
注意:与BlitterAPI 必须是手动编码的着色器。着色器图着色器与Blitter应用程序接口。
添加ColorBlitRendererFeature到当前 URP 渲染器资源。有关更多信息,请参阅向URP渲染器添加渲染器功能。
要更改亮度,请调整 Color Blit 渲染器功能组件中的 Intensity 属性。
注意:如果项目使用XR一个总称,包括虚拟现实 (VR)、增强现实 (AR) 和混合现实 (MR) 应用。支持这些形式的交互式应用程序的设备可以称为 XR 设备。更多信息
请参阅术语表,在项目中安装 MockHMD XR 插件包,然后将 渲染模式(Render Mode) 属性设置为 单通道实例化(Single Pass Instanced)。
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
internal class ColorBlitPass : ScriptableRenderPass
{
ProfilingSampler m_ProfilingSampler = new ProfilingSampler("ColorBlit");
Material m_Material;
RTHandle m_CameraColorTarget;
float m_Intensity;
public ColorBlitPass(Material material)
{
m_Material = material;
renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
}
public void SetTarget(RTHandle colorHandle, float intensity)
{
m_CameraColorTarget = colorHandle;
m_Intensity = intensity;
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
ConfigureTarget(m_CameraColorTarget);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cameraData = renderingData.cameraData;
if (cameraData.camera.cameraType != CameraType.Game)
return;
if (m_Material == null)
return;
CommandBuffer cmd = CommandBufferPool.Get();
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
m_Material.SetFloat("_Intensity", m_Intensity);
Blitter.BlitCameraTexture(cmd, m_CameraColorTarget, m_CameraColorTarget, m_Material, 0);
}
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
CommandBufferPool.Release(cmd);
}
}
可编写脚本的渲染器功能将渲染通道添加到渲染循环中。
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
internal class ColorBlitRendererFeature : ScriptableRendererFeature
{
public Shader m_Shader;
public float m_Intensity;
Material m_Material;
ColorBlitPass m_RenderPass = null;
public override void AddRenderPasses(ScriptableRenderer renderer,
ref RenderingData renderingData)
{
if (renderingData.cameraData.cameraType == CameraType.Game)
renderer.EnqueuePass(m_RenderPass);
}
public override void SetupRenderPasses(ScriptableRenderer renderer,
in RenderingData renderingData)
{
if (renderingData.cameraData.cameraType == CameraType.Game)
{
// Calling ConfigureInput with the ScriptableRenderPassInput.Color argument
// ensures that the opaque texture is available to the Render Pass.
m_RenderPass.ConfigureInput(ScriptableRenderPassInput.Color);
m_RenderPass.SetTarget(renderer.cameraColorTargetHandle, m_Intensity);
}
}
public override void Create()
{
m_Material = CoreUtils.CreateEngineMaterial(m_Shader);
m_RenderPass = new ColorBlitPass(m_Material);
}
protected override void Dispose(bool disposing)
{
CoreUtils.Destroy(m_Material);
}
}
着色器执行渲染的 GPU 端。它从相机在场景中创建特定视点图像的组件。输出要么绘制到屏幕上,要么作为纹理捕获。更多信息
请参阅术语表,然后输出将绿色值设置为所选强度的颜色。
Shader "ColorBlit"
{
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline"}
LOD 100
ZWrite Off Cull Off
Pass
{
Name "ColorBlitPass"
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
// The Blit.hlsl file provides the vertex shader (Vert),
// the input structure (Attributes) and the output structure (Varyings)
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
#pragma vertex Vert
#pragma fragment frag
// Set the color texture from the camera as the input texture
TEXTURE2D_X(_CameraOpaqueTexture);
SAMPLER(sampler_CameraOpaqueTexture);
// Set up an intensity parameter
float _Intensity;
half4 frag (Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
// Sample the color from the input texture
float4 color = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, input.texcoord);
// Output the color from the texture, with the green value set to the chosen intensity
return color * float4(0, _Intensity, 0, 1);
}
ENDHLSL
}
}
}