Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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

              
                <script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejs.org/examples/js/controls/TransformControls.js"></script>
<div id='container'></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                var scene, camera, renderer;
var geometry, material, mesh;
var orbitControl, transformControl;
var intersections;
var t1, v10, v11, v12; // triangle 1 and its vertices
var t2, v20, v21, v22; // triangle 2 and its vertices
var loi;

v10 = new THREE.Vector3(0, 0, 0);
v11 = new THREE.Vector3(300, 0, 0);
v12 = new THREE.Vector3(300, 100, 300);
v20 = new THREE.Vector3(0, 100, 0);
v21 = new THREE.Vector3(300, 100, 0);
v22 = new THREE.Vector3(300, 0, 300);

// v10 = new THREE.Vector3(rando(), rando(), rando());
// v11 = new THREE.Vector3(rando(), rando(), rando());
// v12 = new THREE.Vector3(rando(), rando(), rando());
// v20 = new THREE.Vector3(rando(), rando(), rando());
// v21 = new THREE.Vector3(rando(), rando(), rando());
// v22 = new THREE.Vector3(rando(), rando(), rando());

function rando() {
  return Math.round(Math.random() * 100) * 10;
}

class Triangle {
  constructor(v0, v1, v2, hex = 0xffffff) {
    this.v0 = v0;
    this.v1 = v1;
    this.v2 = v2;
    this.hex = hex;
    
    this.vertices = [];
    this.triangleVertices = [v0.clone(), v1.clone(), v2.clone()];
    this.edges = [[0, 1], [1, 2], [2, 0]];
    this.vertexGeometry = new THREE.Object3D();
    this.addVertex(v0.clone());
    this.addVertex(v1.clone());
    this.addVertex(v2.clone());
    this.addVertex(new THREE.Vector3());
    this.addVertex(new THREE.Vector3());
    
    this.t = new THREE.Triangle();
    // this.t.set(this.v0, this.v1, this.v2);
    this.t.setFromPointsAndIndices(this.vertices, 0, 1, 2);
    this.obj = new THREE.Object3D();
  }
  
  initNormalHelper(direction, origin, hex = 0xffffff, length = 55) {
    // let direction = this.t.normal();
    // let origin = this.t.midpoint();
    this.obj.add(new THREE.ArrowHelper(
      direction,
      origin,
      length,
      hex
    ));
  }
  
  initObject() {
    this.bufferVertices = new Float32Array([
      this.v0.toArray(),
      this.v1.toArray(),
      this.v2.toArray(),
      this.v0.toArray()
    ].reduce(function(a, b) { // flatten array
      return a.concat(b); 
    }));
    
    this.geometry = new THREE.BufferGeometry();
    this.geometry.addAttribute(
      'position', 
      new THREE.BufferAttribute(this.bufferVertices, 3)
    );
    
    // this.geometry = new THREE.Geometry();
    // this.geometry.vertices.push(this.v0, this.v1, this.v2, this.v0);
    
    this.material = new THREE.LineBasicMaterial({
      color: this.hex
    });
    
    this.lines = new THREE.Line(this.geometry, this.material);
    this.obj.add(this.lines);
  }
  
  initMesh() {
    this.polygonGeometry = new THREE.BufferGeometry();
    // this.polygonGeometry.addAttribute(
    //   'position', 
    //   new THREE.BufferAttribute(this.bufferVertices, 3)
    // );
    let material = new THREE.MeshBasicMaterial({
      color: this.hex,
      side: THREE.DoubleSide
    });
    this.polygon = new THREE.Mesh(this.polygonGeometry, material);
    this.obj.add(this.polygon);
  }
  
  initTransformControl() {
    let obj = new THREE.Object3D();
    obj.position.copy(this.t.midpoint());
    let origin = this.t.midpoint();
    let transformControl = new THREE.TransformControls(camera, renderer.domElement);
    transformControl.space = 'local';
    transformControl.addEventListener('change', render);
    transformControl.attach(this.obj);
    obj.add(transformControl);
    this.transformControl = obj;
  }
  
  static intersect(t1, t2) {
    let a, b;
    let [a1, b1] = t1.intersects(t2);
    let [a2, b2] = t2.intersects(t1);
    if (!a1 || !b1 || !a2 || !b2) {
      return;
    }
    if (t2.t.containsPoint(a1) && t1.t.containsPoint(b2)) {
      a = a1;
      b = b2;
      // return new THREE.Line3(a1, b2);
    }
    if (t1.t.containsPoint(a2) && t2.t.containsPoint(b1)) {
      a = a2;
      b = b1;
      // return new THREE.Line3(a2, b1);
    }
    if (t2.t.containsPoint(a1) && t2.t.containsPoint(b1)) {
      a = a1;
      b = b1;
      // return new THREE.Line3(a1, b1);
    }
    if (t1.t.containsPoint(a2) && t1.t.containsPoint(b2)) {
      a = a2;
      b = b2;
      // return new THREE.Line3(a2, b2);
    }
    if (a && b) {
      t1.addVertex(a);
      t1.addVertex(b);
      t2.addVertex(a);
      t2.addVertex(b);
      return new THREE.Line3(a, b);
    }
  }
  
  intersects(t2) {
    let intersectionVertices = [];
    let intersections = [];
    let plane = t2.t.plane();
    this.edges.forEach((edge, i) => {
      let a = this.vertices[edge[0]];
      let b = this.vertices[edge[1]];
      let line = new THREE.Line3(a, b);
      let intersection = plane.intersectLine(line);
      if (intersection) {
        intersections.push(intersection);
      }
    });
    return intersections;
  }
  
