Ejemplo n.º 1
0
        /// <summary>
        /// Creates a platform from its top vertices.
        /// </summary>
        /// <returns>A new 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 = surfaceHeight;
            }
            List <Vector3> bottom = new List <Vector3>(input.vertices.Count);

            for (int i = 0; i < input.vertices.Count; i++)
            {
                bottom.Add(VectorUtil.SetY(input.vertices[i], input.vertices[i].y - height));
            }
            return(CreatePlatform(input, new PlatformInput(bottom)));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates the walls in the level.
 /// </summary>
 /// <param name="wallInput">JSON data for the walls in the level.</param>
 private void CreateWalls(List <WallInput> wallInput)
 {
     wallContainer = ObjectUtil.CreateNewObject("Walls");
     foreach (WallInput wall in wallInput)
     {
         Vector3        direction    = wall.endpoints[1] - wall.endpoints[0];
         Vector3        ortho        = VectorUtil.GetOrthonormal(direction) * wallThickness / 2;
         List <Vector3> wallPoints   = new List <Vector3>(4);
         Vector3        heightOffset = Vector3.up * wallHeight / 2;
         wallPoints.Add(wall.endpoints[0] + ortho + heightOffset);
         wallPoints.Add(wall.endpoints[0] - ortho + heightOffset);
         wallPoints.Add(wall.endpoints[1] - ortho + heightOffset);
         wallPoints.Add(wall.endpoints[1] + ortho + heightOffset);
         PlatformInput wallSurface = new PlatformInput(wallPoints);
         GameObject    wallObject  = CreatePlatform(wallSurface, wallHeight);
         wallObject.layer = LayerMask.NameToLayer("Wall");
         wallObject.name  = "Wall";
         wallObject.GetComponent <Renderer>().material = wallMaterial;
         wallObject.transform.parent = wallContainer.transform;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a platform from both top and bottom vertices.
        /// </summary>
        /// <returns>A new 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("Platform");
            virtualPlatform.AddComponent <MeshFilter>();
            virtualPlatform.AddComponent <MeshRenderer>();
            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 = transform;

            return(virtualPlatform);
        }