Inventory grid: uniforms reach the masked material; Thermal and the look dirty it; pipes fitted with a fillet only between perpendicular bars
Under a stencil Mask Unity draws a clone of the material and may return one it cached without recopying properties, so GetModifiedMaterial now writes every per-grid uniform to the material that is drawn. Thermal is a property that marks the material dirty, and Flush marks it dirty when a look field changed. The thermal pipes are a plain union of blob and bars with a smooth fillet only between each perpendicular pair (no bump on straight bars), refitted to the 16 sprites: worst per-mask rms 0.058 to 0.035 at S32. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
88b3f7afda
commit
7060803ed9
2 changed files with 73 additions and 22 deletions
|
|
@ -2,6 +2,7 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using int2 = CultMath.int2;
|
||||
|
|
@ -33,12 +34,23 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
// Look, in texels of the sheet sprite; the shader's constants (IG-G).
|
||||
public float RuleInset = 1.5f, RuleWidth = 1f, ArmTexels = 1f, FillInset = 3f, Softness = 1f;
|
||||
public float RuleAlpha = .2f, BracketAlpha = 1f, FillAlpha = 13f / 255f;
|
||||
// Thermal pipes as fractions of the cell, fitted to the 16 pipe sprites by the parity harness (-fitThermal; IG-B4).
|
||||
public float BlobRadius = .1157f, PipeHalfWidth = .1257f, Fillet = .0245f, PipeSoft = .0697f;
|
||||
// Thermal pipes as fractions of the cell, fitted to the 16 pipe sprites (IG-B4): bars of half width .127, a blob, and a fillet between each perpendicular pair.
|
||||
public float BlobRadius = .1274f, PipeHalfWidth = .1252f, Fillet = .1557f, PipeSoft = .0429f;
|
||||
// One texel of the sheet sprite in canvas units, per layer. The icon sheet is 66 pixels per unit: 1.51515 at multiplier 1,
|
||||
// 1.01010 at 1.5. The background sheet is 100: 1 at multiplier 1.
|
||||
public float IconTexel = TexelOf(66f, 1f), BgTexel = TexelOf(100f, 1f);
|
||||
public bool Thermal;
|
||||
private bool _thermal;
|
||||
// The thermal view draws the conducting pipes in place of the item shapes.
|
||||
public bool Thermal
|
||||
{
|
||||
get => _thermal;
|
||||
set
|
||||
{
|
||||
if (_thermal == value) return;
|
||||
_thermal = value;
|
||||
SetMaterialDirty();
|
||||
}
|
||||
}
|
||||
|
||||
private int _width, _height;
|
||||
private float _cellSize = 32;
|
||||
|
|
@ -98,6 +110,7 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
// Uploads the records when any changed. Once per frame is the contract; edit-mode callers flush by hand.
|
||||
public void Flush()
|
||||
{
|
||||
NoticeLook();
|
||||
if (!_dirty || !_texture) return;
|
||||
_texture.SetPixelData(_records, 0);
|
||||
_texture.Apply(false);
|
||||
|
|
@ -106,6 +119,23 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
|
||||
private void LateUpdate() => Flush();
|
||||
|
||||
// The look fields are plain public fields: a change reaches the material at the next Flush, which marks it dirty.
|
||||
private float[] _lookSeen = new float[0];
|
||||
|
||||
private float[] Look() => new[]
|
||||
{
|
||||
RuleInset, RuleWidth, ArmTexels, FillInset, Softness, RuleAlpha, BracketAlpha, FillAlpha,
|
||||
BlobRadius, PipeHalfWidth, Fillet, PipeSoft, IconTexel, BgTexel
|
||||
};
|
||||
|
||||
private void NoticeLook()
|
||||
{
|
||||
var look = Look();
|
||||
if (look.SequenceEqual(_lookSeen)) return;
|
||||
_lookSeen = look;
|
||||
SetMaterialDirty();
|
||||
}
|
||||
|
||||
// The block of cells inside this rect as GridLayoutGroup places a middle-centre block with this padding.
|
||||
private Rect Block()
|
||||
{
|
||||
|
|
@ -139,30 +169,44 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
|
||||
public override void SetMaterialDirty()
|
||||
{
|
||||
PushUniforms();
|
||||
PushUniforms(EnsureMaterial());
|
||||
base.SetMaterialDirty();
|
||||
}
|
||||
|
||||
protected override void UpdateMaterial()
|
||||
private Material EnsureMaterial()
|
||||
{
|
||||
PushUniforms();
|
||||
base.UpdateMaterial();
|
||||
}
|
||||
|
||||
private void PushUniforms()
|
||||
{
|
||||
if (!Shader) return;
|
||||
if (!Shader) return null;
|
||||
if (!_material || _material.shader != Shader)
|
||||
{
|
||||
if (_material) DestroyImmediate(_material);
|
||||
_material = new Material(Shader) { hideFlags = HideFlags.HideAndDontSave };
|
||||
}
|
||||
if (m_Material != _material) m_Material = _material;
|
||||
var m = _material;
|
||||
return _material;
|
||||
}
|
||||
|
||||
protected override void UpdateMaterial()
|
||||
{
|
||||
PushUniforms(EnsureMaterial());
|
||||
base.UpdateMaterial();
|
||||
}
|
||||
|
||||
// Under a stencil Mask Unity draws a clone of the material and may hand back one it cached without copying properties
|
||||
// again, so the uniforms are written to the material that is actually drawn, here, every time it is asked for.
|
||||
public override Material GetModifiedMaterial(Material baseMaterial)
|
||||
{
|
||||
var drawn = base.GetModifiedMaterial(baseMaterial);
|
||||
PushUniforms(drawn);
|
||||
return drawn;
|
||||
}
|
||||
|
||||
private void PushUniforms(Material m)
|
||||
{
|
||||
if (!m) return;
|
||||
m.SetVector("_GridCells", new Vector4(_width, _height, 0, 0));
|
||||
m.SetFloat("_CellSize", _cellSize);
|
||||
m.SetFloat("_Flip", _orientation == GridOrientation.UpperRight ? 1 : 0);
|
||||
m.SetFloat("_Thermal", Thermal ? 1 : 0);
|
||||
m.SetFloat("_Thermal", _thermal ? 1 : 0);
|
||||
m.SetFloat("_IconTexel", IconTexel);
|
||||
m.SetFloat("_BgTexel", BgTexel);
|
||||
m.SetFloat("_RuleInset", RuleInset);
|
||||
|
|
|
|||
|
|
@ -42,10 +42,10 @@ Shader "UI/Inventory Grid"
|
|||
_BracketAlpha ("Bracket alpha", Float) = 1
|
||||
_FillAlpha ("Fill alpha", Float) = 0.050980392
|
||||
// Look of the thermal pipes, as fractions of the cell.
|
||||
_BlobRadius ("Blob radius", Float) = 0.1157
|
||||
_PipeHalfWidth ("Pipe half width", Float) = 0.1257
|
||||
_Fillet ("Fillet", Float) = 0.0245
|
||||
_PipeSoft ("Pipe softness", Float) = 0.0697
|
||||
_BlobRadius ("Blob radius", Float) = 0.1274
|
||||
_PipeHalfWidth ("Pipe half width", Float) = 0.1252
|
||||
_Fillet ("Fillet", Float) = 0.1557
|
||||
_PipeSoft ("Pipe softness", Float) = 0.0429
|
||||
}
|
||||
|
||||
SubShader
|
||||
|
|
@ -184,19 +184,26 @@ Shader "UI/Inventory Grid"
|
|||
return lerp(b, a, h) - k * h * (1.0 - h);
|
||||
}
|
||||
|
||||
// The pipes of the thermal view (IG-B4): a blob, and a capsule from the centre to each conducting edge, in cell fractions.
|
||||
// The pipes of the thermal view (IG-B4), in cell fractions: a blob and a bar from the centre to each conducting edge, as a
|
||||
// plain union, with a fillet only between each perpendicular pair of bars (a smooth union everywhere put a bump on straight bars).
|
||||
float Pipes(float2 p, uint cond)
|
||||
{
|
||||
float2 q = p / _CellSize - 0.5;
|
||||
float d = length(q) - _BlobRadius;
|
||||
float2 dirs[4] = { float2(0, 1), float2(1, 0), float2(0, -1), float2(-1, 0) };
|
||||
float bars[4];
|
||||
[unroll] for (int b = 0; b < 4; b++)
|
||||
{
|
||||
if (((cond >> b) & 1u) == 0u) continue;
|
||||
float along = dot(q, dirs[b]);
|
||||
float perp = abs(dot(q, float2(dirs[b].y, -dirs[b].x)));
|
||||
float d2 = along >= 0.0 ? perp - _PipeHalfWidth : length(float2(perp, along)) - _PipeHalfWidth;
|
||||
d = SmoothMin(d, d2, _Fillet);
|
||||
bars[b] = ((cond >> b) & 1u) == 0u ? Far : (along >= 0.0 ? perp - _PipeHalfWidth : length(float2(perp, along)) - _PipeHalfWidth);
|
||||
d = min(d, bars[b]);
|
||||
}
|
||||
[unroll] for (int c = 0; c < 4; c++)
|
||||
{
|
||||
int n = (c + 1) % 4;
|
||||
if (bars[c] < Far && bars[n] < Far && dot(q, dirs[c] + dirs[n]) > 0.0)
|
||||
d = min(d, SmoothMin(bars[c], bars[n], _Fillet));
|
||||
}
|
||||
return saturate(0.5 - d / _PipeSoft);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue