123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #ifndef UNITY_GEOMETRICTOOLS_INCLUDED
- #define UNITY_GEOMETRICTOOLS_INCLUDED
- float2 BoxRayIntersect(float3 start, float3 dir, float3 boxMin, float3 boxMax)
- {
- float3 invDir = 1.0 / dir;
-
- float3 firstPlaneIntersect = (boxMin - start) * invDir;
- float3 secondPlaneIntersect = (boxMax - start) * invDir;
-
- float3 closestPlane = min(firstPlaneIntersect, secondPlaneIntersect);
- float3 furthestPlane = max(firstPlaneIntersect, secondPlaneIntersect);
- float2 intersections;
-
- intersections.x = max(closestPlane.x, max(closestPlane.y, closestPlane.z));
-
- intersections.y = min(min(furthestPlane.x, furthestPlane.y), furthestPlane.z);
- return intersections;
- }
- float BoxRayIntersectSimple(float3 start, float3 dir, float3 boxMin, float3 boxMax)
- {
- float3 invDir = 1.0 / dir;
-
- float3 rbmin = (boxMin - start) * invDir;
- float3 rbmax = (boxMax - start) * invDir;
- float3 rbminmax = (dir > 0.0) ? rbmax : rbmin;
- return min(min(rbminmax.x, rbminmax.y), rbminmax.z);
- }
- float2 SphereRayIntersect(float3 start, float3 dir, float radius, out bool intersect)
- {
- float a = dot(dir, dir);
- float b = dot(dir, start) * 2.0;
- float c = dot(start, start) - radius * radius;
- float discriminant = b * b - 4.0 * a * c;
- float2 intersections = float2(0.0, 0.0);
- intersect = false;
- if (discriminant < 0.0 || a == 0.0)
- {
- intersections.x = 0.0;
- intersections.y = 0.0;
- }
- else
- {
- float sqrtDiscriminant = sqrt(discriminant);
- intersections.x = (-b - sqrtDiscriminant) / (2.0 * a);
- intersections.y = (-b + sqrtDiscriminant) / (2.0 * a);
- intersect = true;
- }
- return intersections;
- }
- float SphereRayIntersectSimple(float3 start, float3 dir, float radius)
- {
- float b = dot(dir, start) * 2.0;
- float c = dot(start, start) - radius * radius;
- float discriminant = b * b - 4.0 * c;
- return abs(sqrt(discriminant) - b) * 0.5;
- }
- float3 RayPlaneIntersect(in float3 rayOrigin, in float3 rayDirection, in float3 planeOrigin, in float3 planeNormal)
- {
- float dist = dot(planeNormal, planeOrigin - rayOrigin) / dot(planeNormal, rayDirection);
- return rayOrigin + rayDirection * dist;
- }
- float DistancePointBox(float3 position, float3 boxMin, float3 boxMax)
- {
- return length(max(max(position - boxMax, boxMin - position), float3(0.0, 0.0, 0.0)));
- }
- float3 ProjectPointOnPlane(float3 position, float3 planePosition, float3 planeNormal)
- {
- return position - (dot(position - planePosition, planeNormal) * planeNormal);
- }
- float DistanceFromPlane(float3 p, float4 plane)
- {
- return dot(float4(p, 1.0), plane);
- }
- bool CullTriangleFrustum(float3 p0, float3 p1, float3 p2, float epsilon, float4 frustumPlanes[6], int numPlanes)
- {
- bool outside = false;
- for (int i = 0; i < numPlanes; i++)
- {
-
- outside = outside || Max3(DistanceFromPlane(p0, frustumPlanes[i]),
- DistanceFromPlane(p1, frustumPlanes[i]),
- DistanceFromPlane(p2, frustumPlanes[i])) < epsilon;
- }
- return outside;
- }
- bool3 CullTriangleEdgesFrustum(float3 p0, float3 p1, float3 p2, float epsilon, float4 frustumPlanes[6], int numPlanes)
- {
- bool3 edgesOutside = false;
- for (int i = 0; i < numPlanes; i++)
- {
- bool3 pointsOutside = bool3(DistanceFromPlane(p0, frustumPlanes[i]) < epsilon,
- DistanceFromPlane(p1, frustumPlanes[i]) < epsilon,
- DistanceFromPlane(p2, frustumPlanes[i]) < epsilon);
-
- edgesOutside.x = edgesOutside.x || (pointsOutside.y && pointsOutside.z);
- edgesOutside.y = edgesOutside.y || (pointsOutside.x && pointsOutside.z);
- edgesOutside.z = edgesOutside.z || (pointsOutside.x && pointsOutside.y);
- }
- return edgesOutside;
- }
- bool CullTriangleBackFace(float3 p0, float3 p1, float3 p2, float epsilon, float3 viewPos, float winding)
- {
- float3 edge1 = p1 - p0;
- float3 edge2 = p2 - p0;
- float3 N = cross(edge1, edge2);
- float3 V = viewPos - p0;
- float NdotV = dot(N, V) * winding;
-
-
-
-
-
- return NdotV < epsilon * sqrt(dot(N, N) * dot(V, V));
- }
- #endif
|