ClipMeshUpgrader.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var lineAB = new THREE.Vector3(0, 0, 0);
  2. var lineBC = new THREE.Vector3(0, 0, 0);
  3. var crossResult = new THREE.Vector3(0, 0, 0);
  4. // update view
  5. async function refreshClipMesh(plane, mesh, imageName, callback) {
  6. let points = LineIntersectionHelper.getLineIntersectionPoints(
  7. EdgesManager.getCubeEdges(),
  8. plane
  9. );
  10. if (points.length < 3) {
  11. return false;
  12. }
  13. //get clipPlaneData From WPF
  14. var clipPlaneData = await getClipPlaneData();
  15. if (clipPlaneData.ErrorCode != 1000) {
  16. console.error("get data error :" + clipPlaneData.ErrorCode);
  17. return false;
  18. } else {
  19. updateMesh();
  20. return true;
  21. }
  22. async function getClipPlaneData() {
  23. //convert points
  24. convertToVTKPoints(points);
  25. let mappingPoints = [];
  26. points.forEach((item) =>
  27. mappingPoints.push(
  28. new cefMappingPoint(
  29. Math.abs(item.x),
  30. Math.abs(item.y),
  31. Math.abs(item.z)
  32. )
  33. )
  34. );
  35. let inputData = {
  36. PointsList: mappingPoints,
  37. ClipImageName: imageName,
  38. };
  39. // let json = jsObj.GetClipPlaneData(
  40. // OSHelper.convertCefParameter(inputData));
  41. let json = await Dart_getClipPlaneData(JSON.stringify(inputData));
  42. // window.alert(json);
  43. return JSON.parse(json);
  44. }
  45. function updateMesh() {
  46. //get verticePoints
  47. convertBackToWorldPoints(clipPlaneData.WorldPoints);
  48. let verticesPoints = [];
  49. clipPlaneData.WorldPoints.forEach((item) =>
  50. verticesPoints.push(new THREE.Vector3(item.X, item.Y, item.Z))
  51. );
  52. //get imageVectors
  53. var imageVectors = [];
  54. clipPlaneData.ImagePoints.forEach((item) =>
  55. imageVectors.push(new THREE.Vector2(item.X, item.Y))
  56. );
  57. LoaderUtility.loadBase64Texture(clipPlaneData.ImageData, (texture) => {
  58. mesh.material.uniforms.tDiffuse.value.dispose();
  59. mesh.material.uniforms.tDiffuse.value = texture;
  60. updateMeshGeometry();
  61. if (callback != undefined) {
  62. callback();
  63. }
  64. });
  65. ///更新Mesh
  66. function updateMeshGeometry() {
  67. mesh.geometry.vertices = verticesPoints;
  68. mesh.geometry.faces = [];
  69. mesh.geometry.faceVertexUvs[0] = [];
  70. lineAB.subVectors(verticesPoints[1], verticesPoints[0]);
  71. lineBC.subVectors(verticesPoints[2], verticesPoints[1]);
  72. crossResult.crossVectors(lineAB, lineBC);
  73. let dotResult = crossResult.dot(plane.normal);
  74. let faceId = 0;
  75. for (let i = 2; i < verticesPoints.length; i++) {
  76. if (dotResult <= 0) {
  77. setMeshGeometryFace(0, i - 1, i);
  78. } else {
  79. setMeshGeometryFace(0, i, i - 1);
  80. }
  81. }
  82. mesh.material.needsUpdate = true;
  83. mesh.geometry.elementsNeedUpdate = true;
  84. mesh.geometry.computeBoundingSphere();
  85. ClipPlaneManager.drawActiveMeshEdges();
  86. function setMeshGeometryFace(x, y, z) {
  87. mesh.geometry.faces.push(new THREE.Face3(x, y, z));
  88. mesh.geometry.faceVertexUvs[0][faceId++] = [
  89. imageVectors[x],
  90. imageVectors[y],
  91. imageVectors[z],
  92. ];
  93. }
  94. }
  95. }
  96. }