public static void DestroyFarseerPhysics(RageSpline spline)
    {
#if !UNITY_FLASH
        var fsConcave = spline.GetComponent <FSConcaveShapeComponent>();
        if (fsConcave != null)
        {
            DestroyImmediate(fsConcave);
        }
        var fsBody = spline.GetComponent <FSBodyComponent>();
        if (fsBody != null)
        {
            DestroyImmediate(fsBody);
        }
        spline.DestroyFarseerNow = true;
#endif
    }
    public static void UpdateFarseerPhysics(RageSpline spline)
    {
#if !UNITY_FLASH
        var fsConcave = spline.GetComponent <FSConcaveShapeComponent>();
        if (fsConcave == null)
        {
            fsConcave = spline.gameObject.AddComponent <FSConcaveShapeComponent>();
        }
        var fsBody = spline.GetComponent <FSBodyComponent>();
        if (fsBody == null)
        {
            spline.gameObject.AddComponent <FSBodyComponent>();
        }
        fsConcave.PointInput = FSShapePointInput.Vector2List;
        var points = new List <Vector2>();
        if (spline.FarseerPhysicsPointsOnly)
        {
            for (int i = 0; i < spline.GetPointCount(); i++)
            {
                points.Add(spline.GetPosition(i));
            }
        }
        else
        {
            int splitCount = spline.LockPhysicsToAppearence ? spline.GetVertexCount() : spline.GetPhysicsColliderCount();
            for (int i = 0; i < splitCount; i++)
            {
                float   splinePos = (float)i / (float)splitCount;
                Vector3 normal    = spline.GetNormal(splinePos);
                points.Add(spline.GetPosition(splinePos) + normal * spline.GetPhysicsNormalOffset());
            }
        }
        fsConcave.PointsCoordinates = points.ToArray();
        fsConcave.ConvertToConvex();
#endif
    }
Exemple #3
0
        private static void GenerateMesh(RageSpline rs, bool refreshTriangulation)
        {
//          if(rs.GetFill() != RageSpline.Fill.None)
//              rs.ShiftOverlappingControlPoints();

            bool  fillAntialiasing = false;
            float aaWidth          = rs.GetAntialiasingWidth();

            if (aaWidth > 0f)
            {
                if (rs.inverseTriangleDrawOrder)
                {
                    fillAntialiasing = true;
                }
                else
                if (rs.GetOutline() == RageSpline.Outline.None ||
                    Mathf.Abs(rs.GetOutlineNormalOffset()) > (rs.GetOutlineWidth() + (aaWidth)))
                {
                    fillAntialiasing = true;
                }
            }

            bool outlineAntialiasing = rs.GetAntialiasingWidth() > 0f;

            bool multipleMaterials = false;
            var  renderer          = rs.GetComponent <Renderer>();

            if (renderer != null)
            {
                if (renderer.sharedMaterials.GetLength(0) > 1)
                {
                    multipleMaterials = true;
                }
            }

            RageSpline.RageVertex[] outlineVerts = rs.GenerateOutlineVerts(outlineAntialiasing, multipleMaterials);
            RageSpline.RageVertex[] fillVerts    = rs.GenerateFillVerts(fillAntialiasing, multipleMaterials);

            int vertexCount = outlineVerts.Length + fillVerts.Length;

            Vector3[] verts = new Vector3[vertexCount];

            Vector2[] uvs  = null;
            Vector2[] uvs2 = null;

            if (rs.PinUvs)
            {
                uvs  = new Vector2[vertexCount];
                uvs2 = new Vector2[vertexCount];
            }

            Color[] colors = new Color[vertexCount];

            for (int i = 0; i < fillVerts.Length; i++)
            {
                verts[i]  = fillVerts[i].position;
                colors[i] = fillVerts[i].color;

                if (uvs == null)
                {
                    continue;
                }
                uvs[i]  = fillVerts[i].uv1;
                uvs2[i] = fillVerts[i].uv2;
            }

            for (int i = 0; i < outlineVerts.Length; i++)
            {
                int v = i + fillVerts.Length;
                verts[v]  = outlineVerts[i].position;
                colors[v] = outlineVerts[i].color;

                if (uvs == null)
                {
                    continue;
                }
                uvs[v]  = outlineVerts[i].uv1;
                uvs2[v] = outlineVerts[i].uv2;
            }

            var mFilter = rs.GetComponent(typeof(MeshFilter)) as MeshFilter;

            if (verts.Length > 0)
            {
                verts[0] += new Vector3(0f, 0f, -0.001f);
            }

            if (mFilter != null)
            {
                var mesh = mFilter.sharedMesh;
                if (mesh == null)
                {
                    mesh = new Mesh();
                }

                if (refreshTriangulation)
                {
                    mesh.Clear();
                }

                //if (rs.optimize) Debug.Log ("mesh verts: " + mesh.vertices.Length + " | triangs: " + mesh.triangles.Length);
                mesh.vertices = verts;

                if (refreshTriangulation)
                {
                    rs.GenerateTriangles(mesh, fillVerts, new RageSpline.RageVertex[0], outlineVerts, fillAntialiasing, false, outlineAntialiasing, multipleMaterials);
                }

                if (rs.PinUvs)
                {
                    mesh.uv  = uvs;
                    mesh.uv2 = uvs2;
                }

                mesh.colors = colors;
                mesh.RecalculateBounds();
                mFilter.sharedMesh = mesh;
            }
        }