Exemplo n.º 1
0
        GameObject MakeSurface(Vector3 position)
        {
            // Make the surface
            GameObject   surface = new GameObject();
            MeshRenderer mr      = surface.AddComponent <MeshRenderer>();
            MeshFilter   mf      = surface.AddComponent <MeshFilter>();


            Vector3 tileBottomLeft = new Vector3();

            tileBottomLeft.x = -(quadsPerTile * cellSize) / 2;
            tileBottomLeft.z = -(quadsPerTile * cellSize) / 2;

            Vector3 tileTopRight = new Vector3();

            tileTopRight.x = (quadsPerTile * cellSize) / 2;
            tileTopRight.z = (quadsPerTile * cellSize) / 2;

            GeneratedMesh gm = new GeneratedMesh(6, 6);

            gm.vertices[0] = tileTopRight;
            gm.vertices[1] = new Vector3(tileBottomLeft.x, 0, tileTopRight.z);
            gm.vertices[2] = tileBottomLeft;
            gm.vertices[3] = new Vector3(tileTopRight.x, 0, tileBottomLeft.z);
            gm.vertices[4] = tileTopRight;
            gm.vertices[5] = tileBottomLeft;

            for (int i = 0; i < 6; i++)
            {
                gm.triangles[i] = i;
            }

            gm.uv[0] = MakeUV(position, quadsPerTile, quadsPerTile);
            gm.uv[1] = MakeUV(position, 0, quadsPerTile);
            gm.uv[2] = MakeUV(position, 0, 0);
            gm.uv[3] = MakeUV(position, quadsPerTile, 0);
            gm.uv[4] = MakeUV(position, quadsPerTile, quadsPerTile);
            gm.uv[5] = MakeUV(position, 0, 0);

            Mesh mesh = new Mesh();

            mesh.vertices  = gm.vertices;
            mesh.uv        = gm.uv;
            mesh.triangles = gm.triangles;
            mesh.RecalculateNormals();

            mf.mesh = mesh;

            mr.castShadows    = false;
            mr.receiveShadows = false;
            mr.material       = ceilingMaterial;
            mr.material.SetTexture("_MainTex", textureGenerator.texture);
            mr.material.SetTexture("_EmissionMap", textureGenerator.texture);
            //Utilities.SetupMaterialWithBlendMode(mr.material, BlendMode.Transparent);

            surface.AddComponent <MeshCollider>().sharedMesh = mesh;
            //Utilities.SetupMaterialWithBlendMode(mr.material, BlendMode.Transparent);
            surface.layer = ceilingLayer;
            surface.name  = "surface";
            return(surface);
        }
Exemplo n.º 2
0
        Mesh GenerateMesh(Vector3 position)
        {
            int verticesPerSegment = 6;

            int triangleCount = verticesPerSegment * ((int)quadsPerTile) * ((int)quadsPerTile);
            int vertexCount   = 4 * ((int)quadsPerTile) * ((int)quadsPerTile);

            int vertex = 0;
            // What cell is x and z for the bottom left of this tile in world space
            Vector3 tileBottomLeft = new Vector3();

            tileBottomLeft.x = -(quadsPerTile * cellSize) / 2;
            tileBottomLeft.z = -(quadsPerTile * cellSize) / 2;

            GeneratedMesh gm = new GeneratedMesh(vertexCount, triangleCount);

            Vector2 texOrigin = position / cellSize;

            texOrigin.x = texOrigin.x % textureGenerator.size;
            texOrigin.y = texOrigin.y % textureGenerator.size;

            float tilesPerTexture = textureGenerator.size / quadsPerTile;
            int   triangleVertex  = 0;

            for (int z = 0; z < quadsPerTile; z++)
            {
                for (int x = 0; x < quadsPerTile; x++)
                {
                    int startVertex = vertex;

                    // Cell bottom left is the 0 indexed position of the cell on a tile
                    // Kinda like local space on the tile
                    // We neeed these to make the vertices out of
                    Vector3 cellBottomLeft  = tileBottomLeft + new Vector3(x * cellSize, 0, z * cellSize);
                    Vector3 cellTopLeft     = tileBottomLeft + new Vector3(x * cellSize, 0, ((z + 1) * cellSize));
                    Vector3 cellTopRight    = tileBottomLeft + new Vector3((x + 1) * cellSize, 0, (z + 1) * cellSize);
                    Vector3 cellBottomRight = tileBottomLeft + new Vector3((x + 1) * cellSize, 0, z * cellSize);

                    // Add all the samplers together to make the height
                    // Cell is the absolute position of the cell in world space. I.e what gets sampled
                    Vector3 cellLeft = new Vector3(-quadsPerTile / 2, 0, -quadsPerTile / 2);
                    Vector3 cell     = (position / cellSize) + cellLeft + new Vector3(x, 0, z);
                    cellBottomLeft.y  = SampleCell(cell.x, cell.z);
                    cellTopLeft.y     = SampleCell(cell.x, cell.z + 1);
                    cellTopRight.y    = SampleCell(cell.x + 1, cell.z + 1);
                    cellBottomRight.y = SampleCell(cell.x + 1, cell.z);

                    // Make the vertices
                    startVertex           = vertex;
                    gm.vertices[vertex++] = cellBottomLeft;
                    gm.vertices[vertex++] = cellTopLeft;
                    gm.vertices[vertex++] = cellTopRight;
                    gm.vertices[vertex++] = cellBottomRight;

                    vertex          = startVertex;
                    gm.uv[vertex++] = MakeUV(position, x, z);
                    gm.uv[vertex++] = MakeUV(position, x, z + 1);
                    gm.uv[vertex++] = MakeUV(position, x + 1, z + 1);
                    gm.uv[vertex++] = MakeUV(position, x + 1, z);

                    gm.triangles[triangleVertex++] = startVertex;
                    gm.triangles[triangleVertex++] = startVertex + 1;
                    gm.triangles[triangleVertex++] = startVertex + 3;
                    gm.triangles[triangleVertex++] = startVertex + 3;
                    gm.triangles[triangleVertex++] = startVertex + 1;
                    gm.triangles[triangleVertex++] = startVertex + 2;
                }
            }
            Mesh mesh = new Mesh();

            mesh.vertices  = gm.vertices;
            mesh.uv        = gm.uv;
            mesh.triangles = gm.triangles;
            mesh.RecalculateNormals();

            return(mesh);
        }