// Copyright (c) Wojciech Figat. All rights reserved. using FlaxEngine; namespace FlaxEditor.Tools.Terrain.Paint { /// /// Paint tool mode. Edits terrain splatmap by painting with the single layer on top of the others. /// /// [HideInEditor] public sealed class SingleLayerMode : Mode { /// /// The paint layers. /// public enum Layers { /// /// The layer 0. /// Layer0, /// /// The layer 0. /// Layer1, /// /// The layer 2. /// Layer2, /// /// The layer 3. /// Layer3, /// /// The layer 4. /// Layer4, /// /// The layer 5. /// Layer5, /// /// The layer 6. /// Layer6, /// /// The layer 7. /// Layer7, } /// /// The layer to paint with it. /// [EditorOrder(10), Tooltip("The layer to paint on. Terrain material can access a per-layer blend weight to perform material or texture blending."), CustomEditorAlias("FlaxEditor.CustomEditors.Editors.TerrainLayerEditor")] public Layers Layer = Layers.Layer0; /// public override int ActiveSplatmapIndex => (int)Layer < 4 ? 0 : 1; /// public override unsafe void Apply(ref ApplyParams p) { var strength = p.Strength; var layer = (int)Layer; var brushPosition = p.Gizmo.CursorPosition; var c = layer % 4; // Apply brush modification Profiler.BeginEvent("Apply Brush"); bool otherModified = false; for (int z = 0; z < p.ModifiedSize.Y; z++) { var zz = z + p.ModifiedOffset.Y; for (int x = 0; x < p.ModifiedSize.X; x++) { var xx = x + p.ModifiedOffset.X; var src = (Color)p.SourceData[zz * p.HeightmapSize + xx]; var samplePositionLocal = p.PatchPositionLocal + new Vector3(xx * FlaxEngine.Terrain.UnitsPerVertex, 0, zz * FlaxEngine.Terrain.UnitsPerVertex); Vector3.Transform(ref samplePositionLocal, ref p.TerrainWorld, out Vector3 samplePositionWorld); var sample = Mathf.Saturate(p.Brush.Sample(ref brushPosition, ref samplePositionWorld)); var paintAmount = sample * strength; if (paintAmount < 0.0f) continue; // Skip when pixel won't be affected // 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; //if (other.ValuesSum > 0.0f) // Skip editing the other splatmap if it's empty { // Remove 'paint' from the other splatmap texture srcOther = Color.Clamp(srcOther - srcOther * decreaseAmount, Color.Zero, Color.White); p.TempBufferOther[z * p.ModifiedSize.X + x] = srcOther; otherModified = true; } } } Profiler.EndEvent(); // Update terrain patch TerrainTools.ModifySplatMap(p.Terrain, ref p.PatchCoord, p.SplatmapIndex, p.TempBuffer, ref p.ModifiedOffset, ref p.ModifiedSize); if (otherModified) TerrainTools.ModifySplatMap(p.Terrain, ref p.PatchCoord, p.SplatmapIndexOther, p.TempBufferOther, ref p.ModifiedOffset, ref p.ModifiedSize); } } }