void OnEnable()
    {
        Debug.Assert(Instance == null, "StaggeredCascade is expected to be a singleton");
        Instance = this;

        m_StaggerStage = StaggerStage.Updating;

        var go = new GameObject("ShadowCamera");

        go.hideFlags = HideFlags.DontSave;        // | HideFlags.NotEditable;
        go.transform.SetParent(transform);
        m_ShadowCamera = go.AddComponent <Camera>();
        m_ShadowCamera.orthographic = true;
        m_ShadowCamera.enabled      = false;

        m_ShaderPassName       = new ShaderPassName("ShadowCaster");
        m_DrawRendererSettings = new DrawRendererSettings {
            rendererConfiguration = RendererConfiguration.None,
            sorting = { flags = SortFlags.QuantizedFrontToBack }
        };
        m_DrawRendererSettings.SetShaderPassName(0, m_ShaderPassName);
        m_FilterRendererSettings = new FilterRenderersSettings(true)
        {
            renderQueueRange = new RenderQueueRange {
                min = (int)UnityEngine.Rendering.RenderQueue.Geometry,
                max = (int)UnityEngine.Rendering.RenderQueue.GeometryLast
            },
        };
    }
    // 指定された Passでキャラクターを描画します
    private void DrawCharacter(ScriptableRenderContext context, Camera camera, ShaderPassName pass, SortFlags sortFlags)
    {
        var settings = new DrawRendererSettings(camera, pass);

        settings.sorting.flags = sortFlags;

        var filterSettings = new FilterRenderersSettings(true)
        {
            renderQueueRange = RenderQueueRange.transparent,
            layerMask        = 1 << LayerDefine.CHARA
        };

        context.DrawRenderers(cull.visibleRenderers, ref settings, filterSettings);
    }
示例#3
0
    public static void Render(ScriptableRenderContext context, IEnumerable<Camera> cameras)
    {
        foreach (Camera camera in cameras)
        {
            ScriptableCullingParameters cullingParams;

            // Stereo-aware culling parameters are configured to perform a single cull for both eyes
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
                continue;
            CullResults cull = new CullResults();
            CullResults.Cull(ref cullingParams, context, ref cull);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            CommandBuffer cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.grey);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Setup global lighting shader variables
            //SetupLightShaderVariables(cull.visibleLights, context);

            // Draw skybox
            context.DrawSkybox(camera);

            // Setup DrawSettings and FilterSettings
            ShaderPassName passName = new ShaderPassName("BasicPass");
            DrawRendererSettings drawSettings = new DrawRendererSettings(camera, passName);
            FilterRenderersSettings filterSettings = new FilterRenderersSettings(true);

            // Draw opaque objects using BasicPass shader pass
            drawSettings.sorting.flags = SortFlags.CommonOpaque;
            filterSettings.renderQueueRange = RenderQueueRange.opaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            // Draw shadow
            VisibleLight[] ls = cull.visibleLights.ToArray();
            SetupLightShaderVariables(cull.visibleLights, context);
            DrawShadowsSettings[] shadowsSettings = new DrawShadowsSettings[ls.Length];
            
            for (int i=0; i<shadowsSettings.Length; i++)
            {
                shadowsSettings[i] = new DrawShadowsSettings(cull, i);
                if(ls[i].light.shadows != LightShadows.None)
                {
                    context.DrawShadows(ref shadowsSettings[i]);
                    Debug.Log("draw shadow for light "+i);
                }
            }

            // Draw transparent objects using BasicPass shader pass
            //drawSettings.sorting.flags = SortFlags.CommonTransparent;
            //filterSettings.renderQueueRange = RenderQueueRange.transparent;
            //context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            context.Submit();
        }
    }
示例#4
0
    private static readonly ShaderPassName m_UnlitPassName = new ShaderPassName("SRPDefaultUnlit"); //For default shaders

    public static void Render(ScriptableRenderContext context, IEnumerable <Camera> cameras)
    {
        foreach (Camera camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            CullResults cull = new CullResults();

            // Record time for performance (before)
            if (camera == Camera.main)
            {
                t = Time.realtimeSinceStartup;
            }

            //Do Culling
            CullResults.Cull(ref cullingParams, context, ref cull);

            // Record time for performance (after)
            if (camera == Camera.main)
            {
                t = Time.realtimeSinceStartup - t;
                if (text == null)
                {
                    text = Camera.main.GetComponent <UpdateText>();
                }
                if (count > skipcount)
                {
                    //After Cull
                    t *= 1000.000000000000f;

                    //Average
                    alltime += t;

                    if (count == 2000)
                    {
                        avgt = alltime / ((count - skipcount) * 1.000000000f);

                        //Show cull time
                        string content = "Delta Time = " + t + "ms \n Average = " + avgt + "ms \n count = " + count;
                        Debug.Log(content);
                        if (text != null)
                        {
                            text.UpdateTextContent(content);
                        }
                    }
                    else if (count < 2000)
                    {
                        Debug.Log("now sampling 1000 frames...");
                        if (text != null)
                        {
                            text.UpdateTextContent("now sampling 1000 frames..." + count);
                        }
                    }
                }
                else
                {
                    Debug.Log("skipping first 1000 frames...");
                    if (text != null)
                    {
                        text.UpdateTextContent("skipping first 1000 frames..." + count);
                    }
                }
                count++;
            }

            //Start rendering part

            context.SetupCameraProperties(camera);

            // clear depth buffer
            CommandBuffer cmd = new CommandBuffer();;
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Setup global lighting shader variables
            SetupLightShaderVariables(cull.visibleLights, context);

            // Draw skybox
            context.DrawSkybox(camera);

            // Setup DrawSettings and FilterSettings
            ShaderPassName       passName     = new ShaderPassName("BasicPass");
            DrawRendererSettings drawSettings = new DrawRendererSettings(camera, passName);
            drawSettings.SetShaderPassName(1, m_UnlitPassName); //For drawing default shaders
            FilterRenderersSettings filterSettings = new FilterRenderersSettings(true);

            // Draw opaque objects using BasicPass shader pass
            drawSettings.sorting.flags      = SortFlags.CommonOpaque;
            filterSettings.renderQueueRange = RenderQueueRange.opaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            // Draw transparent objects using BasicPass shader pass
            drawSettings.sorting.flags      = SortFlags.CommonTransparent;
            filterSettings.renderQueueRange = RenderQueueRange.transparent;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            context.Submit();
        }
    }
示例#5
0
    private static readonly ShaderPassName m_UnlitPassName = new ShaderPassName("SRPDefaultUnlit"); //For default shaders

    public static void Render(ScriptableRenderContext context, Camera[] cameras, SRP02CustomParameter SRP02CP)
    {
        RenderPipeline.BeginFrameRendering(cameras);

        foreach (Camera camera in cameras)
        {
            RenderPipeline.BeginCameraRendering(camera);

            //Culling
            ScriptableCullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            CullResults cull = new CullResults();
            CullResults.Cull(ref cullingParams, context, ref cull);

            context.SetupCameraProperties(camera);

            if (camera.renderingPath == RenderingPath.UsePlayerSettings)
            {
                // clear depth buffer
                CommandBuffer cmd = new CommandBuffer();
                cmd.ClearRenderTarget(true, !SRP02CP.DrawSkybox, SRP02CP.ClearColor);
                context.ExecuteCommandBuffer(cmd);
                cmd.Release();

                // Setup global lighting shader variables
                //SetupLightShaderVariables(cull.visibleLights, context);

                // Setup DrawSettings and FilterSettings
                ShaderPassName          passName       = new ShaderPassName("BasicPass");
                DrawRendererSettings    drawSettings   = new DrawRendererSettings(camera, passName);
                FilterRenderersSettings filterSettings = new FilterRenderersSettings(true);

                //Draw passes that has no light mode (default)
                ShaderPassName       passNameDefault     = new ShaderPassName("");
                DrawRendererSettings drawSettingsDefault = new DrawRendererSettings(camera, passNameDefault);
                drawSettingsDefault.SetShaderPassName(1, m_UnlitPassName);

                if (SRP02CP.DrawSkybox)
                {
                    context.DrawSkybox(camera);
                }

                if (SRP02CP.DrawOpaque)
                {
                    drawSettings.sorting.flags      = SortFlags.CommonOpaque;
                    filterSettings.renderQueueRange = RenderQueueRange.opaque;
                    context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);
                }

                // Default
                drawSettingsDefault.sorting.flags = SortFlags.CommonOpaque;
                context.DrawRenderers(cull.visibleRenderers, ref drawSettingsDefault, filterSettings);

                if (SRP02CP.DrawTransparent)
                {
                    drawSettings.sorting.flags      = SortFlags.CommonTransparent;
                    filterSettings.renderQueueRange = RenderQueueRange.transparent;
                    context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);
                }

                // Default
                drawSettingsDefault.sorting.flags = SortFlags.CommonTransparent;
                context.DrawRenderers(cull.visibleRenderers, ref drawSettingsDefault, filterSettings);
            }

            context.Submit();
        }
    }
示例#6
0
    void Render(ScriptableRenderContext context, Camera camera)
    {
        ScriptableCullingParameters cullingParameters;

        if (!CullResults.GetCullingParameters(camera, out cullingParameters))
        {
            return;
        }

        cullingParameters.shadowDistance = Mathf.Min(shadowDistance, camera.farClipPlane);

#if UNITY_EDITOR
        if (camera.cameraType == CameraType.SceneView)
        {
            ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
        }
#endif
        CullResults.Cull(ref cullingParameters, context, ref cull);

        if (cull.visibleLights.Count > 0)
        {
            ConfigureLights();
            if (mainLightExist)
            {
                RendercascadeShadows(context);
            }
            else
            {
                cameraBuffer.DisableShaderKeyword(cascadeShadowsHardKeyword);
                cameraBuffer.DisableShaderKeyword(cascadeShadowsSoftKeyword);
            }

            if (shadowTileCount > 0)
            {
                RenderShadows(context);
            }
            else
            {
                cameraBuffer.DisableShaderKeyword(shadowsSoftKeyword);
                cameraBuffer.DisableShaderKeyword(shadowsHardKeyword);
            }
        }
        else
        {
            cameraBuffer.SetGlobalVector(lightIndicesOffsetAndCountID, Vector4.zero);
            cameraBuffer.DisableShaderKeyword(cascadeShadowsHardKeyword);
            cameraBuffer.DisableShaderKeyword(cascadeShadowsSoftKeyword);
            cameraBuffer.DisableShaderKeyword(shadowsSoftKeyword);
            cameraBuffer.DisableShaderKeyword(shadowsHardKeyword);
        }


        context.SetupCameraProperties(camera);

        var myPipelineCamera = camera.GetComponent <MyPipelineCamera>();
        MyPostprocessingStack activeStack = myPipelineCamera ?
                                            myPipelineCamera.PostProcessingStack : defaultStack;

        bool scaledRendering = (renderScale <1f || renderScale> 1f) && camera.cameraType == CameraType.Game;
        int  renderWidth     = camera.pixelWidth;
        int  renderHeight    = camera.pixelHeight;
        if (scaledRendering)
        {
            renderWidth  = (int)(renderWidth * renderScale);
            renderHeight = (int)(renderHeight * renderScale);
        }

        int  renderSamples      = camera.allowMSAA ? msaaSamples : 1;
        bool renderToTexture    = scaledRendering || renderSamples > 1 || activeStack;
        bool needsDepth         = activeStack && activeStack.NeedsDepth;
        bool needsDirectDepth   = needsDepth && renderSamples == 1;
        bool needsDepthOnlyPass = needsDepth && renderSamples > 1;

        RenderTextureFormat format = allowHDR && camera.allowHDR ?
                                     RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;

        if (renderToTexture)
        {
            cameraBuffer.GetTemporaryRT(cameraColorTextureID, renderWidth, renderHeight, needsDirectDepth ? 0 : 24, FilterMode.Bilinear,
                                        format, RenderTextureReadWrite.Default, renderSamples);
            if (needsDepth)
            {
                cameraBuffer.GetTemporaryRT(cameraDepthTextureID, renderWidth, renderHeight, 24, FilterMode.Point, RenderTextureFormat.Depth,
                                            RenderTextureReadWrite.Linear, 1);
            }
            if (needsDirectDepth)
            {
                cameraBuffer.SetRenderTarget(cameraColorTextureID, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
                                             cameraDepthTextureID, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            }
            else
            {
                cameraBuffer.SetRenderTarget(cameraColorTextureID, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            }
        }

        CameraClearFlags clearFlags = camera.clearFlags;
        cameraBuffer.ClearRenderTarget(
            (clearFlags & CameraClearFlags.Depth) != 0,
            (clearFlags & CameraClearFlags.Color) != 0,
            camera.backgroundColor
            //new Color(0, 0, 0, 0)
            );
        cameraBuffer.BeginSample("Render Camera");
        cameraBuffer.SetGlobalVectorArray(visibleLightColorID, visibleLightColors);
        cameraBuffer.SetGlobalVectorArray(visibleLightDirectionOrPositionID, visibleLightDirectionsOrPositions);
        cameraBuffer.SetGlobalVectorArray(visibleLightAttenuationsID, visibleLightAttenuations);
        cameraBuffer.SetGlobalVectorArray(visibleLightSpotDirectionID, visibleLightSpotDirections);
        cameraBuffer.SetGlobalVectorArray(visibleLightOcclusionMasksID, visibleLightOcclusionMasks);
        globalShadowData.z = 1f - cullingParameters.shadowDistance * globalShadowData.y;
        cameraBuffer.SetGlobalVector(globalShadowDataID, globalShadowData);
        context.ExecuteCommandBuffer(cameraBuffer);
        cameraBuffer.Clear();

        ShaderPassName passName = new ShaderPassName("SRPDefaultUnlit");

        var drawSettings = new DrawRendererSettings(camera, passName)
        {
            flags = drawFlags
        };

        if (cull.visibleLights.Count > 0)
        {
            drawSettings.rendererConfiguration = RendererConfiguration.PerObjectLightIndices8;
        }

        drawSettings.rendererConfiguration |=
            RendererConfiguration.PerObjectReflectionProbes |
            RendererConfiguration.PerObjectLightmaps |
            RendererConfiguration.PerObjectLightProbe |
            RendererConfiguration.PerObjectLightProbeProxyVolume |
            RendererConfiguration.PerObjectShadowMask |
            RendererConfiguration.PerObjectOcclusionProbe |
            RendererConfiguration.PerObjectOcclusionProbeProxyVolume;

        // drawSettings.flags = drawFlags;
        drawSettings.sorting.flags = SortFlags.CommonOpaque;

        var filterSettings = new FilterRenderersSettings(true)
        {
            renderQueueRange = RenderQueueRange.opaque
        };
        context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

        context.DrawSkybox(camera);

        if (activeStack)
        {
            if (needsDepthOnlyPass)
            {
                var depthOnlyDrawSettings = new DrawRendererSettings(camera, new ShaderPassName("DepthOnly"))
                {
                    flags = drawFlags
                };
                depthOnlyDrawSettings.sorting.flags = SortFlags.CommonOpaque;
                cameraBuffer.SetRenderTarget(cameraDepthTextureID, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
                cameraBuffer.ClearRenderTarget(true, false, Color.clear);
                context.ExecuteCommandBuffer(cameraBuffer);
                cameraBuffer.Clear();
                context.DrawRenderers(cull.visibleRenderers, ref depthOnlyDrawSettings, filterSettings);
            }
            activeStack.RenderAfterOpaque(postProcessingBuffer, cameraColorTextureID, cameraDepthTextureID, renderWidth, renderHeight, renderSamples, format);
            context.ExecuteCommandBuffer(postProcessingBuffer);
            postProcessingBuffer.Clear();

            if (needsDirectDepth)
            {
                cameraBuffer.SetRenderTarget(cameraColorTextureID, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store,
                                             cameraDepthTextureID, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store);
            }
            else
            {
                cameraBuffer.SetRenderTarget(cameraColorTextureID, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store);
            }
            context.ExecuteCommandBuffer(cameraBuffer);
            cameraBuffer.Clear();
        }

        drawSettings.sorting.flags      = SortFlags.CommonTransparent;
        filterSettings.renderQueueRange = RenderQueueRange.transparent;
        context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

        DrawDefaultPipeline(context, camera);

        if (renderToTexture)
        {
            if (activeStack)
            {
                activeStack.RenderAfterTransparent(postProcessingBuffer, cameraColorTextureID, cameraDepthTextureID, renderWidth, renderHeight, renderSamples, format);
                context.ExecuteCommandBuffer(postProcessingBuffer);
                postProcessingBuffer.Clear();
            }
            else
            {
                cameraBuffer.Blit(cameraColorTextureID, BuiltinRenderTextureType.CameraTarget);
            }
            cameraBuffer.ReleaseTemporaryRT(cameraColorTextureID);
            if (needsDepth)
            {
                cameraBuffer.ReleaseTemporaryRT(cameraDepthTextureID);
            }
        }

        cameraBuffer.EndSample("Render Camera");
        context.ExecuteCommandBuffer(cameraBuffer);
        cameraBuffer.Clear();

        context.Submit();

        if (shadowMap)
        {
            RenderTexture.ReleaseTemporary(shadowMap);
            shadowMap = null;
        }

        if (cascadeShadowMap)
        {
            RenderTexture.ReleaseTemporary(cascadeShadowMap);
            cascadeShadowMap = null;
        }
    }
示例#7
0
    private static readonly ShaderPassName m_UnlitPassName = new ShaderPassName("SRPDefaultUnlit"); //For default shaders

    public static void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        RenderPipeline.BeginFrameRendering(cameras);

        string tx = "";

        foreach (Camera camera in cameras)
        {
            RenderPipeline.BeginCameraRendering(camera);

            // Culling
            ScriptableCullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            CullResults cull = new CullResults();
            CullResults.Cull(ref cullingParams, context, ref cull);

            if (camera == Camera.main) //Only generate result from main cam
            {
                tx  = "";
                tx += "<color=#0FF>cullingPlaneCount : </color>" + cullingParams.cullingPlaneCount + "\n";
                for (int i = 0; i < cullingParams.cullingPlaneCount; i++)
                {
                    if (cullingParams.cameraProperties.GetCameraCullingPlane(i).ToString() == cullingParams.cameraProperties.GetShadowCullingPlane(i).ToString())
                    {
                        tx += "<color=#0F0>cameraProperties.GetCameraCullingPlane =  GetShadowCullingPlane (" + i + ") : </color>" + cullingParams.cameraProperties.GetCameraCullingPlane(i) + "\n";
                    }
                    else
                    {
                        tx += "<color=#0FF>cameraProperties.GetCameraCullingPlane(" + i + ") : </color>" + cullingParams.cameraProperties.GetCameraCullingPlane(i) + "\n";
                        tx += "<color=#0FF>cameraProperties.GetShadowCullingPlane(" + i + ") : </color>" + cullingParams.cameraProperties.GetShadowCullingPlane(i) + "\n";
                    }
                }
                //
                tx += "<color=#0FF>cullingFlags : </color>" + cullingParams.cullingFlags.ToString() + "\n";
                tx += "<color=#0FF>cullingMask : </color>" + cullingParams.cullingMask.ToString() + "\n";
                tx += "<color=#0FF>cullingMatrix : </color>" + cullingParams.cullingMatrix + "\n";
                //
                if (cullingParams.isOrthographic == cullingParams.lodParameters.isOrthographic)
                {
                    tx += "<color=#0F0>isOrthographic = lodParameters.isOrthographic : </color>" + cullingParams.isOrthographic + "\n";
                }
                else
                {
                    tx += "<color=#0FF>isOrthographic : </color>" + cullingParams.isOrthographic + "\n";
                }
                if (cullingParams.position == cullingParams.lodParameters.cameraPosition)
                {
                    tx += "<color=#0F0>position = lodParameters.cameraPosition : </color>" + cullingParams.position + "\n";
                }
                else
                {
                    tx += "<color=#0FF>position : </color>" + cullingParams.position + "\n";
                }
                //
                tx += "<color=#0FF>reflectionProbeSortOptions : </color>" + cullingParams.reflectionProbeSortOptions.ToString() + "\n";
                tx += "<color=#0FF>sceneMask : </color>" + cullingParams.sceneMask + "\n";
                tx += "<color=#0FF>shadowDistance : </color>" + cullingParams.shadowDistance + "\n";
                //
                tx += "<color=#0FF>layerCull : </color>" + cullingParams.layerCull + "\n";
                for (int i = 0; i < cullingParams.layerCull; i++)
                {
                    tx += "<color=#0FF>GetLayerCullDistance(" + i + ") : </color>" + cullingParams.GetLayerCullDistance(i) + "\n";
                }
                //
                tx += "<color=#0FF>lodParameters : </color>" + "\n";
                tx += "cameraPixelHeight = " + cullingParams.lodParameters.cameraPixelHeight + "\n";
                if (camera.fieldOfView == cullingParams.lodParameters.fieldOfView)
                {
                    tx += "fieldOfView = camera.fieldOfView : " + "<color=#0F0>" + cullingParams.lodParameters.fieldOfView + "</color>" + "\n";
                }
                else
                {
                    tx += "fieldOfView != camera.fieldOfView : " + "<color=#FF0>" + cullingParams.lodParameters.fieldOfView + "</color>" + "\n";
                }
                if (camera.orthographicSize == cullingParams.lodParameters.orthoSize)
                {
                    tx += "orthoSize = camera.orthographicSize : " + "<color=#0F0>" + cullingParams.lodParameters.orthoSize + "</color>" + "\n";
                }
                else
                {
                    tx += "orthoSize != camera.orthographicSize : " + "<color=#FF0>" + cullingParams.lodParameters.orthoSize + "</color>" + "\n";
                }
                if (cullingParams.isOrthographic != cullingParams.lodParameters.isOrthographic)
                {
                    tx += "isOrthographic = " + cullingParams.lodParameters.isOrthographic + "\n";
                }
                if (cullingParams.position != cullingParams.lodParameters.cameraPosition)
                {
                    tx += "cameraPosition = " + cullingParams.lodParameters.cameraPosition + "\n";
                }

                //Debug.Log(tx);
                if (textMesh != null)
                {
                    textMesh.text = tx;
                    //Debug.Log("<color=#0F0>TextMesh is updated</color>");
                }
                else
                {
                    tx = "<color=#F00>TextMesh is null</color> Please hit play if you hasn't";
                    //Debug.Log(tx);
                }
            }


            context.SetupCameraProperties(camera);

            // clear depth buffer
            CommandBuffer cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, true, camera.backgroundColor);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            //  context.DrawSkybox(camera);

            // Setup DrawSettings and FilterSettings
            ShaderPassName          passName       = new ShaderPassName("BasicPass");
            DrawRendererSettings    drawSettings   = new DrawRendererSettings(camera, passName);
            FilterRenderersSettings filterSettings = new FilterRenderersSettings(true);

            //Draw passes that has no light mode (default)
            ShaderPassName       passNameDefault     = new ShaderPassName("");
            DrawRendererSettings drawSettingsDefault = new DrawRendererSettings(camera, passNameDefault);
            drawSettingsDefault.SetShaderPassName(1, m_UnlitPassName);

            // Draw opaque objects using BasicPass shader pass
            drawSettings.sorting.flags      = SortFlags.CommonOpaque;
            filterSettings.renderQueueRange = RenderQueueRange.opaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            // Default
            drawSettingsDefault.sorting.flags = SortFlags.CommonOpaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettingsDefault, filterSettings);

            // Draw transparent objects using BasicPass shader pass
            drawSettings.sorting.flags      = SortFlags.CommonTransparent;
            filterSettings.renderQueueRange = RenderQueueRange.transparent;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            // Default
            drawSettingsDefault.sorting.flags = SortFlags.CommonTransparent;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettingsDefault, filterSettings);

            context.Submit();
        }
    }
示例#8
0
    public static void Render(ScriptableRenderContext context, IEnumerable <Camera> cameras, SRP07CustomParameter SRP07CP)
    {
        string tx = "";

        foreach (Camera camera in cameras)
        {
            ScriptableCullingParameters cullingParams;

            // Stereo-aware culling parameters are configured to perform a single cull for both eyes
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            CullResults cull = new CullResults();
            CullResults.Cull(ref cullingParams, context, ref cull);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            CommandBuffer cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, !SRP07CP.DrawSkybox, SRP07CP.ClearColor);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Setup global lighting shader variables
            SetupLightShaderVariables(cull.visibleLights, context);

            if (SRP07CP.DrawSkybox)
            {
                // Draw skybox
                context.DrawSkybox(camera);
            }

            // Setup DrawSettings and FilterSettings
            ShaderPassName          passName       = new ShaderPassName("BasicPass");
            DrawRendererSettings    drawSettings   = new DrawRendererSettings(camera, passName);
            FilterRenderersSettings filterSettings = new FilterRenderersSettings(true);

            // ////////////////////////////////////////////////////////////
            VisibleLight[]        ls = cull.visibleLights.ToArray();
            DrawShadowsSettings[] shadowsSettings = new DrawShadowsSettings[ls.Length];

            for (int i = 0; i < shadowsSettings.Length; i++)
            {
                shadowsSettings[i] = new DrawShadowsSettings(cull, i);
            }

            /*
             * if(camera == Camera.main) //Only generate result from main cam
             * {
             *  tx += "DrawShadowsSettings" + "\n"+ "\n";
             *
             *  for (int i=0; i<ls.Length; i++)
             *  {
             *      tx += "lightIndex = " + shadowsSettings[i].lightIndex + " (" + ls[i].light.name + ") " + "\n";
             *      tx += "splitData.cullingPlaneCount = " + shadowsSettings[i].splitData.cullingPlaneCount + "\n";
             *      tx += "splitData.cullingSphere = " + shadowsSettings[i].splitData.cullingSphere + "\n"+ "\n";
             *  }
             *
             *  // Output to text
             *  if (textMesh != null)
             *  {
             *      textMesh.text = tx;
             *      Debug.Log("<color=#0F0>TextMesh is updated</color>");
             *  }
             *  else
             *  {
             *      tx = "<color=#F00>TextMesh is null</color> Please hit play if you hasn't";
             *      Debug.Log(tx);
             *  }
             * }
             */
            // ////////////////////////////////////////////////////////////

            if (SRP07CP.DrawOpaque)
            {
                // Draw opaque objects using BasicPass shader pass
                drawSettings.sorting.flags      = SortFlags.CommonOpaque;
                filterSettings.renderQueueRange = RenderQueueRange.opaque;
                context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

                for (int i = 0; i < shadowsSettings.Length; i++)
                {
                    //if(ls[i].light.shadows != LightShadows.None)
                    //context.DrawShadows(ref shadowsSettings[i]);
                }
            }

            if (SRP07CP.DrawTransparent)
            {
                // Draw transparent objects using BasicPass shader pass
                drawSettings.sorting.flags      = SortFlags.CommonTransparent;
                filterSettings.renderQueueRange = RenderQueueRange.transparent;
                context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);
            }

            context.Submit();
        }
    }
    // Main entry point for our scriptable render loop
    public static void Render(ScriptableRenderContext context, IEnumerable <Camera> cameras)
    {
        bool stereoEnabled = XRSettings.isDeviceActive;

        foreach (Camera camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;

            // Stereo-aware culling parameters are configured to perform a single cull for both eyes
            if (!CullResults.GetCullingParameters(camera, stereoEnabled, out cullingParams))
            {
                continue;
            }
            CullResults cull = new CullResults();
            CullResults.Cull(ref cullingParams, context, ref cull);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            // If stereo is enabled, we also configure stereo matrices, viewports, and XR device render targets
            context.SetupCameraProperties(camera, stereoEnabled);

            // Draws in-between [Start|Stop]MultiEye are stereo-ized by engine
            if (stereoEnabled)
            {
                context.StartMultiEye(camera);
            }

            // clear depth buffer
            CommandBuffer cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Setup global lighting shader variables
            SetupLightShaderVariables(cull.visibleLights, context);

            // Draw skybox
            context.DrawSkybox(camera);

            // Setup DrawSettings and FilterSettings
            ShaderPassName          passName       = new ShaderPassName("BasicPass");
            DrawRendererSettings    drawSettings   = new DrawRendererSettings(camera, passName);
            FilterRenderersSettings filterSettings = new FilterRenderersSettings(true);

            //*************************************************************
            // Block
            RenderStateBlock rsb = new RenderStateBlock(RenderStateMask.Depth | RenderStateMask.Blend | RenderStateMask.Raster | RenderStateMask.Stencil);

            DepthState ds = rsb.depthState;//
            ds.writeEnabled    = true;
            ds.compareFunction = CompareFunction.LessEqual;
            rsb.depthState     = ds;                     //

            BlendState             bs  = rsb.blendState; //
            RenderTargetBlendState rs0 = bs.blendState0; //
            bs.alphaToMask                = false;
            rs0.sourceColorBlendMode      = BlendMode.SrcAlpha;
            rs0.destinationColorBlendMode = BlendMode.One;
            rs0.colorBlendOperation       = BlendOp.Add;
            rs0.sourceAlphaBlendMode      = BlendMode.Zero;
            rs0.destinationAlphaBlendMode = BlendMode.One;
            rs0.alphaBlendOperation       = BlendOp.Add;
            rs0.writeMask  = ColorWriteMask.All;
            bs.blendState0 = rs0;             //
            rsb.blendState = bs;              //

            RasterState rs = rsb.rasterState; //
            rs.cullingMode  = CullMode.Off;
            rs.depthClip    = false;
            rs.offsetFactor = 0;
            rs.offsetUnits  = 0;
            rsb.rasterState = rs;//


            StencilState ss = rsb.stencilState;//
            rsb.stencilReference    = 0;
            ss.compareFunction      = CompareFunction.Disabled;
            ss.compareFunctionBack  = CompareFunction.Disabled;
            ss.compareFunctionFront = CompareFunction.Disabled;
            ss.failOperation        = StencilOp.Keep;
            ss.failOperationBack    = StencilOp.Keep;
            ss.failOperationFront   = StencilOp.Keep;
            ss.passOperation        = StencilOp.Keep;
            ss.passOperationBack    = StencilOp.Keep;
            ss.passOperationFront   = StencilOp.Keep;
            ss.zFailOperation       = StencilOp.Keep;
            ss.zFailOperationBack   = StencilOp.Keep;
            ss.zFailOperationFront  = StencilOp.Keep;
            ss.readMask             = 255;
            ss.writeMask            = 255;
            ss.enabled       = true;
            rsb.stencilState = ss;//

            //**************************************************************
            //mapping

            RenderStateBlock rsb_opaque = new RenderStateBlock(RenderStateMask.Raster | RenderStateMask.Stencil);
            rsb_opaque.rasterState      = rsb.rasterState;
            rsb_opaque.stencilState     = rsb.stencilState;
            rsb_opaque.stencilReference = rsb.stencilReference;

            RenderStateBlock rsb_trans = new RenderStateBlock(RenderStateMask.Blend);
            rsb_trans.blendState = rsb.blendState;

            RenderStateBlock rsb_over = new RenderStateBlock(RenderStateMask.Raster | RenderStateMask.Depth | RenderStateMask.Stencil);
            rsb_over.depthState       = rsb.depthState;
            rsb_over.rasterState      = rsb.rasterState;
            rsb_over.stencilState     = rsb.stencilState;
            rsb_over.stencilReference = rsb.stencilReference;

            List <RenderStateMapping> rsm = new List <RenderStateMapping>
            {
                new RenderStateMapping("Opaque", rsb_opaque),
                new RenderStateMapping("Transparent", rsb_trans),
                new RenderStateMapping("Overlay", rsb_over)
            };

            //**************************************************************

            // Draw opaque objects using BasicPass shader pass
            filterSettings.layerMask        = LayerMask.GetMask("Default");
            drawSettings.sorting.flags      = SortFlags.CommonOpaque;
            filterSettings.renderQueueRange = RenderQueueRange.opaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            // WITH RENDERSTATEBLOCK OPAQUE
            filterSettings.layerMask        = LayerMask.GetMask("TransparentFX");
            drawSettings.sorting.flags      = SortFlags.CommonOpaque;
            filterSettings.renderQueueRange = RenderQueueRange.opaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings, rsb);

            // WITH RENDERSTATEMAPPING OPAQUE
            filterSettings.layerMask        = LayerMask.GetMask("Ignore Raycast");
            drawSettings.sorting.flags      = SortFlags.CommonOpaque;
            filterSettings.renderQueueRange = RenderQueueRange.opaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings, rsm);

            //**************************************************************

            // Draw transparent objects using BasicPass shader pass
            filterSettings.layerMask        = LayerMask.GetMask("Default");
            drawSettings.sorting.flags      = SortFlags.CommonTransparent;
            filterSettings.renderQueueRange = RenderQueueRange.transparent;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            // WITH RENDERSTATEBLOCK TRANSPARENT
            filterSettings.layerMask        = LayerMask.GetMask("TransparentFX");
            drawSettings.sorting.flags      = SortFlags.CommonTransparent;
            filterSettings.renderQueueRange = RenderQueueRange.transparent;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings, rsb);

            // WITH RENDERSTATEMAPPING TRANSPARENT
            filterSettings.layerMask        = LayerMask.GetMask("Ignore Raycast");
            drawSettings.sorting.flags      = SortFlags.CommonTransparent;
            filterSettings.renderQueueRange = RenderQueueRange.transparent;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings, rsm);

            //**************************************************************

            if (stereoEnabled)
            {
                context.StopMultiEye(camera);
                // StereoEndRender will reset state on the camera to pre-Stereo settings,
                // and invoke XR based events/callbacks.
                context.StereoEndRender(camera);
            }

            context.Submit();
        }
    }
示例#10
0
    private static readonly ShaderPassName m_UnlitPassName = new ShaderPassName("SRPDefaultUnlit"); //For default shaders

    public static void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        RenderPipeline.BeginFrameRendering(cameras);

        foreach (Camera camera in cameras)
        {
            RenderPipeline.BeginCameraRendering(camera);

            // Culling
            ScriptableCullingParameters cullingParams;

            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            CullResults cull = new CullResults();
            CullResults.Cull(ref cullingParams, context, ref cull);

            //==============================================
            if (camera.name == "MainCamera" || camera.name == "AllCam" || camera.name == "NoneCam")
            {
                string tx = "";
                tx += "Culling Result of : " + camera.name + " \n";
                tx += "\n";
                //-------------------------------
                VisibleLight[] ls = cull.visibleLights.ToArray();
                tx += "Lights : Visible : " + ls.Length + "\n";

                if (lights != null)
                {
                    for (int i = 0; i < lights.Length; i++)
                    {
                        int existed = 0;
                        for (int j = 0; j < ls.Length; j++)
                        {
                            if (lights[i] == ls[j].light)
                            {
                                existed++;
                            }
                        }
                        if (existed > 0)
                        {
                            tx += lights[i].gameObject.name + " : <color=#0F0>Visible</color>" + "\n";
                        }
                        else
                        {
                            tx += lights[i].gameObject.name + " : <color=#F00>Not Visible</color>" + "\n";
                        }
                    }
                }
                else
                {
                    tx += "Light list is null \n";
                }
                tx += "\n";
                //-------------------------------
                VisibleReflectionProbe[] rs = cull.visibleReflectionProbes.ToArray();
                tx += "Reflection Probes : Visible : " + rs.Length + "\n";

                if (reflprobes != null)
                {
                    for (int i = 0; i < reflprobes.Length; i++)
                    {
                        int existed = 0;
                        for (int j = 0; j < rs.Length; j++)
                        {
                            if (reflprobes[i] == rs[j].probe)
                            {
                                existed++;
                            }
                        }
                        if (existed > 0)
                        {
                            tx += reflprobes[i].gameObject.name + " : <color=#0F0>Visible</color>" + "\n";
                        }
                        else
                        {
                            tx += reflprobes[i].gameObject.name + " : <color=#F00>Not Visible</color>" + "\n";
                        }
                    }
                }
                else
                {
                    tx += "reflection probe list is null \n";
                }
                tx += "\n";
                //-------------------------------

                tx += "Renderers : \n";
                if (rens != null)
                {
                    for (int i = 0; i < rens.Length; i++)
                    {
                        if (rens[i].isVisible)
                        {
                            tx += rens[i].gameObject.name + " <color=#0F0>Yes</color> \n";
                        }
                        else
                        {
                            tx += rens[i].gameObject.name + " <color=#F00>No</color> \n";
                        }
                    }

                    tx += "\n";
                }



                //-------------------------------
                //Show debug msg on TextMesh
                //Debug.Log(tx);

                if (textMesh != null)
                {
                    textMesh.text = tx;
                    //Debug.Log("<color=#0F0>TextMesh is updated</color>");
                }
                else
                {
                    tx = "<color=#F00>TextMesh is null</color> Please hit play if you hasn't";
                    //Debug.Log(tx);
                }

                //update = false;
            }
            //==============================================

            context.SetupCameraProperties(camera);

            // clear depth buffer
            CommandBuffer cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Setup global lighting shader variables
            SetupLightShaderVariables(cull.visibleLights, context);

            // Draw skybox
            context.DrawSkybox(camera);

            // Setup DrawSettings and FilterSettings
            ShaderPassName          passName       = new ShaderPassName("BasicPass");
            DrawRendererSettings    drawSettings   = new DrawRendererSettings(camera, passName);
            FilterRenderersSettings filterSettings = new FilterRenderersSettings(true);

            //Draw passes that has no light mode (default)
            ShaderPassName       passNameDefault     = new ShaderPassName("");
            DrawRendererSettings drawSettingsDefault = new DrawRendererSettings(camera, passNameDefault);
            drawSettingsDefault.SetShaderPassName(1, m_UnlitPassName);

            // Draw opaque objects using BasicPass shader pass
            drawSettings.sorting.flags      = SortFlags.CommonOpaque;
            filterSettings.renderQueueRange = RenderQueueRange.opaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            // Default
            drawSettingsDefault.sorting.flags = SortFlags.CommonOpaque;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettingsDefault, filterSettings);

            // Draw transparent objects using BasicPass shader pass
            drawSettings.sorting.flags      = SortFlags.CommonTransparent;
            filterSettings.renderQueueRange = RenderQueueRange.transparent;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);

            // Default
            drawSettingsDefault.sorting.flags = SortFlags.CommonTransparent;
            context.DrawRenderers(cull.visibleRenderers, ref drawSettingsDefault, filterSettings);

            context.Submit();
        }
    }