Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.Universal;
- namespace HarryH.Outline
- {
- public class ShaderPassToTextureRenderer : ScriptableRenderPass
- {
- public OutlineSettings settings;
- string profilerTag;
- ShaderTagId shaderTagId;
- FilteringSettings filteringSettings;
- RenderQueueRange renderQueue;
- int textureId;
- RenderTargetIdentifier texture;
- bool createTexture;
- int textureDepth;
- RenderTextureFormat textureFormat;
- public ShaderPassToTextureRenderer(string name, string shaderName, RenderQueueRange renderQueue, string textureName, RenderPassEvent renderPassEvent, bool createTexture, int depth, RenderTextureFormat format)
- {
- this.renderPassEvent = renderPassEvent;
- profilerTag = name;
- shaderTagId = new ShaderTagId(shaderName);
- this.renderQueue = renderQueue;
- filteringSettings = new FilteringSettings(renderQueue, ~0);
- textureId = Shader.PropertyToID(textureName);
- texture = new RenderTargetIdentifier(textureId);
- this.createTexture = createTexture;
- textureDepth = depth;
- textureFormat = format;
- }
- public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
- {
- if (createTexture) cmd.GetTemporaryRT(textureId, cameraTextureDescriptor.width, cameraTextureDescriptor.height, textureDepth, FilterMode.Point, textureFormat);
- ConfigureTarget(texture);
- if (createTexture) ConfigureClear(ClearFlag.All, Color.black);
- }
- public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
- {
- CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
- SortingCriteria sortingCriteria = renderQueue == RenderQueueRange.opaque ? renderingData.cameraData.defaultOpaqueSortFlags : SortingCriteria.CommonTransparent;
- DrawingSettings drawingSettings = CreateDrawingSettings(shaderTagId, ref renderingData, sortingCriteria);
- context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings);
- context.ExecuteCommandBuffer(cmd);
- CommandBufferPool.Release(cmd);
- }
- public override void FrameCleanup(CommandBuffer cmd)
- {
- if (createTexture) cmd.ReleaseTemporaryRT(textureId);
- }
- }
- public class FullscreenQuadRenderer : ScriptableRenderPass
- {
- string profilerTag;
- public Material material;
- public ScriptableRenderer renderer;
- bool isDepth;
- int textureId = -1;
- RenderTargetIdentifier texture;
- bool newTexture;
- public FullscreenQuadRenderer(string name)
- {
- renderPassEvent = RenderPassEvent.BeforeRenderingTransparents;
- profilerTag = name;
- }
- public void Init(Material material, ScriptableRenderer newRenderer, bool depth)
- {
- this.material = material;
- renderer = newRenderer;
- textureId = -1;
- isDepth = depth;
- }
- public void Init(Material material, string textureName, bool newTexture)
- {
- this.material = material;
- textureId = Shader.PropertyToID(textureName);
- texture = new RenderTargetIdentifier(textureId);
- renderer = null;
- this.newTexture = newTexture;
- }
- public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
- {
- if (textureId != -1 && newTexture)
- {
- cmd.GetTemporaryRT(textureId, cameraTextureDescriptor.width, cameraTextureDescriptor.height, 24, FilterMode.Point, RenderTextureFormat.ARGBFloat);
- }
- else if (textureId == -1)
- {
- texture = isDepth ? renderer.cameraDepth : renderer.cameraColorTarget;
- }
- ConfigureTarget(texture);
- if (newTexture) ConfigureClear(ClearFlag.All, Color.black);
- }
- public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
- {
- if (material == null) return;
- CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
- Camera camera = renderingData.cameraData.camera;
- cmd.SetGlobalTexture("_MainTex", texture);
- cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
- cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, material);
- cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
- context.ExecuteCommandBuffer(cmd);
- CommandBufferPool.Release(cmd);
- }
- public override void FrameCleanup(CommandBuffer cmd)
- {
- if (textureId != -1 && newTexture) cmd.ReleaseTemporaryRT(textureId);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement