Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- ];
- $(function () {
- $(document).tooltip({
- position: {
- my: "left+15 center",
- at: "right center"
- },
- // tooltipClass: "right",
- content: function () {
- return $(this).prop('title');
- }
- });
- $( "#dialog-message" ).dialog({
- autoOpen: false,
- dialogClass: "no-close",
- modal: true,
- buttons: {
- Ok: function() {
- $( this ).dialog( "close" );
- }
- }
- });
- });
- function showError(title, content){
- $( "#dialog-message" )
- .html(content)
- .dialog("option", "title", title)
- .dialog("open");
- }
- var resultChunks = [];
- var resultPalette= []; // Loaded palette from file
- var colArr = []; // Array of DOM divs with texture name and color
- var colNum = []; // Set, represented enumerate id and color from loader
- var input, file, fr;
- var lastMod;
- var isLoading;
- window.onload = function() {
- // Init color pickers
- var origDiv = document.getElementById("texCol");
- origDiv.style.display = "none";
- colArr.push(origDiv);
- for (var i = 0; i < 23; i++) {
- var divClone = origDiv.cloneNode(true); // the true is for deep cloning
- divClone.style.display = "none";
- origDiv.parentNode.appendChild(divClone);
- colArr.push(divClone);
- }
- }
- function loadFile() {
- isLoading = true;
- colNum = [];
- if (typeof window.FileReader !== 'function') {
- console.log("p", "The file API isn't supported on this browser yet.");
- return;
- }
- input = document.getElementById('fileinput');
- if (!input) {
- console.log("p", "Um, couldn't find the fileinput element.");
- } else if (!input.files) {
- console.log("p", "This browser doesn't seem to support the `files` property of file inputs.");
- } else if (!input.files[0]) {
- console.log("p", "Please select a file before clicking 'Load'");
- } else {
- file = input.files[0];
- lastMod = file.lastModifiedDate
- setInterval(tick, 500)
- fr = new FileReader();
- fr.onload = receivedBinary;
- fr.readAsBinaryString(file);
- }
- }
- function tick(){
- var file = input.files && input.files[0];
- if (file && lastMod && file.lastModifiedDate.getTime() !== lastMod.getTime()) {
- // File was changed, reload
- lastMod = file.lastModifiedDate;
- loadFile();
- }
- }
- function receivedBinary() {
- var obj = loadVolume(fr.result);
- // Check Errors
- if (jQuery.type(obj) == "string"){
- showError("Wrong file", obj);
- return;
- }
- var wholeVolume = obj[0];
- var size = new Vector(obj[1], obj[2], obj[3]);
- // Split volume
- resultChunks = [];
- for (var _x = 0; _x <= Math.floor((size.x - 1) / 16); _x++) {
- for (var _z = 0; _z <= Math.floor((size.z - 1) / 16); _z++) {
- for (var _y = 0; _y <= Math.floor((size.y - 1) / 16); _y++) {
- var vol = [];
- for (var z = 0; z < 16; z++) {
- for (var y = 0; y < 16; y++) {
- for (var x = 0; x < 16; x++) {
- setVal(vol, x, y, z, getVal(wholeVolume, x + _x * 16, y + _y * 16, z + _z * 16))
- }
- }
- }
- resultChunks.push(makeChunks(vol))
- }
- }
- }
- // Show color elements if need
- for (var i = 0; i < 23; i++) {
- if (i < colNum.length) {
- colArr[i].style.display = "block";
- var hex = resultPalette[colNum[i]-1].toString(16);
- colArr[i].querySelector("#colPicker").value = "#" + "000000".substr(0, 6 - hex.length) + hex;
- } else colArr[i].style.display = "none";
- }
- document.getElementById("modelLabel").value = file.name.replace(/\.[^/.]+$/, "");
- // Show on HTML
- isLoading = false;
- updateOutput();
- $(".hiddenIntro").fadeIn(500);
- }
- function updateOutput() {
- if (isLoading) {return;}
- document.getElementById("result").innerHTML = "";
- var ch_count = document.getElementById("ChunksCount")
- ch_count.innerHTML = "";
- ch_count.className = "";
- var fromInput = function(id) {
- return document.getElementById(id).value;
- }
- var fromCheckbox = function(id) {
- return document.getElementById(id).checked;
- }
- var isSplit = fromCheckbox("splitToStates");
- var iters = isSplit ? resultChunks.length / 2 : resultChunks.length;
- for (var i = 0; i < iters; i++) {
- var s = (i == 0) ? "{" : "\n\n{";
- if (fromInput("modelLabel") != "") s += "\n label = \"" + fromInput("modelLabel") + "\",";
- if (fromInput("modelTip") != "") s += "\n tooltip = \"" + fromInput("modelTip") + "\",";
- if (fromInput("lightLevel") != 0) s += "\n lightLevel = " + fromInput("lightLevel") + ",";
- if (fromCheckbox("emitRedstone")) s += "\n emitRedstone = " + fromCheckbox("emitRedstone") + ",";
- if (fromCheckbox("buttonMode")) s += "\n buttonMode = " + fromCheckbox("buttonMode") + ",";
- s += "\n shapes = {";
- var ch_counter = 0;
- var addState = function(pos, state) {
- if (!resultChunks[pos]) return;
- ch_counter += resultChunks[pos].length;
- for (var j = 0; j < resultChunks[pos].length; j++) {
- var l = "\n { ";
- var p1 = resultChunks[pos][j][0],
- p2 = resultChunks[pos][j][1];
- l += p1.x + "," + p1.z + "," + p1.y + ",";
- l += (p2.x + 1) + "," + (p2.z + 1) + "," + (p2.y + 1) + ",";
- var colIndex = colNum.indexOf(resultChunks[pos][j][2]);
- l += "texture = \"" + colArr[colIndex].querySelector("#texture").value + "\"";
- var col = colArr[colIndex].querySelector("#colPicker").value.substring(1);
- if (col != "ffffff") l += ", tint = 0x" + col;
- if (state == true) l += ", state = true";
- l += " },";
- s += l;
- }
- }
- addState(i, false);
- if (isSplit) {
- s += "\n";
- addState(i + iters, true);
- }
- ch_count.innerHTML += " <span " + ((ch_counter > 24) ? "class='red'>" : ">") + ch_counter + "</span>";
- s += "\n }\n}" + ((i != iters - 1) ? "," : "");
- document.getElementById("result").innerHTML += s;
- }
- }
- function loadVolume(r) {
- var c=0; // Caret
- var next_ = function(n){ if(!n){n=1;} c+= n*4;};
- var readInt = function(){ var result = readUInt32(r, c); next_(); return result; };
- var readKey = function(){ var result = r.substring(c, c + 4); next_(); return result; };
- var readByte= function(){ var result = r.charCodeAt(c); c+=1; return result; };
- // Check file format
- if (readKey() != "VOX "){
- return "<p>Cant load file</p><p>This is probably not VOX file</p>"
- }
- var versionNumber = readInt();
- // Check MAIN chunk
- if (readKey() != "MAIN"){
- return "<p>This is VOX file, but i cant read it</p>"
- }
- next_(2)
- // Check PACK chunk
- var keyword = readKey();
- var numPacks = 1;
- if (keyword == "PACK"){
- next_(2)
- numPacks = readInt();
- keyword = readKey();
- }
- // PACKS
- for (var i = 0; i < 1; i++) {
- // TODO: Read more then one pack
- //for (var i = 0; i < numPacks; i++) {
- // Check SIZE chunk
- if (keyword != "SIZE"){
- return "<p>Cant read SIZE chunk</p>"
- }
- next_(2)
- var sizeX = readInt();
- var sizeY = readInt();
- var sizeZ = readInt();
- // Keyword XYZI
- readKey();
- next_(2);
- // Count of voxels in first pack
- var numVoxels = readInt()
- var vol = [];
- // Voxels
- for (var k = 0; k < numVoxels; ++k) {
- var x = readByte();
- var y = readByte();
- var z = readByte();
- var col= readByte();
- setVal(vol, x, y, z, col);
- // setVal(vol, x, y, z, palette[col - 1]);
- }
- }
- // Skip unloaded chunks
- for (var i = 1; i < numPacks; i++) {
- next_(9);
- var numVoxels = readInt()
- next_(numVoxels);
- }
- // Palette
- resultPalette = [];
- if (readKey() != "RGBA"){
- resultPalette = standartPallete;
- }else {
- next_(2);
- for (var k = 0; k < 255; ++k) {
- resultPalette.push(readColor(r, c))
- next_();
- }
- }
- return [vol, sizeX, sizeY, sizeZ]
- }
- var surr = [
- [new Vector(0, 1, 0), new Vector(1, 0, 0), new Vector(0, 0, 1)],
- [new Vector(1, 0, 0), new Vector(0, 1, 0), new Vector(0, 0, 1)],
- [new Vector(0, 0, 1), new Vector(1, 0, 0), new Vector(0, 1, 0)],
- [new Vector(0, 0, 1), new Vector(0, 1, 0), new Vector(1, 0, 0)],
- [new Vector(1, 0, 0), new Vector(0, 0, 1), new Vector(0, 1, 0)],
- [new Vector(0, 1, 0), new Vector(0, 0, 1), new Vector(1, 0, 0)]
- ];
- function makeChunks(vol) {
- var o = []; // Checked points
- var chunks = []; // Array of bounds of chunks
- var findLine = function(x, y, z, dir1, col) {
- var p = new Vector(x, y, z);
- var b2 = new Vector(x, y, z);
- if (!col) col = getVal(vol, p.x, p.y, p.z);
- while (getVal(vol, p.x, p.y, p.z) && col == getVal(vol, p.x, p.y, p.z)) {
- b2 = p.clone();
- p = p.add(dir1);
- }
- return b2;
- }
- var findPlane = function(x, y, z, dir1, dir2, col) {
- var p = new Vector(x, y, z);
- var line = findLine(p.x, p.y, p.z, dir1, col);
- var fst_line = line.clone()
- var nxt = line
- while ((p.subtract(nxt)).length() > 0 && nxt.x >= fst_line.x && nxt.y >= fst_line.y && nxt.z >= fst_line.z) {
- p = p.add(dir2);
- line = nxt
- nxt = findLine(p.x, p.y, p.z, dir1, col);
- }
- // Result must be not greater then first
- line.x = (dir1.x > 0) ? fst_line.x : line.x;
- line.y = (dir1.y > 0) ? fst_line.y : line.y;
- line.z = (dir1.z > 0) ? fst_line.z : line.z;
- return line
- }
- var findBox = function(x, y, z, dir1, dir2, dir3, col) {
- var p = new Vector(x, y, z);
- var plane = findPlane(p.x, p.y, p.z, dir1, dir2, col);
- var fst_plane = plane.clone()
- var nxt = plane
- while ((p.subtract(nxt)).length() > 0 && nxt.x >= fst_plane.x && nxt.y >= fst_plane.y && nxt.z >= fst_plane.z) {
- p = p.add(dir3);
- plane = nxt
- nxt = findPlane(p.x, p.y, p.z, dir1, dir2, col);
- }
- // Result must be not greater then first
- plane.x = (dir1.x > 0 || dir2.x > 0) ? fst_plane.x : plane.x;
- plane.y = (dir1.y > 0 || dir2.y > 0) ? fst_plane.y : plane.y;
- plane.z = (dir1.z > 0 || dir2.z > 0) ? fst_plane.z : plane.z;
- return plane
- }
- var pushChunk = function(b1, b2, col) {
- chunks.push([b1, b2, col]);
- // Add color
- if (colNum.indexOf(col) == -1) {
- colNum.push(col)
- }
- for (var z = b1.z; z <= b2.z; z++) {
- for (var y = b1.y; y <= b2.y; y++) {
- for (var x = b1.x; x <= b2.x; x++) {
- setVal(o, x, y, z, true);
- }
- }
- }
- }
- /////////////////////////////////////////////
- /////////////////////////////////////////////
- /////////////////////////////////////////////
- for (var z = 0; z < 16; z++) {
- for (var x = 0; x < 16; x++) {
- for (var y = 0; y < 16; y++) {
- if (getVal(o, x, y, z) == true) continue;
- var orig = new Vector(x, y, z);
- var p = new Vector(x, y, z);
- var col = getVal(vol, p.x, p.y, p.z);
- if (!col) continue;
- var maxVolume = 0;
- var maxBox;
- for (var i = 0; i < surr.length; i++) {
- var box = findBox(x, y, z, surr[i][0], surr[i][1], surr[i][2], col);
- var uniBox = box.subtract(orig);
- uniBox = uniBox.add(1)
- var scale = uniBox.x * uniBox.y * uniBox.z;
- if (scale > maxVolume) {
- maxVolume = scale;
- maxBox = box;
- }
- }
- pushChunk(orig, maxBox, col);
- }
- }
- }
- return chunks;
- }
- // read big-endian (network byte order) unsigned 32-bit int from data, at offset
- function readUInt32(data, offset) {
- return ((data.charCodeAt(offset) & 0xFF)) +
- ((data.charCodeAt(offset + 1) & 0xFF) << 8) +
- ((data.charCodeAt(offset + 2) & 0xFF) << 16) +
- ((data.charCodeAt(offset + 3) & 0xFF) << 24);
- }
- function readColor(data, offset) {
- return ((data.charCodeAt(offset) & 0xFF) << 16) +
- ((data.charCodeAt(offset + 1) & 0xFF) << 8) +
- ((data.charCodeAt(offset + 2) & 0xFF));
- }
- function setVal(arr, x, y, z, v) {
- if (!v) return;
- if (!arr[z]) arr[z] = [];
- if (!arr[z][y]) arr[z][y] = [];
- arr[z][y][x] = v;
- }
- function getVal(arr, x, y, z) {
- if (!arr) return null;
- if (!arr[z]) return null;
- if (!arr[z][y]) return null;
- return arr[z][y][x];
- }
- function vToStr(v) {
- return "[" + v.x + "," + v.y + "," + v.z + "]"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement