Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class experiments : MonoBehaviour
- {
- public float Radius = 1.0f;
- public Transform Sphere;
- public Transform Camera;
- public Transform P0;
- public Transform P1;
- public Transform P2;
- public Transform P3;
- bool LiangBarsky(float edgeLeft, float edgeRight, float edgeBottom, float edgeTop, // Define the x/y clipping values for the border.
- float x0src, float y0src, float x1src, float y1src, // Define the start and end points of the line.
- ref float x0clip, ref float y0clip, ref float x1clip, ref float y1clip) // The output values, so declare these outside.
- {
- float t0 = 0.0f;
- float t1 = 1.0f;
- float xdelta = x1src - x0src;
- float ydelta = y1src - y0src;
- float p = 0;
- float q = 0;
- float r = 0;
- for (int edge = 0; edge < 4; edge++)
- {
- // Traverse through left, right, bottom, top edges.
- if (edge == 0) { p = -xdelta; q = -(edgeLeft - x0src); }
- if (edge == 1) { p = xdelta; q = (edgeRight - x0src); }
- if (edge == 2) { p = -ydelta; q = -(edgeBottom - y0src); }
- if (edge == 3) { p = ydelta; q = (edgeTop - y0src); }
- r = q / p;
- if (p == 0 && q < 0) return false; // Don't draw line at all. (parallel line outside)
- if (p < 0)
- {
- if (r > t1) return false; // Don't draw line at all.
- else if (r > t0) t0 = r; // Line is clipped!
- }
- else if (p > 0)
- {
- if (r < t0) return false; // Don't draw line at all.
- else if (r < t1) t1 = r; // Line is clipped!
- }
- }
- x0clip = x0src + t0 * xdelta;
- y0clip = y0src + t0 * ydelta;
- x1clip = x0src + t1 * xdelta;
- y1clip = y0src + t1 * ydelta;
- return true; // (clipped) line is drawn
- }
- bool PackedLiangBarsky(float edgeLeft, float edgeRight, float edgeBottom, float edgeTop, // Define the x/y clipping values for the border.
- Vector2 src0, Vector2 src1, // Define the start and end points of the line.
- ref Vector2 clip0, ref Vector2 clip1) // The output values, so declare these outside.
- {
- float t0 = 0.0f;
- float t1 = 1.0f;
- Vector2 delta = src1 - src0;
- for (int edge = 0; edge < 4; edge++)
- {
- float p = 0;
- float q = 0;
- float r = 0;
- // Traverse through left, right, bottom, top edges.
- if (edge == 0) { p = -delta.x; q = -(edgeLeft - src0.x); }
- if (edge == 1) { p = delta.x; q = (edgeRight - src0.x); }
- if (edge == 2) { p = -delta.y; q = -(edgeBottom - src0.y); }
- if (edge == 3) { p = delta.y; q = (edgeTop - src0.y); }
- r = q / p;
- if (p == 0 && q < 0) return false; // Don't draw line at all. (parallel line outside)
- if (p < 0)
- {
- if (r > t1) return false; // Don't draw line at all.
- else if (r > t0) t0 = r; // Line is clipped!
- }
- else if (p > 0)
- {
- if (r < t0) return false; // Don't draw line at all.
- else if (r < t1) t1 = r; // Line is clipped!
- }
- }
- clip0 = src0 + t0 * delta;
- clip1 = src0 + t1 * delta;
- return true; // (clipped) line is drawn
- }
- void OnDrawGizmos()
- {
- Vector3 result = Vector3.zero;
- Gizmos.DrawLine(P0.position, P1.position);
- Gizmos.DrawLine(P2.position, P3.position);
- Vector2 clip1 = Vector2.zero;
- Vector2 clip2 = Vector2.zero;
- Vector2 clip3 = Vector2.zero;
- Vector2 clip4 = Vector2.zero;
- 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);
- gzmo.drawPoint(new Vector3(clip1.x, 0, clip1.y));
- gzmo.drawPoint(new Vector3(clip2.x, 0, clip2.y));
- 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);
- gzmo.drawPoint(new Vector3(clip3.x, clip3.y, 0));
- gzmo.drawPoint(new Vector3(clip4.x, clip4.y, 0));
- //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);
- //gzmo.drawPoint(new Vector3(0, clip1.x, clip1.y));
- //gzmo.drawPoint(new Vector3(0, clip2.x, clip2.y));
- gzmo.drawPoint(Camera.position);
- Gizmos.DrawRay(Camera.position, Camera.forward * 5);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement