Exemplo n.º 1
0
    void saveMesh(GameObject gameObject, AssetImportContext ctx, Settings settings, VoxelModel model)
    {
        var   ChunkModels = model.SplitModelInChunkLModel(factor: FactorSize);
        int   i           = 0;
        float size        = (4 << FactorSize) * settings.SizeShared;

        for (int x = 0; x < ChunkModels.Item2.x; x++)
        {
            for (int y = 0; y < ChunkModels.Item2.y; y++)
            {
                for (int z = 0; z < ChunkModels.Item2.z; z++)
                {
                    if (ChunkModels.Item1[x, y, z].Only0)
                    {
                        continue;
                    }
                    var meshBulder = new MeshGenerator(settings, ChunkModels.Item1[x, y, z]);
                    if (ChunkOptimized)
                    {
                        meshBulder.NeighbourChunck = new Tuple <SurfaceAction, Array3UshortOpt>[6]
                        {
                            z + 1 < ChunkModels.Item2.z ? new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.RenderBasedOnNeighbourChunk, ChunkModels.Item1[x, y, z + 1]) :
                            new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.Render, default),    // front
                            z - 1 >= 0 ? new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.RenderBasedOnNeighbourChunk, ChunkModels.Item1[x, y, z - 1]) :
                            new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.Render, default),    // back
                            y + 1 < ChunkModels.Item2.y ? new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.RenderBasedOnNeighbourChunk, ChunkModels.Item1[x, y + 1, z]) :
                            new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.Render, default),    // top
                            y - 1 >= 0 ? new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.RenderBasedOnNeighbourChunk, ChunkModels.Item1[x, y - 1, z]) :
                            new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.Render, default),    // buttom
                            x - 1 >= 0 ? new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.RenderBasedOnNeighbourChunk, ChunkModels.Item1[x - 1, y, z]) :
                            new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.Render, default),    // right
                            x + 1 < ChunkModels.Item2.x ? new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.RenderBasedOnNeighbourChunk, ChunkModels.Item1[x + 1, y, z]) :
                            new Tuple <SurfaceAction, Array3UshortOpt>(SurfaceAction.Render, default),    // left
                        }
                    }
                    ;
                    meshBulder.GenerateMesh(fileName + "_childMesh_" + i);
                    if (meshBulder.mesh.vertexCount == 0)
                    {
                        continue;
                    }
                    var child = new GameObject(fileName + "_child_" + i);
                    child.transform.parent   = gameObject.transform;
                    child.transform.position = new Vector3(x * size, y * size, z * size);
                    child.AddComponent <MeshRenderer>().materials = settings.GetMaterials();
                    child.AddComponent <MeshFilter>().mesh        = meshBulder.mesh;
                    if (GenerateCollider)
                    {
                        var collideBuilder = new ColliderGenerator(child.transform, ChunkModels.Item1[x, y, z], settings);
                        collideBuilder.GenerateCollider();
                    }

                    ctx.AddObjectToAsset(fileName + "_childMesh_" + i, meshBulder.mesh);
                    i++;
                }
            }
        }
    }
Exemplo n.º 2
0
    //rolls a new map, mesh, and pool of colliders for walls
    private void generateMapMeshCollider(int nodeID)
    {
        GameObject node  = nodes[nodeID];
        WorldNode  wnode = wnodes[nodeID];

        int[,] map = mapGen.GenerateMap();
        wnode.SetMap(map);
        Mesh mesh = meshGen.GenerateMesh(map);

        wnode.meshFilter.mesh = mesh;
        collGen.GenerateCollider(node);
    }