Exemplo n.º 1
0
        public void GenerateMesh(int[,] map, Vector3 squareSize, string caveName, GameObject genCaveGameObj, float wallHeightWorldUnits)
        {
            _triangleDictionary.Clear();
            _outlines.Clear();
            _checkedVertices.Clear();
            WallHeightWorldUnits = wallHeightWorldUnits;


            // This grid will hold the resulting set of control squares showing us where to put walls
            squareGrid = new SquareGrid(map, squareSize);

            // These two lists make up the actual mesh
            _vertices  = new List <Vector3>();
            _triangles = new List <int>();

            for (var x = 0; x < squareGrid.Squares.GetLength(0); x++)
            {
                for (var y = 0; y < squareGrid.Squares.GetLength(1); y++)
                {
                    TriangulateSquare(squareGrid.Squares[x, y]);
                }
            }

            // The actual creation of the mesh in Unity3d  Note:  The mesh is actually a Unity3d Component.
            // The gameObjet is created by CaveMapCreator
            var mesh = new Mesh();

            Cave = MeshCommon.GetChildGameObject(genCaveGameObj, caveName + "Base").GetComponent <MeshFilter>();

            if (Cave == null)
            {
                Debug.Log("The cave object, MeshFilter could not be found");
            }
            else
            {
                Cave.mesh = mesh;
            }

            Walls = MeshCommon.GetChildGameObject(genCaveGameObj, caveName + "Wall").GetComponent <MeshFilter>();
            if (Walls == null)
            {
                Debug.Log("The Wall object, MeshFilter could not be found");
            }


            // the mesh methods require an array and not a list
            mesh.vertices  = _vertices.ToArray();
            mesh.triangles = _triangles.ToArray();
            mesh.RecalculateNormals();
            if (Cave != null)
            {
                Cave.mesh = mesh;
            }

            // We now want to apply a texture to the image as such we must generate the UV map
            var tileAmount = 10;
            var uvs        = new Vector2[_vertices.Count];

            for (var i = 0; i < _vertices.Count; i++)
            {
                var percentX = Mathf.InverseLerp(-map.GetLength(0) / 2.0f * squareSize.x, map.GetLength(0) / 2.0f * squareSize.x, _vertices[i].x) * tileAmount;
                var percentY = Mathf.InverseLerp(-map.GetLength(0) / 2.0f * squareSize.z, map.GetLength(0) / 2.0f * squareSize.z, _vertices[i].z) * tileAmount;
                uvs[i] = new Vector2(percentX, percentY);
            }

            mesh.uv = uvs;

            // Better recalculate the normals
            mesh.RecalculateNormals();

            // Save the mesh as an asset at the path given in the editor menu
            AssetDatabase.CreateAsset(mesh, MeshCommon.CreateUniqueMeshAssetName(caveName));
            AssetDatabase.SaveAssets();


            if (Is2D)
            {
                Generate2DColliders();
            }
            else
            {
                // Now we create the actual walls that will be seen in Unity3D game space
                CreateWallMesh();
            }
        }
Exemplo n.º 2
0
        private void GenerateMap()
        {
            GetCaveData();

            // We must set up a set of game objects to hold the cave.  A parent and two children
            // CaveGenerator as GameObject  with no render component
            //      Cave  with render components
            //      Walls with render components
            CaveGenerator = MeshCommon.CreateUniqueGameObject(_caveName + "Generator", false);
            Cave          = MeshCommon.CreateUniqueGameObject(_caveName + "Base", true, false);
            Wall          = MeshCommon.CreateUniqueGameObject(_caveName + "Wall", true, false);
            Ground        = GameObject.CreatePrimitive(PrimitiveType.Plane);
            Ground.name   = _caveName + "Ground";
            Ground.transform.localScale = new Vector3(WidthWorldUnits / 58, 1, HeightWorldUnits / 97);
            //           Ground.transform.localScale = new Vector3(WidthWorldUnits , 1, HeightWorldUnits );
            Ground.transform.localPosition = new Vector3(0, -WallHeightWorldUnits, 0);


            Cave.GetComponent <MeshRenderer>().material   = CaveMaterial;
            Wall.GetComponent <MeshRenderer>().material   = WallMaterial;
            Ground.GetComponent <MeshRenderer>().material = GroundMaterial;

            // We will place the newly created game object, that holds our mesh at either the origin or just in front of the camera.
            if (!_createAtOrigin && _cam)
            {
                Cave.transform.position          = _cam.transform.position + _cam.transform.forward * 100.0f;
                Wall.transform.position          = _cam.transform.position + _cam.transform.forward * 100f;
                Ground.transform.position        = _cam.transform.position + _cam.transform.forward * 100.0f - new Vector3(0, WallHeightWorldUnits, 0);
                CaveGenerator.transform.position = _cam.transform.position + _cam.transform.forward * 100.0f;
            }

            else
            {
                Cave.transform.position          = Vector3.zero;
                Wall.transform.position          = Vector3.zero;
                Ground.transform.position        = new Vector3(0, -WallHeightWorldUnits, 0);
                CaveGenerator.transform.position = Vector3.zero;
            }

            // Parent the Cave and walls to the Cave Generator
            Cave.transform.parent   = CaveGenerator.transform;
            Wall.transform.parent   = CaveGenerator.transform;
            Ground.transform.parent = CaveGenerator.transform;


            // Note that the map is created without a border it will be added later.
            _map = new int[Width, Height];

            // Let's fill the map with a "random set" of 1's and 0's
            RandomFillMap();

            // cellular automata's has a number of smoothing functions, in this example we have taken the most basic
            // SmoothMap function.
            for (var i = 0; i < 5; i++)
            {
                SmoothMap();
            }

            // Identify all regions and eliminate those that are too small or too big.
            ProcessMap();

            // We will now create a new grid that will contain the to be added border plus the generated map.
            var borderedMap = new int[Width + BorderSize * 2, Height + BorderSize * 2];

            for (var x = 0; x < borderedMap.GetLength(0); x++)
            {
                for (var y = 0; y < borderedMap.GetLength(1); y++)
                {
                    if (x >= BorderSize && x < Width + BorderSize && y >= BorderSize && y < Height + BorderSize)
                    {
                        borderedMap[x, y] = _map[x - BorderSize, y - BorderSize];
                    }
                    else
                    {
                        borderedMap[x, y] = 1;
                    }
                }
            }

            // Time to create the mesh, placing the results in the game object called Cave
            ; // _caveMeshCreator.GenerateMesh(borderedMap, 1, _caveName, CaveGenerator);

            var scaling = new Vector3(WidthWorldUnits / borderedMap.GetLength(0), 1, HeightWorldUnits / borderedMap.GetLength(1));

            _caveMeshCreator.GenerateMesh(borderedMap, scaling, _caveName, CaveGenerator, WallHeightWorldUnits);

            // create the ground plane here
            var newGroundMesh = GenerateGroundPlane(Ground, borderedMap, scaling, _caveName, WallHeightWorldUnits);

            MeshCommon.GetChildGameObject(CaveGenerator, _caveName + "Ground").GetComponent <MeshFilter>().mesh = newGroundMesh;

            // Save the creation to a prefab
            PrefabUtility.CreatePrefab("Assets/PreFabs/Cave.prefab", CaveGenerator, ReplacePrefabOptions.Default);
        }