123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System.Collections.Generic;
- using UnityEngine.Rendering.Universal;
- using UnityEngine.Rendering;
- using UnityEngine.Scripting.APIUpdating;
- namespace UnityEngine.Experimental.Rendering.Universal
- {
- [MovedFrom("UnityEngine.Experimental.Rendering.LWRP")] public class RenderObjectsPass : ScriptableRenderPass
- {
- RenderQueueType renderQueueType;
- FilteringSettings m_FilteringSettings;
- RenderObjects.CustomCameraSettings m_CameraSettings;
- string m_ProfilerTag;
- ProfilingSampler m_ProfilingSampler;
- public Material overrideMaterial { get; set; }
- public int overrideMaterialPassIndex { get; set; }
- List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>();
- public void SetDetphState(bool writeEnabled, CompareFunction function = CompareFunction.Less)
- {
- m_RenderStateBlock.mask |= RenderStateMask.Depth;
- m_RenderStateBlock.depthState = new DepthState(writeEnabled, function);
- }
- public void SetStencilState(int reference, CompareFunction compareFunction, StencilOp passOp, StencilOp failOp, StencilOp zFailOp)
- {
- StencilState stencilState = StencilState.defaultValue;
- stencilState.enabled = true;
- stencilState.SetCompareFunction(compareFunction);
- stencilState.SetPassOperation(passOp);
- stencilState.SetFailOperation(failOp);
- stencilState.SetZFailOperation(zFailOp);
- m_RenderStateBlock.mask |= RenderStateMask.Stencil;
- m_RenderStateBlock.stencilReference = reference;
- m_RenderStateBlock.stencilState = stencilState;
- }
- RenderStateBlock m_RenderStateBlock;
- public RenderObjectsPass(string profilerTag, RenderPassEvent renderPassEvent, string[] shaderTags, RenderQueueType renderQueueType, int layerMask, RenderObjects.CustomCameraSettings cameraSettings)
- {
- m_ProfilerTag = profilerTag;
- m_ProfilingSampler = new ProfilingSampler(profilerTag);
- this.renderPassEvent = renderPassEvent;
- this.renderQueueType = renderQueueType;
- this.overrideMaterial = null;
- this.overrideMaterialPassIndex = 0;
- RenderQueueRange renderQueueRange = (renderQueueType == RenderQueueType.Transparent)
- ? RenderQueueRange.transparent
- : RenderQueueRange.opaque;
- m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
- if (shaderTags != null && shaderTags.Length > 0)
- {
- foreach (var passName in shaderTags)
- m_ShaderTagIdList.Add(new ShaderTagId(passName));
- }
- else
- {
- m_ShaderTagIdList.Add(new ShaderTagId("UniversalForward"));
- m_ShaderTagIdList.Add(new ShaderTagId("LightweightForward"));
- m_ShaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit"));
- }
- m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
- m_CameraSettings = cameraSettings;
- }
- public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
- {
- SortingCriteria sortingCriteria = (renderQueueType == RenderQueueType.Transparent)
- ? SortingCriteria.CommonTransparent
- : renderingData.cameraData.defaultOpaqueSortFlags;
- DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);
- drawingSettings.overrideMaterial = overrideMaterial;
- drawingSettings.overrideMaterialPassIndex = overrideMaterialPassIndex;
- ref CameraData cameraData = ref renderingData.cameraData;
- Camera camera = cameraData.camera;
- // In case of camera stacking we need to take the viewport rect from base camera
- Rect pixelRect = renderingData.cameraData.pixelRect;
- float cameraAspect = (float) pixelRect.width / (float) pixelRect.height;
- CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
- using (new ProfilingScope(cmd, m_ProfilingSampler))
- {
- context.ExecuteCommandBuffer(cmd);
- cmd.Clear();
- if (m_CameraSettings.overrideCamera)
- {
- Matrix4x4 projectionMatrix = Matrix4x4.Perspective(m_CameraSettings.cameraFieldOfView, cameraAspect,
- camera.nearClipPlane, camera.farClipPlane);
- Matrix4x4 viewMatrix = camera.worldToCameraMatrix;
- Vector4 cameraTranslation = viewMatrix.GetColumn(3);
- viewMatrix.SetColumn(3, cameraTranslation + m_CameraSettings.offset);
- cmd.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
- context.ExecuteCommandBuffer(cmd);
- }
- context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings,
- ref m_RenderStateBlock);
- if (m_CameraSettings.overrideCamera && m_CameraSettings.restoreCamera)
- {
- cmd.Clear();
- cmd.SetViewProjectionMatrices(cameraData.viewMatrix, cameraData.projectionMatrix);
- }
- }
- context.ExecuteCommandBuffer(cmd);
- CommandBufferPool.Release(cmd);
- }
- }
- }
|