public static Ray traceRay(Vector3 rayDirection, ARRaytracer.OpticalSystem optics, RuntimeGizmoDrawer drawer = null) { //Debug.Log(optics.eyePosition.ToString("G3")); Vector3 sphereSpaceRayOrigin = optics.worldToSphereSpace.MultiplyPoint(optics.eyePosition); Vector3 sphereSpaceRayDirection = (optics.worldToSphereSpace.MultiplyPoint(optics.eyePosition + rayDirection) - sphereSpaceRayOrigin); sphereSpaceRayDirection = sphereSpaceRayDirection / sphereSpaceRayDirection.magnitude; float intersectionTime = ARRaytracer.intersectLineSphere(sphereSpaceRayOrigin, sphereSpaceRayDirection, Vector3.zero, 0.5f * 0.5f, false); if (intersectionTime < 0f) { Debug.Log("bad ray...."); return(new Ray()); } Vector3 sphereSpaceIntersection = sphereSpaceRayOrigin + (intersectionTime * sphereSpaceRayDirection); //Ellipsoid Normals Vector3 sphereSpaceNormal = -sphereSpaceIntersection / sphereSpaceIntersection.magnitude; sphereSpaceNormal = new Vector3( sphereSpaceNormal.x / Mathf.Pow(optics.ellipseMinorAxis / 2f, 2f), sphereSpaceNormal.y / Mathf.Pow(optics.ellipseMinorAxis / 2f, 2f), sphereSpaceNormal.z / Mathf.Pow(optics.ellipseMajorAxis / 2f, 2f)); sphereSpaceNormal /= sphereSpaceNormal.magnitude; Vector3 worldSpaceIntersection = optics.sphereToWorldSpace.MultiplyPoint(sphereSpaceIntersection); Vector3 worldSpaceNormal = optics.sphereToWorldSpace.MultiplyVector(sphereSpaceNormal); worldSpaceNormal /= worldSpaceNormal.magnitude; Ray firstBounce = new Ray(worldSpaceIntersection, Vector3.Reflect(rayDirection, worldSpaceNormal)); if (drawer != null) { //float halfDistance = constantDistance - Vector3.Distance(optics.eyePosition, firstBounce.origin); drawer.DrawLine(optics.eyePosition, firstBounce.origin); drawer.DrawLine(firstBounce.origin, firstBounce.origin + (firstBounce.direction * 0.5f)); //drawer.DrawSphere(firstBounce.origin + (firstBounce.direction * halfDistance), 0.001f); } // we just need the first bounce return(firstBounce); }
public void OnDrawRuntimeGizmos(RuntimeGizmoDrawer drawer) { Quaternion beginningRot = transform.rotation; if (curFocalPoint.ContainsNaN()) { curFocalPoint = Vector3.zero; } averagedInterSectionPoints = new List <Vector3>(); ARRaytracer.OpticalSystem optics = raytracer.optics; curDrawing = 0; for (float yRot = -30f; yRot < 30f; yRot += 5f) { for (float xRot = -20f; xRot < 70f; xRot += 5f) { //for (float yRot = -0f; yRot < 25f; yRot += 5f) { // for (float xRot = 5f; xRot < 35f; xRot += 5f) { List <Ray> intersectRayList = new List <Ray>(); for (float i = 0f; i < 91; i += 90) { transform.rotation = Quaternion.Euler(0, 0, i);// Quaternion.Euler(xRot, yRot, i); Vector3 convergencePoint = transform.position + ((Vector3.forward) + (Vector3.up * -xRot * 0.01f) + (Vector3.right * yRot * 0.01f)) * focalDistance; optics.eyePosition = transform.position + (transform.right * 0.001f); Ray firstBounceRay = traceRay((convergencePoint - optics.eyePosition).normalized, optics, whichToDraw == curDrawing ? drawer : null); intersectRayList.Add(firstBounceRay); transform.rotation = Quaternion.Euler(0, 0, i + 180f); //Quaternion.Euler(xRot, yRot, i+180f); optics.eyePosition = transform.position + (transform.right * 0.001f); firstBounceRay = traceRay((convergencePoint - optics.eyePosition).normalized, optics, whichToDraw == curDrawing ? drawer : null); intersectRayList.Add(firstBounceRay); } Vector3 intersectSum = Vector3.zero; int numPoints = 0; for (int i = 0; i < (intersectRayList.Count - 1); i += 2) { Vector3 tmp = Fit.ClosestPointOnRayToRay(intersectRayList[i], intersectRayList[i + 1]); drawer.color = i == 0 ? LeapColor.coral : LeapColor.forest; drawer.DrawSphere(tmp, 0.0005f); intersectSum += tmp; numPoints++; } Vector3 ptAverage = intersectSum / numPoints; averagedInterSectionPoints.Add(ptAverage); /*for (int i = 0; i < (intersectRayList.Count - 1); i += 2) { * Vector3 tmp = ClosestPointOnRayToRay(intersectRayList[i], intersectRayList[i + 1]); * drawer.color = LeapColor.red; * drawer.DrawLine(tmp, ptAverage); * }*/ //Debug.Log("Average: " + ptAverage); curDrawing++; } } optics.eyePosition = raytracer.eyePerspective.transform.position; //drawer.color = LeapColor.periwinkle; //for (int i = 0; i < averagedInterSectionPoints.Count; i++) drawer.DrawSphere(averagedInterSectionPoints[i], .0005f); Vector3 position = Vector3.zero; Vector3 normal = Vector3.zero; Fit.Plane(averagedInterSectionPoints, out position, out normal, 200, drawer); newScreen.position = Vector3.ProjectOnPlane(newScreen.position - position, normal) + position; newScreen.rotation = Quaternion.FromToRotation(newScreen.forward, normal) * newScreen.rotation; //log the continous fit to the console ///Debug.Log("Position is " + position + " normal is " + normal); transform.rotation = beginningRot; Vector2 CornerOneUV = ARRaytracer.DisplayUVToRenderUV(new Vector2(0f, 0f), optics, 80); Vector2 CornerTwoUV = ARRaytracer.DisplayUVToRenderUV(new Vector2(1f, 0f), optics, 80); Vector2 CornerThreeUV = ARRaytracer.DisplayUVToRenderUV(new Vector2(0f, 1f), optics, 80); Vector2 CornerFourUV = ARRaytracer.DisplayUVToRenderUV(new Vector2(1f, 1f), optics, 80); drawer.matrix = Matrix4x4.identity; traceRay(ARRaytracer.ViewportPointToRayDirection(new Vector3(CornerOneUV.x, CornerOneUV.y, 1f), raytracer.eyePerspective.transform.position, optics.clipToWorld), optics, drawer); traceRay(ARRaytracer.ViewportPointToRayDirection(new Vector3(CornerTwoUV.x, CornerTwoUV.y, 1f), raytracer.eyePerspective.transform.position, optics.clipToWorld), optics, drawer); traceRay(ARRaytracer.ViewportPointToRayDirection(new Vector3(CornerThreeUV.x, CornerThreeUV.y, 1f), raytracer.eyePerspective.transform.position, optics.clipToWorld), optics, drawer); traceRay(ARRaytracer.ViewportPointToRayDirection(new Vector3(CornerFourUV.x, CornerFourUV.y, 1f), raytracer.eyePerspective.transform.position, optics.clipToWorld), optics, drawer); }