Example #1
0
    parse: function (response) {
        var geometry = new THREE.Geometry();
        _.each(response.points, function (v) {
            geometry.vertices.push(new THREE.Vector3(v[0], v[1], v[2]));
        });
        _.each(response.trilist, function (tl) {
            geometry.faces.push(new THREE.Face3(tl[0], tl[1], tl[2]));
        });
        var material;
        var result;
        var that = this;
        if (response.tcoords) {
            // this mesh has a texture - grab it
            var textureURL = this.get('server').map('textures/' +
                                                    this.id);
            material = new THREE.MeshPhongMaterial(
                {
                    map: THREE.ImageUtils.loadTexture(
                        textureURL, new THREE.UVMapping(),
                        function() {
                            that.trigger("textureSet");
                        } )
                }
            );
            // We expect per-vertex texture coords only. Three js has per
            // face tcoords, so we need to handle the conversion.
            // First - generate all the tcoords in a list
            var tcs = [];
            _.each(response.tcoords, function (tc) {
                tcs.push(new THREE.Vector2(tc[0], tc[1]));
            });
            // now index into them to build up the per-face uvs THREE js
            // uses
            var t; // the indices for the triangle in question
            for (var i = 0; i < geometry.faces.length; i++) {
                t = geometry.faces[i];
                geometry.faceVertexUvs[0].push(
                    [tcs[t.a], tcs[t.b], tcs[t.c]])
            }
            result = {
                t_mesh: new THREE.Mesh(geometry, material),
                texture: material,
                textureOn: true
            };
        } else {
            // default to basic Phong lighting
            result = {
                t_mesh: new THREE.Mesh(geometry, basicMaterial)
            };
        }
        // clean up the vertices
        geometry.mergeVertices();
        geometry.computeCentroids();
        // needed for lighting to work
        geometry.computeFaceNormals();
        geometry.computeVertexNormals();
        geometry.computeBoundingSphere();

        return result;
    }
Example #2
0
  worker.addEventListener('message', function (event) {
    var data = JSON.parse(event.data);
    var targets = [];

    var geometry = new THREE.Geometry();

    geometry.vertices = data.vertices.map(function (vertex) {
      var target = new THREE.Vector3(0, 0, _.random(-7, 7));
      targets.push(target);
      return new THREE.Vector3(vertex.x, vertex.y, vertex.z);
    });

    geometry.faces = data.faces.map(function (face) {
      return new THREE.Face3(face.a, face.b, face.c);
    });

    geometry.morphTargets.push({
      name: 'test',
      vertices: targets
    });

    data.faceVertexUvs.forEach(function (faceVertexUV) {
      var uvs = [
        new THREE.Vector2(faceVertexUV[0][0], faceVertexUV[0][1]),
        new THREE.Vector2(faceVertexUV[1][0], faceVertexUV[1][1]),
        new THREE.Vector2(faceVertexUV[2][0], faceVertexUV[2][1]),
        new THREE.Vector2(faceVertexUV[3][0], faceVertexUV[3][1])
      ];

      geometry.faceVertexUvs[0].push(uvs);
    });

    geometry.mergeVertices();
    geometry.computeFaceNormals();
    geometry.computeVertexNormals();
    geometry.computeMorphNormals();
    geometry.computeBoundingBox();

    worker.terminate();

    cb(null, {
      main: geometry,
      wireframe: geometry.clone()
    });
  });
Example #3
0
plasm.Model = function (complex, viewer) {
  if (!(this instanceof plasm.Model)) {
    return new plasm.Model(complex, viewer);
  }

  var complex = complex || new simplexn.SimplicialComplex();
  var pointset = complex.pointset;
  var topology = complex.topology;
  var dim = topology.dim;
  var cells, n_cells, i_cell;
  var v1, v2, v3; 

  var geometry = new THREE.Geometry();
  var material;
  var mesh;

  if (dim <= 0) {
    cells = topology.cells0d();
    n_cells = cells.length;

    for (i_cell = 0; i_cell < n_cells; i_cell += 1) {
      v1 = pointset.get(cells[i_cell]);
      geometry.vertices.push(new THREE.Vector3(v1[0] || 0, v1[1] || 0, v1[2] || 0));
    }

    material = new plasm.materials.PointMaterial();
    mesh = new THREE.ParticleSystem(geometry, material);
  }

  if (dim === 1) {
    cells = topology.complexes[1];
    n_cells = cells.length;

    for (i_cell = 0; i_cell < n_cells; i_cell += 2) {
      v1 = pointset.get(cells[i_cell + 0]);
      v2 = pointset.get(cells[i_cell + 1]);
      geometry.vertices.push(new THREE.Vector3(v1[0], v1[1], v1[2]));
      geometry.vertices.push(new THREE.Vector3(v2[0], v2[1], v2[2]));
    }

    material = new plasm.materials.LineMaterial();
    mesh = new THREE.Line(geometry, material, THREE.LinePieces);
  }

  if (dim >= 2) {
    cells = topology.complexes[2];
    n_cells = cells.length;

    pointset.forEach(function (v) {
      geometry.vertices.push(new THREE.Vector3(v[0] || 0, v[1] || 0, v[2] || 0));
    });
  
    for (i_cell = 0; i_cell < n_cells; i_cell += 3) {
      geometry.faces.push(new THREE.Face3(
        cells[i_cell + 0], cells[i_cell + 1], cells[i_cell + 2]
      ));
      if (! plasm.DRAW_SINGLE_SIDE) {
        geometry.faces.push(new THREE.Face3(
        cells[i_cell + 2], cells[i_cell + 1], cells[i_cell + 0]
      ));
      }
    }

    geometry.computeCentroids();
    geometry.mergeVertices();
    geometry.computeFaceNormals();

    material = new plasm.materials.MeshMaterial();
    mesh = new THREE.Mesh(geometry, material);
  }

  this.complex = complex;
  this.geometry = geometry;
  this.geometry.dynamic = true;
  this.material = material;
  this.material.side = plasm.DRAW_SINGLE_SIDE ? THREE.FrontSide : THREE.DoubleSide;
  this.mesh = mesh;
  this.mesh.matrixAutoUpdate = true;
  this.viewer = viewer;
};