Beispiel #1
0
        private void UpdateParameters(OutlineParameters parameters, Camera camera, bool editorCamera)
        {
            UpdateSharedParameters(parameters, camera, editorCamera);

            parameters.DepthTarget = RenderTargetUtility.ComposeTarget(parameters, BuiltinRenderTextureType.CameraTarget);

            var targetTexture = camera.targetTexture == null ? camera.activeTexture : camera.targetTexture;

            if (UnityEngine.XR.XRSettings.enabled &&
                !parameters.IsEditorCamera &&
                parameters.EyeMask != StereoTargetEyeMask.None)
            {
                var descriptor = UnityEngine.XR.XRSettings.eyeTextureDesc;
                parameters.TargetWidth  = descriptor.width;
                parameters.TargetHeight = descriptor.height;
            }
            else
            {
                parameters.TargetWidth  = targetTexture != null ? targetTexture.width : camera.scaledPixelWidth;
                parameters.TargetHeight = targetTexture != null ? targetTexture.height : camera.scaledPixelHeight;
            }

            parameters.Antialiasing = editorCamera ? (targetTexture == null ? 1 : targetTexture.antiAliasing) : CameraUtility.GetMSAA(targetCamera);

            parameters.Target = RenderTargetUtility.ComposeTarget(parameters, HasCutomRenderTarget && !editorCamera ? GetRenderTarget(parameters) :
                                                                  BuiltinRenderTextureType.CameraTarget);

            Outlinable.GetAllActiveOutlinables(parameters.Camera, parameters.OutlinablesToRender);
            RendererFilteringUtility.Filter(parameters.Camera, parameters);
        }
Beispiel #2
0
        private static void Postprocess(OutlineParameters parameters, float shiftScale, bool scaleIndependent, float bufferScale, int first, int second, Material material, int iterrations, bool additionalShift, float shiftValue, ref int stencil, Rect viewport)
        {
            if (iterrations <= 0)
            {
                return;
            }

            var scalingFactor = scaleIndependent ? bufferScale : 1.0f;

            parameters.Buffer.SetGlobalInt(ComparisonHash, (int)CompareFunction.Always);

            for (var index = 1; index <= iterrations; index++)
            {
                parameters.Buffer.SetGlobalInt(RefHash, stencil);

                var shift = (additionalShift ? (float)index : 1.0f) * scalingFactor;

                parameters.Buffer.SetGlobalVector(ShiftHash, new Vector4(shift * shiftScale, 0));
                Blit(parameters, RenderTargetUtility.ComposeTarget(parameters, first), RenderTargetUtility.ComposeTarget(parameters, second), RenderTargetUtility.ComposeTarget(parameters, second), material, shiftValue, null, -1, viewport);

                parameters.Buffer.SetGlobalVector(ShiftHash, new Vector4(0, shift * shiftScale));
                Blit(parameters, RenderTargetUtility.ComposeTarget(parameters, second), RenderTargetUtility.ComposeTarget(parameters, first), RenderTargetUtility.ComposeTarget(parameters, first), material, shiftValue, null, -1, viewport);

                stencil = (stencil + 1) & 0xFF;
            }
        }
        public static void GetTemporaryRT(OutlineParameters parameters, int id, int width, int height, int depthBuffer, bool clear, bool forceNoAA, bool noFiltering)
        {
            var info = GetTargetInfo(parameters, width, height, depthBuffer, forceNoAA, noFiltering);

            parameters.Buffer.GetTemporaryRT(id, info.Descriptor, info.FilterMode);
            parameters.Buffer.SetRenderTarget(RenderTargetUtility.ComposeTarget(parameters, id));
            if (clear)
            {
                parameters.Buffer.ClearRenderTarget(true, true, Color.clear);
            }
        }
