Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.24 KB | None | 0 0
  1. var standartPallete = [16777216,16777164,16777113,16777062,16777011,16776960,16764159,16764108,16764057,16764006,16763955,16763904,16751103,16751052,16751001,16750950,16750899,16750848,16738047,16737996,16737945,16737894,16737843,16737792,16724991,16724940,16724889,16724838,16724787,16724736,16711935,16711884,16711833,16711782,16711731,16711680,13434879,13434828,13434777,13434726,13434675,13434624,13421823,13421772,13421721,13421670,13421619,13421568,13408767,13408716,13408665,13408614,13408563,13408512,13395711,13395660,13395609,13395558,13395507,13395456,13382655,13382604,13382553,13382502,13382451,13382400,13369599,13369548,13369497,13369446,13369395,13369344,10092543,10092492,10092441,10092390,10092339,10092288,10079487,10079436,10079385,10079334,10079283,10079232,10066431,10066380,10066329,10066278,10066227,10066176,10053375,10053324,10053273,10053222,10053171,10053120,10040319,10040268,10040217,10040166,10040115,10040064,10027263,10027212,10027161,10027110,10027059,10027008,6750207,6750156,6750105,6750054,6750003,6749952,6737151,6737100,6737049,6736998,6736947,6736896,6724095,6724044,6723993,6723942,6723891,6723840,6711039,6710988,6710937,6710886,6710835,6710784,6697983,6697932,6697881,6697830,6697779,6697728,6684927,6684876,6684825,6684774,6684723,6684672,3407871,3407820,3407769,3407718,3407667,3407616,3394815,3394764,3394713,3394662,3394611,3394560,3381759,3381708,3381657,3381606,3381555,3381504,3368703,3368652,3368601,3368550,3368499,3368448,3355647,3355596,3355545,3355494,3355443,3355392,3342591,3342540,3342489,3342438,3342387,3342336,65535,65484,65433,65382,65331,65280,52479,52428,52377,52326,52275,52224,39423,39372,39321,39270,39219,39168,26367,26316,26265,26214,26163,26112,13311,13260,13209,13158,13107,13056,255,204,153,102,51,15597568,14483456,12255232,11141120,8912896,7798784,5570560,4456448,2228224,1114112,60928,56576,47872,43520,34816,30464,21760,17408,8704,4352,238,221,187,170,136,119,85,68,34,17,15658734,14540253,12303291,11184810,8947848,7829367,5592405,4473924,2236962,1118481
  2. ];
  3.  
  4. $(function () {
  5. $(document).tooltip({
  6. position: {
  7. my: "left+15 center",
  8. at: "right center"
  9. },
  10. // tooltipClass: "right",
  11. content: function () {
  12. return $(this).prop('title');
  13. }
  14. });
  15.  
  16. $( "#dialog-message" ).dialog({
  17. autoOpen: false,
  18. dialogClass: "no-close",
  19. modal: true,
  20. buttons: {
  21. Ok: function() {
  22. $( this ).dialog( "close" );
  23. }
  24. }
  25. });
  26.  
  27. });
  28.  
  29. function showError(title, content){
  30. $( "#dialog-message" )
  31. .html(content)
  32. .dialog("option", "title", title)
  33. .dialog("open");
  34. }
  35.  
  36. var resultChunks = [];
  37. var resultPalette= []; // Loaded palette from file
  38. var colArr = []; // Array of DOM divs with texture name and color
  39. var colNum = []; // Set, represented enumerate id and color from loader
  40. var input, file, fr;
  41. var lastMod;
  42. var isLoading;
  43.  
  44. window.onload = function() {
  45. // Init color pickers
  46. var origDiv = document.getElementById("texCol");
  47. origDiv.style.display = "none";
  48.  
  49. colArr.push(origDiv);
  50. for (var i = 0; i < 23; i++) {
  51. var divClone = origDiv.cloneNode(true); // the true is for deep cloning
  52. divClone.style.display = "none";
  53. origDiv.parentNode.appendChild(divClone);
  54. colArr.push(divClone);
  55. }
  56. }
  57.  
  58. function loadFile() {
  59. isLoading = true;
  60. colNum = [];
  61.  
  62. if (typeof window.FileReader !== 'function') {
  63. console.log("p", "The file API isn't supported on this browser yet.");
  64. return;
  65. }
  66.  
  67. input = document.getElementById('fileinput');
  68. if (!input) {
  69. console.log("p", "Um, couldn't find the fileinput element.");
  70. } else if (!input.files) {
  71. console.log("p", "This browser doesn't seem to support the `files` property of file inputs.");
  72. } else if (!input.files[0]) {
  73. console.log("p", "Please select a file before clicking 'Load'");
  74. } else {
  75. file = input.files[0];
  76. lastMod = file.lastModifiedDate
  77. setInterval(tick, 500)
  78.  
  79. fr = new FileReader();
  80. fr.onload = receivedBinary;
  81. fr.readAsBinaryString(file);
  82. }
  83. }
  84.  
  85. function tick(){
  86. var file = input.files && input.files[0];
  87. if (file && lastMod && file.lastModifiedDate.getTime() !== lastMod.getTime()) {
  88. // File was changed, reload
  89. lastMod = file.lastModifiedDate;
  90. loadFile();
  91. }
  92. }
  93.  
  94. function receivedBinary() {
  95. var obj = loadVolume(fr.result);
  96.  
  97. // Check Errors
  98. if (jQuery.type(obj) == "string"){
  99. showError("Wrong file", obj);
  100. return;
  101. }
  102.  
  103. var wholeVolume = obj[0];
  104. var size = new Vector(obj[1], obj[2], obj[3]);
  105.  
  106. // Split volume
  107. resultChunks = [];
  108. for (var _x = 0; _x <= Math.floor((size.x - 1) / 16); _x++) {
  109. for (var _z = 0; _z <= Math.floor((size.z - 1) / 16); _z++) {
  110. for (var _y = 0; _y <= Math.floor((size.y - 1) / 16); _y++) {
  111.  
  112. var vol = [];
  113. for (var z = 0; z < 16; z++) {
  114. for (var y = 0; y < 16; y++) {
  115. for (var x = 0; x < 16; x++) {
  116. setVal(vol, x, y, z, getVal(wholeVolume, x + _x * 16, y + _y * 16, z + _z * 16))
  117. }
  118. }
  119. }
  120.  
  121. resultChunks.push(makeChunks(vol))
  122. }
  123. }
  124. }
  125.  
  126. // Show color elements if need
  127. for (var i = 0; i < 23; i++) {
  128. if (i < colNum.length) {
  129. colArr[i].style.display = "block";
  130. var hex = resultPalette[colNum[i]-1].toString(16);
  131. colArr[i].querySelector("#colPicker").value = "#" + "000000".substr(0, 6 - hex.length) + hex;
  132. } else colArr[i].style.display = "none";
  133. }
  134. document.getElementById("modelLabel").value = file.name.replace(/\.[^/.]+$/, "");
  135.  
  136. // Show on HTML
  137. isLoading = false;
  138. updateOutput();
  139.  
  140. $(".hiddenIntro").fadeIn(500);
  141. }
  142.  
  143. function updateOutput() {
  144. if (isLoading) {return;}
  145. document.getElementById("result").innerHTML = "";
  146.  
  147. var ch_count = document.getElementById("ChunksCount")
  148. ch_count.innerHTML = "";
  149. ch_count.className = "";
  150.  
  151. var fromInput = function(id) {
  152. return document.getElementById(id).value;
  153. }
  154. var fromCheckbox = function(id) {
  155. return document.getElementById(id).checked;
  156. }
  157.  
  158. var isSplit = fromCheckbox("splitToStates");
  159.  
  160. var iters = isSplit ? resultChunks.length / 2 : resultChunks.length;
  161.  
  162. for (var i = 0; i < iters; i++) {
  163. var s = (i == 0) ? "{" : "\n\n{";
  164. if (fromInput("modelLabel") != "") s += "\n label = \"" + fromInput("modelLabel") + "\",";
  165. if (fromInput("modelTip") != "") s += "\n tooltip = \"" + fromInput("modelTip") + "\",";
  166. if (fromInput("lightLevel") != 0) s += "\n lightLevel = " + fromInput("lightLevel") + ",";
  167. if (fromCheckbox("emitRedstone")) s += "\n emitRedstone = " + fromCheckbox("emitRedstone") + ",";
  168. if (fromCheckbox("buttonMode")) s += "\n buttonMode = " + fromCheckbox("buttonMode") + ",";
  169. s += "\n shapes = {";
  170.  
  171. var ch_counter = 0;
  172.  
  173. var addState = function(pos, state) {
  174. if (!resultChunks[pos]) return;
  175.  
  176. ch_counter += resultChunks[pos].length;
  177.  
  178. for (var j = 0; j < resultChunks[pos].length; j++) {
  179. var l = "\n { ";
  180. var p1 = resultChunks[pos][j][0],
  181. p2 = resultChunks[pos][j][1];
  182. l += p1.x + "," + p1.z + "," + p1.y + ",";
  183. l += (p2.x + 1) + "," + (p2.z + 1) + "," + (p2.y + 1) + ",";
  184.  
  185. var colIndex = colNum.indexOf(resultChunks[pos][j][2]);
  186. l += "texture = \"" + colArr[colIndex].querySelector("#texture").value + "\"";
  187.  
  188. var col = colArr[colIndex].querySelector("#colPicker").value.substring(1);
  189. if (col != "ffffff") l += ", tint = 0x" + col;
  190. if (state == true) l += ", state = true";
  191.  
  192. l += " },";
  193.  
  194. s += l;
  195. }
  196. }
  197.  
  198. addState(i, false);
  199.  
  200. if (isSplit) {
  201. s += "\n";
  202. addState(i + iters, true);
  203. }
  204.  
  205. ch_count.innerHTML += " <span " + ((ch_counter > 24) ? "class='red'>" : ">") + ch_counter + "</span>";
  206.  
  207. s += "\n }\n}" + ((i != iters - 1) ? "," : "");
  208.  
  209. document.getElementById("result").innerHTML += s;
  210. }
  211.  
  212. }
  213.  
  214. function loadVolume(r) {
  215. var c=0; // Caret
  216. var next_ = function(n){ if(!n){n=1;} c+= n*4;};
  217. var readInt = function(){ var result = readUInt32(r, c); next_(); return result; };
  218. var readKey = function(){ var result = r.substring(c, c + 4); next_(); return result; };
  219. var readByte= function(){ var result = r.charCodeAt(c); c+=1; return result; };
  220.  
  221. // Check file format
  222. if (readKey() != "VOX "){
  223. return "<p>Cant load file</p><p>This is probably not VOX file</p>"
  224. }
  225.  
  226. var versionNumber = readInt();
  227.  
  228. // Check MAIN chunk
  229. if (readKey() != "MAIN"){
  230. return "<p>This is VOX file, but i cant read it</p>"
  231. }
  232. next_(2)
  233.  
  234. // Check PACK chunk
  235. var keyword = readKey();
  236. var numPacks = 1;
  237. if (keyword == "PACK"){
  238. next_(2)
  239. numPacks = readInt();
  240. keyword = readKey();
  241. }
  242.  
  243. // PACKS
  244. for (var i = 0; i < 1; i++) {
  245. // TODO: Read more then one pack
  246. //for (var i = 0; i < numPacks; i++) {
  247.  
  248. // Check SIZE chunk
  249. if (keyword != "SIZE"){
  250. return "<p>Cant read SIZE chunk</p>"
  251. }
  252. next_(2)
  253.  
  254. var sizeX = readInt();
  255. var sizeY = readInt();
  256. var sizeZ = readInt();
  257.  
  258. // Keyword XYZI
  259. readKey();
  260. next_(2);
  261.  
  262. // Count of voxels in first pack
  263. var numVoxels = readInt()
  264.  
  265. var vol = [];
  266.  
  267. // Voxels
  268. for (var k = 0; k < numVoxels; ++k) {
  269. var x = readByte();
  270. var y = readByte();
  271. var z = readByte();
  272. var col= readByte();
  273.  
  274. setVal(vol, x, y, z, col);
  275. // setVal(vol, x, y, z, palette[col - 1]);
  276. }
  277. }
  278.  
  279. // Skip unloaded chunks
  280. for (var i = 1; i < numPacks; i++) {
  281. next_(9);
  282. var numVoxels = readInt()
  283. next_(numVoxels);
  284. }
  285.  
  286. // Palette
  287. resultPalette = [];
  288. if (readKey() != "RGBA"){
  289. resultPalette = standartPallete;
  290. }else {
  291. next_(2);
  292. for (var k = 0; k < 255; ++k) {
  293. resultPalette.push(readColor(r, c))
  294. next_();
  295. }
  296. }
  297.  
  298.  
  299. return [vol, sizeX, sizeY, sizeZ]
  300. }
  301.  
  302. var surr = [
  303. [new Vector(0, 1, 0), new Vector(1, 0, 0), new Vector(0, 0, 1)],
  304. [new Vector(1, 0, 0), new Vector(0, 1, 0), new Vector(0, 0, 1)],
  305. [new Vector(0, 0, 1), new Vector(1, 0, 0), new Vector(0, 1, 0)],
  306. [new Vector(0, 0, 1), new Vector(0, 1, 0), new Vector(1, 0, 0)],
  307. [new Vector(1, 0, 0), new Vector(0, 0, 1), new Vector(0, 1, 0)],
  308. [new Vector(0, 1, 0), new Vector(0, 0, 1), new Vector(1, 0, 0)]
  309. ];
  310.  
  311. function makeChunks(vol) {
  312.  
  313. var o = []; // Checked points
  314. var chunks = []; // Array of bounds of chunks
  315.  
  316. var findLine = function(x, y, z, dir1, col) {
  317. var p = new Vector(x, y, z);
  318. var b2 = new Vector(x, y, z);
  319. if (!col) col = getVal(vol, p.x, p.y, p.z);
  320.  
  321. while (getVal(vol, p.x, p.y, p.z) && col == getVal(vol, p.x, p.y, p.z)) {
  322. b2 = p.clone();
  323. p = p.add(dir1);
  324. }
  325.  
  326. return b2;
  327. }
  328.  
  329. var findPlane = function(x, y, z, dir1, dir2, col) {
  330. var p = new Vector(x, y, z);
  331. var line = findLine(p.x, p.y, p.z, dir1, col);
  332. var fst_line = line.clone()
  333. var nxt = line
  334.  
  335. while ((p.subtract(nxt)).length() > 0 && nxt.x >= fst_line.x && nxt.y >= fst_line.y && nxt.z >= fst_line.z) {
  336. p = p.add(dir2);
  337. line = nxt
  338. nxt = findLine(p.x, p.y, p.z, dir1, col);
  339. }
  340.  
  341. // Result must be not greater then first
  342. line.x = (dir1.x > 0) ? fst_line.x : line.x;
  343. line.y = (dir1.y > 0) ? fst_line.y : line.y;
  344. line.z = (dir1.z > 0) ? fst_line.z : line.z;
  345. return line
  346. }
  347.  
  348. var findBox = function(x, y, z, dir1, dir2, dir3, col) {
  349. var p = new Vector(x, y, z);
  350. var plane = findPlane(p.x, p.y, p.z, dir1, dir2, col);
  351. var fst_plane = plane.clone()
  352. var nxt = plane
  353.  
  354. while ((p.subtract(nxt)).length() > 0 && nxt.x >= fst_plane.x && nxt.y >= fst_plane.y && nxt.z >= fst_plane.z) {
  355. p = p.add(dir3);
  356. plane = nxt
  357. nxt = findPlane(p.x, p.y, p.z, dir1, dir2, col);
  358. }
  359.  
  360. // Result must be not greater then first
  361. plane.x = (dir1.x > 0 || dir2.x > 0) ? fst_plane.x : plane.x;
  362. plane.y = (dir1.y > 0 || dir2.y > 0) ? fst_plane.y : plane.y;
  363. plane.z = (dir1.z > 0 || dir2.z > 0) ? fst_plane.z : plane.z;
  364. return plane
  365. }
  366.  
  367. var pushChunk = function(b1, b2, col) {
  368. chunks.push([b1, b2, col]);
  369.  
  370. // Add color
  371. if (colNum.indexOf(col) == -1) {
  372. colNum.push(col)
  373. }
  374.  
  375. for (var z = b1.z; z <= b2.z; z++) {
  376. for (var y = b1.y; y <= b2.y; y++) {
  377. for (var x = b1.x; x <= b2.x; x++) {
  378. setVal(o, x, y, z, true);
  379. }
  380. }
  381. }
  382. }
  383.  
  384. /////////////////////////////////////////////
  385. /////////////////////////////////////////////
  386. /////////////////////////////////////////////
  387.  
  388. for (var z = 0; z < 16; z++) {
  389. for (var x = 0; x < 16; x++) {
  390. for (var y = 0; y < 16; y++) {
  391.  
  392. if (getVal(o, x, y, z) == true) continue;
  393.  
  394. var orig = new Vector(x, y, z);
  395. var p = new Vector(x, y, z);
  396. var col = getVal(vol, p.x, p.y, p.z);
  397.  
  398. if (!col) continue;
  399.  
  400. var maxVolume = 0;
  401. var maxBox;
  402. for (var i = 0; i < surr.length; i++) {
  403. var box = findBox(x, y, z, surr[i][0], surr[i][1], surr[i][2], col);
  404.  
  405. var uniBox = box.subtract(orig);
  406. uniBox = uniBox.add(1)
  407. var scale = uniBox.x * uniBox.y * uniBox.z;
  408. if (scale > maxVolume) {
  409. maxVolume = scale;
  410. maxBox = box;
  411. }
  412. }
  413. pushChunk(orig, maxBox, col);
  414. }
  415. }
  416. }
  417.  
  418. return chunks;
  419. }
  420.  
  421. // read big-endian (network byte order) unsigned 32-bit int from data, at offset
  422. function readUInt32(data, offset) {
  423. return ((data.charCodeAt(offset) & 0xFF)) +
  424. ((data.charCodeAt(offset + 1) & 0xFF) << 8) +
  425. ((data.charCodeAt(offset + 2) & 0xFF) << 16) +
  426. ((data.charCodeAt(offset + 3) & 0xFF) << 24);
  427. }
  428.  
  429. function readColor(data, offset) {
  430. return ((data.charCodeAt(offset) & 0xFF) << 16) +
  431. ((data.charCodeAt(offset + 1) & 0xFF) << 8) +
  432. ((data.charCodeAt(offset + 2) & 0xFF));
  433. }
  434.  
  435. function setVal(arr, x, y, z, v) {
  436. if (!v) return;
  437.  
  438. if (!arr[z]) arr[z] = [];
  439. if (!arr[z][y]) arr[z][y] = [];
  440. arr[z][y][x] = v;
  441. }
  442.  
  443. function getVal(arr, x, y, z) {
  444. if (!arr) return null;
  445. if (!arr[z]) return null;
  446. if (!arr[z][y]) return null;
  447. return arr[z][y][x];
  448. }
  449.  
  450. function vToStr(v) {
  451. return "[" + v.x + "," + v.y + "," + v.z + "]"
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement