Skip to content

Instantly share code, notes, and snippets.

@Langerz82
Last active April 4, 2019 19:37
Show Gist options
  • Save Langerz82/7605efd357f4eab04061e63e19f8734b to your computer and use it in GitHub Desktop.
Save Langerz82/7605efd357f4eab04061e63e19f8734b to your computer and use it in GitHub Desktop.
// This calculates the triangle points in a rectangle grid.
// Used for 3D Webgl texture mapping.
// Copyright Joshua Langley 2019.
var rows=4;
var cols=4;
var LRs=2;
var triPoints = 6;
var grid = rows*cols;
var totalTris = grid*LRs;
var pos = [totalTris*triPoints];
var p1,p2;
var c,d;
var offset;
var index = 0;
var mpOffset = 1;
for (var row = 0; row < rows; ++row)
{
p2 = mpOffset / rows / 2;
offsetY = row * (mpOffset / rows) + (mpOffset / rows / 2);
for (var col = 0; col < cols; ++col)
{
p1 = mpOffset / cols / 2;
offsetX = col * (mpOffset / cols) + (mpOffset / cols / 2);
for (var LR = 0; LR < LRs; ++LR)
{
c = (LR % 2 == 0) ? -p1 : p1;
d = (LR % 2 == 1) ? -p2 : p2;
pos[index+0] = (offsetX - p1);
pos[index+1] = (offsetY - p2);
pos[index+2] = (offsetX + p1);
pos[index+3] = (offsetY + p2);
pos[index+4] = (offsetX + c );
pos[index+5] = (offsetY + d );
index += triPoints;
}
}
}
log.info("pos="+JSON.stringify(pos));
@Langerz82
Copy link
Author

Updated - To fix grids higher than 2x2, and grids with different amounts of rows and columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment