Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2481b5

Browse files
authored
Adds alpha blending for puddles based on their size (unitystation#11114)
1 parent 37024e2 commit b2481b5

3 files changed

Lines changed: 48 additions & 9 deletions

File tree

UnityProject/Assets/Scripts/US13/Systems/Chemistry/Reagent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using ScriptableObjects;
43
using UnityEngine;
54

@@ -8,8 +7,6 @@ namespace Chemistry
87
[CreateAssetMenu(fileName = "reagent", menuName = "ScriptableObjects/Chemistry/Reagent")]
98
public class Reagent : SOTracker , IEquatable<Reagent>
109
{
11-
12-
1310
[SerializeField]
1411
[Tooltip("This is optional")]
1512
string displayName;
@@ -25,6 +22,9 @@ public class Reagent : SOTracker , IEquatable<Reagent>
2522
//Every single reaction this chemical is used in
2623
[NonSerialized] public Reaction[] RelatedReactions = Array.Empty<Reaction>();
2724

25+
// The minimum opacity of a puddle, even if the reagent is very diluted. This is to make sure the puddle is always visible.
26+
public const float MINIMUM_PUDDLE_OPACITY = 0.25f;
27+
2828
/// <summary>
2929
/// Index in the chemistry reagents' singleton. Used in a client-server communication
3030
/// (btw we can't serialize reagent - color field isn't serializable and also a serialization

UnityProject/Assets/Scripts/US13/Tilemaps/Behaviours/Layers/MetaDataLayer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,20 +427,22 @@ public void PaintBlood(Vector3 worldPos, ReagentMix reagents)
427427

428428
public Color GetTileColourMix(ReagentMix reagents)
429429
{
430+
//transparent liquids don't need their alpha bumped when their puddles are full.
431+
if (reagents.MajorMixReagent.color.a <= Reagent.MINIMUM_PUDDLE_OPACITY) return reagents.MixColor;
432+
float fillRatio = Mathf.Clamp01(reagents.Total / REAGENT_LIMIT_PER_CELL);
430433
switch (reagents.MixState)
431434
{
432435
case ReagentState.Liquid:
433436
var liquidColor = reagents.MixColor;
434-
liquidColor.a = Mathf.Clamp(liquidColor.a, 0.1f, 0.65f); //makes sure liquids don't completely hide everything behind it.
437+
liquidColor.a = Mathf.Lerp(0.35f, 0.95f, fillRatio); //makes sure liquids don't completely hide everything behind it.
435438
return liquidColor;
436439
case ReagentState.Gas:
437440
case ReagentState.Solid:
438441
var SolidColor = reagents.MixColor;
439-
SolidColor.a = Mathf.Clamp(SolidColor.a, 0.1f, 1f); //makes sure liquids don't completely hide everything behind it.
442+
SolidColor.a = Mathf.Clamp(SolidColor.a, 0.25f, 1f); //makes sure solids aren't invisible
440443
return SolidColor;
441444
}
442445

443-
444446
return Color.white;
445447
}
446448

UnityProject/Assets/Scripts/US13/Tilemaps/Behaviours/Layers/MetaTileMap.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.Linq;
55
using System.Threading;
6+
using Chemistry;
67
using Logs;
78
using SecureStuff;
89
using UnityEngine;
@@ -1067,7 +1068,15 @@ public Vector3Int SetTile(Vector3Int position, LayerTile tile, Matrix4x4? matrix
10671068

10681069
tileLocation.layerTile = tile;
10691070
tileLocation.transformMatrix = matrixTransform.GetValueOrDefault(Matrix4x4.identity);
1070-
tileLocation.Colour = color.GetValueOrDefault(Color.white);
1071+
1072+
// If a colour was provided, apply it. If the colour is translucent and the tile is an overlay/effects
1073+
// type, average its alpha with the average alpha of its immediate 4-neighbour tiles to produce a blended opacity.
1074+
Color assignedColor = color.GetValueOrDefault(Color.white);
1075+
1076+
// Only blend alpha for overlay/effects-type layers where transparency makes visual sense
1077+
assignedColor = MixLocalOverlayColors(position, assignedColor, tileLocation);
1078+
1079+
tileLocation.Colour = assignedColor;
10711080

10721081
if (preExistingTile != null)
10731082
{
@@ -1110,6 +1119,33 @@ public Vector3Int SetTile(Vector3Int position, LayerTile tile, Matrix4x4? matrix
11101119
return position;
11111120
}
11121121

1122+
private Color MixLocalOverlayColors(Vector3Int position, Color assignedColor, TileLocation tileLocation)
1123+
{
1124+
if (tileLocation.layer.LayerType is not LayerType.Effects or LayerType.UnderObjectsEffects) return assignedColor;
1125+
if (assignedColor.a is >= 1f or < Reagent.MINIMUM_PUDDLE_OPACITY) return assignedColor;
1126+
float neighborAlphaSum = 0f;
1127+
int neighborCount = 0;
1128+
var neighborDirs = new[] { new Vector3Int(1, 0, 0), new Vector3Int(-1, 0, 0), new Vector3Int(0, 1, 0), new Vector3Int(0, -1, 0) };
1129+
foreach (var d in neighborDirs)
1130+
{
1131+
var nPos = position + d;
1132+
var neighborCol = GetColour(nPos, tileLocation.layer.LayerType, useExactForMultilayer: false);
1133+
if (neighborCol != null)
1134+
{
1135+
neighborAlphaSum += neighborCol.Value.a;
1136+
neighborCount++;
1137+
}
1138+
}
1139+
1140+
if (neighborCount > 0)
1141+
{
1142+
float neighborAvg = neighborAlphaSum / neighborCount;
1143+
assignedColor.a = (assignedColor.a + neighborAvg) * 0.5f;
1144+
}
1145+
1146+
return assignedColor;
1147+
}
1148+
11131149
private void HandleBoundChange(TileLocation tileLocation)
11141150
{
11151151

@@ -2197,7 +2233,7 @@ public bool HasOverlayOfType(Vector3Int position, LayerType layerType, OverlayTy
21972233
if (Layers.TryGetValue(layerType, out var layer))
21982234
{
21992235
if (Multilayer)
2200-
{
2236+
{
22012237
lock (MultilayerPresentTiles)
22022238
{
22032239
MultilayerPresentTiles[(int) layer.LayerType].TryGetValue(position, out tileLocations, true);
@@ -3132,4 +3168,5 @@ public enum OverlayType
31323168
DarkMatter,
31333169
Reagents
31343170
}
3135-
}
3171+
}
3172+

0 commit comments

Comments
 (0)