示例#1
0
    private void Start()
    {
        cameraComponent = GetComponent <Camera>();

        if (Application.isMobilePlatform)
        {
            input = new MobileInput();
        }
        else
        {
            input = new DesktopInput();
        }
    }
    /// <summary>
    /// Creates a virtual platform from its top vertices.
    /// </summary>
    /// <returns>A new virtual platform from the specified top vertices.</returns>
    /// <param name="input">The top vertices of the platform.</param>
    /// <param name="height">The thickness of the platform.</param>
    private GameObject CreatePlatform(PlatformInput input, float height = 0)
    {
        if (height == 0)
        {
            height = input.type == PlatformType.Surface ? surfaceHeight : platformHeight;
        }
        List <Vector3> bottom = new List <Vector3>(input.vertices.Count);

        for (int i = 0; i < input.vertices.Count; i++)
        {
            bottom.Add(PathUtil.SetY(input.vertices[i], input.vertices[i].y - height));
        }
        return(CreatePlatform(input, new PlatformInput(bottom, input.type)));
    }
示例#3
0
    /// <summary>
    /// Creates a virtual platform from its top vertices.
    /// </summary>
    /// <returns>A new virtual platform from the specified top vertices.</returns>
    /// <param name="input">The top vertices of the platform.</param>
    /// <param name="height">The thickness of the platform.</param>
    /// <param name="hidden">Whether to render the platform.</param>
    private GameObject CreatePlatform(PlatformInput input, float height = 0, bool hidden = false)
    {
        if (height == 0)
        {
            height = platformHeight;
        }
        List <Vector3> bottom = new List <Vector3>(input.vertices.Count);

        for (int i = 0; i < input.vertices.Count; i++)
        {
            bottom.Add(PathUtil.SetY(input.vertices[i], input.vertices[i].y - height));
        }
        return(CreatePlatform(input, new PlatformInput(bottom), hidden));
    }
    /// <summary>
    /// Creates a virtual platform from both top and bottom vertices.
    /// </summary>
    /// <returns>A new virtual platform from the specified vertices.</returns>
    /// <param name="top">The top vertices of the platform.</param>
    /// <param name="bottom">The bottom vertices of the platform.</param>
    private GameObject CreatePlatform(PlatformInput top, PlatformInput bottom)
    {
        GameObject virtualPlatform = new GameObject();

        virtualPlatform.layer = LayerMask.NameToLayer("Virtual");
        virtualPlatform.AddComponent <MeshFilter>();
        MeshRenderer renderer = virtualPlatform.AddComponent <MeshRenderer>();

        if (top.type == PlatformType.Surface)
        {
            virtualPlatform.name = "Collider";
            renderer.material    = surfaceMaterial;
        }
        else
        {
            virtualPlatform.name = "Virtual Platform";
            renderer.material    = virtualPlatformMaterial;
        }
        Mesh mesh = virtualPlatform.GetComponent <MeshFilter>().mesh;

        // Create the vertices of the platform.
        Vector3[] vertices = new Vector3[top.vertices.Count * 6];

        // Used to determine clockwise/counter-clockwise.
        float edgeSum = 0;

        for (int i = 0; i < top.vertices.Count; i++)
        {
            vertices[i] = top.vertices[i];
            vertices[i + top.vertices.Count] = bottom.vertices[i];
            if (i < top.vertices.Count - 1)
            {
                edgeSum += (top.vertices[i + 1].x - top.vertices[i].x) * (top.vertices[i + 1].z + top.vertices[i].z);
            }
            else
            {
                edgeSum += (top.vertices[0].x - top.vertices[i].x) * (top.vertices[0].z + top.vertices[i].z);
            }
        }
        bool clockwise = edgeSum > 0;

        // Find the triangles that can make up the top and bottom faces of the platform mesh.
        Triangulator triangulator = new Triangulator(top.vertices.ToArray());

        int[] topTriangles = triangulator.Triangulate();
        int[] triangles    = new int[topTriangles.Length * 2 + top.vertices.Count * 6];
        for (int i = 0; i < topTriangles.Length; i += 3)
        {
            triangles[i]     = topTriangles[i];
            triangles[i + 1] = topTriangles[i + 1];
            triangles[i + 2] = topTriangles[i + 2];
            triangles[topTriangles.Length + i]     = topTriangles[i + 2] + top.vertices.Count;
            triangles[topTriangles.Length + i + 1] = topTriangles[i + 1] + top.vertices.Count;
            triangles[topTriangles.Length + i + 2] = topTriangles[i] + top.vertices.Count;
        }

        // Find the triangles for the sides of the platform.
        for (int i = 0; i < top.vertices.Count; i++)
        {
            int triangleOffset = topTriangles.Length * 2 + i * 6;
            int nextIndex      = i < top.vertices.Count - 1 ? i + 1 : 0;

            int vertexOffset = top.vertices.Count * 2 + i * 4;
            vertices[vertexOffset]     = vertices[i];
            vertices[vertexOffset + 1] = vertices[nextIndex];
            vertices[vertexOffset + 2] = vertices[top.vertices.Count + i];
            vertices[vertexOffset + 3] = vertices[top.vertices.Count + nextIndex];

            if (!clockwise)
            {
                triangles[triangleOffset]     = vertexOffset;
                triangles[triangleOffset + 1] = vertexOffset + 1;
                triangles[triangleOffset + 2] = vertexOffset + 2;
                triangles[triangleOffset + 3] = vertexOffset + 3;
                triangles[triangleOffset + 4] = vertexOffset + 2;
                triangles[triangleOffset + 5] = vertexOffset + 1;
            }
            else
            {
                triangles[triangleOffset + 5] = vertexOffset;
                triangles[triangleOffset + 4] = vertexOffset + 1;
                triangles[triangleOffset + 3] = vertexOffset + 2;
                triangles[triangleOffset + 2] = vertexOffset + 3;
                triangles[triangleOffset + 1] = vertexOffset + 2;
                triangles[triangleOffset]     = vertexOffset + 1;
            }
        }

        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();

        virtualPlatform.AddComponent <MeshCollider>();
        virtualPlatform.GetComponent <MeshCollider>().sharedMesh = mesh;

        virtualPlatform.transform.parent = LevelManager.Instance.transform.FindChild("Platforms").transform;

        return(virtualPlatform);
    }