Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- namespace IndirectDraw
- {
- [ExecuteInEditMode]
- public class IndirectRenderingTest : MonoBehaviour
- {
- private bool initialized;
- GraphicsBuffer meshTriangles;
- GraphicsBuffer meshPositions;
- GraphicsBuffer commandBuf;
- GraphicsBuffer.IndirectDrawArgs[] commandData;
- const int commandCount = 2;
- public void OnEnable()
- {
- initialized = true;
- int[] triangles = new int[] { 0, 2, 1 };
- float[] vertices = new float[] { -1, -1, 0, 1, -1, 0, 0, 1, 0 };
- // note: remember to check "Read/Write" on the mesh asset to get access to the geometry data
- meshTriangles = new GraphicsBuffer(GraphicsBuffer.Target.Structured, triangles.Length, sizeof(int));
- meshTriangles.SetData(triangles);
- meshPositions = new GraphicsBuffer(GraphicsBuffer.Target.Structured, vertices.Length / 3, 3 * sizeof(float));
- meshPositions.SetData(vertices);
- commandBuf = new GraphicsBuffer(GraphicsBuffer.Target.IndirectArguments, commandCount, GraphicsBuffer.IndirectDrawArgs.size);
- commandData = new GraphicsBuffer.IndirectDrawArgs[commandCount];
- }
- public void OnDisable()
- {
- if (initialized)
- {
- meshTriangles?.Dispose();
- meshTriangles = null;
- meshPositions?.Dispose();
- meshPositions = null;
- commandBuf?.Dispose();
- commandBuf = null;
- }
- }
- private 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", 0);
- rp.matProps.SetMatrix("_ObjectToWorld", Matrix4x4.Translate(new Vector3(-4.5f, 0, 0)));
- commandData[0].vertexCountPerInstance = 3;
- commandData[0].instanceCount = 10;
- commandData[1].vertexCountPerInstance = 3;
- commandData[1].instanceCount = 10;
- commandBuf.SetData(commandData);
- Graphics.RenderPrimitivesIndirect(rp, MeshTopology.Triangles, commandBuf, commandCount);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement