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
Next revision
Previous revision
static_batching [2021/05/02 17:31]
73.95.178.156 old revision restored (2021/05/02 17:09)
static_batching [2021/12/13 19:18] (current)
uninomiconadmin
Line 23: Line 23:
 The lightmap coordinates from [[lightingdataasset|Lighting Data Asset]] are baked into the Combined Mesh UV coordinates when the scene loads((I think this also happens **during** standalone build, not at runtime, but I'm not sure)). Afterwards, querying ''meshRenderer.lightmapScaleOffset'' **will** return the old scale and transform, even though it has been baked into the mesh. Technically, in the render pipeline, all of these meshes have a lightmapST (scale/transform) of '(1,1,0,0)'. ((It shows the scale and transform it loaded from the LightingDataAsset file for that renderer.)) The lightmap coordinates from [[lightingdataasset|Lighting Data Asset]] are baked into the Combined Mesh UV coordinates when the scene loads((I think this also happens **during** standalone build, not at runtime, but I'm not sure)). Afterwards, querying ''meshRenderer.lightmapScaleOffset'' **will** return the old scale and transform, even though it has been baked into the mesh. Technically, in the render pipeline, all of these meshes have a lightmapST (scale/transform) of '(1,1,0,0)'. ((It shows the scale and transform it loaded from the LightingDataAsset file for that renderer.))
  
 +Static batching can actually work with manually provided lightmap scale/offsets, not coming from Lighting Data Asset. As renderer.lightmapScaleOffset and renderer.lightmapIndex are not serialized, make sure to always set this value every Awake() (and possibly Start()). This approach is used by the Bakery asset.
 +
 +===== Batching and MaterialPropertyBlocks =====
 +
 +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: 
 +
 +{{ ::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).
 +
 +An example, when using SRP batching:
 +
 +<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
 +
 +// 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.))
static_batching.1619976692.txt.gz ยท Last modified: 2021/05/02 17:31 by 73.95.178.156