Exemple #1
0
        private void UpdateTriangleCoords()
        {
            IndexedTriangle t = CurrentTriangle;

            // triangleCoords[0] is always (0, 0)
            triangleCoords[1] = worldToSurface.MultiplyPoint3x4(vertices[t.i2]);
            triangleCoords[2] = worldToSurface.MultiplyPoint3x4(vertices[t.i3]);
        }
Exemple #2
0
        // TODO: implement mesh.GetRawTriangle(int index)
        public static void DrawTriangle(this Mesh mesh, int index, Color color)
        {
            IndexedTriangle triangle = mesh.GetSaneTriangles(0)[index];

            Vector3[] vertices = mesh.vertices;

            for (int i = 0; i < 3; ++i)
            {
                Vector3 start = vertices[triangle[i]];
                Vector3 end   = vertices[triangle[(i + 1) % 3]];
                Debug.DrawLine(start, end, color, 0, false);
            }
        }
Exemple #3
0
        private void GenerateIndex(Mesh mesh)
        {
            TriangleArray triangles = mesh.GetSaneTriangles(0);

            for (int i = 0; i < triangles.Length; ++i)
            {
                IndexedTriangle triangle = triangles[i];

                for (int j = 0; j < 3; ++j)
                {
                    var edge = new IndexedEdge(
                        triangle[j],
                        triangle[(j + 1) % 3]
                        );
                    edgeToTriangleMap.Add(edge, i);
                }
            }
        }
Exemple #4
0
        /**
         * Moves IWalker forward by \param distance, stopping if an edge has been reached
         * (in which case \param distanceLeft > 0)
         */
        public void StepUntilEdge(float distance, out float distanceLeft)
        {
            bool[] filteredEdges = new bool[3];
            CullBackEdges(ref filteredEdges);

            int     intersectedEdge;
            Vector2 intersectionPoint = GetEdgeIntersection(filteredEdges, out intersectedEdge);

            // Have we reached the edge?
            {
                float edgeDistance = (surfaceTransform.localPosition - intersectionPoint).magnitude;
                distanceLeft = distance - edgeDistance;

                if (distanceLeft < 0)
                {
                    // The step haven't reached triangle sides
                    distanceLeft = 0;
                    surfaceTransform.localPosition += LocalDirection * distance;
                    return;
                }
            }

            // The edge had been reached, now we should move to the neighbor triangle

            int neighbor = GetNeighborTriangle(intersectedEdge);

            // Get coordinates and angle in neighbor triangle space
            {
                // Intersected edge direction in old triangle's space
                Vector2 edgeDirection = triangleCoords[(intersectedEdge + 1) % 3]
                                        - triangleCoords[intersectedEdge];

                // Index of intersected edge end vertex, to find corresponding edge
                // in the neighbor triangle (it will be the start vertex index there)
                int intersectedEdgeEnd = CurrentTriangle[(intersectedEdge + 1) % 3];

                // "Intermediate" angle (the angle which is equal between both triangle's coordinate systems)
                float beta = surfaceTransform.angle - (-edgeDirection).GetAngle();

                Vector3 worldPosition = surfaceToWorld.MultiplyPoint3x4(intersectionPoint);
                surfaceTransform.triangleIndex = neighbor;
                UpdateMatrices(surfaceTransform.triangleIndex);
                UpdateTriangleCoords();

                surfaceTransform.localPosition = worldToSurface.MultiplyPoint3x4(worldPosition);

                // Intersected edge direction in new triangle's space
                int             intersectedEdgeNew = 0;
                IndexedTriangle triangle           = CurrentTriangle;
                for (int i = 0; i < 3; i++)
                {
                    if (triangle[i] == intersectedEdgeEnd)
                    {
                        intersectedEdgeNew = i;
                    }
                }
                Vector2 edgeDirectionNew = triangleCoords[(intersectedEdgeNew + 1) % 3]
                                           - triangleCoords[intersectedEdgeNew];
                float newAngle   = MathUtils.NormalizeAngle(beta + edgeDirectionNew.GetAngle());
                float angleDelta = newAngle - surfaceTransform.angle;
                surfaceTransform.angle = newAngle;

                OnTriangleChanged?.Invoke(angleDelta);

#if UNITY_EDITOR
                if (debugDrawEnabled)
                {
                    Vector2 center = (triangleCoords[1] + triangleCoords[2]) / 3;
                    Vector2 start  = triangleCoords[intersectedEdgeNew];
                    Vector2 end    = triangleCoords[(intersectedEdgeNew + 1) % 3];
                    start = Vector2.Lerp(start, center, 0.2f);
                    end   = Vector2.Lerp(end, center, 0.2f);
                    DrawLocalLine(start, end, Color.magenta);
                }
#endif
            }
        }