Ejemplo n.º 1
0
        public void Execute(int index)
        {
            var pi                 = Utils.IndexToXYZ(index, SizeVox, SizeVox2);
            var p                  = new float3(pi.x * HeightmapScale.x, pi.y, pi.z * HeightmapScale.z);
            var terrainHeight      = Heights[Utils.XYZToHeightIndex(pi, SizeVox)];
            var terrainHeightValue = p.y + ChunkAltitude - terrainHeight;

            float2 distances;

            switch (Brush)
            {
            case BrushType.Sphere:
                distances = ComputeSphereDistances(p);
                break;

            case BrushType.HalfSphere:
                distances = ComputeHalfSphereDistances(p);
                break;

            case BrushType.RoundedCube:
                distances = ComputeCubeDistances(p);
                break;

            case BrushType.Stalagmite:
                distances = ComputeConeDistances(p);
                break;

            default:
                return;     // never happens
            }

            Voxel voxel;

            switch (Action)
            {
            case ActionType.Add:
            case ActionType.Dig:
                var intensityWeight = Math.Max(1f, Math.Abs(terrainHeightValue) * 0.75f);
                voxel = ApplyDigAdd(index, pi, Action == ActionType.Dig, distances, intensityWeight);
                break;

            case ActionType.Paint:
                voxel = ApplyPaint(index, distances.x);
                break;

            case ActionType.PaintHoles:
                voxel = ApplyPaintHoles(index, pi, distances.x);
                break;

            case ActionType.Reset:
                voxel = ApplyResetBrush(index, pi, p, terrainHeightValue);
                break;

            default:
                return;     // never happens
            }


            if (voxel.Alteration != Voxel.Unaltered)
            {
                voxel = Utils.AdjustAlteration(voxel, pi, ChunkAltitude, terrainHeightValue, SizeVox, Heights);
            }

            if (voxel.IsAlteredNearBelowSurface || voxel.IsAlteredNearAboveSurface)
            {
#if UNITY_2019_3_OR_NEWER
                if (Action != ActionType.Reset)
                {
                    for (var z = -CutSize.y; z < CutSize.y; ++z)
                    {
                        var pz = pi.z + z;
                        if (pz >= 0 && pz < SizeOfMesh)
                        {
                            for (var x = -CutSize.x; x < CutSize.x; ++x)
                            {
                                var px = pi.x + x;
                                if (px >= 0 && px < SizeOfMesh)
                                {
                                    NativeCollections.Utils.IncrementAt(Holes, pz * SizeOfMesh + px);
                                }
                            }
                        }
                    }
                }
#else
                var pos = new float3(pi.x * HeightmapScale.x, pi.y, pi.z * HeightmapScale.z);
                ToTriggerBounds.Enqueue(pos);
                var wpos = pos + WorldPosition;
                var pCut = new int3((int)(wpos.x * TerrainRelativePositionToHolePosition.x), (int)wpos.y,
                                    (int)(wpos.z * TerrainRelativePositionToHolePosition.y));
                for (var x = -CutSize.x; x < CutSize.x; ++x)
                {
                    for (var z = -CutSize.y; z < CutSize.y; ++z)
                    {
                        ToCut.Enqueue(new CutEntry(
                                          pCut.x + x,
                                          pCut.z + z,
                                          voxel.IsAlteredNearAboveSurface
                                          ));
                    }
                }
#endif
            }


            Voxels[index] = voxel;
        }
        public void Execute(int index)
        {
            var pi                 = Utils.IndexToXYZ(index, SizeVox, SizeVox2);
            var p                  = new float3(pi.x * HeightmapScale.x, pi.y, pi.z * HeightmapScale.z);
            var terrainHeight      = Heights[Utils.XYZToHeightIndex(pi, SizeVox)];
            var terrainHeightValue = p.y + ChunkAltitude - terrainHeight;

            // Always use a spherical brush
            var distances = ComputeSphereDistances(p);

            Voxel voxel;

            switch (Action)
            {
            case ActionType.Smooth:
                voxel = ApplySmooth(index, pi, distances.x, distances.y, terrainHeightValue);
                break;

            case ActionType.BETA_Sharpen:
                voxel = ApplySharpen(index, pi, distances.x, distances.y, terrainHeightValue);
                break;

            default:
                return;     // never happens
            }

            if (voxel.Alteration != Voxel.Unaltered)
            {
                voxel = Utils.AdjustAlteration(voxel, pi, ChunkAltitude, terrainHeightValue, SizeVox, Heights);
            }

            if (voxel.IsAlteredNearBelowSurface || voxel.IsAlteredNearAboveSurface)
            {
#if UNITY_2019_3_OR_NEWER
                for (var z = -CutSize.y; z < CutSize.y; ++z)
                {
                    var pz = pi.z + z;
                    if (pz >= 0 && pz < SizeOfMesh)
                    {
                        for (var x = -CutSize.x; x < CutSize.x; ++x)
                        {
                            var px = pi.x + x;
                            if (px >= 0 && px < SizeOfMesh)
                            {
                                NativeCollections.Utils.IncrementAt(Holes, pz * SizeOfMesh + px);
                            }
                        }
                    }
                }
#else
                var pos = new float3(pi.x * HeightmapScale.x, pi.y, pi.z * HeightmapScale.z);
                ToTriggerBounds.Enqueue(pos);
                var wpos = pos + WorldPosition;
                var pCut = new int3((int)(wpos.x * TerrainRelativePositionToHolePosition.x), (int)wpos.y,
                                    (int)(wpos.z * TerrainRelativePositionToHolePosition.y));
                for (var x = -CutSize.x; x < CutSize.x; ++x)
                {
                    for (var z = -CutSize.y; z < CutSize.y; ++z)
                    {
                        ToCut.Enqueue(new CutEntry(
                                          pCut.x + x,
                                          pCut.z + z,
                                          voxel.IsAlteredNearAboveSurface
                                          ));
                    }
                }
#endif
            }

            VoxelsOut[index] = voxel;
        }