版本:Unity 6 (6000.0)
语言英语
  • C#

Graphics.RenderPrimitivesIndirect

提出更改建议

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们会阅读用户提出的每项更改建议,并在适用时更新。

关闭

提交失败

由于某种原因导致您的更改建议无法提交。请在几分钟后<a>重试</a>。感谢您拨冗帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static void RenderPrimitivesIndirect(ref RenderParams rparams, MeshTopology topology, GraphicsBuffer commandBuffer, int commandCount = 1, int startCommand = 0);

参数

rparams Unity 用于渲染图元参数。
topology 图元拓扑(例如,三角形或线)。
commandBuffer 提供渲染命令参数的命令缓冲区(参见 IndirectDrawArgs)。
commandCount commandBuffer 中执行的渲染命令数。
startCommand commandBuffer 中执行的第一条命令。

说明

利用 commandBuffer 中的渲染命令参数,使用 GPU 实例化和自定义着色器渲染图元。

此函数提供一种方法,可从 GPU 控制渲染命令参数,以渲染给定数量的图元和实例。使用 RenderParams.worldBounds,定义边界以剔除和对采用该方法渲染的几何体进行排序,使其成为一个单一实体。

此函数仅适用于支持 计算着色器 的平台。

根据 UnityIndirect.cginc 中的规定,将以下行添加到着色器的 pass 部分,以访问命令、实例和顶点 ID:其他资源:RenderMeshIndirect

#define UNITY_INDIRECT_DRAW_ARGS IndirectDrawArgs
#include "UnityIndirect.cginc"

将以下行添加到着色器函数开头,以设置 ID 访问函数

InitIndirectDrawArgs(0); // pass SV_DrawID semantic value here for multi-draw support

下列示例执行两条间接呈现命令。每条命令呈现 10 个 Mesh 实例。关联的 Material 必须使用以下自定义着色器

using UnityEngine;

public class ExampleClass : MonoBehaviour { public Material material; public Mesh mesh;

GraphicsBuffer meshTriangles; GraphicsBuffer meshPositions; GraphicsBuffer commandBuf; GraphicsBuffer.IndirectDrawArgs[] commandData; const int commandCount = 2;

void Start() { // note: remember to check "Read/Write" on the mesh asset to get access to the geometry data meshTriangles = new GraphicsBuffer(GraphicsBuffer.Target.Structured, mesh.triangles.Length, sizeof(int)); meshTriangles.SetData(mesh.triangles); meshPositions = new GraphicsBuffer(GraphicsBuffer.Target.Structured, mesh.vertices.Length, 3 * sizeof(float)); meshPositions.SetData(mesh.vertices); commandBuf = new GraphicsBuffer(GraphicsBuffer.Target.IndirectArguments, commandCount, GraphicsBuffer.IndirectDrawArgs.size); commandData = new GraphicsBuffer.IndirectDrawArgs[commandCount]; }

void OnDestroy() { meshTriangles?.Dispose(); meshTriangles = null; meshPositions?.Dispose(); meshPositions = null; commandBuf?.Dispose(); commandBuf = null; }

void Update() { RenderParams rp = new RenderParams(material); rp.worldBounds = new Bounds(Vector3.zero, 10000*Vector3.one); // use tighter bounds rp.matProps = new MaterialPropertyBlock(); rp.matProps.SetBuffer("_Triangles", meshTriangles); rp.matProps.SetBuffer("_Positions", meshPositions); rp.matProps.SetInt("_BaseVertexIndex", (int)mesh.GetBaseVertex(0)); rp.matProps.SetMatrix("_ObjectToWorld", Matrix4x4.Translate(new Vector3(-4.5f, 0, 0))); commandData[0].vertexCountPerInstance = mesh.GetIndexCount(0); commandData[0].instanceCount = 10; commandData[1].vertexCountPerInstance = mesh.GetIndexCount(0); commandData[1].instanceCount = 10; commandBuf.SetData(commandData); Graphics.RenderPrimitivesIndirect(rp, MeshTopology.Triangles, commandBuf, commandCount); } }

将以下示例着色器用于上述 C# 示例代码

          Shader "ExampleShader"
{
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

#include "UnityCG.cginc" #define UNITY_INDIRECT_DRAW_ARGS IndirectDrawArgs #include "UnityIndirect.cginc"

struct v2f { float4 pos : SV_POSITION; float4 color : COLOR0; };

StructuredBuffer<int> _Triangles; StructuredBuffer<float3> _Positions; uniform uint _BaseVertexIndex; uniform float4x4 _ObjectToWorld;

v2f vert(uint svVertexID: SV_VertexID, uint svInstanceID : SV_InstanceID) { InitIndirectDrawArgs(0); v2f o; uint cmdID = GetCommandID(0); uint instanceID = GetIndirectInstanceID(svInstanceID); float3 pos = _Positions[_Triangles[GetIndirectVertexID(svVertexID)] + _BaseVertexIndex]; float4 wpos = mul(_ObjectToWorld, float4(pos + float3(instanceID, cmdID, 0.0f), 1.0f)); o.pos = mul(UNITY_MATRIX_VP, wpos); o.color = float4(cmdID & 1 ? 0.0f : 1.0f, cmdID & 1 ? 1.0f : 0.0f, instanceID / float(GetIndirectInstanceCount()), 0.0f); return o; }

float4 frag(v2f i) : SV_Target { return i.color; } ENDCG } } }