  getIndicesOfVisibleVertices(t2) {
    let indices = [];
    this.vertices.forEach((vertex, i) => {
      // let v = vertex.clone().applyMatrix4(this.obj.matrixWorld);
      if (t2.t.plane().distanceToPoint(vertex) >= -0.1) {
        indices.push(i);
      //   t1.vertexGeometry.children[i].visible = false;
      // } else {
      //   geometry.vertices.push(vertex.clone());
      }
    });
    return indices;
  }
  
  update(t2) {
    this.vertices[0].copy(this.v0)
      .applyMatrix4(this.obj.matrixWorld);
    this.vertices[1].copy(this.v1)
      .applyMatrix4(this.obj.matrixWorld);
    this.vertices[2].copy(this.v2)
      .applyMatrix4(this.obj.matrixWorld);
    
    this.t.setFromPointsAndIndices(this.vertices, 0, 1, 2);
    
    let indices = this.getIndicesOfVisibleVertices(t2);
    this.polygonPositions = new Float32Array(indices.length * 3 * 3);
    for (let i = 1; i < indices.length - 1; i++) {
      let a = this.vertices[indices[0]];
      let b = this.vertices[indices[i]];
      let c = this.vertices[indices[i + 1]];
      let _i = i - 1;
      this.polygonPositions[_i + 0] = a.x;
      this.polygonPositions[_i + 1] = a.y;
      this.polygonPositions[_i + 2] = a.z;
      this.polygonPositions[_i + 3] = b.x;
      this.polygonPositions[_i + 4] = b.y;
      this.polygonPositions[_i + 5] = b.z;
      this.polygonPositions[_i + 6] = c.x;
      this.polygonPositions[_i + 7] = c.y;
      this.polygonPositions[_i + 8] = c.z;
    };
    // console.log(this.polygonPositions);
    this.polygonGeometry.removeAttribute('position');
    this.polygonGeometry.addAttribute('position', new THREE.BufferAttribute(this.polygonPositions, 3));
    
    while(this.vertices.length > 3) {
      this.popVertex();
    }
  }
  
  popVertex() {
    this.vertices.pop();
    // this.vertexGeometry.pop(); // Let's recycle the geometry instead
    for (let i = this.vertices.length - 1; i < this.vertexGeometry.children.length; i++) {
      this.vertexGeometry.children[i].visible = false;
    }
  }
  
  addVertex(vertex) {
    this.vertices.push(vertex);
    // Keep the number of vertex geometry in sync with the number of vertices.
    if (this.vertices.length === this.vertexGeometry.children.length + 1) {
      this.vertexGeometry.add(sphere(this.hex));
    }
  }
  
  showVertices(t2) {
    let indices = this.getIndicesOfVisibleVertices(t2);
    this.vertices.forEach((vertex, i) => {
      let object3D = this.vertexGeometry.children[i];
      if (~indices.indexOf(i)) {
        object3D.visible = true;
      } else {
        object3D.visible = false;
      }
      object3D.position.copy(vertex)
        .applyMatrix4(this.obj.matrixWorld);
    });
  }
}

function sphere(color = 0x888888) {
  var obj = new THREE.Object3D();
  var geometry = new THREE.SphereGeometry(5, 8, 6);
  var material = new THREE.MeshBasicMaterial({
    color: color
  });
  // Set initial position in two flavors
  // geometry.applyMatrix(new THREE.Matrix4().makeTranslation(
  //   position.x, 
  //   position.y, 
  //   position.z
  // ));
  // geometry.translate(position.x, position.y, position.z);
  var sphere = new THREE.Mesh(geometry, material);
  obj.add(sphere);
  obj.visible = false;
  // scene.add(obj);
  return obj;
}

function line() {
  let geometry = new THREE.Geometry();
  geometry.vertices.push(new THREE.Vector3(), new THREE.Vector3());
  let material = new THREE.LineBasicMaterial({
    color: 0xffffff,
    linewidth: 5
  });
  let line = new THREE.Line(geometry, material);
  line.visible = false;
  return line;
}

function init() {
  scene = new THREE.Scene();
  
  camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
  camera.position.z = 1000;
  
  // Renderer
  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  
  t1 = new Triangle(v10, v11, v12, 0xff00ff);
  t1.initObject();
  scene.add(t1.initMesh());
  t1.initNormalHelper(t1.t.normal(), t1.t.midpoint());
  t1.initTransformControl();
  scene.add(t1.vertexGeometry);
  // t1.showVertices(t2);
  scene.add(t1.obj);
  scene.add(t1.transformControl);
  
  t2 = new Triangle(v20, v21, v22, 0xffa500);
  t2.initObject();
  scene.add(t2.initMesh());
  t2.initNormalHelper(t2.t.normal(), t2.t.midpoint());
  t2.initTransformControl();
  scene.add(t2.vertexGeometry);
  // t2.showVertices(t1);
  scene.add(t2.obj);
  scene.add(t2.transformControl);
  
  loi = line();
  scene.add(loi);
  
  orbitControl = new THREE.OrbitControls(camera, renderer.domElement);
  
  var container =  document.getElementById('container');
  container.appendChild(renderer.domElement);
}

function animate() {
  requestAnimationFrame(animate);
  
  t1.update(t2);
  t2.update(t1);
  
  let _loi = Triangle.intersect(t1, t2);
  if (_loi) {
    loi.visible = true;
    loi.geometry.vertices = [_loi.start, _loi.end];
    loi.geometry.verticesNeedUpdate = true;
  } else {
    loi.visible = false;
  }
  
  t1.showVertices(t2);
  t2.showVertices(t1);
  // scene.add(polygon);
  render();
}

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

init();
animate();
              
            
!
999px

Console