public RenderTargetIdentifier(Texture tex) { bool flag = tex == null; if (flag) { this.m_Type = BuiltinRenderTextureType.None; } else { bool flag2 = tex is RenderTexture; if (flag2) { this.m_Type = BuiltinRenderTextureType.RenderTexture; } else { this.m_Type = BuiltinRenderTextureType.BindableTexture; } } this.m_BufferPointer = IntPtr.Zero; this.m_NameID = -1; this.m_InstanceID = (tex ? tex.GetInstanceID() : 0); this.m_MipLevel = 0; this.m_CubeFace = CubemapFace.Unknown; this.m_DepthSlice = 0; }
/// <summary>Only add command buffer if there isn't /// already one of the same name added.</summary> public CommandBuffer Add(Light light, BuiltinRenderTextureType builtinRenderTexture = BuiltinRenderTextureType.CurrentActive) { if (light != null && !string.IsNullOrEmpty(name)) { //we must assign name to a local string here in order to use it //in the Linq query, as struct fields aren't allowed string buffName = name; buffer = light.GetCommandBuffers(lightEvent).ToList().Find(b => b.name == buffName); if (buffer == null) { buffer = new CommandBuffer() { name = buffName }; if (builtinRenderTexture != BuiltinRenderTextureType.None && !string.IsNullOrEmpty(texProperty)) { buffer.SetGlobalTexture(texProperty, builtinRenderTexture); } light.AddCommandBuffer(lightEvent, buffer); } return(buffer); } else { return(null); } }
public void SetSource0(BuiltinRenderTextureType type) { source0 = new RenderTargetIdentifier(type); source0type = type; source0texture = null; source0propertyId = null; }
public void SetTarget1(BuiltinRenderTextureType type) { target1 = new RenderTargetIdentifier(type); target1type = type; target1texture = null; target1propertyId = null; }
private static RenderTextureFormat GBufferFormat(BuiltinRenderTextureType gbuffer, bool hdr) { // https://docs.unity3d.com/Manual/RenderTech-DeferredShading.html switch (gbuffer) { // RT0, ARGB32 format: Diffuse color (RGB), occlusion(A). case BuiltinRenderTextureType.GBuffer0: return(RenderTextureFormat.ARGB32); // RT1, ARGB32 format: Specular color (RGB), roughness (A). case BuiltinRenderTextureType.GBuffer1: return(RenderTextureFormat.ARGB32); // RT2, ARGB2101010 format: World space normal (RGB), unused (A). case BuiltinRenderTextureType.GBuffer2: return(RenderTextureFormat.ARGB2101010); // RT3, Light Buffer case BuiltinRenderTextureType.GBuffer3: return(hdr ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB2101010); default: throw new ArgumentException("Provided render texture type is not a GBuffer", "gbuffer"); } }
public RenderTargetIdentifier(RenderTargetIdentifier renderTargetIdentifier, int mipLevel, CubemapFace cubeFace = CubemapFace.Unknown, int depthSlice = 0) { this.m_Type = renderTargetIdentifier.m_Type; this.m_NameID = renderTargetIdentifier.m_NameID; this.m_InstanceID = renderTargetIdentifier.m_InstanceID; this.m_BufferPointer = renderTargetIdentifier.m_BufferPointer; this.m_MipLevel = mipLevel; this.m_CubeFace = cubeFace; this.m_DepthSlice = depthSlice; }
public RenderTargetIdentifier(RenderBuffer buf, int mipLevel = 0, CubemapFace cubeFace = CubemapFace.Unknown, int depthSlice = 0) { this.m_Type = BuiltinRenderTextureType.BufferPtr; this.m_NameID = -1; this.m_InstanceID = buf.m_RenderTextureInstanceID; this.m_BufferPointer = buf.m_BufferPtr; this.m_MipLevel = mipLevel; this.m_CubeFace = cubeFace; this.m_DepthSlice = depthSlice; }
public RenderTargetIdentifier(BuiltinRenderTextureType type) { this.m_Type = type; this.m_NameID = -1; this.m_InstanceID = 0; this.m_BufferPointer = IntPtr.Zero; this.m_MipLevel = 0; this.m_CubeFace = CubemapFace.Unknown; this.m_DepthSlice = 0; }
public RenderTargetIdentifier(string name) { this.m_Type = BuiltinRenderTextureType.PropertyName; this.m_NameID = Shader.PropertyToID(name); this.m_InstanceID = 0; this.m_BufferPointer = IntPtr.Zero; this.m_MipLevel = 0; this.m_CubeFace = CubemapFace.Unknown; this.m_DepthSlice = 0; }
public RenderTargetIdentifier(int nameID, int mipLevel = 0, CubemapFace cubeFace = CubemapFace.Unknown, int depthSlice = 0) { this.m_Type = BuiltinRenderTextureType.PropertyName; this.m_NameID = nameID; this.m_InstanceID = 0; this.m_BufferPointer = IntPtr.Zero; this.m_MipLevel = mipLevel; this.m_CubeFace = cubeFace; this.m_DepthSlice = depthSlice; }
// constructors public RenderTargetIdentifier(BuiltinRenderTextureType type) { m_Type = type; m_NameID = -1; // FastPropertyName kInvalidIndex m_InstanceID = 0; m_BufferPointer = IntPtr.Zero; m_MipLevel = 0; m_CubeFace = CubemapFace.Unknown; m_DepthSlice = 0; }
public CapturePassMaterial(CameraEvent when = CameraEvent.AfterEverything, BuiltinRenderTextureType source = BuiltinRenderTextureType.CurrentActive) { this.When = when; this.Source = source; this._Material = null; this._RenderTexture = null; this._SupportsAntialiasing = false; this._NeedsRescale = false; this._TextureId = 0; }
public RenderTargetIdentifier(Texture tex, int mipLevel = 0, CubemapFace cubeFace = CubemapFace.Unknown, int depthSlice = 0) { if (tex == null) { this.m_Type = BuiltinRenderTextureType.None; } else if (tex is RenderTexture) { this.m_Type = BuiltinRenderTextureType.RenderTexture; } else { this.m_Type = BuiltinRenderTextureType.BindableTexture; } this.m_BufferPointer = IntPtr.Zero; this.m_NameID = -1; this.m_InstanceID = ((!tex) ? 0 : tex.GetInstanceID()); this.m_MipLevel = mipLevel; this.m_CubeFace = cubeFace; this.m_DepthSlice = depthSlice; }
public RenderTargetIdentifier(Texture tex) { if (tex == null) { this.m_Type = BuiltinRenderTextureType.None; this.m_InstanceID = 0; this.m_BufferPointer = IntPtr.Zero; } else if (tex is RenderTexture) { this.m_Type = BuiltinRenderTextureType.BufferPtr; this.m_BufferPointer = ((RenderTexture)tex).colorBuffer.m_BufferPtr; } else { this.m_Type = BuiltinRenderTextureType.BindableTexture; this.m_BufferPointer = IntPtr.Zero; } this.m_NameID = -1; this.m_InstanceID = ((!tex) ? 0 : tex.GetInstanceID()); this.m_MipLevel = 0; this.m_CubeFace = CubemapFace.Unknown; this.m_DepthSlice = 0; }
public Settings(float blur, CameraEvent camEvent, BuiltinRenderTextureType blurTarget) { this.blur = blur; this.camEvent = camEvent; this.blurTarget = blurTarget; }
/// <summary> /// Initializes a new instance of the <see cref="OutlineRenderer"/> struct. /// </summary> public OutlineRenderer(CommandBuffer commandBuffer, BuiltinRenderTextureType dst) : this(commandBuffer, new RenderTargetIdentifier(dst)) { }
/// <summary> /// <para>Creates a render target identifier.</para> /// </summary> /// <param name="type">Built-in temporary render texture type.</param> /// <param name="name">Temporary render texture name.</param> /// <param name="nameID">Temporary render texture name (as integer, see Shader.PropertyToID).</param> /// <param name="tex">RenderTexture or Texture object to use.</param> public RenderTargetIdentifier(string name) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = Shader.PropertyToID(name); this.m_InstanceID = 0; }
/// <summary> /// <para>Creates a render target identifier.</para> /// </summary> /// <param name="rt">RenderTexture object to use.</param> /// <param name="type">Built-in temporary render texture type.</param> /// <param name="name">Temporary render texture name.</param> /// <param name="nameID">Temporary render texture name (as integer, see Shader.PropertyToID).</param> public RenderTargetIdentifier(RenderTexture rt) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = -1; this.m_InstanceID = !(bool)((Object)rt) ? 0 : rt.GetInstanceID(); }
public RenderTargetIdentifier(Texture tex) { this.m_Type = ((!(tex == null) && !(tex is RenderTexture)) ? BuiltinRenderTextureType.BindableTexture : BuiltinRenderTextureType.None); this.m_NameID = -1; this.m_InstanceID = ((!tex) ? 0 : tex.GetInstanceID()); }
public RenderTargetIdentifier(int nameID) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = nameID; this.m_InstanceID = 0; }
private int CopyGBufferTexture([NotNull] CommandBuffer cmd, [NotNull] string shaderParameterName, BuiltinRenderTextureType texture, bool hdr) { if (cmd == null) { throw new ArgumentNullException("cmd"); } var id = Shader.PropertyToID(shaderParameterName); if (XRSettings.enabled && _camera.stereoEnabled) { var desc = XRSettings.eyeTextureDesc; desc.colorFormat = GBufferFormat(texture, hdr); desc.depthBufferBits = 0; cmd.GetTemporaryRT(id, desc, FilterMode.Point); } else { cmd.GetTemporaryRT(id, -1, -1, 0, FilterMode.Point, GBufferFormat(texture, hdr)); } cmd.Blit(texture, id, _blit); return(id); }
static void SetupCaptureRequest ( AsyncRequest <CaptureState> req, Channel channel, Camera camera, CameraEvent cameraEvent, BuiltinRenderTextureType source, GraphicsFormat format, Func <AsyncRequest <CaptureState>, AsyncRequest <CaptureState> .Result> functor, bool flipY ) { if (functor != null) { // declared for possible capture, to avoid use from other threads. var cameraTargetTexture = camera.targetTexture; RenderTexture target1 = null; RenderTexture target2 = null; Action ReleaseTargets = () => { if (target1 != null && target1 != cameraTargetTexture) { RenderTexture.ReleaseTemporary(target1); target1 = null; } if (target2 != null) { Debug.Assert(target2 != cameraTargetTexture); RenderTexture.ReleaseTemporary(target2); target2 = null; } }; Material depthMaterial = null; if (source == BuiltinRenderTextureType.Depth) { depthMaterial = SelectDepthShaderVariant(format); } #if UNITY_2019_3_OR_NEWER if (scriptableRenderPipeline) { if (CaptureOptions.useBatchReadback) { QueueForAsyncBatchReadback(req, channel, functor, SetupRenderTargets(ref target1, ref target2, camera, null, format, cameraTargetTexture, depthMaterial, flipY)); } else { req.data.SetFunctor(channel, (AsyncRequest <CaptureState> r) => { var target = SetupRenderTargets(ref target1, ref target2, camera, null, format, cameraTargetTexture, depthMaterial, flipY); if (GraphicsUtilities.SupportsAsyncReadback()) { AsyncGPUReadback.Request(target, 0, (AsyncGPUReadbackRequest request) => { ReleaseTargets(); if (request.hasError) { req.error = true; } else { if (functor != null) { req.data.SetBuffer(channel, request.GetData <byte>().ToArray()); req.Enqueue(functor); req.Execute(); } } }); } else { r.data.SetBuffer(channel, GraphicsUtilities.GetPixelsSlow(target)); ReleaseTargets(); req.Enqueue(functor); req.Execute(); } return(AsyncRequest.Result.None); }); } } else #endif // UNITY_2019_3_OR_NEWER { req.data.SetFunctor(channel, functor); CommandBuffer commandBuffer = GetCommandBufferForCamera(cameraEvent, camera); commandBuffer.name = $"CaptureCamera.{channel.ToString()}"; var target = SetupRenderTargets(ref target1, ref target2, camera, commandBuffer, format, cameraTargetTexture, depthMaterial, flipY); if (GraphicsUtilities.SupportsAsyncReadback()) { #if UNITY_2019_3_OR_NEWER if (CaptureOptions.useBatchReadback) { QueueForAsyncBatchReadback(req, channel, functor, target); ReleaseTargets(); } else #endif { commandBuffer.RequestAsyncReadback(target, (AsyncGPUReadbackRequest request) => { commandBuffer.Clear(); if (request.hasError) { req.error = true; } else { functor = req.data.SetFunctor(channel, null); if (functor != null) { req.data.SetBuffer(channel, request.GetData <byte>().ToArray()); req.Enqueue(functor); req.Execute(); } } ReleaseTargets(); }); } } else { Func <AsyncRequest <CaptureState>, AsyncRequest <CaptureState> .Result> wrapper; #if UNITY_2019_3_OR_NEWER if (CaptureOptions.useBatchReadback) { wrapper = (AsyncRequest <CaptureState> r) => { BatchReadback.Instance().QueueReadback(target, data => { r.data.SetBuffer(channel, data); ReleaseTargets(); r.Enqueue(functor); r.Execute(); return(AsyncRequest.Result.Completed); }); return(AsyncRequest.Result.Completed); }; } else #endif // UNITY_2019_3_OR_NEWER { wrapper = (AsyncRequest <CaptureState> r) => { r.data.SetBuffer(channel, GraphicsUtilities.GetPixelsSlow(target)); ReleaseTargets(); r.Enqueue(functor); r.Execute(); return(AsyncRequest.Result.Completed); }; } req.Enqueue(wrapper); req.Execute(AsyncRequest.ExecutionContext.EndOfFrame); } } } }
/// <summary> /// <para>Creates a render target identifier.</para> /// </summary> /// <param name="type">Built-in temporary render texture type.</param> /// <param name="name">Temporary render texture name.</param> /// <param name="nameID">Temporary render texture name (as integer, see Shader.PropertyToID).</param> /// <param name="tex">RenderTexture or Texture object to use.</param> public RenderTargetIdentifier(Texture tex) { this.m_Type = ((tex != null) && !(tex is RenderTexture)) ? BuiltinRenderTextureType.BindableTexture : BuiltinRenderTextureType.None; this.m_NameID = -1; this.m_InstanceID = (tex == null) ? 0 : tex.GetInstanceID(); }
/// <summary> /// <para>Creates a render target identifier.</para> /// </summary> /// <param name="type">Built-in temporary render texture type.</param> /// <param name="name">Temporary render texture name.</param> /// <param name="nameID">Temporary render texture name (as integer, see Shader.PropertyToID).</param> /// <param name="tex">RenderTexture or Texture object to use.</param> public RenderTargetIdentifier(int nameID) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = nameID; this.m_InstanceID = 0; }
/// <summary> /// Initializes a new instance of the <see cref="OutlineRenderer"/> struct. /// </summary> /// <param name="commandBuffer">A <see cref="CommandBuffer"/> to render the effect to. It should be cleared manually (if needed) before passing to this method.</param> /// <param name="rt">Render target.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="commandBuffer"/> is <see langword="null"/>.</exception> public OutlineRenderer(CommandBuffer commandBuffer, BuiltinRenderTextureType rt) : this(commandBuffer, rt, rt) { }
public RenderTargetIdentifier(RenderTexture rt) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = -1; this.m_InstanceID = (rt == null) ? 0 : rt.GetInstanceID(); }
public RenderTargetIdentifier(BuiltinRenderTextureType type) { this.m_Type = type; this.m_NameID = -1; this.m_InstanceID = 0; }
public RenderTargetIdentifier(RenderTexture rt) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = 0; this.m_InstanceID = ((!rt) ? 0 : rt.GetInstanceID()); }
/// <summary> /// <para>Creates a render target identifier.</para> /// </summary> /// <param name="rt">RenderTexture object to use.</param> /// <param name="type">Built-in temporary render texture type.</param> /// <param name="name">Temporary render texture name.</param> /// <param name="nameID">Temporary render texture name (as integer, see Shader.PropertyToID).</param> public RenderTargetIdentifier(RenderTexture rt) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = -1; this.m_InstanceID = !(bool) ((Object) rt) ? 0 : rt.GetInstanceID(); }
/// <summary> /// <para>Creates a render target identifier.</para> /// </summary> /// <param name="type">Built-in temporary render texture type.</param> /// <param name="name">Temporary render texture name.</param> /// <param name="nameID">Temporary render texture name (as integer, see Shader.PropertyToID).</param> /// <param name="tex">RenderTexture or Texture object to use.</param> public RenderTargetIdentifier(BuiltinRenderTextureType type) { this.m_Type = type; this.m_NameID = -1; this.m_InstanceID = 0; }
public RenderTargetIdentifier(string name) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = Shader.PropertyToID(name); this.m_InstanceID = 0; }
private void doRender(int w, int h, RenderTargetIdentifier source, RenderTargetIdentifier destination, BuiltinRenderTextureType rt = BuiltinRenderTextureType.None) { downSample = (float)Math.Floor(downSample * 2.0f) * 0.5f; Camera camera = GetComponent <Camera>(); refreshConstant(); material.SetFloat("screenWidth", (float)camera.pixelWidth / downSample); material.SetFloat("screenHeight", (float)camera.pixelHeight / downSample); material.SetFloat("Distribution", FrequencyMultiplier == FrequencyMultiplierEnum.Single ? SampleDistribution * 2.0f : SampleDistribution); if (SamplesAmount == SamplesAmountEnum.Low) { sampleLow(); } else if (SamplesAmount == SamplesAmountEnum.Med) { sampleMed(); } else if (SamplesAmount == SamplesAmountEnum.High) { sampleHigh(); } else if (SamplesAmount == SamplesAmountEnum.Ultra) { sampleUltra(); } if (ColorBleed) { cmd.EnableShaderKeyword("WAOColorBleed"); } else { cmd.DisableShaderKeyword("WAOColorBleed"); } Matrix4x4 P = camera.projectionMatrix; //Matrix4x4 invP = P.inverse; Vector4 projInfo = new Vector4 ((-2.0f / P[0, 0]), (-2.0f / P[1, 1]), ((1.0f - P[0, 2]) / P[0, 0]), ((1.0f + P[1, 2]) / P[1, 1])); #if UNITY_5_5_OR_NEWER || UNITY_5_5 || UNITY_5_6 if (camera.stereoEnabled) { Matrix4x4 P0 = camera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left); Matrix4x4 P1 = camera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right); Vector4 projInfo0 = new Vector4 ((-2.0f / (P0[0, 0])), (-2.0f / (P0[1, 1])), ((1.0f - P0[0, 2]) / P0[0, 0]), ((1.0f + P0[1, 2]) / P0[1, 1])); Vector4 projInfo1 = new Vector4 ((-2.0f / (P1[0, 0])), (-2.0f / (P1[1, 1])), ((1.0f - P1[0, 2]) / P1[0, 0]), ((1.0f + P1[1, 2]) / P1[1, 1])); material.SetVector("_ProjInfoLeft", projInfo0); material.SetVector("_ProjInfoRight", projInfo1); } #endif material.SetVector("_ProjInfo", projInfo); // used for unprojection //material.SetMatrix ("_InvProj", camera.cameraToWorldMatrix); // Render into texture var rtAO_id = Shader.PropertyToID("WAO_rtAO"); var rtAO = new RenderTargetIdentifier(rtAO_id); cmd.GetTemporaryRT(rtAO_id, (int)(w / downSample), (int)(h / downSample), 0, ColorBleed ? FilterMode.Point : FilterMode.Trilinear, ColorBleed ? RenderTextureFormat.ARGBFloat : RenderTextureFormat.ARGBHalf); material.SetFloat("aoexp", AO_Exponential); if (FrequencyMultiplier == FrequencyMultiplierEnum.Single) { material.SetFloat("Radius", aoRadius); if (NormalSource == NormalSourceEnum.Use_GBuffers_Normal) { cmd.Blit(ColorBleed ? source : BuiltinRenderTextureType.None, rtAO, material, WAOGB); } else if (NormalSource == NormalSourceEnum.Use_NormalDepthTexture) { cmd.Blit(ColorBleed ? source : BuiltinRenderTextureType.None, rtAO, material, WAO); } else { cmd.Blit(ColorBleed ? source : BuiltinRenderTextureType.None, rtAO, material, WAOGN); } } else if (FrequencyMultiplier == FrequencyMultiplierEnum.Double) { material.SetFloat("Radius", aoRadius); material.SetFloat("Radius2", aoRadius + SecondFrequencyStep); if (NormalSource == NormalSourceEnum.Use_GBuffers_Normal) { cmd.Blit(ColorBleed ? source : BuiltinRenderTextureType.None, rtAO, material, FrqMulGB); } else if (NormalSource == NormalSourceEnum.Use_NormalDepthTexture) { cmd.Blit(ColorBleed ? source : BuiltinRenderTextureType.None, rtAO, material, FrqMul); } else { cmd.Blit(ColorBleed ? source : BuiltinRenderTextureType.None, rtAO, material, FrqMulGN); } } // Blur var blurTex_id = Shader.PropertyToID("WAO_blurAO"); var blurTex = new RenderTargetIdentifier(blurTex_id); cmd.GetTemporaryRT(blurTex_id, w, h, 0, ColorBleed ? FilterMode.Point : FilterMode.Trilinear, ColorBleed ? RenderTextureFormat.ARGBFloat : RenderTextureFormat.ARGBHalf); if (useBlur) { cmd.SetGlobalFloat("blurRadius", blurRadius / (float)camera.pixelWidth); cmd.Blit(rtAO, blurTex, material, ColorBleed ? BlurBleedH : BlurAdvancedH); cmd.SetGlobalFloat("blurRadius", blurRadius / (float)camera.pixelHeight); cmd.Blit(blurTex, rtAO, material, ColorBleed ? BlurBleedV : BlurAdvancedV); } if (showAOImageOnly) { // Final composite cmd.Blit(null, destination, material, AOOnly); } else { cmd.Blit(source, destination, material, Composite); } cmd.ReleaseTemporaryRT(rtAO_id); cmd.ReleaseTemporaryRT(blurTex_id); }
public RenderTargetIdentifier(RenderTexture rt) { this.m_Type = BuiltinRenderTextureType.None; this.m_NameID = -1; this.m_InstanceID = ((!rt) ? 0 : rt.GetInstanceID()); }
public void Render(Camera camera) { var cam = camera; if (!cam) { return; } // before lighting buffer CommandBuffer bufferBeforeLighting = null; if (m_Cameras.ContainsKey(cam)) { bufferBeforeLighting = m_Cameras[cam]; if (LockRebuild) { return; } bufferBeforeLighting.Clear(); } else { bufferBeforeLighting = new CommandBuffer(); bufferBeforeLighting.name = "Deferred decals"; m_Cameras[cam] = bufferBeforeLighting; // set this command buffer to be executed just before deferred lighting pass // in the camera var buffers = cam.GetCommandBuffers(CameraEvent.BeforeReflections); for (int i = 0; i < buffers.Length; i++) { if (buffers[i].name.Equals("Deferred decals")) { cam.RemoveCommandBuffer(CameraEvent.BeforeReflections, buffers[i]); } } cam.AddCommandBuffer(CameraEvent.BeforeReflections, bufferBeforeLighting); } if (UseExclusionMask) { if (!m_ExclusionMasks.TryGetValue(cam, out exclusionMaskRenderTarget)) { exclusionMaskRenderTarget = new RenderTexture(cam.pixelWidth, cam.pixelHeight, 16, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear); exclusionMaskRenderTarget.filterMode = FilterMode.Point; exclusionMaskRenderTarget.Create(); exclusionMaskRenderTarget.name = "Exclusion Mask for camera " + cam.name; m_ExclusionMasks.Add(cam, exclusionMaskRenderTarget); } if (exclusionMaskRenderTarget != null && (cam.pixelWidth != exclusionMaskRenderTarget.width || cam.pixelHeight != exclusionMaskRenderTarget.height)) { m_ExclusionMasks.Remove(cam); DestroyImmediate(exclusionMaskRenderTarget, true); exclusionMaskRenderTarget = new RenderTexture(cam.pixelWidth, cam.pixelHeight, 16, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear); exclusionMaskRenderTarget.filterMode = FilterMode.Point; exclusionMaskRenderTarget.Create(); exclusionMaskRenderTarget.name = "Exclusion Mask for camera " + cam.name; m_ExclusionMasks.Add(cam, exclusionMaskRenderTarget); } exclusionCamera.CopyFrom(cam); exclusionCamera.depth = cam.depth - 0.01f; exclusionCamera.transform.position = cam.transform.position; exclusionCamera.transform.rotation = cam.transform.rotation; exclusionCamera.cullingMask = ExclusionMask; exclusionCamera.targetTexture = exclusionMaskRenderTarget; exclusionCamera.renderingPath = RenderingPath.Forward; exclusionCamera.clearFlags = CameraClearFlags.Color; exclusionCamera.backgroundColor = Color.clear; exclusionCamera.RenderWithShader(depthShader, "RenderType"); bufferBeforeLighting.EnableShaderKeyword("EXCLUSIONMASK"); bufferBeforeLighting.SetGlobalTexture("_ExclusionMask", exclusionMaskRenderTarget); } else { bufferBeforeLighting.DisableShaderKeyword("EXCLUSIONMASK"); } bool isSceneViewCamera = cam.cameraType == CameraType.SceneView; CommandBuffer selectionBuffer = null; if (m_CamerasSceneview.ContainsKey(cam)) { selectionBuffer = m_CamerasSceneview[cam]; selectionBuffer.Clear(); } else { if (isSceneViewCamera) { selectionBuffer = new CommandBuffer(); selectionBuffer.name = "Deferred decals selection"; m_CamerasSceneview[cam] = selectionBuffer; // set this command buffer to be executed just before deferred lighting pass // in the camera var buffers = cam.GetCommandBuffers(CameraEvent.AfterLighting); for (int i = 0; i < buffers.Length; i++) { if (buffers[i].name.Equals("Deferred decals selection")) { cam.RemoveCommandBuffer(CameraEvent.AfterLighting, buffers[i]); } } cam.AddCommandBuffer(CameraEvent.AfterLighting, selectionBuffer); } } // frustum culling cameraPlanes = GeometryUtility.CalculateFrustumPlanes(camera); currentDecalsVisibility.Clear(); var system = DeferredDecalsManager.instance; // cache visibility for each decal if (FrustumCulling) { foreach (var decal in system.m_Decals) { if (decal == null) { continue; } SetupBounds(decal); bool isDecalVisible = IsBoundsVisible(cameraPlanes, decal.Bounds); currentDecalsVisibility.Add(decal, isDecalVisible); } } else { foreach (var decal in system.m_Decals) { if (decal == null) { continue; } currentDecalsVisibility.Add(decal, true); } } BuiltinRenderTextureType emissionTexture = cam.allowHDR ? BuiltinRenderTextureType.CameraTarget : BuiltinRenderTextureType.GBuffer3; // copy g-buffer normals into a temporary RT var normalsID = Shader.PropertyToID("_NormalsCopy"); bufferBeforeLighting.GetTemporaryRT(normalsID, -1, -1, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32); bufferBeforeLighting.Blit(BuiltinRenderTextureType.GBuffer2, normalsID); // copy g-buffer specrough into a temporary RT var specularID = Shader.PropertyToID("_SpecularTarget"); bufferBeforeLighting.GetTemporaryRT(specularID, -1, -1, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32); bufferBeforeLighting.Blit(BuiltinRenderTextureType.GBuffer1, specularID); // copy g-buffer specrough into a temporary RT var smoothnessID = Shader.PropertyToID("_SmoothnessTarget"); bufferBeforeLighting.GetTemporaryRT(smoothnessID, -1, -1, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32); bufferBeforeLighting.Blit(BuiltinRenderTextureType.GBuffer1, smoothnessID, specularSmoothnessBlitterMaterial, 0); // copy g-buffer camera target into a temporary RT var emissionID = Shader.PropertyToID("_CameraTargetCopy"); bufferBeforeLighting.GetTemporaryRT(emissionID, -1, -1, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32); bufferBeforeLighting.Blit(emissionTexture, emissionID); instancedBlock.Clear(); var sortedDecalsBySortingOrder = system.m_Decals; decalsGrouped.Clear(); DecalsGroup groupKey; Vector3 camPos = cam.transform.position; float currentDistanceToCamera; float fadeFactor; foreach (var decal in sortedDecalsBySortingOrder) { if (!currentDecalsVisibility[decal]) { continue; } if (DistanceCulling) { currentDistanceToCamera = Vector3.Distance(camPos, decal.transform.position); if (currentDistanceToCamera >= StartFadeDistance + FadeLength) { continue; } fadeFactor = Mathf.InverseLerp(StartFadeDistance, StartFadeDistance + FadeLength, currentDistanceToCamera); decal.DistanceFade = 1 - fadeFactor; } else { decal.DistanceFade = 1f; } groupKey.Material = decal.DecalMaterial; groupKey.SortingOrder = decal.SortingOrder; groupKey.Instancing = decal.DecalMaterial.enableInstancing; List <Decal> decalsByMaterials; if (!decalsGrouped.TryGetValue(groupKey, out decalsByMaterials)) { decalsByMaterials = new List <Decal>(); decalsGrouped.Add(groupKey, decalsByMaterials); } decalsByMaterials.Add(decal); } RenderTargetIdentifier[] mrtDifNormSpecRough = { BuiltinRenderTextureType.GBuffer0, BuiltinRenderTextureType.GBuffer2, emissionTexture }; bufferBeforeLighting.SetRenderTarget(mrtDifNormSpecRough, BuiltinRenderTextureType.CameraTarget); Color tint; Vector4 uv; Vector4 uvOne = new Vector4(1, 1, 0, 0); foreach (var decalGroup in decalsGrouped) { var decalsList = decalGroup.Value; if (decalGroup.Key.Instancing) { bufferBeforeLighting.SetGlobalColor("_NotInstancedColor", Color.white); bufferBeforeLighting.SetGlobalVector("_NotInstancedUV", uvOne); int batchCount = 1 + Mathf.FloorToInt(decalsList.Count / 1024); for (int i = 0; i < batchCount; i++) { int drawCount = Mathf.Min(1023, decalsList.Count); for (int j = 0; j < drawCount; j++) { tintArray[j] = decalsList[j].InstancedColor; tintArray[j].w *= decalsList[j].Fade * decalsList[j].DistanceFade; uvArray[j] = decalsList[j].UV; matrices[j] = decalsList[j].transform.localToWorldMatrix; } if (drawCount > 0) { instancedBlock.SetVectorArray("_Tint", tintArray); instancedBlock.SetVectorArray("_UV", uvArray); bufferBeforeLighting.DrawMeshInstanced(CubeMesh, 0, decalGroup.Key.Material, 0, matrices, drawCount, instancedBlock); } } } else { foreach (var decal in decalsList) { tint = decal.InstancedColor; tint.a *= decal.Fade * decal.DistanceFade; bufferBeforeLighting.SetGlobalColor("_NotInstancedColor", tint); uv = decal.UV; bufferBeforeLighting.SetGlobalVector("_NotInstancedUV", uv); bufferBeforeLighting.DrawMesh(CubeMesh, decal.transform.localToWorldMatrix, decal.DecalMaterial, 0, 0); } } } mrtDifNormSpecRough = new RenderTargetIdentifier[] { specularID, smoothnessID }; bufferBeforeLighting.SetRenderTarget(mrtDifNormSpecRough, BuiltinRenderTextureType.CameraTarget); foreach (var decalGroup in decalsGrouped) { var decalsList = decalGroup.Value; if (decalGroup.Key.Instancing) { bufferBeforeLighting.SetGlobalColor("_NotInstancedColor", Color.white); bufferBeforeLighting.SetGlobalVector("_NotInstancedUV", uvOne); int batchCount = 1 + Mathf.FloorToInt(decalsList.Count / 1024); for (int i = 0; i < batchCount; i++) { int drawCount = Mathf.Min(1023, decalsList.Count); for (int j = 0; j < drawCount; j++) { tintArray[j] = decalsList[j].InstancedColor; tintArray[j].w *= decalsList[j].Fade; uvArray[j] = decalsList[j].UV; matrices[j] = decalsList[j].transform.localToWorldMatrix; } if (drawCount > 0) { instancedBlock.SetVectorArray("_Tint", tintArray); instancedBlock.SetVectorArray("_UV", uvArray); bufferBeforeLighting.DrawMeshInstanced(CubeMesh, 0, decalGroup.Key.Material, 2, matrices, drawCount, instancedBlock); } } } else { foreach (var decal in decalsList) { tint = decal.InstancedColor; tint.a *= decal.Fade; bufferBeforeLighting.SetGlobalColor("_NotInstancedColor", tint); uv = decal.UV; bufferBeforeLighting.SetGlobalVector("_NotInstancedUV", uv); bufferBeforeLighting.DrawMesh(CubeMesh, decal.transform.localToWorldMatrix, decal.DecalMaterial, 0, 2); } } } bufferBeforeLighting.SetGlobalTexture("_Alpha", smoothnessID); bufferBeforeLighting.Blit(specularID, BuiltinRenderTextureType.GBuffer1, specularSmoothnessBlitterMaterial, 1); if (TerrainDecals == TerrainDecalsType.MultiTerrain) { foreach (var d in system.m_Decals) { d.DecalMaterial.EnableKeyword("MULTI_TERRAIN_DECAL"); } } else { foreach (var d in system.m_Decals) { d.DecalMaterial.DisableKeyword("MULTI_TERRAIN_DECAL"); } } if (Terrain.activeTerrains == null || Terrain.activeTerrains.Length == 0) { foreach (var d in system.m_Decals) { d.DecalMaterial.EnableKeyword("NO_TERRAIN"); } } else { foreach (var d in system.m_Decals) { d.DecalMaterial.DisableKeyword("NO_TERRAIN"); } } if (TerrainDecals != TerrainDecalsType.None) { if (TerrainDecals == TerrainDecalsType.OneTerrain) { Terrain terrain = Terrain.activeTerrain; terrain.drawHeightmap = true; Shader.SetGlobalMatrix("_World2Terrain", terrain.transform.worldToLocalMatrix); Shader.SetGlobalTexture("_TerrainHeightMap", terrain.terrainData.heightmapTexture); Shader.SetGlobalVector("_TerrainSize", terrain.terrainData.size); } else if (TerrainDecals == TerrainDecalsType.MultiTerrain) { Terrain[] terrains = Terrain.activeTerrains; if (copyHeightmapsBuffer == null) { CopyHeightmaps(); } terrainsDataBuffer.SetData(terrainsData); SetupToShaders(); } } #if UNITY_EDITOR if (isSceneViewCamera) { float deltaTime = (float)(UnityEditor.EditorApplication.timeSinceStartup - lastRenderTime); lastRenderTime = UnityEditor.EditorApplication.timeSinceStartup; decalsToRemove.Clear(); foreach (var decalTime in lastSelectedDecals) { decalTime.Value.Value += deltaTime; if (decalTime.Value.Value >= selectionDuration) { decalsToRemove.Add(decalTime.Key); } } foreach (var decal in decalsToRemove) { lastSelectedDecals.Remove(decal); } var selectedObjects = UnityEditor.Selection.objects; if (selectedObjects != null) { selectionBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, BuiltinRenderTextureType.CameraTarget); foreach (var o in selectedObjects) { GameObject go = o as GameObject; if (go != null) { Decal decal = go.GetComponent <Decal>(); if (decal != null) { FloatValue time; if (lastSelectedDecals.TryGetValue(decal, out time)) { tint = decal.InstancedColor; tint.a *= decal.Fade; selectionBuffer.SetGlobalColor("_NotInstancedColor", tint); uv = decal.UV; selectionBuffer.SetGlobalVector("_NotInstancedUV", uv); selectionBuffer.SetGlobalFloat("SelectionTime", selectionFadeCurve.Evaluate(time.Value / selectionDuration)); selectionBuffer.DrawMesh(CubeMesh, decal.transform.localToWorldMatrix, decal.DecalMaterial, 0, 1); // selection pass - 1 } } } } } } #endif // release temporary RT bufferBeforeLighting.ReleaseTemporaryRT(normalsID); bufferBeforeLighting.ReleaseTemporaryRT(specularID); if (selectionBuffer != null) { selectionBuffer.ReleaseTemporaryRT(normalsID); } }