Unity 6.3
0 онлайн 34 гостей 3 в системе
Вход

Graphics.RenderPrimitivesIndexed

Declaration

public static void RenderPrimitivesIndexed(ref RenderParams rparams, MeshTopology topology, GraphicsBuffer indexBuffer, int indexCount, int startIndex = 0, int instanceCount = 1);

Параметры

Параметр Описание
рпарам Параметры, используемые Unity для отображения примитивов.
топология Примитивная топология (например, треугольники или линии).
indexBuffer Индексный буфер для отображаемых примитивов.
indexCount Число индексов на экземпляр.
startIndex Первый индекс в indexBuffer.
instanceCount Число экземпляров для отображения.

Описание

Отображает индексированные примитивы с GPU экземпляром и пользовательским шейдером.

Отображает заданное количество экземпляров и примитивов, имеющих определенную топологию. Этот метод требует использования пользовательских шейдеров для получения или вычисления данных вершин с помощью SV_VertexID семантический, который устанавливается со значениями в indexBuffer. Для доступа к экземпляру ID использование SV_InstanceID семантические.

Дополнительные ресурсы: RenderPrimitives.

Следующий пример отображает 10 экземпляров Mesh с использованием RenderPrimitivesIndexed. Соответствующий Материал должен использовать следующий пользовательский шейдер:

using UnityEngine;

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

GraphicsBuffer meshTriangles; GraphicsBuffer meshPositions;

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); }

void OnDestroy() { meshTriangles?.Dispose(); meshTriangles = null; meshPositions?.Dispose(); meshPositions = 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("_Positions", meshPositions); rp.matProps.SetInt("_BaseVertexIndex", (int)mesh.GetBaseVertex(0)); rp.matProps.SetMatrix("_ObjectToWorld", Matrix4x4.Translate(new Vector3(-4.5f, 0, 0))); rp.matProps.SetFloat("_NumInstances", 10.0f); Graphics.RenderPrimitivesIndexed(rp, MeshTopology.Triangles, meshTriangles, meshTriangles.count, (int)mesh.GetIndexStart(0), 10); } }

Используйте следующий пример шейдера с приведенным выше примером кода C#:

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

#include "UnityCG.cginc"

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

StructuredBuffer<float3> _Positions; uniform uint _BaseVertexIndex; uniform float4x4 _ObjectToWorld; uniform float _NumInstances;

v2f vert(uint vertexID: SV_VertexID, uint instanceID : SV_InstanceID) { v2f o; float3 pos = _Positions[vertexID + _BaseVertexIndex]; float4 wpos = mul(_ObjectToWorld, float4(pos + float3(instanceID, 0, 0), 1.0f)); o.pos = mul(UNITY_MATRIX_VP, wpos); o.color = float4(instanceID / _NumInstances, 0.0f, 0.0f, 0.0f); return o; }

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