Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://docs.unity3d.com/560/Documentation/ScriptReference/Graphics.DrawMeshInstancedIndirect.html
  2.  
  3. using UnityEngine;
  4.  
  5. public class InstancedIndirectExample1 : MonoBehaviour
  6. {
  7.     public int instanceCount = 100000;
  8.     public Mesh instanceMesh;
  9.     public Material instanceMaterial;
  10.  
  11.     private int cachedInstanceCount = -1;
  12.     private ComputeBuffer positionBuffer;
  13.     private ComputeBuffer argsBuffer;
  14.     private uint[] args = new uint[5] { 0, 0, 0, 0, 0 };
  15.  
  16.     void Start()
  17.     {
  18.  
  19.         argsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
  20.         UpdateBuffers();
  21.     }
  22.  
  23.     void Update()
  24.     {
  25.  
  26.         // Update starting position buffer
  27.         if (cachedInstanceCount != instanceCount)
  28.             UpdateBuffers();
  29.  
  30.         // Pad input
  31.         //if (Input.GetAxisRaw("Horizontal") != 0.0f)
  32.            // instanceCount = (int)Mathf.Clamp(instanceCount + Input.GetAxis("Horizontal") * 40000, 1.0f, 5000000.0f);
  33.  
  34.         // Render
  35.         Graphics.DrawMeshInstancedIndirect(instanceMesh, 0, instanceMaterial, new Bounds(Vector3.zero, new Vector3(100.0f, 100.0f, 100.0f)), argsBuffer);
  36.     }
  37.  
  38.     void OnGUI()
  39.     {
  40.  
  41.         GUI.Label(new Rect(265, 25, 200, 30), "Instance Count: " + instanceCount.ToString());
  42.         instanceCount = (int)GUI.HorizontalSlider(new Rect(25, 20, 200, 30), (float)instanceCount, 1.0f, 5000000.0f);
  43.     }
  44.  
  45.     void UpdateBuffers()
  46.     {
  47.  
  48.         // positions
  49.         if (positionBuffer != null)
  50.             positionBuffer.Release();
  51.         positionBuffer = new ComputeBuffer(instanceCount, 16);
  52.         Vector4[] positions = new Vector4[instanceCount];
  53.         for (int i = 0; i < instanceCount; i++)
  54.         {
  55.             float angle = Random.Range(0.0f, Mathf.PI * 2.0f);
  56.             float distance = Random.Range(20.0f, 100.0f);
  57.             float height = Random.Range(-2.0f, 2.0f);
  58.             float size = Random.Range(0.05f, 0.25f);
  59.             positions[i] = new Vector4(Mathf.Sin(angle) * distance, height, Mathf.Cos(angle) * distance, size);
  60.         }
  61.         positionBuffer.SetData(positions);
  62.         instanceMaterial.SetBuffer("positionBuffer", positionBuffer);
  63.  
  64.         // indirect args
  65.         uint numIndices = (instanceMesh != null) ? (uint)instanceMesh.GetIndexCount(0) : 0;
  66.         args[0] = numIndices;
  67.         args[1] = (uint)instanceCount;
  68.         argsBuffer.SetData(args);
  69.  
  70.         cachedInstanceCount = instanceCount;
  71.     }
  72.  
  73.     void OnDisable()
  74.     {
  75.  
  76.         if (positionBuffer != null)
  77.             positionBuffer.Release();
  78.         positionBuffer = null;
  79.  
  80.         if (argsBuffer != null)
  81.             argsBuffer.Release();
  82.         argsBuffer = null;
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement