Skip to content

Commit

Permalink
Fix slope rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Mar 29, 2022
1 parent cdae9fa commit 437fd61
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 9 deletions.
29 changes: 21 additions & 8 deletions Drizzle.Lingo.Runtime/LingoImage.CopyPixels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Drizzle.Lingo.Runtime;
public sealed unsafe partial class LingoImage
{
private const float QuadInvBilinearLinearCutOff = 0.0001f;
private const float QuadInvTriCheckCutOff = 0.001f;

// Not used by the editor (but there is a similar lingo API)
// Mostly just for unit tests right now.
Expand Down Expand Up @@ -337,11 +338,18 @@ static Vector2 InvBilinear(Vector2 p, Vector2 a, Vector2 e, Vector2 f, Vector2 g
var k1 = Cross2d(e, f) + Cross2d(h, g);
var k0 = Cross2d(h, e);

// From the shadertoy comments (by the original author):
// Fixes coordinates so large coordinate spaces don't get massive distortions in some edge cases.
k2 /= k0;
k1 /= k0;
k0 = 1.0f;
if (Math.Abs(k0) > QuadInvTriCheckCutOff)
{
// From the shadertoy comments (by the original author):
// Fixes coordinates so large coordinate spaces don't get massive distortions in some edge cases.

// NOTE: when drawing a triangle (i.e. two corners on the same spot),
// k0 will be zero, and doing this will break everything.

k2 /= k0;
k1 /= k0;
k0 = 1.0f;
}

// if edges are parallel, this is a linear equation
if (MathF.Abs(k2) < QuadInvBilinearLinearCutOff)
Expand Down Expand Up @@ -559,9 +567,14 @@ private static (Vector256<float> s, Vector256<float> t) QuadInvBilinearAvx2(
var k1 = Avx.Add(Cross2dAvx2(eX, eY, fX, fY), Cross2dAvx2(hX, hY, gX, gY));
var k0 = Cross2dAvx2(hX, hY, eX, eY);

k2 = Avx.Divide(k2, k0);
k1 = Avx.Divide(k1, k0);
k0 = Vector256.Create(1.0f);
var k0Abs = Avx.AndNot(Vector256.Create(-0.0f), k0);
var triCheck = Avx.CompareLessThan(
k0Abs,
Vector256.Create(QuadInvTriCheckCutOff));

k2 = Avx.BlendVariable(Avx.Divide(k2, k0), k2, triCheck);
k1 = Avx.BlendVariable(Avx.Divide(k1, k0), k1, triCheck);
k0 = Avx.BlendVariable(Vector256.Create(1.0f), k0, triCheck);

var k2Abs = Avx.AndNot(Vector256.Create(-0.0f), k2);
var linearMask = Avx.CompareLessThan(k2Abs, Vector256.Create(QuadInvBilinearLinearCutOff));
Expand Down
87 changes: 86 additions & 1 deletion Drizzle.Lingo.Tests/LingoImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,91 @@ [new LingoSymbol("maskimage")] = layer9.makesilhouette(0).createmask()
});
}

[Test]
public void TestCopyPixelsTriangle()
{
var pxl = MakePxl();
var baseImg = new LingoImage(20, 20, 32);

baseImg.fill(new LingoColor(255, 0, 0));

baseImg.copypixels(
pxl,
new LingoList
{
new LingoPoint(0, 0),
new LingoPoint(0, 0),
new LingoPoint(20, 20),
new LingoPoint(0, 20),
},
new LingoRect(0, 0, 1, 1),
new LingoPropertyList
{
[new LingoSymbol("color")] = LingoColor.White
});

//baseImg.ShowImage();

baseImg.fill(new LingoColor(255, 0, 0));

baseImg.copypixels(
pxl,
new LingoList
{
new LingoPoint(20, 0),
new LingoPoint(20, 0),
new LingoPoint(0, 20),
new LingoPoint(20, 20),
},
new LingoRect(0, 0, 1, 1),
new LingoPropertyList
{
[new LingoSymbol("color")] = LingoColor.White
});

//baseImg.ShowImage();

baseImg.fill(new LingoColor(255, 0, 0));

baseImg.copypixels(
pxl,
new LingoList
{
new LingoPoint(0, 20),
new LingoPoint(0, 20),
new LingoPoint(20, 0),
new LingoPoint(0, 0),
},
new LingoRect(0, 0, 1, 1),
new LingoPropertyList
{
[new LingoSymbol("color")] = LingoColor.White
});

//baseImg.ShowImage();

baseImg.fill(new LingoColor(255, 0, 0));

baseImg.copypixels(
pxl,
new LingoList
{
new LingoPoint(20, 20),
new LingoPoint(20, 20),
new LingoPoint(0, 0),
new LingoPoint(20, 0),
},
new LingoRect(0, 0, 1, 1),
new LingoPropertyList
{
[new LingoSymbol("color")] = LingoColor.White
});

//baseImg.ShowImage();


}

private static LingoImage ImageFromResource(string name)
{
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
Expand All @@ -296,4 +381,4 @@ private static LingoImage MakePxl(bool markAsSuch=false)
img.IsPxl = markAsSuch;
return img;
}
}
}

0 comments on commit 437fd61

Please sign in to comment.