Source: models/TriangleList.js

  1. /**
  2. * @fileOverview
  3. * @author David Gossow - dgossow@willowgarage.com
  4. */
  5. /**
  6. * A TriangleList is a THREE object that can be used to display a list of triangles as a geometry.
  7. *
  8. * @constructor
  9. * @param options - object with following keys:
  10. *
  11. * * material (optional) - the material to use for the object
  12. * * vertices - the array of vertices to use
  13. * * colors - the associated array of colors to use
  14. */
  15. ROS3D.TriangleList = function(options) {
  16. options = options || {};
  17. var material = options.material || new THREE.MeshBasicMaterial();
  18. var vertices = options.vertices;
  19. var colors = options.colors;
  20. THREE.Object3D.call(this);
  21. // set the material to be double sided
  22. material.side = THREE.DoubleSide;
  23. // construct the geometry
  24. var geometry = new THREE.Geometry();
  25. for (i = 0; i < vertices.length; i++) {
  26. geometry.vertices.push(new THREE.Vector3(vertices[i].x, vertices[i].y, vertices[i].z));
  27. }
  28. // set the colors
  29. var i, j;
  30. if (colors.length === vertices.length) {
  31. // use per-vertex color
  32. for (i = 0; i < vertices.length; i += 3) {
  33. var faceVert = new THREE.Face3(i, i + 1, i + 2);
  34. for (j = i * 3; j < i * 3 + 3; i++) {
  35. var color = new THREE.Color();
  36. color.setRGB(colors[i].r, colors[i].g, colors[i].b);
  37. faceVert.vertexColors.push(color);
  38. }
  39. geometry.faces.push(faceVert);
  40. }
  41. material.vertexColors = THREE.VertexColors;
  42. } else if (colors.length === vertices.length / 3) {
  43. // use per-triangle color
  44. for (i = 0; i < vertices.length; i += 3) {
  45. var faceTri = new THREE.Face3(i, i + 1, i + 2);
  46. faceTri.color.setRGB(colors[i / 3].r, colors[i / 3].g, colors[i / 3].b);
  47. geometry.faces.push(faceTri);
  48. }
  49. material.vertexColors = THREE.FaceColors;
  50. } else {
  51. // use marker color
  52. for (i = 0; i < vertices.length; i += 3) {
  53. var face = new THREE.Face3(i, i + 1, i + 2);
  54. geometry.faces.push(face);
  55. }
  56. }
  57. geometry.computeBoundingBox();
  58. geometry.computeBoundingSphere();
  59. geometry.computeFaceNormals();
  60. this.add(new THREE.Mesh(geometry, material));
  61. };
  62. ROS3D.TriangleList.prototype.__proto__ = THREE.Object3D.prototype;
  63. /**
  64. * Set the color of this object to the given hex value.
  65. *
  66. * @param hex - the hex value of the color to set
  67. */
  68. ROS3D.TriangleList.prototype.setColor = function(hex) {
  69. this.mesh.material.color.setHex(hex);
  70. };