Difference between revisions of "GPU Instancer:Features"

From GurBu Wiki
Jump to: navigation, search
(Automatically Add-Remove prefab instances without any aditional code)
(Prefab Instancing Features)
Line 6: Line 6:
  
 
=== Automatically Add-Remove prefab instances without any aditional code ===
 
=== Automatically Add-Remove prefab instances without any aditional code ===
 
+
----
 
1. Easiest way is to enable Auto. Add/Remove Instances option in the prototype options. When enabled, the Prefab Manager will add a script called "GPUInstancerPrefabRuntimeHandler" to the prefab object and handle all runtime changes automatically.
 
1. Easiest way is to enable Auto. Add/Remove Instances option in the prototype options. When enabled, the Prefab Manager will add a script called "GPUInstancerPrefabRuntimeHandler" to the prefab object and handle all runtime changes automatically.
  
Line 58: Line 58:
 
</div>
 
</div>
  
 +
 +
<div style="clear: both"></div>
 
=== Nested Prefabs support (Unity 2018.3 and later) ===
 
=== Nested Prefabs support (Unity 2018.3 and later) ===
 +
----
 +
 +
 +
<div style="clear: both"></div>
 +
=== Add-Remove-Update prefab instances with or without instantiating GameObjects (examples included). ===
 +
 +
With GPU Instancer, you can render prefab instances without instantiating GameObjects by supplying an array of Matrix4x4s.
 +
 +
<div class="code" style="border: 1px solid #c8ccd1;padding:5px;"><ol class="csharp" style="font-family:monospace;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">UnityEngine</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">GPUInstancer</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NoGameObject <span style="color: #008000;">:</span> MonoBehaviour</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// reference to Prefab Manager</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> GPUInstancerPrefabManager prefabManager<span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// reference to prefab</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> GPUInstancerPrefab prefab<span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// size of array and buffers</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> bufferSize<span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// transform data array</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> Matrix4x4<span style="color: #008000;">[</span><span style="color: #008000;">]</span> _matrix4x4Array<span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Use this for initialization</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">void</span> Awake <span style="color: #008000;">(</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// initialize the array with the max size</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; _matrix4x4Array <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Matrix4x4<span style="color: #008000;">[</span>bufferSize<span style="color: #008000;">]</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// set the data of the array</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">(</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> _matrix4x4Array<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _matrix4x4Array<span style="color: #008000;">[</span>i<span style="color: #008000;">]</span> <span style="color: #008000;">=</span> Matrix4x4<span style="color: #008000;">.</span><span style="color: #0000FF;">TRS</span><span style="color: #008000;">(</span>Random<span style="color: #008000;">.</span><span style="color: #0000FF;">insideUnitSphere</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">15</span>, Quaternion<span style="color: #008000;">.</span><span style="color: #0000FF;">identity</span>, Vector3<span style="color: #008000;">.</span><span style="color: #0000FF;">one</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// initialize the buffers with array</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; GPUInstancerAPI<span style="color: #008000;">.</span><span style="color: #0000FF;">InitializeWithMatrix4x4Array</span><span style="color: #008000;">(</span>prefabManager, prefab<span style="color: #008000;">.</span><span style="color: #0000FF;">prefabPrototype</span>, _matrix4x4Array<span style="color: #008000;">)</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Update<span style="color: #008000;">(</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">(</span>Input<span style="color: #008000;">.</span><span style="color: #0000FF;">anyKeyDown</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// change the data of the array</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">(</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> _matrix4x4Array<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _matrix4x4Array<span style="color: #008000;">[</span>i<span style="color: #008000;">]</span> <span style="color: #008000;">=</span> Matrix4x4<span style="color: #008000;">.</span><span style="color: #0000FF;">TRS</span><span style="color: #008000;">(</span>Random<span style="color: #008000;">.</span><span style="color: #0000FF;">insideUnitSphere</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">15</span>, Quaternion<span style="color: #008000;">.</span><span style="color: #0000FF;">identity</span>, Vector3<span style="color: #008000;">.</span><span style="color: #0000FF;">one</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// update buffers</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPUInstancerAPI<span style="color: #008000;">.</span><span style="color: #0000FF;">UpdateVisibilityBufferWithMatrix4x4Array</span><span style="color: #008000;">(</span>prefabManager, prefab<span style="color: #008000;">.</span><span style="color: #0000FF;">prefabPrototype</span>, _matrix4x4Array<span style="color: #008000;">)</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
</ol></div>
  
 
== Detail Instancing Features ==
 
== Detail Instancing Features ==

Revision as of 15:09, 5 December 2018

About | Features | Getting Started | Terminology | Best Practices | F.A.Q.

General Features

Prefab Instancing Features

Automatically Add-Remove prefab instances without any aditional code


1. Easiest way is to enable Auto. Add/Remove Instances option in the prototype options. When enabled, the Prefab Manager will add a script called "GPUInstancerPrefabRuntimeHandler" to the prefab object and handle all runtime changes automatically.

autoaddremoveinstances.png

2. Optionally you can add prefab instances through our API methods with the AddPrefabInstance and RemovePrefabInstance methods. Keep in mind that Add/Remove Instances At Runtime option should be enabled in the prototype definition for this API methods to work. Also Extra Buffer Size should have high enough number to add all the prefab instances generated at runtime.

addremoveinstances.png

3. Another option is to register the prefab instances and reinitialize the Prefab Manager with first calling RegisterPrefabInstanceList method and then calling InitializeGPUInstancer method.

using System.Collections.Generic;
using UnityEngine;
 
namespace GPUInstancer.Extras
{
    public class GPUIRegisterPrefabInstances : MonoBehaviour
    {
        // Keeping a static reference to the manager so it can be shared between scenes.
        public static GPUInstancerPrefabManager prefabManager;
        // The list of prefab instances in scene
        public List<GPUInstancerPrefab> prefabInstanceList;
 
 
        // In this example we fill the list in the Reset method which works when adding the component for the first time (or using the reset command in the inspector).
        // You can fill the list in any way you want.
        public void Reset()
        {
            // Add prefabs in the scene to the list
            prefabInstanceList = new List<GPUInstancerPrefab>(FindObjectsOfType<GPUInstancerPrefab>());
        }
 
        void Start()
        {
            // Find the prefab manager and set it to the static property
            if(prefabManager == null && GPUInstancerAPI.GetActiveManagers() != null)
            {
                GPUInstancerManager manager = GPUInstancerAPI.GetActiveManagers().Find(m => m is GPUInstancerPrefabManager);
                prefabManager = (GPUInstancerPrefabManager)manager;
            }
 
            // Register the prefab instance list to the manager
            if (prefabManager != null && prefabManager.isActiveAndEnabled)
            {
                GPUInstancerAPI.RegisterPrefabInstanceList(prefabManager, prefabInstanceList);
                GPUInstancerAPI.InitializeGPUInstancer(prefabManager);
            }
        }
    }
}


Nested Prefabs support (Unity 2018.3 and later)



Add-Remove-Update prefab instances with or without instantiating GameObjects (examples included).

With GPU Instancer, you can render prefab instances without instantiating GameObjects by supplying an array of Matrix4x4s.

    using UnityEngine;
    using GPUInstancer;
     
    public class NoGameObject : MonoBehaviour
    {
        // reference to Prefab Manager
        public GPUInstancerPrefabManager prefabManager;
        // reference to prefab
        public GPUInstancerPrefab prefab;
        // size of array and buffers
        public int bufferSize;
     
        // transform data array
        private Matrix4x4[] _matrix4x4Array;
     
        // Use this for initialization
        void Awake ()
        {
            // initialize the array with the max size
            _matrix4x4Array = new Matrix4x4[bufferSize];
            // set the data of the array
            for (int i = 0; i < _matrix4x4Array.Length; i++)
                _matrix4x4Array[i] = Matrix4x4.TRS(Random.insideUnitSphere * 15, Quaternion.identity, Vector3.one);
            // initialize the buffers with array
            GPUInstancerAPI.InitializeWithMatrix4x4Array(prefabManager, prefab.prefabPrototype, _matrix4x4Array);
        }
     
        private void Update()
        {
            if (Input.anyKeyDown)
            {
                // change the data of the array
                for (int i = 0; i < _matrix4x4Array.Length; i++)
                    _matrix4x4Array[i] = Matrix4x4.TRS(Random.insideUnitSphere * 15, Quaternion.identity, Vector3.one);
                // update buffers
                GPUInstancerAPI.UpdateVisibilityBufferWithMatrix4x4Array(prefabManager, prefab.prefabPrototype, _matrix4x4Array);
            }
        }
    }
     

Detail Instancing Features

Tree Instancing Features

Third Party Integrations