Advertisement
Guest User

aaaaahtrrht

a guest
Jun 30th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class experiments : MonoBehaviour
  5. {
  6. public float Radius = 1.0f;
  7. public Transform Sphere;
  8. public Transform Camera;
  9.  
  10. public Transform P0;
  11. public Transform P1;
  12. public Transform P2;
  13. public Transform P3;
  14.  
  15. bool LiangBarsky(float edgeLeft, float edgeRight, float edgeBottom, float edgeTop, // Define the x/y clipping values for the border.
  16. float x0src, float y0src, float x1src, float y1src, // Define the start and end points of the line.
  17. ref float x0clip, ref float y0clip, ref float x1clip, ref float y1clip) // The output values, so declare these outside.
  18. {
  19.  
  20. float t0 = 0.0f;
  21. float t1 = 1.0f;
  22. float xdelta = x1src - x0src;
  23. float ydelta = y1src - y0src;
  24. float p = 0;
  25. float q = 0;
  26. float r = 0;
  27.  
  28. for (int edge = 0; edge < 4; edge++)
  29. {
  30. // Traverse through left, right, bottom, top edges.
  31. if (edge == 0) { p = -xdelta; q = -(edgeLeft - x0src); }
  32. if (edge == 1) { p = xdelta; q = (edgeRight - x0src); }
  33. if (edge == 2) { p = -ydelta; q = -(edgeBottom - y0src); }
  34. if (edge == 3) { p = ydelta; q = (edgeTop - y0src); }
  35. r = q / p;
  36. if (p == 0 && q < 0) return false; // Don't draw line at all. (parallel line outside)
  37.  
  38. if (p < 0)
  39. {
  40. if (r > t1) return false; // Don't draw line at all.
  41. else if (r > t0) t0 = r; // Line is clipped!
  42. }
  43. else if (p > 0)
  44. {
  45. if (r < t0) return false; // Don't draw line at all.
  46. else if (r < t1) t1 = r; // Line is clipped!
  47. }
  48. }
  49.  
  50. x0clip = x0src + t0 * xdelta;
  51. y0clip = y0src + t0 * ydelta;
  52. x1clip = x0src + t1 * xdelta;
  53. y1clip = y0src + t1 * ydelta;
  54.  
  55. return true; // (clipped) line is drawn
  56. }
  57.  
  58. bool PackedLiangBarsky(float edgeLeft, float edgeRight, float edgeBottom, float edgeTop, // Define the x/y clipping values for the border.
  59. Vector2 src0, Vector2 src1, // Define the start and end points of the line.
  60. ref Vector2 clip0, ref Vector2 clip1) // The output values, so declare these outside.
  61. {
  62. float t0 = 0.0f;
  63. float t1 = 1.0f;
  64. Vector2 delta = src1 - src0;
  65.  
  66. for (int edge = 0; edge < 4; edge++)
  67. {
  68. float p = 0;
  69. float q = 0;
  70. float r = 0;
  71. // Traverse through left, right, bottom, top edges.
  72. if (edge == 0) { p = -delta.x; q = -(edgeLeft - src0.x); }
  73. if (edge == 1) { p = delta.x; q = (edgeRight - src0.x); }
  74. if (edge == 2) { p = -delta.y; q = -(edgeBottom - src0.y); }
  75. if (edge == 3) { p = delta.y; q = (edgeTop - src0.y); }
  76. r = q / p;
  77. if (p == 0 && q < 0) return false; // Don't draw line at all. (parallel line outside)
  78.  
  79. if (p < 0)
  80. {
  81. if (r > t1) return false; // Don't draw line at all.
  82. else if (r > t0) t0 = r; // Line is clipped!
  83. }
  84. else if (p > 0)
  85. {
  86. if (r < t0) return false; // Don't draw line at all.
  87. else if (r < t1) t1 = r; // Line is clipped!
  88. }
  89. }
  90.  
  91. clip0 = src0 + t0 * delta;
  92. clip1 = src0 + t1 * delta;
  93.  
  94. return true; // (clipped) line is drawn
  95. }
  96.  
  97. void OnDrawGizmos()
  98. {
  99. Vector3 result = Vector3.zero;
  100.  
  101.  
  102. Gizmos.DrawLine(P0.position, P1.position);
  103. Gizmos.DrawLine(P2.position, P3.position);
  104.  
  105. Vector2 clip1 = Vector2.zero;
  106. Vector2 clip2 = Vector2.zero;
  107. Vector2 clip3 = Vector2.zero;
  108. Vector2 clip4 = Vector2.zero;
  109. PackedLiangBarsky(-1, 1, -1, 1, new Vector2(P0.position.x, P0.position.z), new Vector2(P1.position.x, P1.position.z), ref clip1, ref clip2);
  110. gzmo.drawPoint(new Vector3(clip1.x, 0, clip1.y));
  111. gzmo.drawPoint(new Vector3(clip2.x, 0, clip2.y));
  112.  
  113. PackedLiangBarsky(-1, 1, -1, 1, new Vector2(P0.position.x, P0.position.y), new Vector2(P1.position.x, P1.position.y), ref clip3, ref clip4);
  114. gzmo.drawPoint(new Vector3(clip3.x, clip3.y, 0));
  115. gzmo.drawPoint(new Vector3(clip4.x, clip4.y, 0));
  116.  
  117. //PackedLiangBarsky(-1, 1, -1, 1, new Vector2(P0.position.y, P0.position.z), new Vector2(P1.position.y, P1.position.z), ref clip1, ref clip2);
  118. //gzmo.drawPoint(new Vector3(0, clip1.x, clip1.y));
  119. //gzmo.drawPoint(new Vector3(0, clip2.x, clip2.y));
  120.  
  121.  
  122. gzmo.drawPoint(Camera.position);
  123.  
  124.  
  125. Gizmos.DrawRay(Camera.position, Camera.forward * 5);
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement