Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public void CreateMesh()
        {
            GetMeshData();
            // The list that will contain all of the vertices's must be reset every time we wish to create a new mesh.
            verticies.Clear();
            tangents.Clear();
            uVs.Clear();
            triangles.Clear();

            // Every segment has two vertices's  so total count is segments + 1
            _vCountX = _widthSegment + 1;
            _vCountY = _heightSegment + 1;

            // The size of every segment in world space units
            _vSizeX = _widthWorldUnits / _widthSegment;
            _vSizeY = _heightWorldUnits / _heightSegment;

            // UV factor is equal to....
            _uvFactorX = 1.0f / _widthSegment;
            _uvFactorY = 1.0f / _heightSegment;

            // The mesh will need a user selectable anchor point
            _getAnchor = new Anchor(_anchor, _widthWorldUnits, _heightWorldUnits);
            _offset = _getAnchor.AnchorOffset;
            _offsetId = _getAnchor.AnchorId;

            // We will use the origin as the offset to put the center of the mesh at 0,0,0
            _origin = new Vector3(_widthWorldUnits / 2f, 0, _heightWorldUnits / 2f);

            _plane = MeshCommon.CreateUniqueGameObject(_planeName);


            // 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)
                _plane.transform.position = _cam.transform.position + _cam.transform.forward * 5.0f;
            else
                _plane.transform.position = Vector3.zero;

            //// The mesh we create will need a name and if it was not give use the default SimpleMesh.
            var planeMesh = new Mesh { name = !string.IsNullOrEmpty(_meshName) ? _meshName : "SimpleMesh" };

            // When we save the mesh as an asset it can and will have a different name than the plane mesh
            // Here we create the root path + name
            var meshAssetName = _meshName + _vCountX + "x" + _vCountY + "W" + _widthWorldUnits + "L" + _heightWorldUnits + (_orientation == Orientation.Horizontal ? "H" : "V") + _offsetId;

            // Return a unique version of the meshAssetName root
            meshAssetName = MeshCommon.CreateUniqueMeshAssetName(_meshName, meshAssetName, _meshPath);

            if (!_hexMesh)
            {
                // Fill the vertex list with all of the mesh's vertices's, tangents and uVs as defined for a plane mesh..
                CreatePlaneVertex();

                // Now create the triangles for a plane mesh
                CreatePlaneTriangles();
            }
            else
            {
                // Fill the vertex list with all of the mesh's vertices's, tangents and uVs as defined for a plane mesh..
                CreateHexVertex();

                // Now create the triangles for a plane mesh
                CreateHexTriangles();
            }


            // Move all of the mesh data from Lists to arrays for Unity3D
            planeMesh.vertices = verticies.ToArray();
            planeMesh.triangles = triangles.ToArray();
            planeMesh.uv = uVs.ToArray();
            planeMesh.tangents = tangents.ToArray();

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

            // Load the calculated values into the mesh that was created
            _plane.GetComponent<MeshFilter>().mesh = planeMesh;
            _plane.GetComponent<MeshRenderer>().material = _meshMaterial;

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

        }