修改URP扩展包

This commit is contained in:
MuHua-123
2025-04-11 11:13:21 +08:00
parent 909d9040a4
commit f6d1cd4d87
28 changed files with 11 additions and 11 deletions
@@ -0,0 +1,17 @@
{
"name": "MuHua.RenderPipeline",
"rootNamespace": "",
"references": [
"GUID:15fc0a57446b3144c949da3e2b9737a9",
"GUID:df380645f10b7bc4b97d4f5eb6303d95"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 5a23fafc293b0ed42911c6987db945b1
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c7895046b327cae439109de0bdd8793a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,85 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace MuHua {
/// <summary>
/// 轮廓渲染设置
/// </summary>
public class SRFOutlineSettings {
/// <summary> 辅助材质 </summary>
public Material unlit;
/// <summary> 轮廓材质 </summary>
public Material outline;
/// <summary> 混合材质 </summary>
public Material blend;
/// <summary> 渲染对象 </summary>
public Renderer[] renderObjs = new Renderer[0];
/// <summary> 渲染事件 </summary>
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
}
/// <summary>
/// 轮廓渲染通道
/// </summary>
public class SRFOutlinePass : ScriptableRenderPass {
public const string ProfilerTag = "Outline";
/// <summary> 渲染设置 </summary>
public SRFOutlineSettings settings;
/// <summary> 临时纹理 </summary>
public RTHandle tempRTHandle;
/// <summary> 轮廓纹理 </summary>
public RTHandle outlineRTHandle;
/// <summary> 渲染前设置 </summary>
public void Setup(SRFOutlineSettings settings, in RenderingData renderingData) {
this.settings = settings;
renderPassEvent = settings.renderPassEvent;
RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
descriptor.depthBufferBits = (int)DepthBits.None;
RenderingUtils.ReAllocateIfNeeded(ref outlineRTHandle, descriptor, name: "OutlineRT");
RenderingUtils.ReAllocateIfNeeded(ref tempRTHandle, descriptor, name: "TempRT");
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
if (renderingData.cameraData.cameraType == CameraType.SceneView || renderingData.cameraData.cameraType == CameraType.Preview) return;
CommandBuffer command = CommandBufferPool.Get(ProfilerTag);
// 设置渲染目标为tempRTHandle
CoreUtils.SetRenderTarget(command, tempRTHandle);
// 清除纹理内容
CoreUtils.ClearRenderTarget(command, ClearFlag.All, Color.clear);
// 绘制渲染物体
DrawRenderer(command, settings.unlit);
// 设置tempRTHandle为outline的主纹理
settings.outline.SetTexture("_MainTex", tempRTHandle);
// 渲染出轮廓
Blitter.BlitTexture(command, tempRTHandle, outlineRTHandle, settings.outline, 0);
// 设置outlineRTHandle为blend的主纹理
settings.blend.SetTexture("_MainTex", outlineRTHandle);
// 把缓冲区的内容渲染到renderingData
Blit(command, ref renderingData, settings.blend, 0);
// 执行缓冲区
context.ExecuteCommandBuffer(command);
// 释放
CommandBufferPool.Release(command);
tempRTHandle.Release();
outlineRTHandle?.Release();
}
public void DrawRenderer(CommandBuffer command, Material material) {
for (int i = 0; i < settings.renderObjs.Length; i++) {
Renderer renderer = settings.renderObjs[i];
if (renderer == null) { continue; }
command.DrawRenderer(renderer, material, 0, 0);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 61c5c213b0b1e584ba58e39b77340e05
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 821f2aa73c2761649a4466ee441ad752
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,70 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace MuHua {
/// <summary>
/// 渲染轮廓功能
/// </summary>
public class SRFOutline : ScriptableRendererFeature {
[Tooltip("轮廓大小")] public float size = 5;
[Tooltip("辅助材质")] public Material unlit;
[Tooltip("轮廓材质")] public Material outline;
[Tooltip("混合材质")] public Material blend;
/// <summary> 渲染Event </summary>
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
/// <summary> 渲染对象 </summary>
public static List<Renderer> RenderObjs = new List<Renderer>();
/// <summary> 是否有效 </summary>
public bool IsValid => unlit != null && outline != null && blend != null;
/// <summary> 渲染通道 </summary>
private SRFOutlinePass outlinePass;
/// <summary> 渲染设置 </summary>
private SRFOutlineSettings settings;
public override void Create() {
outlinePass = new SRFOutlinePass();
settings = new SRFOutlineSettings();
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {
if (!IsValid) { return; }
RenderObjs.RemoveAll(obj => obj == null);
settings.unlit = unlit;
settings.outline = outline;
settings.blend = blend;
settings.renderObjs = RenderObjs.ToArray();
settings.renderPassEvent = renderPassEvent;
outlinePass.Setup(settings, renderingData);
renderer.EnqueuePass(outlinePass);
Dispose();
}
/// <summary> 添加到渲染队列 </summary>
public static void Add(Renderer renderer, bool isClear) {
if (isClear) { Clear(); }
if (RenderObjs.Contains(renderer)) { RenderObjs.Add(renderer); }
}
/// <summary> 添加到渲染队列 </summary>
public static void Add(Renderer[] renderers, bool isClear) {
if (isClear) { Clear(); }
RenderObjs.AddRange(renderers);
}
/// <summary> 移出渲染队列 </summary>
public static void Remove(Renderer renderer) {
if (RenderObjs.Contains(renderer)) { RenderObjs.Remove(renderer); }
}
/// <summary> 清空队列 </summary>
public static void Clear() {
RenderObjs?.Clear();
RenderObjs = new List<Renderer>();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5ff7eee3bc7d4944ca16babef3b86bac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: