开发者

Morton Code sort gives the wrong coordinates

开发者 https://www.devze.com 2022-12-07 22:14 出处:网络
For the past few days I\'ve been trying to procedurally piece together a bunch of small tiles that are sorted using Morton code. I got most of the script to work with a 4x4 test, but for whatever reas

For the past few days I've been trying to procedurally piece together a bunch of small tiles that are sorted using Morton code. I got most of the script to work with a 4x4 test, but for whatever reason the image is slightly offset on the Y axis the further right it goes. I'm not sure how to fix this, I'm not really an expert on bitwise operators but maybe it has something to do with that?

Here's the code:

var fileDirectory = Folder.selectDialog("Select a folder");
var files = fileDirectory.getFiles("*.png");
var activeDoc = app.activeDocument;
var scale = 4;

var MASKS = [0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF];
var SHIFTS = [1, 2, 4, 8];

for (var fileIdx = 0; fileIdx < files.length; fileIdx++)
{
   app.load(files[fileIdx]);
   backFile = app.activeDocument;
   backFile.selection.selectAll();
   backFile.selection.copy();
   backFile.close(开发者_StackOverflowSaveOptions.DONOTSAVECHANGES);
   activeDoc.paste();
   activeDoc.activeLayer.name = fileIdx;
   
   var x = fileIdx % scale;
   var y = fileIdx / scale;
   
    x = (x | (x << SHIFTS[3])) & MASKS[3];
    x = (x | (x << SHIFTS[2])) & MASKS[2];
    x = (x | (x << SHIFTS[1])) & MASKS[1];
    x = (x | (x << SHIFTS[0])) & MASKS[0];

    y = (y | (y << SHIFTS[3])) & MASKS[3];
    y = (y | (y << SHIFTS[2])) & MASKS[2];
    y = (y | (y << SHIFTS[1])) & MASKS[1];
    y = (y | (y << SHIFTS[0])) & MASKS[0];
    
    var id = x | (y << 1);
    
    x = (id % scale) * 256;
    y = (id / scale) * 256;
    
    var LB = activeDoc.activeLayer.bounds;
    activeDoc.activeLayer.translate(-LB[0].as('px') + new UnitValue(x, 'px'), -LB[1].as('px') + new UnitValue(y, 'px'));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消