Exemplo n.º 1
0
        private void InitTabs()
        {
            _tabs      = MeshCommon.GetListFromEnum <Tabs>();
            _tabLabels = new List <string>();

            foreach (var tab in _tabs)
            {
                _tabLabels.Add(tab.ToString());
            }
        }
Exemplo n.º 2
0
        private static void CreateStore(string path, string name)
        {
            if (path != "" && name != "")
            {
                if (name == "MeshData")
                {
                    MeshCommon.CreateScriptableAsset <MeshData>(path + name + ".asset");
                }

                if (name == "CaveData")
                {
                    MeshCommon.CreateScriptableAsset <CaveData>(path + name + ".asset");
                }
            }

            else
            {
                Debug.Log("The path or name was not valid");
            }
        }
Exemplo n.º 3
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.º 4
0
        private void CreateWallMesh()
        {
            CalculateMeshOutlines();

            var   wallVertices  = new List <Vector3>();
            var   wallTriangles = new List <int>();
            var   wallMesh      = new Mesh();
            float wallHeight    = WallHeightWorldUnits;

            foreach (var outline in _outlines)
            {
                for (var i = 0; i < outline.Count - 1; i++)
                {
                    var startIndex = wallVertices.Count;

                    // We must add the 4 vertices's for the wall mesh to a new list for conversion to an array
                    // when we create the actual mesh.
                    wallVertices.Add(_vertices[outline[i]]);                               // Top left
                    wallVertices.Add(_vertices[outline[i + 1]]);                           // Top right
                    wallVertices.Add(_vertices[outline[i]] - Vector3.up * wallHeight);     // bottom left
                    wallVertices.Add(_vertices[outline[i + 1]] - Vector3.up * wallHeight); // bottom right

                    // Each wall will be made up of two triangles
                    // Triangle one - note: he is going counter clock wise "because we are in side"
                    wallTriangles.Add(startIndex + 0);
                    wallTriangles.Add(startIndex + 2);
                    wallTriangles.Add(startIndex + 3);

                    // Triangle two
                    wallTriangles.Add(startIndex + 3);
                    wallTriangles.Add(startIndex + 1);
                    wallTriangles.Add(startIndex + 0);
                }
            }

            // Create the actual mesh in Unity3D game space.  Remember all meshes must be Arrays and not a C# list
            // - a vertices's and triangles array
            wallMesh.vertices  = wallVertices.ToArray();
            wallMesh.triangles = wallTriangles.ToArray();
            Walls.mesh         = wallMesh;

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

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


            if (Walls.gameObject.GetComponent <MeshCollider>() == null)
            {
                var wallCollider = Walls.gameObject.AddComponent <MeshCollider>();
            }

            else
            {
                DestroyImmediate(Walls.gameObject.GetComponent <MeshCollider>());
                var wallCollider = Walls.gameObject.AddComponent <MeshCollider>();
                wallCollider.sharedMesh = wallMesh;
            }
        }
Exemplo n.º 5
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.º 6
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();

        }