Pen Settings



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


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


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.


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.


<!doctype html>
    <title>Two-barn starter</title>
      body { max-width: 100%; }
      /* feel free to style the canvas any way you want. If you want it to
      use the entire window, set width: 100% and height: 100%. */
      canvas {
          width: 100%;
          height: 500px;

  <h1>Simple Three.js Exercise: Two-barn starter</h1>





                // The "classic" barn, converted to THREE.js by Jos Dirksen, with thanks.
function createBarnVertices(w, h, len) {
    var barnGeometry = new THREE.Geometry();
    // add the front
    barnGeometry.vertices.push(new THREE.Vector3(0, 0, 0));
    barnGeometry.vertices.push(new THREE.Vector3(w, 0, 0));
    barnGeometry.vertices.push(new THREE.Vector3(w, h, 0));
    barnGeometry.vertices.push(new THREE.Vector3(0, h, 0));
    barnGeometry.vertices.push(new THREE.Vector3(0.5 * w, h + 0.5 * w, 0));

    // just add the back also manually
    barnGeometry.vertices.push(new THREE.Vector3(0, 0, -len));
    barnGeometry.vertices.push(new THREE.Vector3(w, 0, -len));
    barnGeometry.vertices.push(new THREE.Vector3(w, h, -len));
    barnGeometry.vertices.push(new THREE.Vector3(0, h, -len));
    barnGeometry.vertices.push(new THREE.Vector3(0.5 * w, h + 0.5 * w, -len));

    return barnGeometry;

function createBarnFaces(barnGeometry) {
    // now that we've got the vertices we need to define the faces.
    // front faces
    barnGeometry.faces.push(new THREE.Face3(0, 1, 2));
    barnGeometry.faces.push(new THREE.Face3(0, 2, 3));
    barnGeometry.faces.push(new THREE.Face3(3, 2, 4));

    // back faces
    barnGeometry.faces.push(new THREE.Face3(5, 7, 6));
    barnGeometry.faces.push(new THREE.Face3(5, 8, 7));
    barnGeometry.faces.push(new THREE.Face3(7, 8, 9));

    // roof faces.
    barnGeometry.faces.push(new THREE.Face3(3, 4, 8));
    barnGeometry.faces.push(new THREE.Face3(4, 9, 8));
    barnGeometry.faces.push(new THREE.Face3(2, 7, 9));
    barnGeometry.faces.push(new THREE.Face3(4, 2, 9));

    // side faces
    barnGeometry.faces.push(new THREE.Face3(6, 2, 1));
    barnGeometry.faces.push(new THREE.Face3(7, 2, 6));
    barnGeometry.faces.push(new THREE.Face3(0, 3, 5));
    barnGeometry.faces.push(new THREE.Face3(3, 8, 5));

    // floor faces
    barnGeometry.faces.push(new THREE.Face3(0, 5, 1));
    barnGeometry.faces.push(new THREE.Face3(5, 6, 1));

    // calculate the normals for shading

    return barnGeometry;

// Here's our translation function
function translateX(vertices, deltax) {
    var len = vertices.length;
    for (var i = 0; i < len; i++) {
        vertices[i].x += deltax;

// We always need a scene
var scene = new THREE.Scene();

// ====================================================================

var barnWidth = 50;
var barnHeight = 30;
var barnDepth = 40;

var barn1geom = createBarnVertices(barnWidth, barnHeight, barnDepth);

// the createMesh function adds a "demo" material to the geometry

var barn1mesh = TW.createMesh(barn1geom);

// the scene is the set of things to render, so add the barn.


// ADD CODE HERE to create a half-size barn and add it to the scene //

// ================================================================
// We always need a renderer

var renderer = new THREE.WebGLRenderer();

TW.mainInit(renderer, scene);

// MODIFY CODE BELOW to adjust the bounding box for the two barns

               {minx: 0, maxx: 20,
                miny: 0, maxy: barnHeight, // a bit low
                minz: -barnDepth, maxz: 0});