Ejemplo n.º 1
0
        public void LightsAreSortedByLightOrder()
        {
            var light1 = m_TestObject1.AddComponent <Light2D>();
            var light2 = m_TestObject2.AddComponent <Light2D>();
            var light3 = m_TestObject3.AddComponent <Light2D>();

            light1.lightOrder = 1;
            light2.lightOrder = 2;
            light3.lightOrder = 0;

            var camera    = m_TestObject4.AddComponent <Camera>();
            var cameraPos = camera.transform.position;

            light1.transform.position = cameraPos;
            light2.transform.position = cameraPos;
            light3.transform.position = cameraPos;

            light1.UpdateMesh();
            light1.UpdateBoundingSphere();
            light2.UpdateMesh();
            light2.UpdateBoundingSphere();
            light3.UpdateMesh();
            light3.UpdateBoundingSphere();

            var cullResult    = new Light2DCullResult();
            var cullingParams = new ScriptableCullingParameters();

            camera.TryGetCullingParameters(out cullingParams);
            cullResult.SetupCulling(ref cullingParams, camera);

            Assert.AreSame(light3, cullResult.visibleLights[0]);
            Assert.AreSame(light1, cullResult.visibleLights[1]);
            Assert.AreSame(light2, cullResult.visibleLights[2]);
        }
        public override void SetupCullingParameters(ref ScriptableCullingParameters cullingParameters, ref CameraData cameraData)
        {
            ShadowTextureRenderer shadowTextureRenderer = cameraData.camera.GetComponent <ShadowTextureRenderer>();

            if (shadowTextureRenderer == null)
            {
#if UNITY_EDITOR
                Debug.LogWarning("No ShadowTextureRenderer found.");
#endif
                return;
            }
            shadowTextureRenderer.UpdateVisibilityAndPrepareRendering();
            cullingParameters.cullingMask = 0;
            if (shadowTextureRenderer.isProjectorVisible)
            {
                DrawSceneObject drawScene = cameraData.camera.GetComponent <DrawSceneObject>();
                if (drawScene != null)
                {
                    cullingParameters.cullingMask = (uint)drawScene.cullingMask.value;
                }
                DrawTargetObject drawTarget = shadowTextureRenderer.GetComponent <DrawTargetObject>();
                if (drawTarget != null)
                {
                    drawTarget.SendMessage("OnPreCull");
                }
                shadowTextureRenderer.ConfigureRenderTarget(m_renderShadowTexturePass, ref cameraData);
            }
            cullingParameters.cullingOptions = CullingOptions.None;
            cullingParameters.shadowDistance = 0;
        }
Ejemplo n.º 3
0
 protected override void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera hdCamera)
 {
     cullingParameters.cullingMask |= (uint)videoObjectMask.value;
     if (fixDepthBufferJittering)
     {
         cullingParameters.cullingMask |= (uint)fixDepthBufferJitteringMask.value;
     }
 }
Ejemplo n.º 4
0
 internal void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera hdCamera)
 {
     foreach (var pass in customPasses)
     {
         if (pass != null && pass.enabled)
         {
             pass.InternalAggregateCullingParameters(ref cullingParameters, hdCamera);
         }
     }
 }
Ejemplo n.º 5
0
        // Stopgap method used to extract stereo combined matrix state.
        public void UpdateStereoDependentState(ref ScriptableCullingParameters cullingParams)
        {
            // XRTODO: remove this after culling management is finished
            if (camera.stereoEnabled && viewCount > 1)
            {
                var view = cullingParams.stereoViewMatrix;
                var proj = cullingParams.stereoProjectionMatrix;

                UpdateViewConstants(ref mainViewConstants, proj, view, cullingParams.origin, IsTAAEnabled());
            }
        }
Ejemplo n.º 6
0
        bool Culling(ScriptableRenderContext context, Decal decal, ref RenderingData renderingData, out CullingResults cullingResults)
        {
            // Setup
            var camera     = renderingData.cameraData.camera;
            var localScale = decal.transform.lossyScale;

            cullingResults = new CullingResults();

            // Never draw in Preview
            if (camera.cameraType == CameraType.Preview)
            {
                return(false);
            }

            // Test for Decal behind Camera
            var maxRadius  = Mathf.Max(Mathf.Max(localScale.x * 0.5f, localScale.y * 0.5f), localScale.z) + kErrorMargin;
            var positionVS = camera.WorldToViewportPoint(decal.transform.position);

            if (positionVS.z < -maxRadius)
            {
                return(false);
            }

            // Get Decal bounds
            var boundsScale = new Vector3(maxRadius, maxRadius, maxRadius);
            var bounds      = new Bounds(decal.transform.position, boundsScale);

            // Test against frustum planes
            var planes = GeometryUtility.CalculateFrustumPlanes(camera);

            if (!GeometryUtility.TestPlanesAABB(planes, bounds))
            {
                return(false);
            }

            // Get CullingParameters
            var cullingParameters = new ScriptableCullingParameters();

            if (!camera.TryGetCullingParameters(out cullingParameters))
            {
                return(false);
            }

            // Set culling planes
            cullingParameters.cullingPlaneCount = 6;
            for (int i = 0; i < 6; ++i)
            {
                cullingParameters.SetCullingPlane(i, decal.clipPlanes[i]);
            }

            // Culling Results
            cullingResults = context.Cull(ref cullingParameters);
            return(true);
        }
Ejemplo n.º 7
0
        public override void SetupCullingParameters(ref ScriptableCullingParameters cullingParameters,
            ref CameraData cameraData)
        {
            Camera camera = cameraData.camera;
            // TODO: PerObjectCulling also affect reflection probes. Enabling it for now.
            // if (asset.additionalLightsRenderingMode == LightRenderingMode.Disabled ||
            //     asset.maxAdditionalLightsCount == 0)
            // {
            //     cullingParameters.cullingOptions |= CullingOptions.DisablePerObjectCulling;
            // }

            cullingParameters.shadowDistance = Mathf.Min(cameraData.maxShadowDistance, camera.farClipPlane);
        }
Ejemplo n.º 8
0
        public void SetupCulling(ref ScriptableCullingParameters cullingParameters, Camera camera)
        {
            Profiler.BeginSample("Cull 2D Lights");
            m_VisibleLights.Clear();
            foreach (var light in Light2DManager.lights)
            {
                if ((camera.cullingMask & (1 << light.gameObject.layer)) == 0)
                {
                    continue;
                }

#if UNITY_EDITOR
                if (!UnityEditor.SceneManagement.StageUtility.IsGameObjectRenderedByCamera(light.gameObject, camera))
                {
                    continue;
                }
#endif

                if (light.lightType == Light2D.LightType.Global)
                {
                    m_VisibleLights.Add(light);
                    continue;
                }

                Profiler.BeginSample("Test Planes");
                var position = light.boundingSphere.position;
                var culled   = false;
                for (var i = 0; i < cullingParameters.cullingPlaneCount; ++i)
                {
                    var plane = cullingParameters.GetCullingPlane(i);
                    // most of the time is spent getting world position
                    var distance = math.dot(position, plane.normal) + plane.distance;
                    if (distance < -light.boundingSphere.radius)
                    {
                        culled = true;
                        break;
                    }
                }
                Profiler.EndSample();
                if (culled)
                {
                    continue;
                }

                m_VisibleLights.Add(light);
            }

            // must be sorted here because light order could change
            m_VisibleLights.Sort((l1, l2) => l1.lightOrder - l2.lightOrder);
            Profiler.EndSample();
        }
Ejemplo n.º 9
0
        // Stopgap method used to extract stereo combined matrix state.
        public void UpdateStereoDependentState(ref ScriptableCullingParameters cullingParams)
        {
            if (!m_frameSettings.enableStereo)
            {
                return;
            }

            // What constants in UnityPerPass need updating for stereo considerations?
            // _ViewProjMatrix - It is used directly for generating tesselation factors. This should be the same
            //                   across both eyes for consistency, and to keep shadow-generation eye-independent
            // _DetViewMatrix -  Used for isFrontFace determination, should be the same for both eyes. There is the scenario
            //                   where there might be multi-eye sets that are divergent enough where this assumption is not valid,
            //                   but that's a future problem
            // _InvProjParam -   Intention was for generating linear depths, but not currently used.  Will need to be stereo-ized if
            //                   actually needed.
            // _FrustumPlanes -  Also used for generating tesselation factors.  Should be fine to use the combined stereo VP
            //                   to calculate frustum planes.

            // TODO: Would it be worth calculating my own combined view/proj matrix in Update?
            // In engine, we modify the view and proj matrices accordingly in order to generate the single cull
            // * Get the center eye view matrix, and pull it back to cover both eyes
            // * Generated an expanded projection matrix (one method - max bound of left/right proj matrices)
            //   and move near/far planes to match near/far locations of proj matrices located at eyes.
            // I think using the cull matrices is valid, as long as I only use them for tess factors in shader.
            // Using them for other calculations (like light list generation) could be problematic.

            var stereoCombinedViewMatrix = cullingParams.cullStereoView;

            if (ShaderConfig.s_CameraRelativeRendering != 0)
            {
                // This is pulled back from the center eye, so set the offset
                var translation = stereoCombinedViewMatrix.GetColumn(3);
                translation += centerEyeTranslationOffset;
                stereoCombinedViewMatrix.SetColumn(3, translation);
            }

            viewMatrix = stereoCombinedViewMatrix;
            var stereoCombinedProjMatrix = cullingParams.cullStereoProj;

            projMatrix = GL.GetGPUProjectionMatrix(stereoCombinedProjMatrix, true);

            detViewMatrix = viewMatrix.determinant;

            frustum = Frustum.Create(viewProjMatrix, true, true);

            // Left, right, top, bottom, near, far.
            for (int i = 0; i < 6; i++)
            {
                frustumPlaneEquations[i] = new Vector4(frustum.planes[i].normal.x, frustum.planes[i].normal.y, frustum.planes[i].normal.z, frustum.planes[i].distance);
            }
        }
Ejemplo n.º 10
0
        //这个函数在需要绘制管线的时候调用。
        public override void Render(ScriptableRenderContext context, Camera[] cameras)
        {
            base.Render(context, cameras);

            if (_cb == null)
            {
                _cb = new CommandBuffer();             // new CommandBuffer
            }
            //对于每一个相机执行操作。
            foreach (var camera in cameras)
            {
                //将上下文设置为当前相机的上下文。
                context.SetupCameraProperties(camera);
                _cb.name = "Setup";
                //显式将当前渲染目标设置为相机Backbuffer。
                _cb.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
                //设置渲染目标的颜色为相机背景色。
                _cb.ClearRenderTarget(true, true, camera.backgroundColor);
                context.ExecuteCommandBuffer(_cb);
                _cb.Clear();

                //绘制天空盒子,注意需要在ClearRenderTarget之后进行,不然颜色会被覆盖。
                context.DrawSkybox(camera);

                // 裁剪(Culling)
                var param = new ScriptableCullingParameters();
                CullResults.GetCullingParameters(camera, out param);    // 取出相机的裁剪信息
                param.isOrthographic = false;
                var culled = CullResults.Cull(ref param, context);

                // 过滤(Filtering)
                var fs = new FilterRenderersSettings(true);
                //设置只绘制不透明物体。
                fs.renderQueueRange = RenderQueueRange.opaque;
                //设置绘制所有层
                fs.layerMask = ~0;

                // 绘制设置(Renderer Settings)
                //注意在构造的时候就需要传入Lightmode参数
                var rs = new DrawRendererSettings(camera, new ShaderPassName("Unlit"));
                //由于绘制不透明物体可以借助Z-Buffer,因此不需要额外的排序。
                rs.sorting.flags = SortFlags.None;

                context.DrawRenderers(culled.visibleRenderers, ref rs, fs);

                context.Submit();                    // submit

                //开始执行管线
                context.Submit();
            }
        }
Ejemplo n.º 11
0
 public void GetCullingParameters(Camera camera, int cullingPassIndex, out ScriptableCullingParameters scriptableCullingParameters)
 {
     if (!Internal_TryGetCullingParams(camera, cullingPassIndex, out scriptableCullingParameters))
     {
         if (camera == null)
         {
             throw new ArgumentNullException("camera");
         }
         else
         {
             throw new IndexOutOfRangeException("cullingPassIndex");
         }
     }
 }
        public void GetCullingParameters(Camera camera, int cullingPassIndex, out ScriptableCullingParameters scriptableCullingParameters)
        {
            bool flag = !this.Internal_TryGetCullingParams(camera, cullingPassIndex, out scriptableCullingParameters);

            if (!flag)
            {
                return;
            }
            bool flag2 = camera == null;

            if (flag2)
            {
                throw new ArgumentNullException("camera");
            }
            throw new IndexOutOfRangeException("cullingPassIndex");
        }
Ejemplo n.º 13
0
        public void LightIsNotInVisibleListIfNotInCameraView()
        {
            var camera = m_TestObject1.AddComponent <Camera>();
            var light  = m_TestObject2.AddComponent <Light2D>();

            light.transform.position = camera.transform.position + new Vector3(9999.0f, 0.0f, 0.0f);
            light.UpdateMesh();
            light.UpdateBoundingSphere();

            var cullResult    = new Light2DCullResult();
            var cullingParams = new ScriptableCullingParameters();

            camera.TryGetCullingParameters(out cullingParams);
            cullResult.SetupCulling(ref cullingParams, camera);

            Assert.IsFalse(cullResult.visibleLights.Contains(light));
        }
Ejemplo n.º 14
0
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        foreach (var camera in cameras)
        {
            // 这边主要是设置一些相机相关的渲染参数,比如MPV等等,这些都与相机的位置、朝向、是否正交等相关
            context.SetupCameraProperties(camera);

            // 绘制一个天空盒,传入相机只不过是使用相机的clear flags来确定该天空盒是否需要绘制
            context.DrawSkybox(camera);

            // 剪裁,这边应该是相机视锥剪裁相关
            // 自定义一个剪裁参数,cullParam类里面有很多可以设置的东西。我们先简单的采用相机的默认剪裁参数
            ScriptableCullingParameters cullParam = new ScriptableCullingParameters();
            // 直接使用相机默认剪裁参数
            camera.TryGetCullingParameters(out cullParam);
            // 对相机的裁剪参数做一些修改,非正交相机
            cullParam.isOrthographic = false;
            // 获取剪裁之后的全部结果(其中不仅有渲染物体,还有相关的其他渲染要素)
            CullingResults cullResults = context.Cull(ref cullParam);

            // 此时也就获得了视锥内的所有需要渲染的内容,这里还可以获取灯光等相关渲染需要的参数。

            // 渲染设置
            // 渲染时,会牵扯到渲染排序,所以要先进行一个相机的排序设置,这里Unity内置了一些默认的排序可以调用
            SortingSettings sortSet = new SortingSettings(camera)
            {
                criteria = SortingCriteria.CommonOpaque
            };
            // 这边进行渲染相关设置,需要指定渲染shader的光照模式(就是这里,如果shader中没有标注LightMode的话,使用该shader的物体就没法进行渲染了)和上面的排序设置两个参数
            // 下面的意思是使用Shader中LightMode为Always的Pass来绘制
            DrawingSettings drawSetting = new DrawingSettings(new ShaderTagId("Always"), sortSet);

            // 过滤
            // 这边是指定渲染的种类(对应Shader中的RenderType)和相关Layer的设置(-1表示全部Layer)
            // 下面这句话的意思是渲染Geometry通道里面的所有物体,物体所处的渲染通道,是shader中Queue指定的。至于为啥RenderQueueRange.opaque对应Geometry,这个不太清楚
            // Layer是Unity面板中指定的
            FilteringSettings filterSet = new FilteringSettings(RenderQueueRange.opaque, -1);

            context.DrawRenderers(cullResults, ref drawSetting, ref filterSet);
            context.Submit();
        }
    }
Ejemplo n.º 15
0
        public override void SetupCullingParameters(ref ScriptableCullingParameters cullingParameters,
                                                    ref CameraData cameraData)
        {
            Camera camera = cameraData.camera;

            // TODO: PerObjectCulling also affect reflection probes. Enabling it for now.
            // if (asset.additionalLightsRenderingMode == LightRenderingMode.Disabled ||
            //     asset.maxAdditionalLightsCount == 0)
            // {
            //     cullingParameters.cullingOptions |= CullingOptions.DisablePerObjectCulling;
            // }

            // If shadow is disabled, disable shadow caster culling
            if (Mathf.Approximately(cameraData.maxShadowDistance, 0.0f))
            {
                cullingParameters.cullingOptions &= ~CullingOptions.ShadowCasters;
            }

            cullingParameters.shadowDistance = cameraData.maxShadowDistance;
        }
Ejemplo n.º 16
0
        void DrawObjectMotionVectors(ScriptableRenderContext context, ref RenderingData renderingData, CommandBuffer cmd, Camera camera)
        {
            // Get CullingParameters
            var cullingParameters = new ScriptableCullingParameters();

            if (!camera.TryGetCullingParameters(out cullingParameters))
            {
                return;
            }

            // Culling Results
            var cullingResults = context.Cull(ref cullingParameters);

            var drawingSettings   = GetDrawingSettings(ref renderingData);
            var filteringSettings = new FilteringSettings(RenderQueueRange.opaque, camera.cullingMask);
            var renderStateBlock  = new RenderStateBlock(RenderStateMask.Nothing);

            // Draw Renderers
            context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings, ref renderStateBlock);
        }
Ejemplo n.º 17
0
        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            ScriptableCullingParameters cullingParameters = new ScriptableCullingParameters();

            renderingData.cameraData.camera.TryGetCullingParameters(out cullingParameters);
            var cullResults = context.Cull(ref cullingParameters);

            CommandBuffer cmd = CommandBufferPool.Get(Tag);

            using (new ProfilingSample(cmd, Tag))
            {
                context.ExecuteCommandBuffer(cmd);
                cmd.Clear();
                var cam          = renderingData.cameraData.camera;
                var sortFlags    = renderingData.cameraData.defaultOpaqueSortFlags;
                var drawSettings = CreateDrawingSettings(shaderTagIdList, ref renderingData, sortFlags);

                context.DrawRenderers(cullResults, ref drawSettings, ref filteringSettings);
            }
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
Ejemplo n.º 18
0
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        foreach (var camera in cameras)
        {
            context.SetupCameraProperties(camera);
            context.DrawSkybox(camera);
            var cullParam = new ScriptableCullingParameters();
            camera.TryGetCullingParameters(out cullParam);
            cullParam.isOrthographic = false;

            var cullResults = context.Cull(ref cullParam);
            var sortSet     = new SortingSettings(camera)
            {
                criteria = SortingCriteria.CommonOpaque
            };
            var drawSet = new DrawingSettings(new ShaderTagId("Always"), sortSet);
            var filtSet = new FilteringSettings(RenderQueueRange.opaque, -1);
            context.DrawRenderers(cullResults, ref drawSet, ref filtSet);

            context.Submit();
        }
    }
Ejemplo n.º 19
0
        // Stopgap method used to extract stereo combined matrix state.
        public void UpdateStereoDependentState(ref ScriptableCullingParameters cullingParams)
        {
            if (!camera.stereoEnabled)
            {
                return;
            }

            // What constants in UnityPerPass need updating for stereo considerations?
            // _ViewProjMatrix - It is used directly for generating tesselation factors. This should be the same
            //                   across both eyes for consistency, and to keep shadow-generation eye-independent
            // _InvProjParam -   Intention was for generating linear depths, but not currently used.  Will need to be stereo-ized if
            //                   actually needed.
            // _FrustumPlanes -  Also used for generating tesselation factors.  Should be fine to use the combined stereo VP
            //                   to calculate frustum planes.

            // TODO: Would it be worth calculating my own combined view/proj matrix in Update?
            // In engine, we modify the view and proj matrices accordingly in order to generate the single cull
            // * Get the center eye view matrix, and pull it back to cover both eyes
            // * Generated an expanded projection matrix (one method - max bound of left/right proj matrices)
            //   and move near/far planes to match near/far locations of proj matrices located at eyes.
            // I think using the cull matrices is valid, as long as I only use them for tess factors in shader.
            // Using them for other calculations (like light list generation) could be problematic.

            var stereoCombinedViewMatrix = cullingParams.stereoViewMatrix;

            viewMatrix = stereoCombinedViewMatrix;
            var stereoCombinedProjMatrix = cullingParams.stereoProjectionMatrix;

            projMatrix = GL.GetGPUProjectionMatrix(stereoCombinedProjMatrix, true);

            Frustum.Create(frustum, viewProjMatrix, true, true);

            // Left, right, top, bottom, near, far.
            for (int i = 0; i < 6; i++)
            {
                frustumPlaneEquations[i] = new Vector4(frustum.planes[i].normal.x, frustum.planes[i].normal.y, frustum.planes[i].normal.z, frustum.planes[i].distance);
            }
        }
        public void Render(ref ScriptableRenderContext context, GODRPParameters p)
        {
            ScriptableCullingParameters cullingParam = new ScriptableCullingParameters();

            p.camera.TryGetCullingParameters(out cullingParam);
            p.cullingResults = context.Cull(ref cullingParam);

            foreach (Transform child in transform)
            {
                if (!child.gameObject.activeInHierarchy)
                {
                    continue;
                }
                child.GetComponents(components);
                for (int i = 0; i < components.Count; i++)
                {
                    if (components[i] is IRenderTreeItem renderTreeItem)
                    {
                        renderTreeItem.Render(ref context, p);
                    }
                }
            }
        }
Ejemplo n.º 21
0
    public void RenderCamera(ScriptableRenderContext context, Camera camera)
    {
        CommandBuffer cameraBuffer = CommandBufferPool.Get(BUFFER_CAMERA);

#if UNITY_EDITOR
        if (camera.cameraType == CameraType.SceneView)
        {
            ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
        }
#endif
        context.SetupCameraProperties(camera);


        CameraClearFlags clearFlags = camera.clearFlags;
        cameraBuffer.ClearRenderTarget(
            (clearFlags & CameraClearFlags.Depth) != 0,
            (clearFlags & CameraClearFlags.Color) != 0,
            camera.backgroundColor
            );

        context.ExecuteCommandBuffer(cameraBuffer);


        CommandBufferPool.Release(cameraBuffer);

        cullingParameters = new ScriptableCullingParameters();

        if (camera.TryGetCullingParameters(out cullingParameters))
        {
            CullingResults cullingResult = context.Cull(ref cullingParameters);
            //RenderGBuffer(context, camera, ref cullingResult);
            DeferedRender(context, camera, ref cullingResult);
        }

        context.DrawSkybox(camera);
        context.Submit();
    }
Ejemplo n.º 22
0
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        //全部相机逐次渲染
        foreach (var camera in cameras)
        {
            //设置渲染相关相机参数,包含相机的各个矩阵和剪裁平面等
            context.SetupCameraProperties(camera);

            //1.剪裁:这边应该是相机的视锥剪裁相关。
            //自定义一个剪裁参数,cullParam类里有很多可以设置的东西。我们先简单采用相机的默认剪裁参数。
            ScriptableCullingParameters cullParam = new ScriptableCullingParameters();
            //直接使用相机默认剪裁参数
            camera.TryGetCullingParameters(out cullParam);
            //非正交相机
            cullParam.isOrthographic = false;
            //获取剪裁之后的全部结果(其中不仅有渲染物体,还有相关的其他渲染要素)
            CullingResults cullResults = context.Cull(ref cullParam);

            //2.渲染设置:渲染时,会牵扯到渲染排序,所以先要进行一个相机的排序设置,这里Unity内置了一些默认的排序可以调用
            SortingSettings sortSet = new SortingSettings(camera)
            {
                criteria = SortingCriteria.CommonOpaque
            };
            //这边进行渲染的相关设置,需要指定渲染的shader的光照模式(就是这里,如果shader中没有标注LightMode的
            //话,使用该shader的物体就没法进行渲染了)和上面的排序设置两个参数
            DrawingSettings drawSet = new DrawingSettings(new ShaderTagId("Always"), sortSet);

            //3.过滤:这边是指定渲染的种类(对应shader中的Rendertype)和相关Layer的设置(-1表示全部layer)
            FilteringSettings filtSet = new FilteringSettings(RenderQueueRange.opaque, -1);
            context.DrawRenderers(cullResults, ref drawSet, ref filtSet);

            //绘制天空球
            context.DrawSkybox(camera);
            //开始执行上下文
            context.Submit();
        }
    }
Ejemplo n.º 23
0
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        CommandBuffer command = new CommandBuffer();

        foreach (var camera in cameras)
        {
            //清除深度缓存和颜色缓存,输出纯颜色
            // context.SetupCameraProperties(camera);
            // command.ClearRenderTarget(true,true,Color.yellow);
            // context.ExecuteCommandBuffer(command);
            // context.Submit();

            //一定要设置这个,否则渲染不出任何东西
            context.SetupCameraProperties(camera);
            //渲染天空盒要在下面的代码之前,如果放到下面代码之后,则不能渲染出其他物体(应该是天空盒把后面的东西给覆盖了)
            context.DrawSkybox(camera);

            // Cull
            ScriptableCullingParameters cullParam = new ScriptableCullingParameters();
            camera.TryGetCullingParameters(out cullParam);
            cullParam.isOrthographic = false;
            CullingResults cullResults = context.Cull(ref cullParam);

            //render
            SortingSettings sortSet = new SortingSettings(camera)
            {
                criteria = SortingCriteria.CommonOpaque
            };
            DrawingSettings drawSet = new DrawingSettings(new ShaderTagId("Always"), sortSet);
            //filter
            FilteringSettings filtSet = new FilteringSettings(RenderQueueRange.opaque, -1);

            context.DrawRenderers(cullResults, ref drawSet, ref filtSet);
            context.Submit();
        }
    }
Ejemplo n.º 24
0
        //这个函数在需要绘制管线的时候调用。
        public override void Render(ScriptableRenderContext context, Camera[] cameras)
        {
            base.Render(context, cameras);
            if (_cb == null)
            {
                _cb = new CommandBuffer();             // new CommandBuffer
            }
            //准备好光源名称
            var _LightDir   = Shader.PropertyToID("_LightDir");
            var _LightColor = Shader.PropertyToID("_LightColor");
            var _CameraPos  = Shader.PropertyToID("_CameraPos");


            //对于每一个相机执行操作。
            foreach (var camera in cameras)
            {
                //将上下文设置为当前相机的上下文。
                context.SetupCameraProperties(camera);
                _cb.name = "Setup";
                //显式将当前渲染目标设置为相机Backbuffer。
                _cb.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
                //设置渲染目标的颜色为相机背景色。
                _cb.ClearRenderTarget(true, true, camera.backgroundColor);

                //设置相机的着色器全局变量_CameraPos
                Vector4 cameraPosition = new Vector4(camera.transform.localPosition.x, camera.transform.localPosition.y, camera.transform.localPosition.z, 1.0f);
                _cb.SetGlobalVector(_CameraPos, camera.transform.localToWorldMatrix * cameraPosition);
                context.ExecuteCommandBuffer(_cb);
                _cb.Clear();

                //绘制天空盒子,注意需要在ClearRenderTarget之后进行,不然颜色会被覆盖。
                context.DrawSkybox(camera);

                // 裁剪(Culling)
                var param = new ScriptableCullingParameters();
                CullResults.GetCullingParameters(camera, out param);    // 取出相机的裁剪信息
                param.isOrthographic = false;
                var culled = CullResults.Cull(ref param, context);

                //获取所有的灯光 All pass
                var lights = culled.visibleLights;
                _cb.name = "RenderLights";
                foreach (var light in lights)
                {
                    //我们只处理平行光
                    if (light.lightType != LightType.Directional)
                    {
                        continue;
                    }
                    //获取光源方向
                    Vector4 pos            = light.localToWorld.GetColumn(2);
                    Vector4 lightDirection = new Vector4(-pos.x, -pos.y, -pos.z, 0);
                    //获取光源颜色
                    Color LightColor = light.finalColor;
                    //构建shader常量缓存
                    _cb.SetGlobalVector(_LightDir, lightDirection);
                    _cb.SetGlobalColor(_LightColor, LightColor);
                    context.ExecuteCommandBuffer(_cb);
                    _cb.Clear();

                    // 过滤(Filtering)
                    var fs = new FilterRenderersSettings(true);
                    //设置只绘制不透明物体。
                    fs.renderQueueRange = RenderQueueRange.opaque;
                    //设置绘制所有层
                    fs.layerMask = ~0;

                    // 绘制设置(Renderer Settings)
                    //使用Shader中指定光照模式为BaseLit的pass
                    var drs = new DrawRendererSettings(camera, new ShaderPassName("BaseLit"));
                    //绘制物体
                    context.DrawRenderers(culled.visibleRenderers, ref drs, fs);

                    break;
                }
                //开始执行管线
                context.Submit();
            }
        }
Ejemplo n.º 25
0
 extern private bool Internal_TryGetCullingParams(Camera camera, int cullingPassIndex, out ScriptableCullingParameters scriptableCullingParameters);
 public void UpdateCullingParameters(ref ScriptableCullingParameters cullingParams)
 {
     cullingParams.shadowDistance = Mathf.Min(VolumeManager.instance.stack.GetComponent <HDShadowSettings>().maxShadowDistance, cullingParams.shadowDistance);
 }
Ejemplo n.º 27
0
 public void UpdateCullingParameters(ref ScriptableCullingParameters cullingParams, float maxShadowDistance)
 {
     cullingParams.shadowDistance = Mathf.Min(maxShadowDistance, cullingParams.shadowDistance);
 }
Ejemplo n.º 28
0
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        if (_cb == null)
        {
            _cb = new CommandBuffer();
        }

        var _lightDir   = Shader.PropertyToID("_LightDir");
        var _lightColor = Shader.PropertyToID("_LightColor");
        var _cameraPos  = Shader.PropertyToID("_CameraPos");

        foreach (var camera in cameras)
        {
            context.SetupCameraProperties(camera);

            _cb.name = "Setup";
            _cb.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
            _cb.ClearRenderTarget(true, true, camera.backgroundColor);

            Vector4 cameraPosition = new Vector4(camera.transform.localPosition.x, camera.transform.localPosition.y, camera.transform.localPosition.z, 1.0f);
            _cb.SetGlobalVector(_cameraPos, camera.transform.localToWorldMatrix * cameraPosition);
            context.ExecuteCommandBuffer(_cb);
            _cb.Clear();

            context.DrawSkybox(camera);

            ScriptableCullingParameters cullingParam = new ScriptableCullingParameters();
            camera.TryGetCullingParameters(out cullingParam);
            cullingParam.isOrthographic = false;
            CullingResults cullingResult = context.Cull(ref cullingParam);

            var lights = cullingResult.visibleLights;
            _cb.name = "RenderLights";
            foreach (var light in lights)
            {
                if (light.lightType != LightType.Directional)
                {
                    continue;
                }

                Vector4 pos            = light.localToWorldMatrix.GetColumn(2);
                Vector4 lightDirection = new Vector4(-pos.x, -pos.y, -pos.z, 0);

                Color lightColor = light.finalColor;

                _cb.SetGlobalVector(_lightDir, lightDirection);
                _cb.SetGlobalVector(_lightColor, lightColor);
                context.ExecuteCommandBuffer(_cb);
                _cb.Clear();

                FilteringSettings filterSetting   = new FilteringSettings(RenderQueueRange.opaque);
                DrawingSettings   drawingSettings = new DrawingSettings(new ShaderTagId("forwardbase"), new SortingSettings(camera)
                {
                    criteria = SortingCriteria.CommonOpaque
                });

                context.DrawRenderers(cullingResult, ref drawingSettings, ref filterSetting);
                break;
            }

            context.Submit();
        }
    }
Ejemplo n.º 29
0
 protected override void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera hdCamera)
 => cullingParameters.cullingMask |= (uint)maskLayer.value;
Ejemplo n.º 30
0
        private bool StartCullingIfVisible(ScriptableRenderContext context, Camera cam)
        {
            if (m_frustumVertices == null)
            {
                return(false);
            }
            ScriptableCullingParameters cullingParameters = new ScriptableCullingParameters();

            if (!cam.TryGetCullingParameters(IsStereoEnabled(cam), out cullingParameters))
            {
                return(false);
            }
            if (m_temporaryData == null)
            {
                m_temporaryData = new TemporaryData();
            }
            uint  flags   = 0xff;
            ulong flags64 = 0;

            for (int i = 0; i < 8; ++i)
            {
                Vector3 v = m_temporaryData.m_vertices[i] = transform.TransformPoint(m_frustumVertices[i]);
                uint    f = 0;
                for (int j = 0; j < cullingParameters.cullingPlaneCount; ++j)
                {
                    Plane plane = cullingParameters.GetCullingPlane(j);
                    if (plane.GetDistanceToPoint(v) < 0)
                    {
                        f |= (1U << j);
                    }
                }
                flags   &= f;
                flags64 |= (((ulong)f) << (8 * i));
            }
            if (flags != 0)
            {
                // projector is not visible from the camera
                return(false);
            }
            if (!m_requiresCullingResult)
            {
                return(true);
            }
            uint cameraPlanes = 0;
            int  planeCount   = 0;

            // -x
            flags = (uint)((flags64 >> 0) & (flags64 >> 8) & (flags64 >> 32) & (flags64 >> 40)) & 0xFF;
            if (flags == 0)
            {
                m_temporaryData.m_clipPlanes[planeCount++] = new Plane(m_temporaryData.m_vertices[0], m_temporaryData.m_vertices[1], m_temporaryData.m_vertices[4]);
            }
            else
            {
                cameraPlanes |= flags;
            }
            // +x
            flags = (uint)((flags64 >> 16) & (flags64 >> 24) & (flags64 >> 48) & (flags64 >> 56)) & 0xFF;
            if (flags == 0)
            {
                m_temporaryData.m_clipPlanes[planeCount++] = new Plane(m_temporaryData.m_vertices[3], m_temporaryData.m_vertices[2], m_temporaryData.m_vertices[7]);
            }
            else
            {
                cameraPlanes |= flags;
            }
            // -y
            flags = (uint)((flags64 >> 0) & (flags64 >> 16) & (flags64 >> 32) & (flags64 >> 48)) & 0xFF;
            if (flags == 0)
            {
                m_temporaryData.m_clipPlanes[planeCount++] = new Plane(m_temporaryData.m_vertices[2], m_temporaryData.m_vertices[0], m_temporaryData.m_vertices[6]);
            }
            else
            {
                cameraPlanes |= flags;
            }
            // +y
            flags = (uint)((flags64 >> 8) & (flags64 >> 24) & (flags64 >> 40) & (flags64 >> 56)) & 0xFF;
            if (flags == 0)
            {
                m_temporaryData.m_clipPlanes[planeCount++] = new Plane(m_temporaryData.m_vertices[1], m_temporaryData.m_vertices[3], m_temporaryData.m_vertices[5]);
            }
            else
            {
                cameraPlanes |= flags;
            }
            // near
            flags = (uint)((flags64 >> 0) & (flags64 >> 8) & (flags64 >> 16) & (flags64 >> 24)) & 0xFF;
            if (flags == 0)
            {
                m_temporaryData.m_clipPlanes[planeCount++] = new Plane(m_temporaryData.m_vertices[0], m_temporaryData.m_vertices[2], m_temporaryData.m_vertices[1]);
            }
            else
            {
                cameraPlanes |= flags;
            }
            // far
            flags = (uint)((flags64 >> 32) & (flags64 >> 40) & (flags64 >> 48) & (flags64 >> 56)) & 0xFF;
            if (flags == 0)
            {
                m_temporaryData.m_clipPlanes[planeCount++] = new Plane(m_temporaryData.m_vertices[4], m_temporaryData.m_vertices[5], m_temporaryData.m_vertices[6]);
            }
            else
            {
                cameraPlanes |= flags;
            }
            int maxPlaneCount = ScriptableCullingParameters.maximumCullingPlaneCount;

            for (int i = 0; i < cullingParameters.cullingPlaneCount && planeCount < maxPlaneCount; ++i)
            {
                if ((cameraPlanes & (1U << i)) != 0)
                {
                    m_temporaryData.m_clipPlanes[planeCount++] = cullingParameters.GetCullingPlane(i);
                }
            }
            cullingParameters.cullingPlaneCount = planeCount;
            for (int i = 0; i < planeCount; ++i)
            {
                cullingParameters.SetCullingPlane(i, m_temporaryData.m_clipPlanes[i]);
            }
#if DEBUG
            // To avoid the error: Assertion failed on expression: 'params.cullingPlaneCount == kPlaneFrustumNum'
            cullingParameters.cullingPlaneCount = 6;
#endif
            cullingParameters.cullingOptions &= ~(CullingOptions.NeedsReflectionProbes | CullingOptions.ShadowCasters);
            CullingResults cullingResults = context.Cull(ref cullingParameters);
            m_cullingResults.Add(cam, cullingResults);
            return(true);
        }