void PointLightShadowGenerationCheckAddFaces(ref Vector3 lightPosition, ref Sphere meshBoundingSphere, bool[] result) { if (pointLightShadowGenerationPlanes == null) { pointLightShadowGenerationPlanes = new Plane[6][]; for (int nFace = 0; nFace < 6; nFace++) { PointLightFace face = PointLightFace.PositiveX; //flipped switch (nFace) { case 0: face = PointLightFace.NegativeY; break; case 1: face = PointLightFace.PositiveY; break; case 2: face = PointLightFace.PositiveZ; break; case 3: face = PointLightFace.NegativeZ; break; case 4: face = PointLightFace.PositiveX; break; case 5: face = PointLightFace.NegativeX; break; } var planes = GetPlanesForPointLightShadowGeneration(/* Vec3.Zero, */ face); pointLightShadowGenerationPlanes[nFace] = planes; } } var sphereLocalOrigin = meshBoundingSphere.Origin - lightPosition; for (int nFace = 0; nFace < 6; nFace++) { var planes = pointLightShadowGenerationPlanes[nFace]; bool add = true; for (int n = 0; n < planes.Length; n++) { double distance = planes[n].GetDistance(sphereLocalOrigin); // meshSphere.Origin ); if (distance < -meshBoundingSphere.Radius) { add = false; break; } } result[nFace] = add; } }
static Plane[] GetPlanesForPointLightShadowGeneration(/*Vec3 lightPosition, */ PointLightFace face) { Vector3[] points = new Vector3[4]; switch (face) { case PointLightFace.PositiveX: points[0] = new Vector3(1, -1, -1); points[1] = new Vector3(1, -1, 1); points[2] = new Vector3(1, 1, 1); points[3] = new Vector3(1, 1, -1); break; case PointLightFace.NegativeX: points[0] = new Vector3(-1, 1, -1); points[1] = new Vector3(-1, 1, 1); points[2] = new Vector3(-1, -1, 1); points[3] = new Vector3(-1, -1, -1); break; case PointLightFace.PositiveY: points[0] = new Vector3(1, 1, -1); points[1] = new Vector3(1, 1, 1); points[2] = new Vector3(-1, 1, 1); points[3] = new Vector3(-1, 1, -1); break; case PointLightFace.NegativeY: points[0] = new Vector3(-1, -1, -1); points[1] = new Vector3(-1, -1, 1); points[2] = new Vector3(1, -1, 1); points[3] = new Vector3(1, -1, -1); break; case PointLightFace.PositiveZ: points[0] = new Vector3(-1, 1, 1); points[1] = new Vector3(1, 1, 1); points[2] = new Vector3(1, -1, 1); points[3] = new Vector3(-1, -1, 1); break; case PointLightFace.NegativeZ: points[0] = new Vector3(-1, -1, -1); points[1] = new Vector3(1, -1, -1); points[2] = new Vector3(1, 1, -1); points[3] = new Vector3(-1, 1, -1); break; } Plane[] clipPlanes = new Plane[4]; for (int n = 0; n < 4; n++) { clipPlanes[n] = Plane.FromPoints(Vector3.Zero, points[(n + 1) % 4], points[n]); } //clipPlanes[ n ] = Plane.FromPoints( lightPosition, lightPosition + points[ ( n + 1 ) % 4 ], lightPosition + points[ n ] ); return(clipPlanes); }