Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a modified version of a Scriptable Render Pass written by Harry Heath.
- // Twitter: https://twitter.com/harryh___h/status/1328024692431540224
- // Pastebin: https://pastebin.com/LstBHRZF
- // The original code was used in a recreation of a Mako illustration:
- // https://twitter.com/harryh___h/status/1328006632102526976
- using System.Linq;
- using UnityEngine;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.Universal;
- namespace HarryH.Outline
- {
- public class ShaderPassToTextureRenderer : ScriptableRenderPass
- {
- public OutlineSettings settings;
- private string[] _profilerTag;
- private ShaderTagId[] _shaderTagId;
- private FilteringSettings _filteringSettings;
- private RenderQueueRange _renderQueue;
- private int[] _textureId;
- private string[] _textureNames;
- RenderTargetIdentifier[] _textureRTId;
- private bool[] _createTexture;
- int[] _textureDepth;
- RenderTextureFormat[] _textureFormats;
- public ShaderPassToTextureRenderer(OutlinePassTarget[] outlinePassTargets, RenderQueueRange renderQueue, RenderPassEvent renderPassEvent)
- {
- this.renderPassEvent = renderPassEvent;
- _filteringSettings = new FilteringSettings(renderQueue, ~0);
- _renderQueue = renderQueue;
- for (var i = 0; i < outlinePassTargets.Length; i++)
- {
- _profilerTag[i] = outlinePassTargets[i].name;
- _shaderTagId[i] = new ShaderTagId(outlinePassTargets[i].shaderName);
- _textureId[i] = Shader.PropertyToID(outlinePassTargets[i].textureName);
- _textureRTId = _textureId.Select(texId => new RenderTargetIdentifier(texId)).ToArray();
- _createTexture[i] = outlinePassTargets[i].createTexture;
- _textureDepth[i] = outlinePassTargets[i].depth;
- _textureFormats[i] = outlinePassTargets[i].format;
- }
- }
- public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
- {
- foreach (var createTexBool in _createTexture)
- {
- if (createTexBool)
- for (var texName = 0; texName < _textureNames.Length; texName++)
- {
- cmd.GetTemporaryRT(_textureId[texName], cameraTextureDescriptor.width, cameraTextureDescriptor.height, _textureDepth[texName],
- FilterMode.Point,
- _textureFormats[texName]);
- }
- foreach (var targetID in _textureRTId)
- {
- ConfigureTarget(new[] { targetID }, depthAttachment: _textureRTId[^1]);
- }
- if (createTexBool) ConfigureClear(ClearFlag.All, Color.black);
- }
- }
- public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
- {
- foreach (var tagName in _profilerTag)
- {
- var cmd = CommandBufferPool.Get(tagName);
- var sortingCriteria = _renderQueue == RenderQueueRange.opaque
- ? renderingData.cameraData.defaultOpaqueSortFlags
- : SortingCriteria.CommonTransparent;
- foreach (var tagId in _shaderTagId)
- {
- var drawingSettings = CreateDrawingSettings(tagId, ref renderingData, sortingCriteria);
- context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref _filteringSettings);
- }
- context.ExecuteCommandBuffer(cmd);
- CommandBufferPool.Release(cmd);
- }
- }
- public override void FrameCleanup(CommandBuffer cmd)
- {
- foreach (var createTexBool in _createTexture)
- {
- for (var texName = 0; texName < _textureNames.Length; texName++)
- {
- if (createTexBool) cmd.ReleaseTemporaryRT(_textureId[texName]);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment