Advertisement
Zgragselus

Indirect Rendering Unity

Apr 19th, 2024
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | Software | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. namespace IndirectDraw
  5. {
  6.     [ExecuteInEditMode]
  7.     public class IndirectRenderingTest : MonoBehaviour
  8.     {
  9.         private bool initialized;
  10.  
  11.         GraphicsBuffer meshTriangles;
  12.         GraphicsBuffer meshPositions;
  13.         GraphicsBuffer commandBuf;
  14.         GraphicsBuffer.IndirectDrawArgs[] commandData;
  15.         const int commandCount = 2;
  16.  
  17.         public void OnEnable()
  18.         {
  19.             initialized = true;
  20.  
  21.             int[] triangles = new int[] { 0, 2, 1 };
  22.             float[] vertices = new float[] { -1, -1, 0, 1, -1, 0, 0, 1, 0 };
  23.  
  24.             // note: remember to check "Read/Write" on the mesh asset to get access to the geometry data
  25.             meshTriangles = new GraphicsBuffer(GraphicsBuffer.Target.Structured, triangles.Length, sizeof(int));
  26.             meshTriangles.SetData(triangles);
  27.             meshPositions = new GraphicsBuffer(GraphicsBuffer.Target.Structured, vertices.Length / 3, 3 * sizeof(float));
  28.             meshPositions.SetData(vertices);
  29.             commandBuf = new GraphicsBuffer(GraphicsBuffer.Target.IndirectArguments, commandCount, GraphicsBuffer.IndirectDrawArgs.size);
  30.             commandData = new GraphicsBuffer.IndirectDrawArgs[commandCount];
  31.  
  32.         }
  33.  
  34.         public void OnDisable()
  35.         {
  36.             if (initialized)
  37.             {
  38.                 meshTriangles?.Dispose();
  39.                 meshTriangles = null;
  40.                 meshPositions?.Dispose();
  41.                 meshPositions = null;
  42.                 commandBuf?.Dispose();
  43.                 commandBuf = null;
  44.             }
  45.         }
  46.  
  47.         private void Update()
  48.         {
  49.             RenderParams rp = new RenderParams(material);
  50.             rp.worldBounds = new Bounds(Vector3.zero, 10000 * Vector3.one); // use tighter bounds
  51.             rp.matProps = new MaterialPropertyBlock();
  52.             rp.matProps.SetBuffer("_Triangles", meshTriangles);
  53.             rp.matProps.SetBuffer("_Positions", meshPositions);
  54.             rp.matProps.SetInt("_BaseVertexIndex", 0);
  55.             rp.matProps.SetMatrix("_ObjectToWorld", Matrix4x4.Translate(new Vector3(-4.5f, 0, 0)));
  56.             commandData[0].vertexCountPerInstance = 3;
  57.             commandData[0].instanceCount = 10;
  58.             commandData[1].vertexCountPerInstance = 3;
  59.             commandData[1].instanceCount = 10;
  60.             commandBuf.SetData(commandData);
  61.             Graphics.RenderPrimitivesIndirect(rp, MeshTopology.Triangles, commandBuf, commandCount);
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement