public static void StencilBilinearSample(this Serializable2DFloatArray array, Vector2 normalizedCoord, out float strength, bool ignoreNegativeKeys = true) { if (normalizedCoord.x < 0 || normalizedCoord.x > 1 || normalizedCoord.y < 0 || normalizedCoord.y > 1) { strength = 0; return; } normalizedCoord = new Vector2(normalizedCoord.x * array.Width, normalizedCoord.y * array.Height); int xMin = Mathf.FloorToInt(normalizedCoord.x); int xMax = Mathf.CeilToInt(normalizedCoord.x); int yMin = Mathf.FloorToInt(normalizedCoord.y); int yMax = Mathf.CeilToInt(normalizedCoord.y); xMin = Mathf.Clamp(xMin, 0, array.Width - 1); xMax = Mathf.Clamp(xMax, 0, array.Width - 1); yMin = Mathf.Clamp(yMin, 0, array.Height - 1); yMax = Mathf.Clamp(yMax, 0, array.Height - 1); float v1 = array[xMin, yMin]; float v2 = array[xMin, yMax]; float v3 = array[xMax, yMin]; float v4 = array[xMax, yMax]; int v1Index; int v2Index; int v3Index; int v4Index; MiscUtilities.DecompressStencil(v1, out v1Index, out v1); MiscUtilities.DecompressStencil(v2, out v2Index, out v2); MiscUtilities.DecompressStencil(v3, out v3Index, out v3); MiscUtilities.DecompressStencil(v4, out v4Index, out v4); if (ignoreNegativeKeys) { v1 = v1Index > 0 ? v1 : 0; v2 = v1Index > 0 ? v2 : 0; v3 = v1Index > 0 ? v3 : 0; v4 = v1Index > 0 ? v4 : 0; } if (Math.Abs(v1 + v2 + v3 + v4) < .01f) { strength = 0; return; } float xFrac = normalizedCoord.x.Frac(); float yFrac = normalizedCoord.y.Frac(); v1 *= (1 - xFrac) * (1 - yFrac); v2 *= (1 - xFrac) * (/*1 - */ yFrac); v3 *= (/*1 - */ xFrac) * (1 - yFrac); v4 *= (/*1 - */ xFrac) * (/*1 -*/ yFrac); strength = Mathf.Clamp01(v1 + v2 + v3 + v4); }
public static void ColoriseStencil(Serializable2DFloatArray stencil) { for (var u = 0; u < stencil.Width; ++u) { for (var v = 0; v < stencil.Height; ++v) { int key; float strength; DecompressStencil(stencil[u, v], out key, out strength); if (strength > 0 && key > 0) { stencil[u, v] = MiscUtilities.CompressStencil(key, 1); } else { stencil[u, v] = 0; } } } }