diff --git a/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/AbstractMeshBuilder.cs b/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/AbstractMeshBuilder.cs
index d5ebc33d3..726e1d1f2 100644
--- a/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/AbstractMeshBuilder.cs
+++ b/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/AbstractMeshBuilder.cs
@@ -856,22 +856,22 @@ private Material GetDefaultMaterial(TextureCacheService.TextureArrayTypes textur
///
/// The SingleMesh shader only samples _MainTex. A 1x1 texture with the material's color gives the same look.
- /// Cached per color, as every VOB using it shares the very same material.
+ /// The texture is cached per color; the material is per renderer, as the light services write each renderer's
+ /// lights and world-light tint into it (a shared one lit every X mark like the last one). Same shader variant,
+ /// so they still batch.
///
private Material GetUntexturedMaterial(System.Drawing.Color color)
{
var unityColor = new Color32(color.R, color.G, color.B, color.A);
- if (_multiTypeCacheService.UntexturedMaterials.TryGetValue(unityColor, out var material))
- return material;
-
- var texture = new Texture2D(1, 1);
- texture.SetPixel(0, 0, unityColor);
- texture.Apply();
-
- material = new Material(Constants.ShaderSingleMeshLit) { mainTexture = texture };
- _multiTypeCacheService.UntexturedMaterials[unityColor] = material;
+ if (!_multiTypeCacheService.UntexturedTextures.TryGetValue(unityColor, out var texture))
+ {
+ texture = new Texture2D(1, 1);
+ texture.SetPixel(0, 0, unityColor);
+ texture.Apply();
+ _multiTypeCacheService.UntexturedTextures[unityColor] = texture;
+ }
- return material;
+ return new Material(Constants.ShaderSingleMeshLit) { mainTexture = texture };
}
protected Material GetWaterMaterial()
diff --git a/Assets/Gothic-Core/Scripts/Services/Caches/MultiTypeCacheService.cs b/Assets/Gothic-Core/Scripts/Services/Caches/MultiTypeCacheService.cs
index 33439e93a..7ce2685a7 100644
--- a/Assets/Gothic-Core/Scripts/Services/Caches/MultiTypeCacheService.cs
+++ b/Assets/Gothic-Core/Scripts/Services/Caches/MultiTypeCacheService.cs
@@ -39,10 +39,10 @@ public class MultiTypeCacheService
public Dictionary AudioClips = new();
///
- /// Materials of texture-less, color-only meshes (e.g. G2's treasure X marks) - shared by all VOBs with the same
- /// color.
+ /// 1x1 textures of texture-less, color-only meshes (e.g. G2's treasure X marks) - shared by all VOBs with the
+ /// same color. Each renderer gets its own material with it: per-renderer light values are written there.
///
- public Dictionary UntexturedMaterials = new();
+ public Dictionary UntexturedTextures = new();
///
/// Colliders of items without pre-cached ones, calculated from their mesh once per mesh name
@@ -65,7 +65,7 @@ public void Dispose()
FontCache.Clear();
Meshes.Clear();
AudioClips.Clear();
- UntexturedMaterials.Clear();
+ UntexturedTextures.Clear();
ItemColliders.Clear();
NpcCache.Clear();
VobCache.Clear();
diff --git a/Assets/Gothic-Core/Scripts/Services/Meshes/DynamicMaterialService.cs b/Assets/Gothic-Core/Scripts/Services/Meshes/DynamicMaterialService.cs
index 91fbaa5d3..6757e737b 100644
--- a/Assets/Gothic-Core/Scripts/Services/Meshes/DynamicMaterialService.cs
+++ b/Assets/Gothic-Core/Scripts/Services/Meshes/DynamicMaterialService.cs
@@ -40,7 +40,6 @@ private class CacheEntry
{ Constants.ShaderLitAlphaToCoverageName, new (Constants.ShaderLitAlphaToCoverageDynamic, (int)RenderQueue.AlphaTest) }
};
- private static readonly int _fatnessShaderId = Shader.PropertyToID("_Fatness");
// Some objects (like NPCs) have multiple meshes. We therefore add all self+children renderers/materials.
private Dictionary _cache = new();
@@ -109,7 +108,8 @@ private static bool IsOutdated(GameObject go, CacheEntry entry)
}
///
- /// Only our own dynamic material copies are changed - the default materials are shared with other objects.
+ /// Only our own dynamic material copies are changed - the default materials stay as they are (some are
+ /// shared with other objects, e.g. Constants.LoadingMaterial; VOB world materials are per renderer).
///
private static void SetValue(CacheEntry entry, int shaderProperty, float shaderValue)
{
@@ -147,14 +147,13 @@ private void CacheGameObject(GameObject go)
continue;
}
- var newMaterial = new Material(shaderMapEntry.dynamicShader)
+ // A copy of the original keeps every per-renderer value (texture, fatness, the world-light tint
+ // _VobLightTint, the stationary light indices): a chest in the Old Mine stays dark while focused.
+ var newMaterial = new Material(mat)
{
- mainTexture = mat.mainTexture,
+ shader = shaderMapEntry.dynamicShader,
renderQueue = shaderMapEntry.shaderType
};
- // A fat NPC stays fat while it is highlighted or see-through.
- if (mat.HasProperty(_fatnessShaderId) && newMaterial.HasProperty(_fatnessShaderId))
- newMaterial.SetFloat(_fatnessShaderId, mat.GetFloat(_fatnessShaderId));
dynamicSlots[i] = newMaterial;
}
diff --git a/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs b/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs
index 256e5441c..3b8c3bb29 100644
--- a/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs
+++ b/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs
@@ -161,7 +161,7 @@ private void UpdateRenderer(MeshRenderer renderer)
var nonAllocMaterials = _rendererMaterials;
var indicesMatrix = Matrix4x4.identity;
- renderer.GetSharedMaterials(nonAllocMaterials);
+ GetOwnedMaterials(renderer, nonAllocMaterials);
for (var i = 0; i < Mathf.Min(16, rendererLights.Count); i++)
{
indicesMatrix[i / 4, i % 4] = rendererLights[i].Index;
@@ -307,7 +307,6 @@ public void ClearWorldChunks()
public bool IsWorldLit(Renderer renderer) => _worldLitRenderers.Contains(renderer);
// Lightmap light per cell (ToCell) of the light mapped world polygons, sampled at their centers by WorldMeshBuilder.
private readonly Dictionary _lightmapCells = new();
- private MaterialPropertyBlock _vobLightBlock;
///
/// The lightmap light of the world's light mapped polygons per cell (rgb summed, w = polygon count).
@@ -387,19 +386,38 @@ public void ApplyWorldLight(GameObject vobGo)
if (!light.HasValue)
return;
- _vobLightBlock ??= new MaterialPropertyBlock();
var tint = new Vector4(light.Value.r, light.Value.g, light.Value.b, 1f);
foreach (var renderer in renderers)
{
if (renderer is MeshRenderer meshRenderer && IsWorldChunk(meshRenderer))
continue;
- renderer.GetPropertyBlock(_vobLightBlock);
- _vobLightBlock.SetVector(_vobLightTintShaderId, tint);
- renderer.SetPropertyBlock(_vobLightBlock);
+ // Into the renderer's own materials (UpdateRenderer writes its light indices there too), not a
+ // MaterialPropertyBlock: the SRP batcher skips renderers with one, ~1900 VOBs in Khorinis were drawn
+ // one by one.
+ GetOwnedMaterials(renderer, _rendererMaterials);
+ foreach (var material in _rendererMaterials)
+ if (material.HasProperty(_vobLightTintShaderId))
+ material.SetVector(_vobLightTintShaderId, tint);
_worldLitRenderers.Add(renderer);
}
}
+ ///
+ /// The renderer's materials this service writes per-renderer values into (light indices, world-light tint).
+ /// Every VOB and chunk renderer has its own (mesh builders); the loading material is the one shared exception,
+ /// left in place when a mesh failed to build - writing there would light every renderer still using it.
+ ///
+ private static void GetOwnedMaterials(Renderer renderer, List materials)
+ {
+ renderer.GetSharedMaterials(materials);
+ // Backwards and without a lambda: called for hundreds of renderers at once (GC spikes on Quest).
+ for (var i = materials.Count - 1; i >= 0; i--)
+ {
+ if (materials[i] == null || materials[i] == Constants.LoadingMaterial)
+ materials.RemoveAt(i);
+ }
+ }
+
// Diagnostics: how many VOBs got the world light, with a few examples (VOBs in the Old Mine stayed bright).
private int _vobLightApplied;
private int _vobLightMissed;
diff --git a/Assets/Gothic-Core/Shaders/Lit-World.shader b/Assets/Gothic-Core/Shaders/Lit-World.shader
index 1a0f2d4f2..5cdd9b9ec 100644
--- a/Assets/Gothic-Core/Shaders/Lit-World.shader
+++ b/Assets/Gothic-Core/Shaders/Lit-World.shader
@@ -31,8 +31,6 @@ Shader "Lit/World"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
- float _FocusBrightness;
-
struct appdata
{
float4 vertex : POSITION;
@@ -68,6 +66,8 @@ Shader "Lit/World"
SAMPLER(sampler_WorldLightmaps);
float _WorldLightmapBrightness;
+ // Every material property in here, the same in every pass - else the SRP batcher skips the world and
+ // draws it chunk by chunk.
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_TexelSize;
int _StationaryLightCount;
@@ -75,6 +75,7 @@ Shader "Lit/World"
float4x4 _StationaryLightIndices2;
float _WindStrength;
float4 _VobLightTint;
+ float _FocusBrightness;
CBUFFER_END
#include "GothicIncludes.hlsl"
diff --git a/Docs/architecture.md b/Docs/architecture.md
index a63b9ccae..a2e44992d 100644
--- a/Docs/architecture.md
+++ b/Docs/architecture.md
@@ -376,8 +376,10 @@ player's view, which in VR causes nausea.
### P4. Rendering has a budget
**Rule:** A feature that adds draw calls, materials, realtime lights, shadows, transparency, post-processing or
-shader variants says what it costs on Quest in its PR, and has a quality setting if it's optional. Meshes share
-materials and texture arrays so they batch.
+shader variants says what it costs on Quest in its PR, and has a quality setting if it's optional. World and VOB
+shaders stay SRP batcher compatible: every `Properties` entry sits in `UnityPerMaterial`, the same in every pass, and
+world/VOB renderers get no `MaterialPropertyBlock`. A per-renderer value goes into the renderer's own material (the
+mesh builders give each one its own); meshes share texture arrays.
**Why:** On a mobile GPU the frame is usually lost in rendering, not in C#. Each eye renders the scene, so every
draw call and every overdrawn pixel counts.
diff --git a/Docs/world-lighting.md b/Docs/world-lighting.md
index 4e720ef16..ddf768d5a 100644
--- a/Docs/world-lighting.md
+++ b/Docs/world-lighting.md
@@ -32,6 +32,7 @@ Core code is in `Assets/Gothic-Core/Scripts/`.
|------|----------|
| A static VOB in a light mapped area takes the light of the floor below it (nearest lightmap cell, searched ring by ring) | `StationaryLightsService.ApplyWorldLight`, called by `VobInitializerDomain` |
| A VOB lit this way ignores the baked static lights, else it gets the light twice | `Adapters/Vob/StationaryLight.cs` |
+| The tint is `_VobLightTint` in the VOB's own materials, never a MaterialPropertyBlock: VOBs stay SRP batched; the focus highlight copies the material, so the tint stays | `StationaryLightsService.ApplyWorldLight`, `DynamicMaterialService.CacheGameObject` |
## Fireplaces and torches