You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
4.2 KiB
C#
98 lines
4.2 KiB
C#
#nullable enable
|
|
using System ;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
public class OutlineRenderFeature: ScriptableRendererFeature {
|
|
static Material? outlineMaterial ;
|
|
OutlinePass? outlinePass ;
|
|
|
|
class OutlinePass: ScriptableRenderPass {
|
|
RTHandle? tempColorTexture,
|
|
cameraRTHandle ;
|
|
RenderTargetIdentifier cameraTarget ;
|
|
|
|
public OutlinePass( Material material ) => outlineMaterial = material ;
|
|
|
|
[Obsolete( "This rendering path is for compatibility mode only (when Render Graph is disabled). " +
|
|
"Use Render Graph API instead.", false )]
|
|
public override void Configure( CommandBuffer cmd,
|
|
RenderTextureDescriptor cameraTextureDescriptor ) {
|
|
/*cmd.GetTemporaryRT(tempColorTexture.id, cameraTextureDescriptor); // Create temp texture
|
|
ConfigureTarget(tempColorTexture.Identifier());*/
|
|
|
|
// Allocate RTHandles properly
|
|
tempColorTexture = RTHandles.Alloc( cameraTextureDescriptor.width,
|
|
cameraTextureDescriptor.height ) ;
|
|
cameraRTHandle = RTHandles.Alloc( cameraTextureDescriptor.width,
|
|
cameraTextureDescriptor.height ) ;
|
|
|
|
cmd.GetTemporaryRT( Shader.PropertyToID( tempColorTexture?.name ),
|
|
cameraTextureDescriptor ) ; // Create temp texture
|
|
|
|
ConfigureTarget( tempColorTexture ) ;
|
|
}
|
|
|
|
[Obsolete( "This rendering path is for compatibility mode only (when Render Graph is disabled). " +
|
|
"Use Render Graph API instead.", false )]
|
|
public override void Execute( ScriptableRenderContext context,
|
|
ref RenderingData renderingData ) {
|
|
/*
|
|
if ( !outlineMaterial ) return ;
|
|
|
|
CommandBuffer cmd = CommandBufferPool.Get("Outline Pass");
|
|
cameraTarget = renderingData.cameraData.renderer.cameraColorTargetHandle; // ✅ Safe usage here
|
|
Blit(cmd, cameraTarget, tempColorTexture ); // Copy to temp texture
|
|
Blit(cmd, tempColorTexture, cameraTarget, outlineMaterial); // Apply outline effect
|
|
|
|
// Use new RTHandles API:
|
|
Blitter.BlitCameraTexture( cmd, cameraRTHandle, tempColorTexture, Vector4.one ) ;
|
|
Blitter.BlitCameraTexture( cmd, tempColorTexture,
|
|
cameraRTHandle, 1, outlineMaterial ) ;
|
|
|
|
// Copy to tempRT
|
|
|
|
context.ExecuteCommandBuffer( cmd ) ;
|
|
CommandBufferPool.Release( cmd ) ;
|
|
*/
|
|
if (!outlineMaterial || tempColorTexture == null || cameraRTHandle == null) return;
|
|
|
|
CommandBuffer cmd = CommandBufferPool.Get("Outline Pass");
|
|
|
|
// Copy camera render target into temp texture
|
|
Blitter.BlitCameraTexture( cmd, renderingData.cameraData.renderer.cameraColorTargetHandle,
|
|
tempColorTexture, Vector4.one ) ;
|
|
|
|
// Apply outline material and write back to camera target
|
|
Blitter.BlitCameraTexture(cmd, tempColorTexture,
|
|
renderingData.cameraData.renderer.cameraColorTargetHandle,
|
|
0, outlineMaterial ) ;
|
|
|
|
context.ExecuteCommandBuffer(cmd);
|
|
CommandBufferPool.Release(cmd);
|
|
}
|
|
|
|
public override void OnCameraCleanup( CommandBuffer cmd ) {
|
|
tempColorTexture?.Release();
|
|
cameraRTHandle?.Release();
|
|
}
|
|
}
|
|
|
|
public override void Create( ) {
|
|
if ( !outlineMaterial ) {
|
|
Debug.LogError( "Outline material is not assigned." ) ;
|
|
return ;
|
|
}
|
|
|
|
outlinePass = new( outlineMaterial ) {
|
|
renderPassEvent = RenderPassEvent.AfterRenderingOpaques, // ✅ Executes after opaque objects
|
|
} ;
|
|
}
|
|
|
|
public override void AddRenderPasses( ScriptableRenderer renderer,
|
|
ref RenderingData renderingData ) {
|
|
if ( outlinePass is not null ) renderer.EnqueuePass( outlinePass ) ;
|
|
}
|
|
}
|