Vector3 GetNoisePosition(Vector3 v, Vector3 t) { return(new Vector3( Noise2.perlin(t.x + v.x + .01f, t.y + v.y, t.z + v.z), Noise2.perlin(t.z + v.x, t.x + v.y + .01f, t.y + v.z), Noise2.perlin(t.y + v.x, t.z + v.y, t.x + v.z + .01f))); }
public void FillTexture() { if (_texture.width != resolution) { _texture.Resize(resolution, resolution); } Vector3 point00 = transform.TransformPoint(new Vector3(-0.5f, -0.5f)); Vector3 point10 = transform.TransformPoint(new Vector3(0.5f, -0.5f)); Vector3 point01 = transform.TransformPoint(new Vector3(-0.5f, 0.5f)); Vector3 point11 = transform.TransformPoint(new Vector3(0.5f, 0.5f)); NoiseMethod2 method = Noise2.noiseMethods[(int)type][dimensions - 1]; float stepSize = 1f / resolution; for (int y = 0; y < resolution; y++) { Vector3 point0 = Vector3.Lerp(point00, point01, (y + 0.5f) * stepSize); Vector3 point1 = Vector3.Lerp(point10, point11, (y + 0.5f) * stepSize); for (int x = 0; x < resolution; x++) { Vector3 point = Vector3.Lerp(point0, point1, (x + 0.5f) * stepSize); //float sample = method(point, frequency); float sample = Noise2.Sum(method, point, frequency, octaves, lacunarity, persistence).value; if (type != NoiseMethodType2.Value) { sample = sample * 0.5f + 0.5f; } //_texture.SetPixel(x, y, Color.white * sample); _texture.SetPixel(x, y, coloring.Evaluate(sample)); } } _texture.Apply(); }
public void Refresh() { if (resolution != _currentResolution) { CreateGrid(); } Quaternion q = Quaternion.Euler(rotation); Quaternion qInv = Quaternion.Inverse(q); Vector3 point00 = q * new Vector3(-0.5f, -0.5f) + offset; Vector3 point10 = q * new Vector3(0.5f, -0.5f) + offset; Vector3 point01 = q * new Vector3(-0.5f, 0.5f) + offset; Vector3 point11 = q * new Vector3(0.5f, 0.5f) + offset; NoiseMethod2 method = Noise2.noiseMethods[(int)type][dimensions - 1]; float stepSize = 1f / resolution; float amplitude = damping ? strength / frequency : strength; for (int v = 0, y = 0; y <= resolution; y++) { Vector3 point0 = Vector3.Lerp(point00, point01, y * stepSize); Vector3 point1 = Vector3.Lerp(point10, point11, y * stepSize); for (int x = 0; x <= resolution; x++, v++) { Vector3 point = Vector3.Lerp(point0, point1, x * stepSize); NoiseSample sample = Noise2.Sum(method, point, frequency, octaves, lacunarity, persistence); sample = type == NoiseMethodType2.Value ? (sample - 0.5f) : (sample * 0.5f); if (coloringForStrength) { _colors[v] = coloring.Evaluate(sample.value + 0.5f); sample *= amplitude; } else { sample *= amplitude; _colors[v] = coloring.Evaluate(sample.value + 0.5f); } _vertices[v].y = sample.value; sample.derivative = qInv * sample.derivative; if (analyticalDerivatives) { //calcualtes the analytical normals _normals[v] = new Vector3(-sample.derivative.x, 1f, -sample.derivative.y).normalized; } } } _mesh.vertices = _vertices; _mesh.colors = _colors; //_mesh.RecalculateNormals(); if (!analyticalDerivatives) { CalculateNormals(); //a version of the above RecalculateNormals() method } _mesh.normals = _normals; }
// Gets data for noise map public void GenerateMap() { stopwatch = new Stopwatch(); erosionSimulator = FindObjectOfType <Erosion2>(); noiseMap = Noise2.GenerateNoiseMap(levelOfDetail, seed, noiseScale, lacunarity, gain, numOfLayers); stopwatch.Start(); if (erosion) { HydraulicErosion(); } GenerateMesh(); stopwatch.Stop(); print("Run time: " + stopwatch.Elapsed); }
private void PositionParticles() { Quaternion q = Quaternion.Euler(surface.rotation); Quaternion qInv = Quaternion.Inverse(q); NoiseMethod2 method = Noise2.noiseMethods[(int)surface.type][surface.dimensions - 1]; float amplitude = surface.damping ? surface.strength / surface.frequency : surface.strength; for (int i = 0; i < _particles.Length; i++) { Vector3 position = _particles[i].position; Vector3 point = q * new Vector3(position.x, position.z) + surface.offset; NoiseSample sample = Noise2.Sum(method, point, surface.frequency, surface.octaves, surface.lacunarity, surface.persistence); sample = surface.type == NoiseMethodType2.Value ? (sample - 0.5f) : (sample * 0.5f); sample *= amplitude; sample.derivative = qInv * sample.derivative; Vector3 curl = new Vector3(sample.derivative.y, 0f, -sample.derivative.x); position += curl * Time.deltaTime * flowStrength; position.y = sample.value + _system.startSize; _particles[i].position = position; } }
// ... protected override Vector3 GetForce() { // I could also pre-multiply the frequency, but // all the octave variants also use frequency // within themselves, so it would cause redundant // multiplication. float xX = parameters.particlePosition.x + offsetX; float yX = parameters.particlePosition.y + offsetX; float zX = parameters.particlePosition.z + offsetX; float xY = parameters.particlePosition.x + offsetY; float yY = parameters.particlePosition.y + offsetY; float zY = parameters.particlePosition.z + offsetY; float xZ = parameters.particlePosition.x + offsetZ; float yZ = parameters.particlePosition.y + offsetZ; float zZ = parameters.particlePosition.z + offsetZ; Vector3 force; switch (noiseType) { case NoiseType.PseudoPerlin: { // This isn't really right, but... it gives believable-enough results. // It's also much faster than real perlin noise. // It works well where you don't have to animate a large field where // the repeating pattern would otherwise be easily seen. // Examples of good uses: smoke trail particle turbulence. // Example of bad uses: particle box simulating waves or something... float noiseX = Mathf.PerlinNoise(xX * frequency, yY * frequency); float noiseY = Mathf.PerlinNoise(xX * frequency, zY * frequency); float noiseZ = Mathf.PerlinNoise(xX * frequency, xY * frequency); noiseX = Mathf.Lerp(-1.0f, 1.0f, noiseX); noiseY = Mathf.Lerp(-1.0f, 1.0f, noiseY); noiseZ = Mathf.Lerp(-1.0f, 1.0f, noiseZ); Vector3 forceX = (Vector3.right * noiseX); Vector3 forceY = (Vector3.up * noiseY); Vector3 forceZ = (Vector3.forward * noiseZ); force = forceX + forceY + forceZ; break; } // ... default: case NoiseType.Perlin: { force.x = Noise2.perlin(xX * frequency, yX * frequency, zX * frequency); force.y = Noise2.perlin(xY * frequency, yY * frequency, zY * frequency); force.z = Noise2.perlin(xZ * frequency, yZ * frequency, zZ * frequency); return(force); } // ... case NoiseType.Simplex: { force.x = Noise2.simplex(xX * frequency, yX * frequency, zX * frequency); force.y = Noise2.simplex(xY * frequency, yY * frequency, zY * frequency); force.z = Noise2.simplex(xZ * frequency, yZ * frequency, zZ * frequency); break; } // ... case NoiseType.OctavePerlin: { force.x = Noise2.octavePerlin(xX, yX, zX, frequency, octaves, octaveMultiplier, octaveScale); force.y = Noise2.octavePerlin(xY, yY, zY, frequency, octaves, octaveMultiplier, octaveScale); force.z = Noise2.octavePerlin(xZ, yZ, zZ, frequency, octaves, octaveMultiplier, octaveScale); break; } case NoiseType.OctaveSimplex: { force.x = Noise2.octaveSimplex(xX, yX, zX, frequency, octaves, octaveMultiplier, octaveScale); force.y = Noise2.octaveSimplex(xY, yY, zY, frequency, octaves, octaveMultiplier, octaveScale); force.z = Noise2.octaveSimplex(xZ, yZ, zZ, frequency, octaves, octaveMultiplier, octaveScale); break; } } return(force); }