ClipPlaneMover.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. let moveSpeed, movePlane, moveLine, moveStartVector, moveEndVector;
  2. //初始化平移切面参数
  3. function initMoveClipPlaneArgs() {
  4. moveSpeed = 5;
  5. movePlane = new THREE.Plane();
  6. moveLine = new THREE.Line3();
  7. moveStartVector = new THREE.Vector3();
  8. moveEndVector = new THREE.Vector3();
  9. }
  10. //平移切面
  11. function moveClipPlane(currentMouse, lastMouse) {
  12. let distance = getMoveDistance(currentMouse, lastMouse);
  13. if (distance == undefined || !canMoveForward(distance)) {
  14. return;
  15. }
  16. let newConstant = ClipPlaneManager.activeMeshInfo.Mesh.Plane.constant + distance;
  17. movePlane.set(ClipPlaneManager.activeMeshInfo.Mesh.Plane.normal, newConstant);
  18. refreshClipMesh(movePlane,
  19. ClipPlaneManager.activeMeshInfo.Mesh.Object,
  20. ClipPlaneManager.activeMeshInfo.Mesh.ImageName,
  21. function () {
  22. if (ClipPlaneManager.activeMeshInfo != undefined) {
  23. ClipPlaneManager.activeMeshInfo.Mesh.Plane.set(movePlane.normal, newConstant);
  24. addRotateInfo();
  25. }
  26. EventCenter.emit("updateLastMousePos", currentMouse);
  27. });
  28. }
  29. //计算平移距离
  30. function getMoveDistance(currentMouse, lastMouse) {
  31. setMouseVector(OSHelper.clientX(currentMouse), OSHelper.clientY(currentMouse), moveEndVector);
  32. setMouseVector(lastMouse.x, lastMouse.y, moveStartVector);
  33. moveLine.set(moveStartVector, moveEndVector);
  34. let distance = moveLine.distance() * moveSpeed;
  35. if (distance <= 2) {
  36. return undefined;
  37. }
  38. if (moveEndVector.clone().sub(moveStartVector).dot(ClipPlaneManager.activeMeshInfo.Mesh.Plane.normal) > 0) {
  39. distance = -distance;
  40. }
  41. return distance;
  42. }
  43. //判断切面是否移动到了最远点
  44. function canMoveForward(moveDistance) {
  45. if (ClipPlaneManager.activeMeshInfo.Mesh != undefined) {
  46. if (moveDistance > 0 ||
  47. ClipPlaneManager.activeMeshInfo.Mesh.Plane.constant + moveDistance >
  48. ClipPlaneManager.activeMeshInfo.MaxForwardDistance) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. //平面鼠标坐标转三维坐标
  55. function setMouseVector(mouseX,mouseY, MouseVector) {
  56. MouseVector.set((mouseX / window.innerWidth) * 2 - 1,
  57. 1 - (mouseY / window.innerHeight) * 2, 0.5);
  58. MouseVector.unproject(camera);
  59. }