Beispiel #4
0
        public RenderTexture GetTarget(OutlineParameters parameters, string name)
        {
            RenderTexture result = null;

            if (!targets.TryGetValue(name, out result))
            {
                var info = RenderTargetUtility.GetTargetInfo(parameters, parameters.TargetWidth, parameters.TargetHeight, 24, false, false);
                result            = RenderTexture.GetTemporary(info.Descriptor);
                result.filterMode = info.FilterMode;

                targets.Add(name, result);

                return(result);
            }

            Shader.SetGlobalTexture(name, result);
            return(result);
        }
Beispiel #5
0
        public static void SetupOutline(OutlineParameters parameters)
        {
            parameters.Buffer.SetGlobalVector(ScaleHash, parameters.Scale);

            PrepareTargets(parameters);

            Profiler.BeginSample("Setup outline");

            Profiler.BeginSample("Check materials");
            InitMaterials();
            Profiler.EndSample();

            var effectShift  = ComputeEffectShift(parameters);
            var targetWidth  = parameters.TargetWidth;
            var targetHeight = parameters.TargetHeight;

            parameters.Camera.forceIntoRenderTexture = parameters.EyeMask == StereoTargetEyeMask.None || !UnityEngine.XR.XRSettings.enabled || parameters.IsEditorCamera;

            parameters.Buffer.SetGlobalInt(SrcBlendHash, (int)BlendMode.One);
            parameters.Buffer.SetGlobalInt(DstBlendHash, (int)BlendMode.Zero);

            var outlineRef = 1;

            parameters.Buffer.SetGlobalInt(OutlineRefHash, outlineRef);

            SetupDilateKeyword(parameters);

            RenderTargetUtility.GetTemporaryRT(parameters, TargetHash, targetWidth, targetHeight, 24, true, false, false);
            var scaledWidth  = (int)(targetWidth * parameters.PrimaryBufferScale);
            var scaledHeight = (int)(targetHeight * parameters.PrimaryBufferScale);

            if (parameters.EyeMask != StereoTargetEyeMask.None)
            {
                if (scaledWidth % 2 != 0)
                {
                    scaledWidth++;
                }

                if (scaledHeight % 2 != 0)
                {
                    scaledHeight++;
                }
            }

            var scaledViewVector = parameters.MakeScaledVector(scaledWidth, scaledHeight);

            RenderTargetUtility.GetTemporaryRT(parameters, PrimaryBufferHash, scaledWidth, scaledHeight, 24, true, false, false);
            RenderTargetUtility.GetTemporaryRT(parameters, HelperBufferHash, scaledWidth, scaledHeight, 24, true, false, false);

            if (parameters.UseInfoBuffer)
            {
                var scaledInfoWidth = (int)(targetWidth * Mathf.Min(parameters.PrimaryBufferScale, parameters.InfoBufferScale));
                if (scaledInfoWidth % 2 != 0)
                {
                    scaledInfoWidth++;
                }

                var scaledInfoHeight = (int)(targetHeight * Mathf.Min(parameters.PrimaryBufferScale, parameters.InfoBufferScale));
                if (scaledInfoHeight % 2 != 0)
                {
                    scaledInfoHeight++;
                }

                RenderTargetUtility.GetTemporaryRT(parameters, InfoTargetHash, targetWidth, targetHeight, 0, false, false, false);

                RenderTargetUtility.GetTemporaryRT(parameters, PrimaryInfoBufferHash, scaledInfoWidth, scaledInfoHeight, 0, true, true, false);
                RenderTargetUtility.GetTemporaryRT(parameters, HelperInfoBufferHash, scaledInfoWidth, scaledInfoHeight, 0, true, true, false);
            }

            Profiler.BeginSample("Updating mesh");
            BlitUtility.SetupMesh(parameters, effectShift);
            Profiler.EndSample();

            parameters.Buffer.SetRenderTarget(RenderTargetUtility.ComposeTarget(parameters, TargetHash), parameters.DepthTarget);
            if (parameters.CustomViewport.HasValue)
            {
                parameters.Buffer.SetViewport(parameters.CustomViewport.Value);
            }

            DrawOutlineables(parameters, CompareFunction.LessEqual, false, 0.0f, x => true, x => Color.clear, x => ZPrepassMaterial, RenderStyle.FrontBack | RenderStyle.Single, OutlinableDrawingMode.ZOnly);

            parameters.Buffer.DisableShaderKeyword(KeywordsUtility.GetEnabledInfoBufferKeyword());

            if (parameters.UseInfoBuffer)
            {
                parameters.Buffer.EnableShaderKeyword(KeywordsUtility.GetInfoBufferStageKeyword());

                parameters.Buffer.SetRenderTarget(RenderTargetUtility.ComposeTarget(parameters, InfoTargetHash), parameters.DepthTarget);
                parameters.Buffer.ClearRenderTarget(false, true, Color.clear);
                if (parameters.CustomViewport.HasValue)
                {
                    parameters.Buffer.SetViewport(parameters.CustomViewport.Value);
                }

                DrawOutlineables(parameters, CompareFunction.Always, false, 0.0f, x => x.OutlineParameters.Enabled, x => new Color(x.OutlineParameters.DilateShift * parameters.DilateShift, x.OutlineParameters.BlurShift * parameters.BlurShift, 0, 1),
                                 x => OutlineMaterial,
                                 RenderStyle.Single,
                                 OutlinableDrawingMode.Normal);

                DrawOutlineables(parameters, CompareFunction.NotEqual, false, 0.0f, x => x.BackParameters.Enabled, x => new Color(x.BackParameters.DilateShift * parameters.DilateShift, x.BackParameters.BlurShift * parameters.BlurShift, 0, 1),
                                 x => OutlineMaterial,
                                 RenderStyle.FrontBack);

                DrawOutlineables(parameters, CompareFunction.LessEqual, false, 0.0f, x => x.FrontParameters.Enabled, x => new Color(x.FrontParameters.DilateShift * parameters.DilateShift, x.FrontParameters.BlurShift * parameters.BlurShift, 0, 1),
                                 x => OutlineMaterial,
                                 RenderStyle.FrontBack,
                                 OutlinableDrawingMode.Normal);

                DrawOutlineables(parameters, CompareFunction.LessEqual, false, 0.0f, x => true, x => new Color(0, 0, GetMaskingValueForMode(x.DrawingMode), 1),
                                 x => ObstacleMaterial,
                                 RenderStyle.Single | RenderStyle.FrontBack,
                                 OutlinableDrawingMode.Obstacle | OutlinableDrawingMode.Mask);

                parameters.Buffer.SetGlobalInt(ComparisonHash, (int)CompareFunction.Always);
                parameters.Buffer.SetGlobalInt(RefHash, 0);
                Blit(parameters,
                     RenderTargetUtility.ComposeTarget(parameters, InfoTargetHash),
                     RenderTargetUtility.ComposeTarget(parameters, PrimaryInfoBufferHash),
                     RenderTargetUtility.ComposeTarget(parameters, PrimaryInfoBufferHash),
                     BasicBlitMaterial, effectShift, null,
                     -1);

                var infoRef     = 0;
                var bufferScale = Mathf.Min(parameters.PrimaryBufferScale, parameters.InfoBufferScale);
                Postprocess(parameters, 1.0f, false, bufferScale, PrimaryInfoBufferHash, HelperInfoBufferHash, DilateMaterial,
                            (parameters.DilateQuality == DilateQuality.Base ? parameters.DilateIterations : parameters.DilateIterations * 2) + parameters.BlurIterations, false,
                            effectShift, ref infoRef,
                            new Rect(0, 0, scaledViewVector.x, scaledViewVector.y));

                parameters.Buffer.SetRenderTarget(RenderTargetUtility.ComposeTarget(parameters, InfoTargetHash), parameters.DepthTarget);
                if (parameters.CustomViewport.HasValue)
                {
                    parameters.Buffer.SetViewport(parameters.CustomViewport.Value);
                }

                DrawOutlineables(parameters, CompareFunction.Always, true, effectShift, x => x.OutlineParameters.Enabled, x => new Color(x.OutlineParameters.DilateShift * 0.5f * parameters.DilateShift, x.OutlineParameters.BlurShift * 0.5f * parameters.BlurShift, 0, 1.0f), x => EdgeDilateMaterial, RenderStyle.Single, OutlinableDrawingMode.Normal);
                DrawOutlineables(parameters, CompareFunction.NotEqual, true, effectShift, x => x.BackParameters.Enabled, x => new Color(x.BackParameters.DilateShift * 0.5f * parameters.DilateShift, x.BackParameters.BlurShift * 0.5f * parameters.BlurShift, 0, x.ComplexMaskingEnabled ? 0.0f : 0.2f), x => EdgeDilateMaterial, RenderStyle.FrontBack);
                DrawOutlineables(parameters, CompareFunction.LessEqual, true, effectShift, x => x.FrontParameters.Enabled, x => new Color(x.FrontParameters.DilateShift * 0.5f * parameters.DilateShift, x.FrontParameters.BlurShift * 0.5f * parameters.BlurShift, 0, 1.0f), x => EdgeDilateMaterial, RenderStyle.FrontBack, OutlinableDrawingMode.Normal);

                parameters.Buffer.SetGlobalTexture(InfoBufferHash, PrimaryInfoBufferHash);

                parameters.Buffer.DisableShaderKeyword(KeywordsUtility.GetInfoBufferStageKeyword());
            }

            if (parameters.UseInfoBuffer)
            {
                parameters.Buffer.EnableShaderKeyword(KeywordsUtility.GetEnabledInfoBufferKeyword());
            }

            parameters.Buffer.SetRenderTarget(RenderTargetUtility.ComposeTarget(parameters, TargetHash), parameters.DepthTarget);
            parameters.Buffer.ClearRenderTarget(false, true, Color.clear);
            if (parameters.CustomViewport.HasValue)
            {
                parameters.Buffer.SetViewport(parameters.CustomViewport.Value);
            }

            var drawnOutlinablesCount = 0;

            drawnOutlinablesCount += DrawOutlineables(parameters, CompareFunction.Always, false, 0.0f, x => x.OutlineParameters.Enabled, x => x.OutlineParameters.Color,
                                                      x => OutlineMaterial,
                                                      RenderStyle.Single, OutlinableDrawingMode.Normal);

            drawnOutlinablesCount += DrawOutlineables(parameters, CompareFunction.NotEqual, false, 0.0f, x => x.BackParameters.Enabled, x => x.BackParameters.Color,
                                                      x => OutlineMaterial,
                                                      RenderStyle.FrontBack, OutlinableDrawingMode.Normal);

            drawnOutlinablesCount += DrawOutlineables(parameters, CompareFunction.LessEqual, false, 0.0f, x => x.FrontParameters.Enabled, x => x.FrontParameters.Color,
                                                      x => OutlineMaterial,
                                                      RenderStyle.FrontBack, OutlinableDrawingMode.Normal);

            parameters.Buffer.SetGlobalInt(ComparisonHash, (int)CompareFunction.Always);

            var postProcessingRef = 0;

            if (drawnOutlinablesCount > 0)
            {
                Blit(parameters, RenderTargetUtility.ComposeTarget(parameters, TargetHash),
                     RenderTargetUtility.ComposeTarget(parameters, PrimaryBufferHash),
                     RenderTargetUtility.ComposeTarget(parameters, PrimaryBufferHash),
                     BasicBlitMaterial, effectShift, null, -1,
                     new Rect(0, 0, scaledViewVector.x, scaledViewVector.y));

                Postprocess(parameters, parameters.DilateShift, parameters.ScaleIndependent, parameters.PrimaryBufferScale, PrimaryBufferHash, HelperBufferHash, DilateMaterial, parameters.DilateIterations, false, effectShift, ref postProcessingRef,
                            new Rect(0, 0, scaledViewVector.x, scaledViewVector.y));
            }

            parameters.Buffer.SetRenderTarget(RenderTargetUtility.ComposeTarget(parameters, TargetHash), parameters.DepthTarget);
            if (drawnOutlinablesCount > 0)
            {
                parameters.Buffer.ClearRenderTarget(false, true, Color.clear);
            }

            if (parameters.CustomViewport.HasValue)
            {
                parameters.Buffer.SetViewport(parameters.CustomViewport.Value);
            }

            var drawnSimpleEdgeDilateOutlinesCount = DrawOutlineables(parameters, CompareFunction.Always, true, -1, x => x.OutlineParameters.Enabled, x => x.OutlineParameters.Color, x => EdgeDilateMaterial, RenderStyle.Single);

            drawnSimpleEdgeDilateOutlinesCount += DrawOutlineables(parameters, CompareFunction.NotEqual, true, -1, x => x.BackParameters.Enabled, x => x.BackParameters.Color, x => EdgeDilateMaterial, RenderStyle.FrontBack);
            drawnSimpleEdgeDilateOutlinesCount += DrawOutlineables(parameters, CompareFunction.LessEqual, true, -1, x => x.FrontParameters.Enabled, x => x.FrontParameters.Color, x => EdgeDilateMaterial, RenderStyle.FrontBack);

            if (drawnSimpleEdgeDilateOutlinesCount > 0)
            {
                Blit(parameters,
                     RenderTargetUtility.ComposeTarget(parameters, TargetHash),
                     RenderTargetUtility.ComposeTarget(parameters, PrimaryBufferHash),
                     RenderTargetUtility.ComposeTarget(parameters, PrimaryBufferHash),
                     PartialBlitMaterial, effectShift, null, -1,
                     new Rect(0, 0, scaledViewVector.x, scaledViewVector.y));
            }

            if (parameters.BlurIterations > 0)
            {
                SetupBlurKeyword(parameters);
                Postprocess(parameters, parameters.BlurShift, parameters.ScaleIndependent, parameters.PrimaryBufferScale, PrimaryBufferHash, HelperBufferHash, BlurMaterial, parameters.BlurIterations, false,
                            effectShift,
                            ref postProcessingRef,
                            new Rect(0, 0, scaledViewVector.x, scaledViewVector.y));
            }

            DrawOutlineables(parameters, CompareFunction.LessEqual, false, 0.0f, x => true, x => Color.clear, x => OutlineMaskMaterial, RenderStyle.FrontBack | RenderStyle.Single, OutlinableDrawingMode.GenericMask);

            parameters.Buffer.SetGlobalInt(ComparisonHash, (int)CompareFunction.NotEqual);
            parameters.Buffer.SetGlobalInt(ReadMaskHash, 255);
            parameters.Buffer.SetGlobalInt(OperationHash, (int)StencilOp.Replace);

            Blit(parameters,
                 RenderTargetUtility.ComposeTarget(parameters, PrimaryBufferHash),
                 parameters.Target,
                 parameters.DepthTarget,
                 FinalBlitMaterial,
                 effectShift, null, -1,
                 parameters.CustomViewport);

            DrawFill(parameters, parameters.Target);

            parameters.Buffer.SetGlobalFloat(EffectSizeHash, effectShift);
            BlitUtility.Draw(parameters, parameters.Target, parameters.DepthTarget, ClearStencilMaterial, parameters.CustomViewport);

            parameters.Buffer.ReleaseTemporaryRT(PrimaryBufferHash);

            parameters.Buffer.ReleaseTemporaryRT(HelperBufferHash);
            parameters.Buffer.ReleaseTemporaryRT(TargetHash);

            if (parameters.UseInfoBuffer)
            {
                parameters.Buffer.ReleaseTemporaryRT(InfoBufferHash);
                parameters.Buffer.ReleaseTemporaryRT(PrimaryInfoBufferHash);
                parameters.Buffer.ReleaseTemporaryRT(HelperInfoBufferHash);
            }

            Profiler.EndSample();
        }