Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<div id="dialog-message" title="Wrong file">
  text
</div>


<form action='#' onsubmit="return false;" onchange='updateOutput();'>
  <h1>Convert Options</h1>
  <div class="inset">
    <p>
      <input type='file' id='fileinput' onchange='loadFile();' title="<p>Chose <b>.vox</b> file from <b>MagicaVoxel</b>, or drag&drop it.</p><p class='grey'>If file was changed, script reload it automaticaly</p>">
    </p>
    <div class="hiddenIntro">
      <p title="<p>Split whole wolume on states</p><p class='grey'>Use if you want make button</p>">
        <input type="checkbox" name="splitToStates" id="splitToStates">
        <label for="splitToStates">Split to states</label>
      </p>
      <p>
        <label for="modelLabel">Model Label</label>
        <input type="text" name="modelLabel" id="modelLabel" value="new 3d model">
      </p>
      <p>
        <label for="modelTip">Model tooltip</label>
        <input type="text" name="modelTip" id="modelTip" value="">
      </p>
      <p>
        <div class="dblock">
          <input type="checkbox" name="emitRedstone" id="emitRedstone">
          <label for="emitRedstone">Emit redstone</label>
        </div>
        <div class="dblock">
          <input type="checkbox" name="buttonMode" id="buttonMode">
          <label for="buttonMode">Button mode</label>
        </div>
        <label for="lightLevel">Light level</label>
        <input type="number" name="lightLevel" id="lightLevel" min="0" max="15" value=0>
      </p>
    </div>
  </div>
  <div class="inset">
    <p class="p-container">
      <!--<input type="submit" name="go" id="go" value="Log in">-->
      <div id="texCol">
        <input id="texture" type="text" name="texture" value="iron_block">
        <input id="colPicker" type="color" value="#ffffff">
      </div>
    </p>
  </div>
  <span id="copyright">Done by Krutoy242 for <a href="http://computercraft.ru/topic/768-konvertirovanie-modelei-iz-vox-dlia-3d-printov/">Computercraft.ru</a> | <a href="https://oc.cil.li/index.php?/topic/599-magicavoxel-vox-converter-for-print3d/">Official forum post</a></span>
</form>
<form class="output hiddenIntro">
  <div class="inset">
    <h1 title="<p>Remember, there cant be more then 24 chunks per model</p><p class='grey'>Excess chunks will be colored in red</p>">Chunks: <b><span id="ChunksCount"></span></b></h1>
    <textarea id="result" cols="80" rows="40"></textarea>
  </div>
</form>
              
            
!

CSS

              
                * { box-sizing: border-box; padding:0; margin: 0; }

body {
	font-family: "HelveticaNeue-Light","Helvetica Neue Light","Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
  color:white;
  font-size:12px;
  background:#333;
  width:100%;
  height:100%;
}

.hiddenIntro{
  display:none;
}

/*********************************************/
/* TOOLTIPS */
/*********************************************/
.ui-tooltip {
  background: #222;
  color: white;
  border: none;
  padding: 0;
  opacity: 1;
  border-radius: 0.4em;
  
  border:1px solid #191919;
  box-shadow: none;
}

.ui-tooltip-content {
  position: relative;
  padding: 1em;
}
.ui-tooltip-content p {
  margin-bottom: 0.5em;
}
.ui-tooltip-content p.grey {
  color: grey;
}
.no-close .ui-dialog-titlebar-close {
  display: none;
}
/*********************************************/
/*********************************************/

form {
 	background:#111; 
  width:350px;
  margin:10px;
  border-radius:0.4em;
  border:1px solid #191919;
  overflow:hidden;
  position:relative;
  float:left;
  box-shadow: 0 5px 10px 5px rgba(0,0,0,0.2);
  display: block;
}

.output{
  width:auto;
  padding-right:10px;
}

form:after {
  content:"";
  display:block;
  position:absolute;
  height:1px;
  width:100px;
  left:20%;
  background:linear-gradient(left, #111, #444, #b6b6b8, #444, #111);
  top:0;
}

form:before {
 	content:"";
  display:block;
  position:absolute;
  width:8px;
  height:5px;
  border-radius:50%;
  left:34%;
  top:-7px;
  box-shadow: 0 0 6px 4px #fff;
}

.inset {
 	padding:10px; 
  border-top:1px solid #19191a;
}

h1 {
  font-size:18px;
  text-shadow:0 1px 0 black;
  text-align:center;
  padding:10px 0;
  border-bottom:1px solid rgba(0,0,0,1);
  position:relative;
}

h1:after {
 	content:"";
  display:block;
  width:250px;
  height:100px;
  position:absolute;
  top:0;
  left:50px;
  pointer-events:none;
  transform:rotate(70deg);
  -webkit-transform: rotate(70deg);
  background:linear-gradient(50deg, rgba(255,255,255,0.15), rgba(0,0,0,0));
   background-image: -webkit-linear-gradient(50deg, rgba(255,255,255,0.05), rgba(0,0,0,0)); /* For Safari */

}

.output h1{
  text-align:left;
}

label {
 	color:#666;
  display:block;
  padding-bottom:3px;
}

textarea,
input[type=text],
input[type=password],
input[type=file],
input[type=number],
input[type=color]{
 	width:100%;
  padding:4px 5px;
  background:linear-gradient(#3e4248, #4e5258);
  border:1px solid #222;
  box-shadow:
    0 1px 0 rgba(255,255,255,0.1);
  border-radius:0.3em;
  margin-bottom:15px;
}

input[type=file] {
  padding:0px 0px;
}

input[type=number] {
  width:50px;
}

label[for=emitRedstone],
label[for=splitToStates],
label[for=buttonMode]{
 	color:white;
  display:inline-block;
  padding-bottom:0;
  padding:10px 0;
}

input[type=checkbox] {
 	display:inline-block;
  margin-right:10px;
}
#texCol input{
  margin-bottom:5px;
}

#texCol input[type=text] {
 	width:80%;
}
#texCol input[type=color] {
 	width:18%;
  float:right;
  padding:0;
  border:0;
  background:0;
}

.dblock{
  display:inline-block;
  padding-right:30px;
}
.p-container {
 	padding:0 20px 20px 20px; 
}

.p-container:after {
 	clear:both;
  display:table;
  content:"";
}

#copyright{
  display:block;
  float:right;
  color:#222222;
  padding-top:8px;
}

a:link, a:visited {color: #003566;}
a:hover   {color: #005cb3;}
a:active  {color: #001a33;}


#ChunksCount{
  color:#0d93ff;
}
#ChunksCount .red{
  color:#ff0d0d;
}

input[type=text]:hover,
input[type=password]:hover,
label:hover ~ input[type=text],
label:hover ~ input[type=password] {
 	background:#27292c;
}

.output textarea{
  margin:5px;
}
              
            
!

JS

              
                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 + "]"
}
              
            
!
999px

Console