title: UV alignment code for 2D surfaces inexplicably reverses pattern below a fixed threshold
body: <p>This code is part of a more complex library I'm making, the whole thing would require larger context so I'm hoping the simplified version is enough to work with. It's part of a function for positioning 3D surfaces in a Minecraft type voxel engine I'm making in Godot (GDScript), notably generating the UV coordinates so at any surface or texture scale the image matches and repeatedly starts from the top-left corner of the largest unit. The surface scale and image resolution are independent values but each must be a division or power of two, eg: 0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8, 16, etc.</p>
  <pre><code># res = surface resolution setting, tex = texture resolution setting
  # pos is the center of the plane in 3D space, size is the 2D scale of the plane across the direction it spans
  var scale_x = 1 / tex
  var scale_y = 1 / tex
  var ofs_x = 0.5 - (res / 2) if res &lt; 0.5 else res / 2
  var ofs_y = 0.5 - (res / 2) if res &lt; 0.5 else res / 2
  if dir == +x or dir == -x:
      ofs = Vector2((pos.y - size.x - ofs_x) * scale_x, (pos.z - size.y - ofs_y) * scale_y)
  elif dir == +y or dir == -y:
      ofs = Vector2((pos.x - size.x - ofs_x) * scale_x, (pos.z - size.y - ofs_y) * scale_y)
  elif dir == +z or dir == -z:
      ofs = Vector2((pos.x - size.x - ofs_x) * scale_x, (pos.y - size.y - ofs_y) * scale_y)
  </code></pre>
  <p>When the sync is working correctly it looks like this in my test:</p>
  <p><a href="https://i.stack.imgur.com/aCgc7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aCgc7.jpg" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/jw8aB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jw8aB.jpg" alt="enter image description here" /></a></p>
  <p>Problem is I couldn't get a predictable result. After over a day of struggling to understand why the texture wouldn't align correctly sometimes and trying values manually to find a pattern, I made a rather baffling discovery: When the geometry resolution drops below 0.5, the pattern changes and even reverses direction. If at values above 0.5 the offset is a simple <code>res / 2</code> for values below 0.5 it becomes <code>0.5 - (res / 2)</code>. I tested it many times and could clearly confirm the following combinations work for each of those resolution values:</p>
  <pre><code>res: 0.0625 = ofs: 0.46875
  res: 0.125 = ofs: 0.4375
  res: 0.25 = ofs: 0.375
  res: 0.5 = ofs: 0.25
  res: 1 = ofs: 0.5
  res: 2 = ofs: 1
  res: 4 = ofs: 2
  </code></pre>
  <p>Why isn't it a fixed pattern in either direction? The weird offset variables I coded above seem to be producing the desired results, but I still don't understand how or why they're doing so, unless I do so this feels like a hack and I'm left wondering if I should be fixing something else. Can anyone explain the bizarre pattern please, and if there's a more efficient way to code my solution?</p>