如何在自定义通道中渲染基本游戏对象(Unity:ScriptableRenderPass)?

问题描述 投票:0回答:0

我想做什么: 在我的自定义通道中渲染特定层的游戏对象。我想稍后再做一些事情,但我只是想让它完全按照 URP 现在的方式呈现。

我刚刚开始学习 ScriptableRenderPass/ScriptableRendererFeature,所以我从基础开始。

这是我想要的:

这是我得到的:

这是我的通行证执行的样子:

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        CommandBuffer cmd = CommandBufferPool.Get("EnemyClearDepthPass"); // Take this outside if you don't need this to change at all.

        foreach (var renderer in _associatedObjects)
        {
            if (renderer.isVisible)
            {
                cmd.DrawRenderer(renderer, renderer.sharedMaterial);
            }
        }

        context.ExecuteCommandBuffer(cmd);
        CommandBufferPool.Release(cmd);
    }

看起来它的旋转(因此,它的 MVP 矩阵)是正确的……但是它没有光照。而且我不知道第二个黑暗立方体是什么。所以:

Option #1) 找出我需要传递给命令缓冲区的命令以应用照明......并修复黑暗方块的任何内容......并希望当我开始绘制更复杂的时候就是这样着色器。

但后来我有一个想法:

Option #2) 也许我可以使用 ScriptableRenderContext.DrawRenderers(...) 来绘制它,因为我认为它会更“开箱即用”。

无论我尝试用这种方法做什么,这都是我得到的:

(我知道我可以什么都不说……但是……)

为了开始这种方法,我只是想做一个吸引一切的传球。但我可以让它画任何东西。为什么?

using System;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class JustDrawSomethingPls : ScriptableRendererFeature
{
    // https://docs.unity3d.com/Packages/[email protected]/manual/containers/create-custom-renderer-feature-1.html
    class DrawEverythingPass : ScriptableRenderPass
    {
        public DrawEverythingPass() { }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            CommandBuffer cmd = CommandBufferPool.Get("DrawEverythingPass");
            // https://docs.unity3d.com/ScriptReference/Rendering.DrawingSettings.html
            DrawingSettings drawingSettings = new DrawingSettings();
            drawingSettings.mainLightIndex = 0;
                SortingSettings sortingSettings = new SortingSettings();
                sortingSettings.criteria = SortingCriteria.None;
            drawingSettings.sortingSettings = sortingSettings;
            // https://docs.unity3d.com/ScriptReference/Rendering.FilteringSettings.html
            FilteringSettings filteringSettings = new FilteringSettings();
            filteringSettings.excludeMotionVectorObjects = false;
            filteringSettings.layerMask = int.MaxValue;
            filteringSettings.renderingLayerMask = uint.MaxValue;
            filteringSettings.renderQueueRange = new RenderQueueRange(0, 5000); // The upper bound must be at least 0 and at most 5000
            filteringSettings.sortingLayerRange = new SortingLayerRange(0, Int16.MaxValue);
            // https://docs.unity3d.com/ScriptReference/Rendering.RenderStateBlock.html
            RenderStateBlock renderStateBlock = new RenderStateBlock(RenderStateMask.Everything);

            context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings, ref renderStateBlock);

            // Execute the command buffer
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
    }

    private DrawEverythingPass _drawEverythingPass;
    public override void Create()
    {
        _drawEverythingPass = new DrawEverythingPass();
    }
    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(_drawEverythingPass);
    }
}
unity3d opengl graphics 3d
© www.soinside.com 2019 - 2024. All rights reserved.