public static bool MeshRaycast(Mesh mesh, Ray ray, out pb_RaycastHit hit)
        {
            Vector3[] vertices  = mesh.vertices;
            int[]     triangles = mesh.triangles;

            float   dist = Mathf.Infinity;
            Vector3 point = Vector3.zero;
            Vector3 a, b, c;

            for (int i = 0; i < triangles.Length; i += 3)
            {
                a = vertices[triangles[i + 0]];
                b = vertices[triangles[i + 1]];
                c = vertices[triangles[i + 2]];

                if (pb_Geometry.RayIntersectsTriangle(ray, a, b, c, Culling.Front, out dist, out point))
                {
                    hit          = new pb_RaycastHit();
                    hit.point    = point;
                    hit.distance = Vector3.Distance(hit.point, ray.origin);
                    hit.normal   = Vector3.Cross(b - a, c - a);
                    hit.triangle = new int[] { triangles[i], triangles[i + 1], triangles[i + 2] };
                    return(true);
                }
            }

            hit = null;
            return(false);
        }
示例#2
0
		public static bool MeshRaycast(Mesh mesh, Ray ray, out pb_RaycastHit hit)
		{
			Vector3[] vertices = mesh.vertices;
			int[] triangles = mesh.triangles;

			float dist = Mathf.Infinity;
			Vector3 point = Vector3.zero;
			Vector3 a, b, c;

			for(int i = 0; i < triangles.Length; i += 3)
			{
				a = vertices[triangles[i+0]];
				b = vertices[triangles[i+1]];
				c = vertices[triangles[i+2]];

				if(pb_Geometry.RayIntersectsTriangle(ray, a, b, c, Culling.Front, out dist, out point))
				{
					hit = new pb_RaycastHit();
					hit.point = point;
					hit.distance = Vector3.Distance(hit.point, ray.origin);
					hit.normal = Vector3.Cross(b-a, c-a);
					hit.triangle = new int[] { triangles[i], triangles[i+1], triangles[i+2] };
					return true;
				}
			}

			hit = null;
			return false;
		}