Exemplo n.º 1
0
        private float computeTiltAngle(IndexStruct position)
        {
            Vector3 norm = new Vector3(heightMap[position.X - 1, position.Y] - heightMap[position.X + 1, position.Y], heightMap[position.X, position.Y - 1] - heightMap[position.X, position.Y + 1], 2 * gridSize);

            norm.Normalize();
            return((float)Math.Abs(Math.Sin(Math.Acos(Vector3.Dot(norm, new Vector3(0, 0, 1))))));
        }
Exemplo n.º 2
0
        private float[,] singleWaterCycle(float[,] heightData, int startX, int startY, int width, int length, float lifeTime)
        {
            float       sedementPickUp           = 0.03f;
            IndexStruct lowest                   = new IndexStruct(startX, startY);
            IndexStruct Currentlowest            = new IndexStruct(startX, startY);
            float       previousHeigthDifference = 0;
            float       heightDifference         = 0;
            float       sediment                 = 0;
            float       sedimentsolved           = 0;

            for (int i = 0; i < lifeTime; i++)
            {
                if (Currentlowest.X > 0 && Currentlowest.Y > 0 && Currentlowest.X < width - 1 && Currentlowest.Y < length - 1)
                {
                    Currentlowest    = findlowest(heightData, lowest);
                    heightDifference = heightData[lowest.X, lowest.Y] - heightData[Currentlowest.X, Currentlowest.Y];
                    sediment         = heightDifference * sedementPickUp * ((lifeTime - i) / (lifeTime)) - sedimentsolved;
                    sedimentsolved  += sediment;
                    heightData[lowest.X, lowest.Y] += -sediment;
                    lowest = Currentlowest;
                    previousHeigthDifference = heightDifference;
                }
                else
                {
                    return(heightData);
                }
            }
            return(heightData);
        }
Exemplo n.º 3
0
        //home brew
        private void ComputeSingleErosion(IndexStruct position)
        {
            if (velocityMap[position.X, position.Y].LengthSquared() > 0.00001f)
            {
                float tiltAngle = computeTiltAngle(position);
                float sedimentCapacityFactor = tiltAngle * velocityMap[position.X, position.Y].Length();
                // disolving
                float sedimentDisolved = sedimentCapacityFactor * getDissolutionConstant(position);
                float diff             = sedimentDisolved - GetOldErrosionMap()[position.X, position.Y].Y;
                newHeightMap[position.X, position.Y] += heightMap[position.X, position.Y] - diff;
                //push
                if (tiltAngle > 0.001f)
                {
                    int klqsdf = 0;
                }
                float draggedSediment = sedimentCapacityFactor * getPushConstant(position);
                newHeightMap[position.X, position.Y] -= draggedSediment;

                distributeErosionSediment(position, sedimentDisolved, draggedSediment);
            }
            else
            {
                newHeightMap[position.X, position.Y] += heightMap[position.X, position.Y];
            }
        }
Exemplo n.º 4
0
        private void riverCycle(float[,] heightData, IndexStruct index)
        {
            float              sedementPickUp           = 0.03f;
            IndexStruct        lowest                   = index;
            IndexStruct        Currentlowest            = index;
            float              previousHeigthDifference = 0;
            float              heightDifference         = 0;
            float              sediment                 = 0;
            float              sedimentsolved           = 0;
            List <IndexStruct> surroundingLowest;

            while (heightData[lowest.X, lowest.Y] > 0)
            {
                surroundingLowest = findlowestForRiver(heightData, lowest, 0.01f);
                for (int i = 1; i < surroundingLowest.Count; i++)
                {
                    riverCycle(heightData, surroundingLowest[i]);
                }
                Currentlowest    = surroundingLowest[0];
                heightDifference = heightData[lowest.X, lowest.Y] - heightData[Currentlowest.X, Currentlowest.Y];
                sediment         = heightDifference * sedementPickUp - sedimentsolved;
                sedimentsolved  += sediment;
                heightData[lowest.X, lowest.Y] += -sediment;
                lowest = Currentlowest;
            }
        }
Exemplo n.º 5
0
        private void computeOutflow(Vector4 diff, Vector4 water, IndexStruct index)
        {
            Vector4 param = Vector4.Min(new Vector4(Math.Abs(diff.X), Math.Abs(diff.Y), Math.Abs(diff.Z), Math.Abs(diff.W)), (new Vector4(GetOldWaterMap()[index.X, index.Y]) + water) * 0.5f);

            param = Vector4.Min(new Vector4(gridSize), param);
            Vector4 newFlow = Damping * GetOldFlowMap()[index.X, index.Y] + constantParam * diff * param;

            newFlow = Vector4.Max(Vector4.Zero, newFlow);

            float maxWater       = gridSize * gridSize * GetOldWaterMap()[index.X, index.Y];
            float waterOutFactor = (newFlow.X + newFlow.Y + newFlow.W + newFlow.Z) * TimeStep;

            if ((waterOutFactor > GetOldWaterMap()[index.X, index.Y]))//should be EROSIM_NUM_ZERO_GENERAL
            {
                waterOutFactor = maxWater / waterOutFactor;
                if (waterOutFactor > 1.0)
                {
                    waterOutFactor = 1.0f;
                }
                newFlow = newFlow * waterOutFactor;
            }


            updateFlowMap(newFlow, index);
        }
Exemplo n.º 6
0
        private void computeErosionAndDeposition(IndexStruct position)// needs major clean up and implementation on  the gpu forsure
        {
            float tiltAngle = computeTiltAngle(position);

            if (tiltAngle < minTiltAngle)
            {
                tiltAngle = minTiltAngle;
            }
            float sedimentCapacityFactor = tiltAngle * velocityMap[position.X, position.Y].Length();
            float finalMaxSediment       = SedimentCapacity * sedimentCapacityFactor;
            float totalSedimentDif       = 0;
            float sedimentC = GetOldErrosionMap()[position.X, position.Y].Y;

            if (sedimentC > finalMaxSediment)
            {
                totalSedimentDif -= depositionC * (sedimentC - finalMaxSediment);
            }
            else
            {
                float maxS = finalMaxSediment;
                totalSedimentDif = getDissolutionConstant(position) * (maxS - sedimentC);
            }

            // change the terrains height
            heightMap[position.X, position.Y] -= totalSedimentDif;
            advectSediment(position, totalSedimentDif);
        }
Exemplo n.º 7
0
 private void singleDistribution(IndexStruct indexFrom, IndexStruct indexTo)
 {
     if ((heightMap[indexFrom.X, indexFrom.Y] + waterHeight[indexFrom.X, indexFrom.Y]) > (heightMap[indexTo.X, indexTo.Y] + waterHeight[indexTo.X, indexTo.Y]) && waterHeight[indexTo.X, indexTo.Y] != -1)
     {
         waterHeight[indexTo.X, indexTo.Y] += waterHeight[indexFrom.X, indexFrom.Y] / lower;// + (((heightMap[indexFrom.X, indexFrom.Y] + waterHeight[indexFrom.X, indexFrom.Y]) - (heightMap[indexTo.X, indexTo.Y] + waterHeight[indexTo.X, indexTo.Y])) - meanLowest);
         watergone += waterHeight[indexFrom.X, indexFrom.Y] / lower;
     }
 }
Exemplo n.º 8
0
 public computeVelocityParams(IndexStruct position, Vector4[,] oldFlowMap, float[,] newWaterMap, float[,] getOldWaterMap, float size)
 {
     Position       = position;
     OldFlowMap     = oldFlowMap;
     NewWaterMap    = newWaterMap;
     GetOldWaterMap = getOldWaterMap;
     Size           = size;
 }
Exemplo n.º 9
0
        private void updateFlowMap(Vector4 flow, IndexStruct index)
        {
            GetNewWaterMap()[index.X, index.Y]     += MathHelper.Max(GetOldWaterMap()[index.X, index.Y] - (flow.X + flow.Y + flow.Z + flow.W) - evaperation * TimeStep, 0);
            GetNewWaterMap()[index.X + 1, index.Y] += flow.X * TimeStep;
            GetNewWaterMap()[index.X - 1, index.Y] += flow.Y * TimeStep;
            GetNewWaterMap()[index.X, index.Y + 1] += flow.Z * TimeStep;
            GetNewWaterMap()[index.X, index.Y - 1] += flow.W * TimeStep;

            GetNewFlowMap()[index.X, index.Y] = flow;
        }
Exemplo n.º 10
0
 private float getDissolutionConstant(IndexStruct pos)
 {
     //for (int q = 0; q < springs.Count; q++)
     //{
     //    if (pos.X==springs[q].Position.X&&pos.X==springs[q].Position.X)
     //    {
     //        return 0.001f;
     //    }
     //}
     return(0.000000001f);
 }
Exemplo n.º 11
0
        private Vector4 getSurroundingHeight(float[,] data, IndexStruct index)
        {
            Vector4 surroundings = new Vector4();

            surroundings.X = data[index.X + 1, index.Y];
            surroundings.Y = data[index.X - 1, index.Y];
            surroundings.Z = data[index.X, index.Y + 1];
            surroundings.W = data[index.X, index.Y - 1];

            return(surroundings);
        }
Exemplo n.º 12
0
 private void ComputeWaterOutFlow(IndexStruct index)
 {
     if (GetOldWaterMap()[index.X, index.Y] > 0)
     {
         Vector4 height  = getSurroundingHeight(heightMap, index);
         Vector4 water   = getSurroundingHeight(GetOldWaterMap(), index);
         float   heightC = heightMap[index.X, index.Y] + GetOldWaterMap()[index.X, index.Y];
         Vector4 diff    = new Vector4();
         diff = new Vector4(heightC) - water - height;
         computeOutflow(diff, water, index);
     }
 }
Exemplo n.º 13
0
 private void DistributeWater(IndexStruct index)
 {
     calculateSurroundingValues(index);
     singleDistribution(index, new IndexStruct(index.X + 1, index.Y));
     singleDistribution(index, new IndexStruct(index.X, index.Y + 1));
     singleDistribution(index, new IndexStruct(index.X - 1, index.Y));
     singleDistribution(index, new IndexStruct(index.X, index.Y - 1));
     singleDistribution(index, new IndexStruct(index.X + 1, index.Y + 1));
     singleDistribution(index, new IndexStruct(index.X - 1, index.Y - 1));
     singleDistribution(index, new IndexStruct(index.X + 1, index.Y - 1));
     singleDistribution(index, new IndexStruct(index.X - 1, index.Y + 1));
 }
Exemplo n.º 14
0
        private IndexStruct findlowest(float[,] heightData, IndexStruct XY)
        {
            float       lowest   = heightData[XY.X, XY.Y];
            IndexStruct lowestXY = XY;

            if (lowest > heightData[XY.X + 1, XY.Y])
            {
                lowest   = heightData[XY.X + 1, XY.Y];
                lowestXY = new IndexStruct(XY.X + 1, XY.Y);
            }
            if (lowest > heightData[XY.X, XY.Y + 1])
            {
                lowest   = heightData[XY.X, XY.Y + 1];
                lowestXY = new IndexStruct(XY.X, XY.Y + 1);
            }
            if (lowest > heightData[XY.X - 1, XY.Y])
            {
                lowest   = heightData[XY.X - 1, XY.Y];
                lowestXY = new IndexStruct(XY.X - 1, XY.Y);
            }
            if (lowest > heightData[XY.X - 1, XY.Y - 1])
            {
                lowest   = heightData[XY.X - 1, XY.Y - 1];
                lowestXY = new IndexStruct(XY.X - 1, XY.Y - 1);
            }
            if (lowest > heightData[XY.X + 1, XY.Y + 1])
            {
                lowest   = heightData[XY.X + 1, XY.Y + 1];
                lowestXY = new IndexStruct(XY.X + 1, XY.Y + 1);
            }
            if (lowest > heightData[XY.X + 1, XY.Y - 1])
            {
                lowest   = heightData[XY.X + 1, XY.Y - 1];
                lowestXY = new IndexStruct(XY.X + 1, XY.Y - 1);
            }
            if (lowest > heightData[XY.X - 1, XY.Y + 1])
            {
                lowest   = heightData[XY.X - 1, XY.Y + 1];
                lowestXY = new IndexStruct(XY.X - 1, XY.Y + 1);
            }
            if (lowest > heightData[XY.X, XY.Y - 1])
            {
                lowest   = heightData[XY.X, XY.Y - 1];
                lowestXY = new IndexStruct(XY.X, XY.Y - 1);
            }

            return(lowestXY);
        }
Exemplo n.º 15
0
        private void distributeErosionSediment(IndexStruct position, float disolvedAmount, float draggedAmount)
        {
            Vector4 flow     = GetOldFlowMap()[position.X, position.Y];
            Vector2 velocity = velocityMap[position.X, position.Y];

            velocity.Normalize();
            float lost = 0f;

            if (flow.X > 0)
            {
                GetNewErrosionMap()[position.X - 1, position.Y].Y += disolvedAmount * flow.X;
                lost += disolvedAmount * flow.X;
            }
            if (flow.Y > 0)
            {
                GetNewErrosionMap()[position.X + 1, position.Y].Y += disolvedAmount * flow.Y;
                lost += disolvedAmount * flow.Y;
            }
            if (flow.Z > 0)
            {
                GetNewErrosionMap()[position.X, position.Y - 1].Y += disolvedAmount * flow.Z;
                lost += disolvedAmount * flow.Z;
            }
            if (flow.W > 0)
            {
                GetNewErrosionMap()[position.X, position.Y + 1].Y += disolvedAmount * flow.W;
                lost += disolvedAmount * flow.W;
            }
            GetNewErrosionMap()[position.X, position.Y].Y += disolvedAmount - lost;


            if (velocity.X > 0)
            {
                newHeightMap[position.X + 1, position.Y] += velocity.X * draggedAmount;
            }
            else
            {
                newHeightMap[position.X - 1, position.Y] += velocity.X * draggedAmount;
            }
            if (velocity.Y > 0)
            {
                newHeightMap[position.X, position.Y + 1] += velocity.Y * draggedAmount;
            }
            else
            {
                newHeightMap[position.X, position.Y - 1] += velocity.Y * draggedAmount;
            }
        }
