cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Untitled

A Pen By Captain Anonymous

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r69/three.min.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://threejs.org/examples/js/controls/TrackballControls.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>
<div id="renderHere"></div>
<button id="download">Export Binary STL</button>
            
          
!
            
              body { margin: 0; } canvas { width: 100%; height: 100% }

button {
  position: absolute;
  top: 10px;
  left: 10px;
}
            
          
!
            
              var scene = new THREE.Scene();
var controls = null;
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);

document.getElementById("renderHere").appendChild(renderer.domElement);

				var geometry = new THREE.CylinderGeometry( 0, 10, 30, 4, 1 );
				var material =  new THREE.MeshLambertMaterial( { color:0xffffff, shading: THREE.FlatShading } );

				light = new THREE.PointLight( 0xffffff );
				light.position.set( 10, 8, 5 );
				scene.add( light );

				light = new THREE.PointLight( 0x002288 );
				light.position.set( -10, -5, 2 );
				scene.add( light );
				light = new THREE.AmbientLight( 0x222222 );
				scene.add( light );

var cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 50;
camera.position.y = 10;
controls = new THREE.TrackballControls( camera );

function render() {
  requestAnimationFrame(render);
  controls.update()
  renderer.render(scene, camera);
}

render();

$("#download").click(function() {
  BinaryStlWriter.save(geometry, "download.stl");
});

var BinaryStlWriter = (function () {
    var that = {};

    var writeVector = function (dataview, offset, vector, isLittleEndian) {
        offset = writeFloat(dataview, offset, vector.x, isLittleEndian);
        offset = writeFloat(dataview, offset, vector.y, isLittleEndian);
        return writeFloat(dataview, offset, vector.z, isLittleEndian);
    };

    var writeFloat = function (dataview, offset, float, isLittleEndian) {
        dataview.setFloat32(offset, float, isLittleEndian);
        return offset + 4;
    };

    var geometryToDataView = function (geometry) {
        var tris = geometry.faces;
        var verts = geometry.vertices;

        var isLittleEndian = true; // STL files assume little endian, see wikipedia page

        var bufferSize = 84 + (50 * tris.length);
        var buffer = new ArrayBuffer(bufferSize);
        var dv = new DataView(buffer);
        var offset = 0;

        offset += 80; // Header is empty

        dv.setUint32(offset, tris.lengtb, isLittleEndian);
        offset += 4;

        for (var n = 0; n < tris.length; n++) {
            offset = writeVector(dv, offset, tris[n].normal, isLittleEndian);
            offset = writeVector(dv, offset, verts[tris[n].a], isLittleEndian);
            offset = writeVector(dv, offset, verts[tris[n].b], isLittleEndian);
            offset = writeVector(dv, offset, verts[tris[n].c], isLittleEndian);
            offset += 2; // unused 'attribute byte count' is a Uint16
        }

        return dv;
    };

    var save = function (geometry, filename) {
        var dv = geometryToDataView(geometry);
        var blob = new Blob([dv], {
            type: 'application/octet-binary'
        });

        // FileSaver.js defines `saveAs` for saving files out of the browser
        saveAs(blob, filename);
    };

    that.save = save;
    return that;
}());
            
          
!
999px
Loading ..................

Console