private void OpenSimplexNoiseCalculation(object sender, DoWorkEventArgs e) { float octaveWeight = 1; float octaveMultiplier = 1; int sizeCompensator = TerrainUtilities.GetReversedSizeCompensator(TerrainSize); float[] helper = new float[TerrainPoints.Length]; for (int octaves = 0; octaves < OSNOctaves; octaves++) { for (int x = 0; x < TerrainSize; x++) { for (int z = 0; z < TerrainSize; z++) { float value = 0; float xValue = (float)((((0.00025f / OSNScale) / OSNScaleX) * (x * sizeCompensator) + OSNSeed) * octaveMultiplier); float zValue = (float)((((0.00025f / OSNScale) / OSNScaleZ) * (z * sizeCompensator) + OSNSeed) * octaveMultiplier); if (octaves == 0) { value = (float)(((_openSimplexNoise.Evaluate(xValue, zValue) * octaveWeight) + 1) / 2); } else { value = (float)((_openSimplexNoise.Evaluate(xValue, zValue) * octaveWeight) / 2); } helper[z + x * TerrainSize] += value * OSNStrength; } } octaveWeight /= 2.0f - (OSNOctaveWeight - 0.5f); octaveMultiplier = octaves * 2.0f; int progressPercentage = (int)(((float)octaves / (float)OSNOctaves) * 100); (sender as BackgroundWorker).ReportProgress(progressPercentage); } for (int x = 0; x < TerrainSize; x++) { for (int z = 0; z < TerrainSize; z++) { TerrainPoints[z + x * TerrainSize] = TerrainUtilities.Apply(TerrainPoints[z + x * TerrainSize], helper[z + x * TerrainSize], CurrentApplicationMode); } } }
private void SlopeCalculation(object sender, DoWorkEventArgs e) { float value = 0; float heightRange = StartHeight - EndHeight; float startPosition = (float)StartPosition / 100.0f * TerrainSize; float endPosition = (float)EndPosition / 100.0f * TerrainSize; float positionRange = endPosition - startPosition; if (positionRange < 0) { positionRange = -positionRange; } if (heightRange < 0) { heightRange = -heightRange; } for (int x = 0; x < TerrainSize; x++) { for (int z = 0; z < TerrainSize; z++) { switch (CurrentDirection) { case Direction.X_Axis: switch (CurrentInterpolationMode) { case InterpolationMode.Linear: if (x < startPosition) { value = StartHeight; } else if (x > endPosition) { value = EndHeight; } else { if (StartHeight < EndHeight) { value = StartHeight + ((((float)x - startPosition) / positionRange) * heightRange); } else { value = StartHeight - ((((float)x - startPosition) / positionRange) * heightRange); } } break; case InterpolationMode.Smooth: if (x < startPosition) { value = StartHeight; } else if (x > endPosition) { value = EndHeight; } else { if (StartHeight < EndHeight) { value = StartHeight + (float)(Math.Cos(Math.PI - ((x - startPosition) / positionRange) * Math.PI) / 2.0f + 0.5f) * heightRange; } else { value = StartHeight - (float)(Math.Cos(Math.PI - ((x - startPosition) / positionRange) * Math.PI) / 2.0f + 0.5f) * heightRange; } } break; } break; case Direction.Z_Axis: switch (CurrentInterpolationMode) { case InterpolationMode.Linear: if (z < startPosition) { value = StartHeight; } else if (z > endPosition) { value = EndHeight; } else { if (StartHeight < EndHeight) { value = StartHeight + ((((float)z - startPosition) / positionRange) * heightRange); } else { value = StartHeight - ((((float)z - startPosition) / positionRange) * heightRange); } } break; case InterpolationMode.Smooth: if (z < startPosition) { value = StartHeight; } else if (z > endPosition) { value = EndHeight; } else { if (StartHeight < EndHeight) { value = StartHeight + (float)(Math.Cos(Math.PI - ((z - startPosition) / positionRange) * Math.PI) / 2.0f + 0.5f) * heightRange; } else { value = StartHeight - (float)(Math.Cos(Math.PI - ((z - startPosition) / positionRange) * Math.PI) / 2.0f + 0.5f) * heightRange; } } break; } break; } TerrainPoints[z + x * TerrainSize] = TerrainUtilities.Apply(TerrainPoints[z + x * TerrainSize], value, CurrentApplicationMode); } int progressPercentage = (int)(((float)x / (float)TerrainSize) * 100); (sender as BackgroundWorker).ReportProgress(progressPercentage); } }
private void IslandCalculation(object sender, DoWorkEventArgs e) { float value = 0; float heightRange = InnerHeight - OutterHeight; float highestValue = 1; float lowestValue = 0; float distance = 0; float midPosition = TerrainSize / 2; float maxDistance = midPosition * (Size + 0.5f); if (InnerHeight < OutterHeight) { highestValue = OutterHeight; lowestValue = InnerHeight; } else { highestValue = InnerHeight; lowestValue = OutterHeight; } for (int x = 0; x < TerrainSize; x++) { for (int z = 0; z < TerrainSize; z++) { float currentPositionX = x - midPosition; float currentPositionZ = z - midPosition; distance = (float)Math.Sqrt((double)(currentPositionX * currentPositionX + currentPositionZ * currentPositionZ)); switch (CurrentInterpolationMode) { case InterpolationMode.Linear: if (OutterHeight >= InnerHeight) { value = lowestValue - ((distance / maxDistance) * heightRange); } else { value = highestValue - ((distance / maxDistance) * heightRange); } if (value < lowestValue) { value = lowestValue; } else if (value > highestValue) { value = highestValue; } break; case InterpolationMode.Smooth: if (heightRange < 0) { value = highestValue - (float)((Math.Cos(Math.PI - (1 - ((distance / maxDistance))) * Math.PI)) / 2.0f + 0.5f) * -heightRange; } else { value = lowestValue + (float)((Math.Cos(Math.PI - (1 - ((distance / maxDistance))) * Math.PI)) / 2.0f + 0.5f) * heightRange; } if (distance >= maxDistance) { value = OutterHeight; } break; } TerrainPoints[z + x * TerrainSize] = TerrainUtilities.Apply(TerrainPoints[z + x * TerrainSize], value, CurrentApplicationMode); } int progressPercentage = (int)(((float)x / (float)TerrainSize) * 100); (sender as BackgroundWorker).ReportProgress(progressPercentage); } }