From 0ee5ef8cf5a002a6f184218941eb267df2f3edfe Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 26 Feb 2024 16:44:55 +0100 Subject: [PATCH] Improve terrain paint blending of the existing layer values #2258 --- .../Tools/Terrain/Paint/SingleLayerMode.cs | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Source/Editor/Tools/Terrain/Paint/SingleLayerMode.cs b/Source/Editor/Tools/Terrain/Paint/SingleLayerMode.cs index 5921f7d10..df6b17c44 100644 --- a/Source/Editor/Tools/Terrain/Paint/SingleLayerMode.cs +++ b/Source/Editor/Tools/Terrain/Paint/SingleLayerMode.cs @@ -93,22 +93,21 @@ namespace FlaxEditor.Tools.Terrain.Paint if (paintAmount < 0.0f) continue; // Skip when pixel won't be affected - // Paint on the active splatmap texture - src[c] = Mathf.Saturate(src[c] + paintAmount); - src[(c + 1) % 4] = Mathf.Saturate(src[(c + 1) % 4] - paintAmount); - src[(c + 2) % 4] = Mathf.Saturate(src[(c + 2) % 4] - paintAmount); - src[(c + 3) % 4] = Mathf.Saturate(src[(c + 3) % 4] - paintAmount); - p.TempBuffer[z * p.ModifiedSize.X + x] = src; + // Other layers reduction based on their sum and current paint intensity + var srcOther = (Color)p.SourceDataOther[zz * p.HeightmapSize + xx]; + var otherLayersSum = src.ValuesSum + srcOther.ValuesSum - src[c]; + var decreaseAmount = paintAmount / otherLayersSum; + + // Paint on the active splatmap texture + var srcNew = Color.Clamp(src - src * decreaseAmount, Color.Zero, Color.White); + srcNew[c] = Mathf.Saturate(src[c] + paintAmount); + p.TempBuffer[z * p.ModifiedSize.X + x] = srcNew; - var other = (Color)p.SourceDataOther[zz * p.HeightmapSize + xx]; //if (other.ValuesSum > 0.0f) // Skip editing the other splatmap if it's empty { // Remove 'paint' from the other splatmap texture - other[c] = Mathf.Saturate(other[c] - paintAmount); - other[(c + 1) % 4] = Mathf.Saturate(other[(c + 1) % 4] - paintAmount); - other[(c + 2) % 4] = Mathf.Saturate(other[(c + 2) % 4] - paintAmount); - other[(c + 3) % 4] = Mathf.Saturate(other[(c + 3) % 4] - paintAmount); - p.TempBufferOther[z * p.ModifiedSize.X + x] = other; + srcOther = Color.Clamp(srcOther - srcOther * decreaseAmount, Color.Zero, Color.White); + p.TempBufferOther[z * p.ModifiedSize.X + x] = srcOther; otherModified = true; } }