Example #1
0
    void GenerateMap()
    {
        int resolutionCube = resolution * resolution * resolution;

        if (map.Length != resolutionCube)
        {
            map = new float[resolution, resolution, resolution];
        }

        NativeList <int3> usedIndices = new NativeList <int3>(Allocator.Temp);

        for (int x = 0; x < resolution - 1; x++)
        {
            for (int y = 0; y < resolution - 1; y++)
            {
                for (int z = 0; z < resolution - 1; z++)
                {
                    float4 pos    = new float4(position.x + x * scale, position.y + y * scale, position.z + z * scale, position.w);
                    float  perlin = noise.cnoise(pos);
                    map[x, y, z] = perlin > thresHold ? 1 : 0;
                    usedIndices.Add(new int3(x, y, z));
                }
            }
        }

        MarchingCube.CreateMeshData(map, usedIndices, scale, ref mesh);
        usedIndices.Dispose();

        filter.mesh = mesh;
    }
Example #2
0
    /// <summary>
    /// Uncompleted Bursted version
    /// </summary>
    public static void GenerateMeshBurst(NativeArray <SPHSystem.WaterParticle> waterParticles, int count, float3 minPosition, float3 maxPosition, int resolution, ref Mesh mesh)
    {
        Profiler.BeginSample("WaterMarchingCube");

        int resolutionCube = resolution * resolution * resolution;

        var nativeMap         = new NativeArray <float>(resolutionCube, Allocator.TempJob);
        var nativeUsedIndices = new NativeList <int3>(resolutionCube, Allocator.TempJob);

        float invResolution = 1f / resolution;

        FillMapJob fillMapJob = new FillMapJob()
        {
            map            = nativeMap,
            usedIndices    = nativeUsedIndices,
            resolution     = resolution,
            invResolution  = invResolution,
            maxPosition    = maxPosition,
            minPosition    = minPosition,
            waterParticles = waterParticles,
        };

        fillMapJob.Run();

        Profiler.EndSample();
        MarchingCube.CreateMeshData(nativeMap, nativeUsedIndices, resolution, invResolution, ref mesh);
        nativeMap.Dispose();
        nativeUsedIndices.Dispose();
    }
Example #3
0
    public static void GenerateMesh(NativeArray <SPHSystem.WaterParticle> waterParticles, int count, float3 minPosition, float3 maxPosition, int resolution, ref Mesh mesh)
    {
        Profiler.BeginSample("WaterMarchingCube");
        int resolutionCube = resolution * resolution * resolution;

        if (usedGrid == null || usedGrid.Length != resolutionCube)
        {
            usedGrid = new bool[resolution, resolution, resolution];
            map      = new float[resolution, resolution, resolution];
        }
        else
        {
            Array.Clear(usedGrid, 0, resolutionCube);
            Array.Clear(map, 0, resolutionCube);
        }

        float             invResolution = 1f / resolution;
        float             step          = math.distance(maxPosition.x, minPosition.x) * invResolution;
        float             invStep       = 1f / step;
        NativeList <int3> usedIndices   = new NativeList <int3>(resolution * resolution, Allocator.Temp);

        for (int i = 0; i < count; i++)
        {
            int3 index = GetPositionIndex(waterParticles[i].position, minPosition, maxPosition, resolution, invStep);

            //Only calculate marching cube for the 8 corners per cube
            for (int j = 0; j < MarchingCubeTables.CornerTable.Length; j++)
            {
                int3 conerIndex = index + MarchingCubeTables.CornerTable[j];
                if (!usedGrid[conerIndex.x, conerIndex.y, conerIndex.z])
                {
                    usedGrid[conerIndex.x, conerIndex.y, conerIndex.z] = true;
                    usedIndices.Add(conerIndex);
                }
                int3 conerIndex2 = index - MarchingCubeTables.CornerTable[j];
                if (!usedGrid[conerIndex2.x, conerIndex2.y, conerIndex2.z])
                {
                    usedGrid[conerIndex2.x, conerIndex2.y, conerIndex2.z] = true;
                    usedIndices.Add(conerIndex2);
                }
            }

            map[index.x, index.y, index.z] = 1;
        }

        Profiler.EndSample();
        MarchingCube.CreateMeshData(map, usedIndices, invResolution, ref mesh);
        usedIndices.Dispose();
    }