Exemplo n.º 1
0
    Vector3 getInterpolationNormal(List <int> triIndexList, ref Vector3 movablePos, ref Vector3 headUp)
    {
        var triCount = triIndexList.Count;

        for (var i = 0; i < triCount; ++i)
        {
            var triIndex = triIndexList[i];
            int v0Index  = triangles[3 * triIndex];
            int v1Index  = triangles[3 * triIndex + 1];
            int v2Index  = triangles[3 * triIndex + 2];

            var v0 = vertices[v0Index];
            var v1 = vertices[v1Index];
            var v2 = vertices[v2Index];

            var parentPos = transform.position;
            v0 += parentPos;
            v1 += parentPos;
            v2 += parentPos;
            var N = GeometryTool.CalculateTriangleNormal(ref v0, ref v1, ref v2);

            // 擊中三角形所在的平面
            Vector3 hitPos;
            if (!GeometryTool.RayHitPlane(movablePos, -N, N, v0, out hitPos))
            {
                continue;
            }

            bool  isGetValue;
            float a, b, r;
            GeometryTool.CalculateBarycentricCoordinates(ref v0, ref v1, ref v2, ref hitPos, out isGetValue, out a, out b, out r);
            if (!isGetValue)
            {
                continue;
            }

            if (!GeometryTool.isInTriangle(a, b, r))
            {
                continue;
            }

            Debug.DrawLine(v0, v1, Color.red);
            Debug.DrawLine(v1, v2, Color.green);
            Debug.DrawLine(v2, v0, Color.blue);

            var n0 = normals[v0Index];
            var n1 = normals[v1Index];
            var n2 = normals[v2Index];

            Debug.DrawRay(v0, n0 * 5);
            Debug.DrawRay(v1, n1 * 5);
            Debug.DrawRay(v2, n2 * 5);

            var normal = GeometryTool.CalculateInterpolationValueByBarycentricCoordinates(ref n0, ref n1, ref n2, a, b, r);
            return(normal.normalized);
        }
        return(headUp);
    }