Difference between revisions of "GPU Instancer:Features"
GurBu Admin (talk | contribs) (→Automatically Add-Remove prefab instances without any aditional code) |
GurBu Admin (talk | contribs) (→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;"> </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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> </div> | ||
+ | <div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <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;"> <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;"> </div> | ||
+ | <div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <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;"> <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;"> <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: #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;"> _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;"> <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;"> <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;"><</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;"> _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;"> <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;"> 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;"> <span style="color: #008000;">}</span></div> | ||
+ | <div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> </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;">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;"> <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;">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;"> <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: #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;"> <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;"><</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;"> _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;"> <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;"> 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;"> <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;"><span style="color: #008000;">}</span></div> | ||
+ | <div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> </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.
Contents
[hide]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.
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.
3. Another option is to register the prefab instances and reinitialize the Prefab Manager with first calling RegisterPrefabInstanceList method and then calling InitializeGPUInstancer method.
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.