コード例 #1
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;
            }
        }
コード例 #2
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();
            }
        }
コード例 #3
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();

        }