Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


static_batching

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
static_batching [2021/12/12 23:11]
starasgames Added a bit of informaton about how static batching works in Editor
static_batching [2021/12/13 19:18]
uninomiconadmin old revision restored (2021/12/11 21:47)
Line 18: Line 18:
  
 Static batching is done at build-time, so mobile developers should be aware that static batching can unexpectedly increase your build size. Static Batching can result in significantly more mesh data being generated, if many batch meshes are created. Static batching is done at build-time, so mobile developers should be aware that static batching can unexpectedly increase your build size. Static Batching can result in significantly more mesh data being generated, if many batch meshes are created.
- 
-In the Editor static batching occurs after the scene is loaded (after Awake and OnEnable). It is considered a [[https://issuetracker.unity3d.com/issues/static-batching-is-inconsistent-with-the-editor-when-used-in-player-build|bug]]. 
  
 ==== Lightmap UV Coordinates ==== ==== Lightmap UV Coordinates ====
Line 29: Line 27:
 ===== Batching and MaterialPropertyBlocks ===== ===== Batching and MaterialPropertyBlocks =====
  
-Static and SRP batching is incompatible with MaterialPropertyBlocks, meaning you can't set per-object (not per-material) parameters without losing it. However, there is a hacky workaround, at least tested with SRP batching.+Static and SRP batching is incompatible with MaterialPropertyBlocks, meaning you can't set per-renderer (not per-material) parameters without breaking batching. However, there is a hacky workaround, at least tested with SRP batching.
  
-This is the data sent by the SRP batcher for each draw call: https://blog-api.unity.com/sites/default/files/2019/02/Screen-Shot-2019-02-27-at-3.50.52-PM.png+This is the data sent by the SRP batcher for each draw call:  
 + 
 +{{ ::screen-shot-2019-02-27-at-3.50.52-pm.png?400 | https://blog-api.unity.com/sites/default/files/2019/02/Screen-Shot-2019-02-27-at-3.50.52-PM.png}}
  
 This data is efficiently managed by the engine and uploaded to the GPU as fast as Unity can. The idea is to abuse some of these built-in values to pass custom data. Good candidates are ''unity_DynamicLightmapST'' (Enlighten data, still uploaded even if non-Enlighten lightmaps are used) and ''unity_RenderingLayer'' (its value is directly alterable in MeshRenderer UI). This data is efficiently managed by the engine and uploaded to the GPU as fast as Unity can. The idea is to abuse some of these built-in values to pass custom data. Good candidates are ''unity_DynamicLightmapST'' (Enlighten data, still uploaded even if non-Enlighten lightmaps are used) and ''unity_RenderingLayer'' (its value is directly alterable in MeshRenderer UI).
  
-When using SRP batching, the recipe is following: +An example, when using SRP batching:
- +
-''renderer.realtimeLightmapIndex = 0;'' forces "real-time lightmapped"" behaviour on the renderer (also not really required on objects using a regular static lightmap) +
- +
-''renderer.realtimeLightmapScaleOffset = Vector4 of custom data'' +
  
-For custom SRPs: ''drawingSettings.perObjectData PerObjectData.Lightmaps | otherStuffYouNeed;''+<code csharp> 
 +renderer.realtimeLightmapIndex 0; // forces "real-time lightmapped"" behaviour on the renderer (also not really required on objects using a regular static lightmap) 
 +renderer.realtimeLightmapScaleOffset = ...; // Vector4 of custom data
  
-On the GPU you should have a ''UnityPerDraw'' cbuffer with ''float4 unity_DynamicLightmapST'' in it, and it will be filled with custom data. Note that this cbuffer is already present in standard shaders of existing RPs.+// For custom SRPs: 
 +// (Only relevant for custom SRPs; in existing RPs it should likely just work as is, as they all support lightmapping in the main pass) 
 +drawingSettings.perObjectData = ...; // PerObjectData.Lightmaps or otherStuffYouNeed; 
 +</code>
  
 +On the GPU you should have a ''UnityPerDraw'' cbuffer with ''float4 unity_DynamicLightmapST'' in it, and it will be filled with custom data. ((This CBuffer is already present in standard shaders of existing RPs.))
  
-Note that the "drawingSettings" part is only relevant for custom SRPs; in existing RPs it should likely just work as is, as they all support lightmapping in the main pass anyway.+Note that the "drawingSettings" part is 
static_batching.txt · Last modified: 2021/12/13 19:18 by uninomiconadmin