예제 #1
0
    public void Generate()
    {
        mesh = marching.Generate(Terrain.Level, region, Terrain.terrainConfiguration.terrainMap);

        meshFilter.sharedMesh = mesh;

        UpdateCollider(mesh);
    }
예제 #2
0
    public void CreateMesh()
    {
        if (material == null)
        {
            var shader = Shader.Find("Custom/StandardVertex");
            material = new Material(shader);
            material.SetColor("_Color", new Color(0.4622642f, 0.2678384f, 0.1940637f, 1));
        }
        visualize = false;

        marching.Surface = surface;
        var verticies = new List <Vector3>();
        var indices   = new List <int>();

        marching.Generate(map.Get(), width, height, depth, verticies, indices);

        //--- Building the Mesh

        var maxVerticesPerMesh = 30000;
        int numberOfMeshes     = verticies.Count / maxVerticesPerMesh + 1;

        for (int i = 0; i < numberOfMeshes; i++)
        {
            var splitVertices = new List <Vector3>();
            var splitIndices  = new List <int>();

            for (int j = 0; j < maxVerticesPerMesh; j++)
            {
                int index = i * maxVerticesPerMesh + j;

                if (index < verticies.Count)
                {
                    splitVertices.Add(verticies[index]);
                    splitIndices.Add(j);
                }
            }

            if (splitVertices.Count == 0)
            {
                continue;
            }

            Mesh mesh = new Mesh();
            mesh.SetVertices(splitVertices);
            mesh.SetTriangles(splitIndices, 0);

            mesh.RecalculateBounds();
            mesh.RecalculateNormals();

            GameObject go = new GameObject("Meeesh");
            go.transform.parent = transform;
            var meshFilter   = go.AddComponent <MeshFilter>();
            var meshRenderer = go.AddComponent <MeshRenderer>();
            go.GetComponent <MeshRenderer>().material = material;
            meshFilter.sharedMesh = mesh;
        }
    }
예제 #3
0
    public IEnumerator <object> genericTest(Tests type, string name, int cpuLimit, int pmbLimit, Func <Vector3Int, int, int, int, float> voxelGenerator)
    {
        int CPU_LIMIT = cpuLimit;
        int PMB_LIMIT = pmbLimit;

        UnityEngine.Debug.Log("Starting Test " + name);
        perfData[(int)type] = new PerfData
        {
            testName = name,
            sizes    = new TestSize[testSizes.Length],
        };

        int limitCounter = 0;

        for (int i = 0; i < testSizes.Length; i++)
        {
            Vector3Int size = (Vector3Int)testSizes[i];
            UnityEngine.Debug.Log("size: " + size);
            perfData[(int)type].sizes[i] = new TestSize
            {
                sizeName     = string.Format("({0}, {1}, {2})", size.x, size.y, size.z),
                cpuTime      = new int[ROUNDS],
                pmbTime      = new int[ROUNDS],
                cpuTriangles = 0,
                pmbTriangles = 0,
            };


            float[] voxel;

            string path = Path.Combine(performanceDataDir, string.Format("{0}{1}.dat", name, perfData[(int)type].sizes[i].sizeName));
            if (File.Exists(path))
            {
                UnityEngine.Debug.Log("loading voxelData from file:");
                Stream          voxelStream  = File.OpenRead(path);
                BinaryFormatter deserializer = new BinaryFormatter();
                voxel = (float[])deserializer.Deserialize(voxelStream);
                voxelStream.Close();
            }
            else
            {
                UnityEngine.Debug.Log("recreating voxelData:");
                voxel = new float[size.x * size.y * size.z];
                for (int x = 0; x < size.x; x++)
                {
                    for (int y = 0; y < size.y; y++)
                    {
                        for (int z = 0; z < size.z; z++)
                        {
                            voxel[z * size.x * size.y + y * size.x + x] = voxelGenerator(size, x, y, z);
                        }
                    }
                }
                Stream          SaveFileStream = File.Create(path);
                BinaryFormatter serializer     = new BinaryFormatter();
                serializer.Serialize(SaveFileStream, voxel);
                SaveFileStream.Close();
            }

            UnityEngine.Debug.Log("executing Test:");

            for (int round = 0; round < ROUNDS; round++)
            {
                Stopwatch watch = Stopwatch.StartNew();

                if (limitCounter < CPU_LIMIT)
                {
                    List <Vector3> verts   = new List <Vector3>();
                    List <int>     indices = new List <int>();
                    List <Vector3> normals = new List <Vector3>();
                    marching.Generate(voxel, size.x, size.y, size.z, verts, indices, normals);

                    watch.Stop();

                    UnityEngine.Debug.LogFormat("\tCPU took {0}ms", watch.ElapsedMilliseconds);
                    perfData[(int)type].sizes[i].cpuTime[round] = (int)watch.ElapsedMilliseconds;
                    perfData[(int)type].sizes[i].cpuTriangles   = indices.Count / 3;
                }
                else
                {
                    UnityEngine.Debug.Log("\tCPU skipped");
                    perfData[(int)type].sizes[i].cpuTime[round] = -1;
                }

                if (limitCounter < PMB_LIMIT)
                {
                    VoxelBlock <Voxel> block = InitBlock(size);
                    UnityEngine.Debug.Log(voxel.Length + " " + voxel[0] + " " + voxel[1] + " " + voxel[2] + " " + voxel[3] + " " + voxel[4] + " " + voxel[5] + " " + voxel[6] + " " + voxel[7]);
                    helper.scheduleOnMainThread(() =>
                    {
                        watch = Stopwatch.StartNew();
                        pmb.ReInit(block);
                        RenderBuffers buffers = pmb.calculate(voxel, size.x, size.y, size.z, 0.5f);

                        int[] args = new int[4];
                        buffers.argsBuffer.GetData(args);
                        watch.Stop();

                        UnityEngine.Debug.LogFormat("\tPMB took {0}ms", watch.ElapsedMilliseconds);
                        perfData[(int)type].sizes[i].pmbTime[round] = (int)watch.ElapsedMilliseconds;
                        perfData[(int)type].sizes[i].pmbTriangles   = args[0] / 3;

                        buffers.vertexBuffer.Dispose();
                        buffers.indexBuffer.Dispose();
                        buffers.normalBuffer.Dispose();
                        buffers.argsBuffer.Dispose();

                        UnityEngine.Debug.Log("PMB triags inside thread: " + perfData[(int)type].sizes[i].pmbTriangles);
                    }).wait();
                    UnityEngine.Debug.Log("PMB triags after thread: " + perfData[(int)type].sizes[i].pmbTriangles);
                }
                else
                {
                    UnityEngine.Debug.Log("\tPMB skipped");
                    perfData[(int)type].sizes[i].pmbTime[round] = -1;
                    watch.Stop();
                }

                yield return(null);
            }
            limitCounter++;
        }
    }