LinesDrawingManager.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var LinesDrawingManager = {
  2. meshEdgeLines: undefined,
  3. rotateLine: undefined,
  4. rotateLineGeometry: new THREE.Geometry(),
  5. rotateLineMesh: new MeshLine(),
  6. clipLine: undefined,
  7. clipLineMaterial: new THREE.MeshBasicMaterial({ color: new THREE.Color(0x00FA9A) }),
  8. clipLineMultiplyMatrix: new THREE.Matrix4().set(1, 0, 0, 0,
  9. 0, 0, 1, 0,
  10. 0, -1, 0, 0,
  11. 0, 0, 0, 1),
  12. initialMatrix: new THREE.Matrix4(),
  13. clipLineObject3D: new THREE.Object3D(),
  14. initLine: function () {
  15. this.initActiveLine();
  16. this.initRotateLine();
  17. this.initClipLine();
  18. scene.add(this.rotateLine);
  19. scene.add(this.meshEdgeLines);
  20. scene.add(this.clipLine);
  21. this.hideAll();
  22. },
  23. initActiveLine: function () {
  24. let activeLineGeometry = new THREE.BufferGeometry();
  25. let activeLineMaterial = new THREE.LineBasicMaterial({
  26. color: 0x00FA9A,
  27. clippingPlanes: []
  28. });
  29. this.meshEdgeLines = new THREE.Line(activeLineGeometry, activeLineMaterial);
  30. },
  31. showMeshEdgeLines: function (points, clippingPlanes) {
  32. if (points.length > 1) {
  33. points.push(points[0]);
  34. this.hideMeshEdgeLines();
  35. this.meshEdgeLines.material.clippingPlanes = clippingPlanes;
  36. this.meshEdgeLines.geometry.setFromPoints(points);
  37. this.meshEdgeLines.visible = true;
  38. }
  39. },
  40. hideMeshEdgeLines: function () {
  41. this.meshEdgeLines.visible = false;
  42. },
  43. initRotateLine: function () {
  44. let rotateLineMaterial = new MeshLineMaterial({
  45. useMap: false,
  46. color: new THREE.Color(0xFFFF00),
  47. opacity: 1,
  48. lineWidth: 5
  49. });
  50. this.rotateLine = new THREE.Mesh(this.rotateLineMesh.geometry, rotateLineMaterial);
  51. },
  52. showRotateLine: function (rotateLine) {
  53. this.rotateLineGeometry.vertices.splice(0, this.rotateLineGeometry.vertices.length);
  54. this.rotateLineGeometry.vertices.push(rotateLine.start, rotateLine.end);
  55. this.rotateLineMesh.geometry.dispose();
  56. this.rotateLineMesh.setGeometry(this.rotateLineGeometry);
  57. this.rotateLine.visible = true;
  58. },
  59. hideRotateLine: function () {
  60. this.rotateLine.visible = false;
  61. },
  62. initClipLine: function () {
  63. var meshGeometry = new THREE.CylinderBufferGeometry();
  64. this.clipLine = new THREE.Mesh(meshGeometry, this.clipLineMaterial);
  65. },
  66. showClipLine: function (start, end) {
  67. this.updateClipLineByTwoPoints(start, end);
  68. this.clipLine.visible = true;
  69. },
  70. hideClipLine: function () {
  71. this.clipLine.visible = false;
  72. },
  73. hideAll: function () {
  74. this.hideClipLine();
  75. this.hideRotateLine();
  76. this.hideMeshEdgeLines();
  77. },
  78. //threejs two point draw a cylinder
  79. //https://stackoverflow.com/questions/15316127/three-js-line-vector-to-cylinder
  80. updateClipLineByTwoPoints: function (start, end) {
  81. let height = end.distanceTo(start);
  82. this.clipLine.geometry.dispose();
  83. var meshGeometry = new THREE.CylinderBufferGeometry(2, 2, height, 8, 1);
  84. meshGeometry.userData = end;
  85. this.clipLine.geometry = meshGeometry;
  86. this.clipLine.matrix.lookAt(start, end, this.clipLineObject3D.up);
  87. this.clipLine.matrix.multiply(this.clipLineMultiplyMatrix);
  88. this.clipLine.applyMatrix(this.initialMatrix);
  89. // position based on midpoints - there may be a better solution than this
  90. this.clipLine.position.set((end.x + start.x) / 2, (end.y + start.y) / 2, (end.z + start.z) / 2);
  91. }
  92. };