Critique my ModulatedNoise function

This topic is 2640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

So, I have a ModulatedNoise function which uses a "spatial hash" to generate things like heightmaps and clouds. But, I can probably vastly improve it. So, tell me how.

 // Instead of calculated smoothed noise values over again, I cache them. float?[][,] smoothedNoise; public unsafe void ModulatedValueNoise(float offsetX, float offsetY, float scaleX, float scaleY, Bitmap bitmap) { float persistance = 0.5F; int octaves = 5; float divisor = 0F; /* for (int t = 0; t < octaves; ++t) { float amplitude = (float)Math.Pow(persistance, t); divisor += amplitude; } */ // Some place to store pre-calculated frequency and amplitude values, float[] frequencies = new float[octaves]; float[] amplitudes = new float[octaves]; smoothedNoise = new float?[octaves][,]; for (int t = 0; t < octaves; ++t) { frequencies[t] = (float)Math.Pow(2, t); // smoothedNoise[t] = new float?[bitmap.Width + (int)(frequencies[t] * 2F + 3F), bitmap.Height + (int)(frequencies[t] * 2F + 3F)]; // smoothedNoise[t] = new float?[(int)(bitmap.Width * (frequencies[t] * 2 / scaleX) + frequencies[t]) + 2, (int)(bitmap.Width * 2 * (frequencies[t] / scaleY)) + 2]; /* smoothedNoise[t] = new float?[ (int)(((bitmap.Width * (frequencies[t])) / scaleX) + (frequencies[t] * scaleX)) + 3, (int)(((bitmap.Height * (frequencies[t])) / scaleX) + (frequencies[t] * scaleX)) + 3 ]; */ // Took a bit of work to get the exact dimensions calculated. smoothedNoise[t] = new float?[ (int)(((bitmap.Width * (frequencies[t])) / scaleX) + scaleX) + 3, (int)(((bitmap.Height * (frequencies[t])) / scaleX) + scaleX) + 3 ]; // ((bitmap.Width * (frequencies[0])) / scaleX) + (frequencies[0] * scaleX) // (int)(((bitmap.Width * (frequencies[1])) / scaleX) + scaleX) + 1 amplitudes[t] = (float)Math.Pow(persistance, t); // Keeps the noise totals within or at 1.0F. divisor += (float)Math.Pow(persistance, t); } // Significant speed-up caching these values. float[] cosines = new float[361]; float[] powers = new float[361]; for (int t = 0; t < 361; ++t) { cosines[t] = (float)Math.Cos(t / 360.0 * Math.PI); powers[t] = (float)Math.Pow(t / 360.0, 2); } float total; byte value; { // It doesn't optimize "unsafe" code, but this speeds it up from 1400 to 860 ms. BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat); void* ptr = data.Scan0.ToPointer(); byte* bptr = (byte*)ptr; int pa = 0; byte alpha = 255; byte blue = 235; int y, x; int xs, ys; // From trying a FNV hash suggested by JTippetts. Was about the same speed or 20 ms slower, despite doing less. // uint offsetBasis2 = 2166136261; // int offsetBasis = (int)offsetBasis2; // int FNVPrime = 16777619; // int xs0 = 0, ys0 = 0; for (y = 0; y < bitmap.Height; ++y) { for (x = 0; x < bitmap.Width; ++x) { total = 0; for (int t = 0; t < octaves; ++t) { // From before I inlined the functions. // total += InterpolatedNoise(((x + offsetX) / scaleX) * frequencies[t], ((y + offsetY) / scaleY) * frequencies[t]) * (amplitudes[t] / divisor); // The float actual position. Not really adding a hash; it just makes the layers scroll at different rates. float x2 = (((x / scaleX) + offsetX) * frequencies[t]) + hash, y2 = (((y / scaleY) + offsetY) * frequencies[t]) + hash; // The integer smoothedNoise[t] position. xs = (int)((scaleX + ((((x) / scaleX) + (offsetX % 1F)) * frequencies[t])) + (hash % 1F)); ys = (int)((scaleY + ((((y) / scaleY) + (offsetY % 1F)) * frequencies[t])) + (hash % 1F)); // Old test values from adjusting the size of SmoothedNoise. /* if (t == 1) { xs0 = xs; ys0 = ys; } */ int xi = (int)x2, yi = (int)y2; float xf = x2 - xi, yf = y2 - yi; int seed; float corners; float sides; float self; float v1, v2, v3, v4; int xi2, yi2; #region InterpolatedNoise corners = 0F; sides = 0F; self = 0F; xi2 = xi; yi2 = yi; // No need to recalculate what we've already calculated. if (!smoothedNoise[t][xs, ys].HasValue) { seed = (xi2 - 1) + (yi2 - 1) * 1000000007; // From testing FNV. /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ // Running the hashing algorithm on a "perlin noise" page twice seems to give good results. seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 - 1) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 - 1) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; self += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); smoothedNoise[t][xs, ys] = v1 = self / 4F + sides / 8F + corners / 16F; // v1 = self / 4F + sides / 8F + corners / 16F; } else v1 = smoothedNoise[t][xs, ys].Value; corners = 0F; sides = 0F; self = 0F; xi2 = (int)(xi + 1); yi2 = yi; if (!smoothedNoise[t][xs + 1, ys].HasValue) { seed = (xi2 - 1) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 - 1) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 - 1) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; self += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); smoothedNoise[t][xs + 1, ys] = v2 = self / 4F + sides / 8F + corners / 16F; // v2 = self / 4F + sides / 8F + corners / 16F; } else v2 = smoothedNoise[t][xs + 1, ys].Value; corners = 0F; sides = 0F; self = 0F; xi2 = xi; yi2 = (int)(yi + 1); if (!smoothedNoise[t][xs, ys + 1].HasValue) { seed = (xi2 - 1) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 - 1) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 - 1) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; self += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); smoothedNoise[t][xs, ys + 1] = v3 = self / 4F + sides / 8F + corners / 16F; // v3 = self / 4F + sides / 8F + corners / 16F; } else v3 = smoothedNoise[t][xs, ys + 1].Value; corners = 0F; sides = 0F; self = 0F; xi2 = (int)(xi + 1); yi2 = (int)(yi + 1); if (!smoothedNoise[t][xs + 1, ys + 1].HasValue) { seed = (xi2 - 1) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 - 1) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; corners += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 - 1) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 - 1) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2 + 1) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2 + 1) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2 - 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2 - 1) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2 + 1) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2 + 1) * 1000000007); seed = (seed << 13) ^ seed; sides += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); seed = (xi2) + (yi2) * 1000000007; /* seed = offsetBasis ^ seed; seed = seed * FNVPrime; */ seed = (seed << 13) ^ seed; seed = ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); seed = seed ^ ((xi2) + (yi2) * 1000000007); seed = (seed << 13) ^ seed; self += 0.5F + ((1.0F - ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824.0F) * 0.5F); smoothedNoise[t][xs + 1, ys + 1] = v4 = self / 4F + sides / 8F + corners / 16F; // v4 = self / 4F + sides / 8F + corners / 16F; } else v4 = smoothedNoise[t][xs + 1, ys + 1].Value; float i1, i2, i; i = xf; // i *= 3.1415926535897932384626433832795F; // i = (float)((1F - Math.Cos(i)) * 0.5); // Cosine interpolation, without actual Math.Cos();. i = cosines[(int)((1F - i) * 180F)]; i1 = v1 * (1F - i) + v2 * i; i2 = v3 * (1F - i) + v4 * i; i = yf; // i *= 3.1415926535897932384626433832795F; // i = (float)((1F - Math.Cos(i)) * 0.5); i = cosines[(int)((1F - i) * 180F)]; #endregion total += (i1 * (1F - i) + i2 * i) * (amplitudes[t] / divisor); } // total *= InterpolatedNoise(((x + offsetX + hash) / (scaleX * 16F)) * frequencies[0], ((y + offsetY + hash / 2.0F) / (scaleY * 16F)) * frequencies[0]); // total = (float)Math.Sqrt(total); // total = (float)Math.Pow(total, InterpolatedNoise(((x + offsetX + hash) / (scaleX * 16F)) * frequencies[0], ((y + offsetY + hash / 2.0F) / (scaleY * 16F)) * frequencies[0])); // total = (float)Math.Pow(total, ModulatedValueNoise(((x + offsetX + hash) / (scaleX * 16F)) * frequencies[0], ((y + offsetY + hash / 2.0F) / (scaleY * 16F)) * frequencies[0])); // total = (float)Math.Pow(total, 2); // Square the result (a float from 0.0F to 1.0F). Not really necessary; I just like the way it looks, most of the time. total = powers[(int)(total * 360F)]; // Turning the float value into a byte. value = (byte)(total * 255F); // bitmap.SetPixel(x, y, Color.FromArgb(255, (int)(total * 135), (int)(total * 206), 235)); // bitmap.SetPixel(x, y, Color.FromArgb(255, value, value, 235)); // Clouds! *(bptr + pa + 0) = blue; *(bptr + pa + 1) = value; *(bptr + pa + 2) = value; *(bptr + pa + 3) = alpha; pa += 4; } } // It was ignoring my test values. So I fixed it. Then I didn't need them at the moment. /* int temp = xs0; xs0 = ys0; ys0 = temp; temp = xs0; xs0 = ys0; ys0 = temp; */ // Let's not leave data pointers lying around. bitmap.UnlockBits(data); } } 

Share on other sites
I suspect you're going to want to encapsulate your hashing to clean up the readability of the code. Also, when you submit code like this in order for other people to evaluate it, you should eliminate those commented-out sections, again for readability.

You seem to be doing a lot of redundant bit-fiddling in your hashing algorithm. Are you certain the hash can't be trimmed down a bit? Encapsulating the hash will make it much easier for you to experiment with different algorithms and possibly find one that isn't so involved. Also, if you are going to call this function more than once (say, for generating more than one buffer full of data) you really ought to pull out the table generation and initialization stuff into another function so it doesn't get called redundantly.

As it stands, it's just a bit too messy to really evaluate.

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633759
• Total Posts
3013720
×