I am having an issue where the editor eventually bugs my project and the plane calculations (projectedEv1&2) creep downward. Copying the assets folder into a new project works without any changes in the code base. This is how I am using rays and planes in my codebase. Think of it as finding the vertices to walls (as they are seen to the camera) in a doom style fashion where the segment buffer is the edges of the floor – but it is a visibility polygon.
for (int i = 2; i < segmentBuffer.Value.Length; i++)
{
VisibilitySegment s = segmentBuffer.Value[i];
float2 v1 = (float2)s.x;
float2 v2 = (float2)s.y;
float2 ev1 = (float2)ExtendVertex(v1, origin);
float2 ev2 = (float2)ExtendVertex(v2, origin);
Vector3 v1V3 = new Vector3(v1.x, 0, v1.y);
Vector3 v2V3 = new Vector3(v2.x, 0, v2.y);
Vector3 ev1V3 = new Vector3(ev1.x, 0, ev1.y);
Vector3 ev2V3 = new Vector3(ev2.x, 0, ev2.y);
Vector3 planeNormal = CalculatePlaneNormal(v1V3, v2V3);
Plane p = new Plane(planeNormal, v1V3);
//Vector3 projectedEv1V3 = ProjectOntoPlane(v.CamPosition, ev1V3, p);
//Vector3 projectedEv2V3 = ProjectOntoPlane(v.CamPosition, ev2V3, p);
int baseIndex = vertices.Count;
float depth1 = Vector3.Distance(originV3, v1V3);
float depth2 = Vector3.Distance(originV3, v2V3);
Vector2 v1V2 = new Vector2(v1V3.x, v1V3.z);
Vector2 v2V2 = new Vector2(v2V3.x, v2V3.z);
Vector2 originV2 = new Vector2(originV3.x, originV3.z);
Vector2 depthV1 = v1V2 - originV2;
Vector2 depthV2 = v2V2 - originV2;
Color color1 = new Color(depth1, 0, 0, 0.5f);
Color color2 = new Color(depth2, 0, 0, 0.5f);
Vector3 projectedEv1V3 = v1V3 + Vector3.up * 25f;
Vector3 projectedEv2V3 = v2V3 + Vector3.up * 25f;
Plane plane = new Plane(v1V3, projectedEv1V3, v2V3);
float enter;
Vector3 tempv = (float3)(ev1V3) - v.CamPosition;
Ray ray = new Ray(v.CamPosition, tempv.normalized);
float3 cameraForward = math.normalize(v.Fwd);
float dotProduct = math.dot(ev1V3 - originV3, cameraForward);
int right = i - 1;
int left = i + 1;
//Debug.DrawLine(v1V3, projectedEv1V3, Color.red);
//Debug.DrawLine(projectedEv1V3, v2V3, Color.red);
//Debug.DrawLine(v2V3, v1V3, Color.red);
if (left == segmentBuffer.Value.Length)
{
left = 0;
}
if (dotProduct >= 0)
{
/* V1 is on the right s.x
* V2 is on the left s.y
*
*/
if (true)
{
/*
*(vr)
* |-----| (vother)
* v2 (v1)
* y x
*/
double2 vother = segmentBuffer.Value[right].y;
double2 temp = vother - v1;
if (math.abs(temp.x) < 1e-3d && math.abs(temp.y) < 1e-3d)
{
}
else
{
if (plane.Raycast(ray, out enter))
{
projectedEv1V3 = ray.GetPoint(enter);
if (s.specialRender == true)
{
if (math.length(v2 - v1) < 1)
{
double2 vr = segmentBuffer.Value[left].x;
temp = vr - v2;
if (math.abs(temp.x) < 1e-3d && math.abs(temp.y) < 1e-3d)
{
Vector3 vright = new Vector3((float)vr.x, 0f, (float)vr.y);
//if (math.length(vright - originV3) < math.length(v1V3 - originV3))
//{
//Debug.DrawLine(projectedEv2V3, vright);
if (true)
{
Vector3 votherright = new Vector3((float)segmentBuffer.Value[left].y.x, 0f, (float)segmentBuffer.Value[left].y.y);
Plane rightPlane = new Plane(vright, votherright, vright + Vector3.up * 25f);
tempv = (float3)(projectedEv1V3) - v.CamPosition;
ray = new Ray(v.CamPosition, tempv.normalized);
if (rightPlane.Raycast(ray, out enter))
{
Vector3 r1 = ray.GetPoint(enter);
tempv = (float3)(v1V3 + Vector3.up * 25f) - v.CamPosition;
ray = new Ray(v.CamPosition, tempv.normalized);
if (rightPlane.Raycast(ray, out enter))
{
Vector3 r2 = ray.GetPoint(enter);
//Debug.DrawLine(r1, r2, Color.magenta);
tempv = (float3)(v1V3) - v.CamPosition;
ray = new Ray(v.CamPosition, tempv.normalized);
if (rightPlane.Raycast(ray, out enter))
{
Vector3 r3 = ray.GetPoint(enter);
//Debug.DrawLine(r2, r3, Color.white);
projectedEv1V3 = v1V3 + Vector3.up * 25f;
vertices.AddRange(new Vector3[] { r1, r2, r3 });
baseIndex = vertices.Count;
colors.AddRange(new Color[] { color1, color1, color1 });
// Add quad triangles
triangles.AddRange(new int[] {
baseIndex, baseIndex + 2, baseIndex + 1,});
//Debug.DrawLine(r3, r1, Color.red);
}
}
}
}
}
}
}
}
}
}
}
dotProduct = math.dot(ev2V3 - originV3, cameraForward);
if (dotProduct >= 0)
{
if (true)
{
double2 vo = segmentBuffer.Value[left].x;
double2 temp = vo - v2;
if (math.abs(temp.x) < 1e-3d && math.abs(temp.y) < 1e-3d)
{
}
else
{
ray = new Ray(v.CamPosition, (float3)ev2V3 - v.CamPosition);
if (plane.Raycast(ray, out enter))
{
projectedEv2V3 = ray.GetPoint(enter);
if (s.specialRender == true)
{
if (math.length(v2 - v1) < 1)
{
double2 vl = segmentBuffer.Value[right].y;
temp = vl - v1;
if (math.abs(temp.x) < 1e-3d && math.abs(temp.y) < 1e-3d)
{
Vector3 vleft = new Vector3((float)vl.x, 0f, (float)vl.y);
//if (math.length(vleft - originV3) < math.length(v2V3 - originV3))
//{
//Debug.DrawLine(projectedEv2V3, vleft);
if (true)
{
Vector3 votherleft = new Vector3((float)segmentBuffer.Value[right].x.x, 0f, (float)segmentBuffer.Value[right].x.y);
Plane leftPlane = new Plane(votherleft, vleft, vleft + Vector3.up * 25f);
tempv = (float3)projectedEv2V3 - v.CamPosition;
ray = new Ray(v.CamPosition, tempv.normalized);
//Debug.DrawLine(votherleft, projectedEv2V3);
//Debug.DrawRay(ray.origin, ray.direction);
if (leftPlane.Raycast(ray, out enter))
{
Vector3 l1 = ray.GetPoint(enter);
//Debug.DrawLine(l1, projectedEv2V3);
tempv = ((float3)(v2V3 + Vector3.up * 25f) - v.CamPosition);
ray = new Ray(v.CamPosition, tempv.normalized);
if (leftPlane.Raycast(ray, out enter))
{
Vector3 l2 = ray.GetPoint(enter);
//Debug.DrawLine(l1, l2, Color.magenta);
tempv = (float3)(v2V3) - v.CamPosition;
ray = new Ray(v.CamPosition, tempv.normalized);
if (leftPlane.Raycast(ray, out enter))
{
Vector3 l3 = ray.GetPoint(enter);
//Debug.DrawLine(l2, l3, Color.white);
projectedEv2V3 = v2V3 + Vector3.up * 25f;
vertices.AddRange(new Vector3[] { l1, l2, l3 });
baseIndex = vertices.Count;
colors.AddRange(new Color[] { color1, color2, color2 });
// Add quad triangles
triangles.AddRange(new int[] {
baseIndex, baseIndex + 2, baseIndex + 1,});
// Debug.DrawLine(l3, l1, Color.red);
}
}
}
}
}
}
}
}
}
}
}
Debug.DrawLine(projectedEv1V3, v1V3, Color.red);
Debug.DrawLine(projectedEv2V3, v2V3, Color.red);
Debug.DrawLine(v1V3, projectedEv2V3, Color.yellow);
baseIndex = vertices.Count;
// Add quad vertices
vertices.AddRange(new Vector3[] { v1V3, projectedEv1V3, projectedEv2V3, v2V3 });
colors.AddRange(new Color[] { color1, color1, color2, color2 });
// Add quad triangles
triangles.AddRange(new int[] {
baseIndex, baseIndex + 1, baseIndex + 2,
baseIndex, baseIndex + 2, baseIndex + 3
});