Exemplo n.º 16
0
        private void calculateSurroundingValues(IndexStruct XY)
        {
            lower = 0;;
            float totallowest = 0;
            float lowest      = heightMap[XY.X, XY.Y] + waterHeight[XY.X, XY.Y];

            if (lowest > (heightMap[XY.X + 1, XY.Y] + waterHeight[XY.X + 1, XY.Y]))
            {
                lower++;
                totallowest += lowest - heightMap[XY.X + 1, XY.Y] + waterHeight[XY.X + 1, XY.Y];
            }
            if (lowest > heightMap[XY.X, XY.Y + 1] + waterHeight[XY.X, XY.Y + 1])
            {
                lower++;
                totallowest += lowest - heightMap[XY.X, XY.Y + 1] + waterHeight[XY.X, XY.Y + 1];
            }
            if (lowest > heightMap[XY.X - 1, XY.Y] + waterHeight[XY.X - 1, XY.Y])
            {
                lower++;
                totallowest += lowest - heightMap[XY.X - 1, XY.Y] + waterHeight[XY.X - 1, XY.Y];
            }
            if (lowest > heightMap[XY.X - 1, XY.Y - 1] + waterHeight[XY.X - 1, XY.Y - 1])
            {
                lower++;
                totallowest += lowest - heightMap[XY.X - 1, XY.Y - 1] + waterHeight[XY.X - 1, XY.Y - 1];
            }
            if (lowest > heightMap[XY.X + 1, XY.Y + 1] + waterHeight[XY.X + 1, XY.Y + 1])
            {
                lower++;
                totallowest += lowest - heightMap[XY.X + 1, XY.Y + 1] + waterHeight[XY.X + 1, XY.Y + 1];
            }
            if (lowest > heightMap[XY.X + 1, XY.Y - 1] + waterHeight[XY.X + 1, XY.Y - 1])
            {
                lower++;
                totallowest += lowest - heightMap[XY.X + 1, XY.Y - 1] + waterHeight[XY.X + 1, XY.Y - 1];
            }
            if (lowest > heightMap[XY.X - 1, XY.Y + 1] + waterHeight[XY.X - 1, XY.Y + 1])
            {
                lower++;
                totallowest += lowest - heightMap[XY.X - 1, XY.Y + 1] + waterHeight[XY.X - 1, XY.Y + 1];
            }
            if (lowest > heightMap[XY.X, XY.Y - 1] + waterHeight[XY.X, XY.Y - 1])
            {
                lower++;
                totallowest += lowest - heightMap[XY.X, XY.Y - 1] + waterHeight[XY.X, XY.Y - 1];
            }
            meanLowest = totallowest / lower;
        }
Exemplo n.º 17
0
        private void advectSediment(IndexStruct position, float sedimentDif)
        {
            float   sediment = sedimentDif + GetOldErrosionMap()[position.X, position.Y].Y;
            Vector2 velocity = velocityMap[position.X, position.Y] * velocityFactor;

            if (velocity.X < 0)
            {
                GetNewErrosionMap()[position.X - 1, position.Y].Y += velocity.X * sediment;
            }
            else
            {
                GetNewErrosionMap()[position.X + 1, position.Y].Y += velocity.X * sediment;
            }
            if (velocity.Y < 0)
            {
                GetNewErrosionMap()[position.X, position.Y - 1].Y += velocity.Y * sediment;
            }
            else
            {
                GetNewErrosionMap()[position.X, position.Y + 1].Y += velocity.Y * sediment;
            }
        }
        private void InitThreadEntry()
        {
            FileStream      istream = new FileStream(IndexFilePath, FileMode.Open);
            BinaryFormatter bf      = new BinaryFormatter();
            IndexStruct     iss     = (IndexStruct)bf.Deserialize(istream);

            istream.Close();

            Map = iss.IndexHashTable;
            if (iss.UpdateID == UpdateID)
            {
                if (OnReady != null)
                {
                    OnReady(this, null);
                }
            }
            else
            {
                Map.Clear();
                OnIndexRebuilt += new OnIndexRebuiltHandler(InitIndexSink);
                RebuildIndex();
            }
        }
Exemplo n.º 19
0
 public Spring(IndexStruct position, float debiet, int radius)
 {
     Position = position;
     Debiet   = debiet;
     Radius   = radius;
 }
Exemplo n.º 20
0
 public Spring(IndexStruct position, float debiet)
 {
     Position = position;
     Debiet   = debiet;
     Radius   = 3;
 }
Exemplo n.º 21
0
        private List <IndexStruct> findlowestForRiver(float[,] heightData, IndexStruct XY, float maxDifference)
        {
            List <IndexStruct> indices = new List <IndexStruct>();
            float       lowest         = heightData[XY.X + 1, XY.Y];
            IndexStruct lowestXY       = new IndexStruct(XY.X + 1, XY.Y);

            if (lowest > heightData[XY.X + 1, XY.Y])
            {
                lowest   = heightData[XY.X + 1, XY.Y];
                lowestXY = new IndexStruct(XY.X + 1, XY.Y);
            }
            if (lowest > heightData[XY.X, XY.Y + 1])
            {
                lowest   = heightData[XY.X, XY.Y + 1];
                lowestXY = new IndexStruct(XY.X, XY.Y + 1);
            }
            if (lowest > heightData[XY.X - 1, XY.Y])
            {
                lowest   = heightData[XY.X - 1, XY.Y];
                lowestXY = new IndexStruct(XY.X - 1, XY.Y);
            }
            if (lowest > heightData[XY.X - 1, XY.Y - 1])
            {
                lowest   = heightData[XY.X - 1, XY.Y - 1];
                lowestXY = new IndexStruct(XY.X - 1, XY.Y - 1);
            }
            if (lowest > heightData[XY.X + 1, XY.Y + 1])
            {
                lowest   = heightData[XY.X + 1, XY.Y + 1];
                lowestXY = new IndexStruct(XY.X + 1, XY.Y + 1);
            }
            if (lowest > heightData[XY.X + 1, XY.Y - 1])
            {
                lowest   = heightData[XY.X + 1, XY.Y - 1];
                lowestXY = new IndexStruct(XY.X + 1, XY.Y - 1);
            }
            if (lowest > heightData[XY.X - 1, XY.Y + 1])
            {
                lowest   = heightData[XY.X - 1, XY.Y + 1];
                lowestXY = new IndexStruct(XY.X - 1, XY.Y + 1);
            }
            if (lowest > heightData[XY.X, XY.Y - 1])
            {
                lowest   = heightData[XY.X, XY.Y - 1];
                lowestXY = new IndexStruct(XY.X, XY.Y - 1);
            }

            indices.Add(lowestXY);
            // now we have the lowest
            if (lowest > heightData[XY.X + 1, XY.Y] + maxDifference && lowest != heightData[XY.X + 1, XY.Y])
            {
                indices.Add(new IndexStruct(XY.X + 1, XY.Y));
            }
            if (lowest > heightData[XY.X, XY.Y + 1] + maxDifference && lowest != heightData[XY.X + 1, XY.Y])
            {
                indices.Add(new IndexStruct(XY.X, XY.Y + 1));
            }
            if (lowest > heightData[XY.X - 1, XY.Y] + maxDifference && lowest != heightData[XY.X + 1, XY.Y])
            {
                indices.Add(new IndexStruct(XY.X - 1, XY.Y));
            }
            if (lowest > heightData[XY.X - 1, XY.Y - 1] + maxDifference && lowest != heightData[XY.X + 1, XY.Y])
            {
                indices.Add(new IndexStruct(XY.X - 1, XY.Y - 1));
            }
            if (lowest > heightData[XY.X + 1, XY.Y + 1] + maxDifference && lowest != heightData[XY.X + 1, XY.Y])
            {
                indices.Add(new IndexStruct(XY.X + 1, XY.Y + 1));
            }
            if (lowest > heightData[XY.X + 1, XY.Y - 1] + maxDifference && lowest != heightData[XY.X + 1, XY.Y])
            {
                indices.Add(new IndexStruct(XY.X + 1, XY.Y - 1));
            }
            if (lowest > heightData[XY.X - 1, XY.Y + 1] + maxDifference && lowest != heightData[XY.X + 1, XY.Y])
            {
                indices.Add(new IndexStruct(XY.X - 1, XY.Y + 1));
            }
            if (lowest > heightData[XY.X, XY.Y - 1] + maxDifference && lowest != heightData[XY.X + 1, XY.Y])
            {
                indices.Add(new IndexStruct(XY.X, XY.Y - 1));
            }
            return(indices);
        }
Exemplo n.º 22
0
 public void AddSpring(IndexStruct Position, float debiet)
 {
     springs.Add(new Spring(Position, debiet));
 }
Exemplo n.º 23
0
 public void AddSpring(IndexStruct Position, float debiet, int radius)
 {
     springs.Add(new Spring(Position, debiet, radius));
 }
Exemplo n.º 24
0
 private float getPushConstant(IndexStruct Pos)
 {
     return(0.01f);
 }