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)); NoiseMethod method = Noise.methods[(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); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } texture.SetPixel(x, y, Color.white * sample); } } texture.Apply(); }
private void PositionParticles() { Quaternion q = Quaternion.Euler(surface.rotation); Quaternion qInv = Quaternion.Inverse(q); NoiseMethod method = Noise.methods[(int)surface.noiseType][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.y) + surface.offset; //Color temp = new Color((position.z + surface.offset.y)*255,system.startColor.g, system.startColor.b); //system.startColor = temp; float sample = Noise.Sum(method, point, surface.frequency, surface.octaves, surface.lacunarity, surface.persistence); //sample = sample * 0.5f; sample *= amplitude; position += Vector3.one * Time.deltaTime * flowStrength; position.y = sample + system.startSize; particles[i].position = position; if (position.x < -0.5f || position.x > 0.5f || position.z < -0.5f || position.z > 0.5f) { particles[i].lifetime = 0f; } } }
/* public void FillTexture()//random * { * if (texture.width != resolution) * { * texture.Resize(resolution, resolution); * } * * Vector3 point00 = new Vector3(-0.5f, -0.5f); * Vector3 point10 = new Vector3(0.5f, -0.5f); * Vector3 point01 = new Vector3(-0.5f, 0.5f); * Vector3 point11 = new Vector3(0.5f, 0.5f); * * * float stepSize = 1f / resolution; * Random.seed = 42; * 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); * texture.SetPixel(x, y, Color.white * Random.value); * } * } * texture.Apply(); * }*/ public void FillTexture()//value//perlin { if (texture.width != resolution) { texture.Resize(resolution, resolution); } Vector3 point00 = new Vector3(-0.5f, -0.5f); Vector3 point10 = new Vector3(0.5f, -0.5f); Vector3 point01 = new Vector3(-0.5f, 0.5f); Vector3 point11 = new Vector3(0.5f, 0.5f); 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); texture.SetPixel(x, y, Color.white * NoiseMethod.Value3D(point, 22)); // } } texture.Apply(); }
private void FillTexture() { NoiseMethod method = Noise.noiseMethods[(int)type][dimensions - 1]; float stepSize = 1f / 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)); 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); NoiseSample sample = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); sample = sample * 0.5f + 0.5f; texture.SetPixel(x, y, Color.white * coloring.Evaluate(sample.value)); } } texture.Apply(); }
public void FillTexture() { //If the resolution field has changed, resize the texture. 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)); NoiseMethod method = Noise.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); //_texture.SetPixel(x, y, new Color(point.x, point.y, point.z)); float sample = Noise.Sum(method, point, Frequency, octaves, lacunarity, persistence); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } _texture.SetPixel(x, y, Color.white * sample); } } _texture.Apply(); }
public void Refresh() { if (resolution != currentResolution) { CreateGrid(); } Quaternion q = Quaternion.Euler(rotation); 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; NoiseMethod method = Noise.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 = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); sample = type == NoiseMethodType.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 *= strength; if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } vertices[v].y = sample.value; // colors[v] = coloring.Evaluate(sample + 0.5f); } } mesh.vertices = vertices; mesh.colors = colors; CalculateNormals(); mesh.normals = normals; }
/** * Generate a new procedural texture * http://catlikecoding.com/unity/tutorials/noise/ */ private void generateTexture(Texture2D texture, Transform parent, TextureParameters args) { int scale = args.scale; int resolution = args.resolution; Vector3 point00 = parent.TransformPoint(new Vector3(seed, seed)); Vector3 point10 = parent.TransformPoint(new Vector3(seed + scale, seed)); Vector3 point01 = parent.TransformPoint(new Vector3(seed, seed + scale)); Vector3 point11 = parent.TransformPoint(new Vector3(seed + scale, seed + scale)); NoiseMethod method = Noise.methods[(int)type][args.dimensions - 1]; float stepSize = 1.0f / 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 = Noise.Sum(method, point, args.frequency, args.octaves, args.lacunarity, args.persistence); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } texture.SetPixel(x, y, new Color(sample, sample, sample)); } } texture.Apply(); }
public static float[,] GenerateNoiseMap(int width, int length, int depth, Vector3 pos, float frequency, int octaves, float lacunarity, float persistence) { float[,] noiseMap = new float[width, length]; NoiseMethod method = noiseMethods[1][1]; Vector3 point00 = pos + new Vector3(-0.5f, 0, -0.5f); Vector3 point10 = pos + new Vector3(0.5f, 0, -0.5f); Vector3 point01 = pos + new Vector3(-0.5f, 0, 0.5f); Vector3 point11 = pos + new Vector3(0.5f, 0, 0.5f); float stepSize = 1f / length; for (int x = 0; x < length; x++) { Vector3 point0 = Vector3.Lerp(point00, point01, (x + 0.5f) * stepSize); Vector3 point1 = Vector3.Lerp(point10, point11, (x + 0.5f) * stepSize); for (int z = 0; z < length; z++) { Vector3 point = Vector3.Lerp(point0, point1, (z + 0.5f) * stepSize); float sample = Sum(method, point, frequency, octaves, lacunarity, persistence); sample = sample * 0.5f + 0.5f; sample *= depth; noiseMap[x, z] = sample; } } return(noiseMap); }
public void Generate() { float[,] terrainheights = new float[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)); NoiseMethod method = Noise.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); //_texture.SetPixel(x, y, new Color(point.x, point.y, point.z)); float sample = Noise.Sum(method, point, Frequency, octaves, lacunarity, persistence); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } terrainheights[y, x] = sample * NoiseScale; } } terrainheights = GenerateMountainAddativley(terrainheights); terrain.terrainData.SetHeightsDelayLOD(0, 0, terrainheights); }
} // OnUpdate /// <summary> /// Compute and store the current perlin noise. /// </summary> private void ComputeNoise() { if (_texture.width != resolution.Value) { _texture.Resize(resolution.Value, resolution.Value); } _go = Fsm.GetOwnerDefaultTarget(gameObject); if (_go != null) { _t = _go.transform; } if (_t != null) { point00 = _t.TransformPoint(new Vector3(-0.5f, -0.5f)); point10 = _t.TransformPoint(new Vector3(0.5f, -0.5f)); point01 = _t.TransformPoint(new Vector3(-0.5f, 0.5f)); point11 = _t.TransformPoint(new Vector3(0.5f, 0.5f)); } else { point00 = new Vector3(-0.5f, -0.5f); point10 = new Vector3(0.5f, -0.5f); point01 = new Vector3(-0.5f, 0.5f); point11 = new Vector3(0.5f, 0.5f); } NoiseMethod method = Noise.methods[(int)type][dimensions.Value - 1]; float stepSize = 1f / resolution.Value; for (int y = 0; y < resolution.Value; 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.Value; x++) { Vector3 point = Vector3.Lerp(point0, point1, (x + 0.5f) * stepSize); float sample = Noise.Sum(method, point, frequency.Value, octaves.Value, lacunarity.Value, persistence.Value); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } _texture.SetPixel(x, y, coloring.Evaluate(sample)); } } _texture.Apply(); _resolution = resolution.Value; _frequency = frequency.Value; _octaves = octaves.Value; _lacunarity = lacunarity.Value; _persistence = persistence.Value; _dimensions = dimensions.Value; _type = type; _coloring = coloring; }
public DiamondSquare() { pTypes = new NoiseMethod[] { DiamondP2 }; colors = new Color32[res * res]; }
public override NoiseSample interpolate(float x, float y, float z) { NoiseMethod method = Noise.methods[(int)NoiseMethodType.Simplex][2]; NoiseSample sample = Noise.Sum(method, new Vector3(x, y, z), 1, 2, 2, .5f); return(sample); }
public void FillTexture() { 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)); if (texture.width != resolution) { texture.Resize(resolution, resolution); } NoiseMethod method = Noise.noiseMethods[(int)type][dimensions - 1]; float stepSize = 1f / resolution; for (int i = 0; i < resolution; i++) { Vector3 point0 = Vector3.Lerp(point00, point01, (i + 0.5f) * stepSize); Vector3 point1 = Vector3.Lerp(point10, point11, (i + 0.5f) * stepSize); for (int j = 0; j < resolution; j++) { Vector3 point = Vector3.Lerp(point0, point1, (j + 0.5f) * stepSize); float sample = Noise.Sum(method, point, frequency, octaves, lacunarity, gain); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } texture.SetPixel(j, i, new Color(sample, sample, sample)); } } texture.Apply(); }
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)); //Ici nous choisissons quel bruit utilisé et quelles dimensions lui appliquer NoiseMethod method = Noise.noiseMethods[(int)type][dimensions - 1]; float stepSize = 1f / resolution; Random.InitState(42); for (int y = 0; y < resolution; y++) { //Linear interpolation (Lerp) 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); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } texture.SetPixel(x, y, Color.white * sample); } } texture.Apply(); }
public void FillTexture() { if (texture.width != resolution) { texture.Resize(resolution, resolution); } // getting the corners of the quad Vector3 bottomLeft = transform.TransformPoint(new Vector3(-.5f, -.5f)); Vector3 bottomRight = transform.TransformPoint(new Vector3(.5f, -.5f)); Vector3 topLeft = transform.TransformPoint(new Vector3(-.5f, .5f)); Vector3 topRight = transform.TransformPoint(new Vector3(.5f, .5f)); // set the color of the pixel based on its Global coordinates NoiseMethod method = Noise.noiseMethods[(int)noiseType][dimensions - 1]; float stepSize = 1f / resolution; for (int y = 0; y < resolution; y++) { Vector3 point0 = Vector3.Lerp(bottomLeft, topLeft, (y + .5f) * stepSize); Vector3 point1 = Vector3.Lerp(bottomRight, topRight, (y + .5f) * stepSize); for (int x = 0; x < resolution; x++) { Vector3 point = Vector3.Lerp(point0, point1, (x + .5f) * stepSize); float sampledPoint = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); // needed so that the Perlin value returned is >= 0 if (noiseType == NoiseMethodType.Perlin) { sampledPoint = sampledPoint * 0.5f + 0.5f; } texture.SetPixel(x, y, coloring.Evaluate(sampledPoint)); } } texture.Apply(); }
void MakeNoise() { Quaternion q = Quaternion.Euler(rotation); Vector3 point00 = q * transform.TransformPoint(new Vector3(-0.5f, -0.5f)); Vector3 point10 = q * transform.TransformPoint(new Vector3(0.5f, -0.5f)); Vector3 point01 = q * transform.TransformPoint(new Vector3(-0.5f, 0.5f)); Vector3 point11 = q * transform.TransformPoint(new Vector3(0.5f, 0.5f)); NoiseMethod method = Noise.noiseMethods[(int)type][dimensions - 1]; float stepSize = 1f / resolution; 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); float sample = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); sample = type == NoiseMethodType.Value ? (sample - 0.5f) : (sample * 0.5f); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } colors[v] = coloring.Evaluate(sample); vertices[v].y = Mathf.Lerp(0, maxHeight, sample); } } mesh.vertices = vertices; mesh.colors = colors; mesh.RecalculateNormals(); }
private void PositionParticles() { Quaternion q = Quaternion.Euler(surface.rotation); Quaternion qInv = Quaternion.Inverse(q); NoiseMethod method = Noise.methods[(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 = Noise.Sum(method, point, surface.frequency, surface.octaves, surface.lacunarity, surface.persistence); sample = 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.main.startSize.constant; particles[i].position = position; if (position.x < -0.5f || position.x > 0.5f || position.z < -0.5f || position.z > 0.5f) { particles[i].remainingLifetime = 0f; } } }
public void FillHeights() { int resolution = terrainData.heightmapWidth; terrainHeight = terrainData.heightmapHeight; heights = new float[resolution, resolution]; Vector3 point00 = position + new Vector3(-0.5f, -0.5f); Vector3 point10 = position + new Vector3(0.5f, -0.5f); Vector3 point01 = position + new Vector3(-0.5f, 0.5f); Vector3 point11 = position + new Vector3(0.5f, 0.5f); NoiseMethod method = Noise.methods[(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 = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } heights[y, x] = (sample * amplitude * terrainData.heightmapWidth * (1f / 100f)) + (offset); } } terrainData.SetHeights(0, 0, heights); }
public override void FillColor() { if (skin.width != res) { skin.Resize(res, res); } Vector3 vecaa = obj.TransformPoint(new Vector3(-0.5f, -0.5f)); Vector3 vecba = obj.TransformPoint(new Vector3(0.5f, -0.5f)); Vector3 vecab = obj.TransformPoint(new Vector3(-0.5f, 0.5f)); Vector3 vecbb = obj.TransformPoint(new Vector3(0.5f, 0.5f)); NoiseMethod method = pTypes[0]; float r1, r2, r3, r4; r1 = Random.value; r2 = Random.value; r3 = Random.value; r4 = Random.value; iterateDivision(0.0f, 0.0f, res, r1, r2, r3, r4); skin.SetPixels32(colors); skin.Apply(); }
public NoiseCommand(NoiseMethod method, int x, int y, int z, int octaves, float frequency, float lacunarity, float persistance) { this.x = x; this.y = y; this.z = z; this.octaves = octaves; this.frequency = frequency; this.lacunarity = lacunarity; this.persistance = persistance; this.method = method; }
public void FillTexture() { if (texture.width != resolution) { texture.Resize(resolution, resolution); } NoiseMethod method = Noise.noiseMethods[(int)type][dimension - 1]; Vector3 point00 = transform.TransformPoint(new Vector3(-0.5f, -0.5f)); Vector3 point01 = transform.TransformPoint(new Vector3(0.5f, -0.5f)); Vector3 point10 = transform.TransformPoint(new Vector3(-0.5f, 0.5f)); Vector3 point11 = transform.TransformPoint(new Vector3(0.5f, 0.5f)); float stepSize = 1.0f / resolution; for (int y = 0; y < resolution; y++) { Vector3 point0 = Vector3.Lerp(point00, point10, (y + 0.5f) * stepSize); Vector3 point1 = Vector3.Lerp(point01, point11, (y + 0.5f) * stepSize); for (int x = 0; x < resolution; x++) { Vector3 point = Vector3.Lerp(point0, point1, (x + 0.5f) * stepSize); //texture.SetPixel(x, y, new Color(point.x, point.y, point.z)); //texture.SetPixel(x, y, Color.white * method(point, frequency)); texture.SetPixel(x, y, Color.white * Noise.Sum(method, point, frequency, octaves, lacunarity, persistence)); } } texture.Apply(); }
public void Refresh() { if (resolution != _currentResolution) { CreateGrid(); } Quaternion q = Quaternion.Euler(rotation); 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; NoiseMethod method = Noise.noiseMethods[(int)type][dimensions - 1]; float stepSize = 1f / resolution; 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); float sample = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); sample = type == NoiseMethodType.Value ? (sample - 0.5f) : (sample * 0.5f); _vertices[v].y = sample; _colors[v] = coloring.Evaluate(sample + 0.5f); } } _mesh.vertices = _vertices; _mesh.colors = _colors; _mesh.RecalculateNormals(); }
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)); NoiseMethod method = Noise.noiseMethods[(int)type] [dimensions - 1]; float stepSize = 1f / resolution; Random.seed = 42; 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 = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } texture.SetPixel(x, y, coloring.Evaluate(sample)); } } texture.Apply(); }
public override void FillColor() { if (hash.Length != 512) { resetHash(); } Debug.Log("Hash Length = " + hash.Length); if (skin.width != res) { skin.Resize(res, res); } Vector3 vecaa = obj.TransformPoint(new Vector3(-0.5f, -0.5f)); Vector3 vecba = obj.TransformPoint(new Vector3(0.5f, -0.5f)); Vector3 vecab = obj.TransformPoint(new Vector3(-0.5f, 0.5f)); Vector3 vecbb = obj.TransformPoint(new Vector3(0.5f, 0.5f)); NoiseMethod method = pTypes[dimensions - 2]; float step = 1.0f / res; for (int y = 0; y < res; y++) { Vector3 veca = Vector3.Lerp(vecaa, vecab, (y + 0.5f) * step); Vector3 vecb = Vector3.Lerp(vecba, vecbb, (y + 0.5f) * step); for (int x = 0; x < res; x++) { Vector3 vec = Vector3.Lerp(veca, vecb, (x + 0.5f) * step); float sample = Sum(method, vec, frequency, octaves, lacunarity, persistence, hash); skin.SetPixel(x, y, color.Evaluate(sample)); } } skin.Apply(); }
private void PositionParticles() { Quaternion q = Quaternion.Euler(rotation); Quaternion qInv = Quaternion.Inverse(q); NoiseMethod method = Noise.methods[(int)type][dimensions - 1]; float amplitude = damping ? strength / frequency : strength; morphOffset += Time.deltaTime * morphSpeed; if (morphOffset > 256f) { morphOffset -= 256f; } for (int i = 0; i < particles.Length; i++) { Vector3 position = particles[i].position; Vector3 point = q * new Vector3(position.z, position.y, position.x + morphOffset) + offset; NoiseSample sampleX = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); sampleX *= amplitude; sampleX.derivative = qInv * sampleX.derivative; point = q * new Vector3(position.x + 100f, position.z, position.y + morphOffset) + offset; NoiseSample sampleY = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); sampleY *= amplitude; sampleY.derivative = qInv * sampleY.derivative; point = q * new Vector3(position.y, position.x + 100f, position.z + morphOffset) + offset; NoiseSample sampleZ = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); sampleZ *= amplitude; sampleZ.derivative = qInv * sampleZ.derivative; Vector3 curl; curl.x = sampleZ.derivative.x - sampleY.derivative.y; curl.y = sampleX.derivative.x - sampleZ.derivative.y; curl.z = sampleY.derivative.x - sampleX.derivative.y; particles[i].velocity = curl; } }
/* private Vector3[] GetVertives() * { * int sum = Mathf.FloorToInt((segment.x + 1) * (segment.y + 1)); * float w = size.x / segment.x; * float h = size.y / segment.y; * * int index = 0; * GetUV(); * GetTriangles(); * vertives = new Vector3[sum]; * for (int i = 0; i < segment.y + 1; i++) * { * for (int j = 0; j < segment.x + 1; j++) * { * float tempHeight = 0; * if (heightMap != null) * { * tempHeight = GetHeight(heightMap, uvs[index]); * } * vertives[index] = new Vector3(j * w, tempHeight, i * h); * index++; * } * } * return vertives; * } */ /* private Vector3[] GetVertives()//texture * { * int sum = Mathf.FloorToInt((segment.x + 1) * (segment.y + 1)); * float w = size.x / segment.x; * float h = size.y / segment.y; * * int index = 0; * GetUV(); * GetTriangles(); * vertives = new Vector3[sum]; * Vector3 point00 = new Vector3(-0.5f, -0.5f); * Vector3 point10 = new Vector3(0.5f, -0.5f); * Vector3 point01 = new Vector3(-0.5f, 0.5f); * Vector3 point11 = new Vector3(0.5f, 0.5f); * * * * Random.seed = 42; * * * for (int i = 0; i < segment.y + 1; i++) * { * for (int j = 0; j < segment.x + 1; j++) * { * float tempHeight = 0; * // if ((i+j)%5 == 0) * { * * tempHeight =Mathf.Abs (heightMap.GetPixel(i , j ).r -0.5f)*2* 10;// * * } * vertives[index] = new Vector3(j * w, tempHeight, i * h); * index++; * } * } * return vertives; * } */ /* private Vector3[] GetVertives()//perlin * { * * * * int sum = Mathf.FloorToInt((segment.x + 1) * (segment.y + 1)); * float w = size.x / segment.x; * float h = size.y / segment.y; * * int index = 0; * GetUV(); * GetTriangles(); * vertives = new Vector3[sum]; * * * int resolution = 256; * Vector3 point00 = new Vector3(-0.5f, -0.5f); * Vector3 point10 = new Vector3(0.5f, -0.5f); * Vector3 point01 = new Vector3(-0.5f, 0.5f); * Vector3 point11 = new Vector3(0.5f, 0.5f); * * * float stepSize = 1f / resolution; * * * * Random.seed = 42; * * * for (int i = 0; i < segment.y + 1; i++) * { * Vector3 point0 = Vector3.Lerp(point00, point01, (i + 0.5f) * stepSize); * Vector3 point1 = Vector3.Lerp(point10, point11, (i + 0.5f) * stepSize); * for (int j = 0; j < segment.x + 1; j++) * { * float tempHeight = 0; * // if ((i+j)%5 == 0) * { * Vector3 point = Vector3.Lerp(point0, point1, (j + 0.5f) * stepSize); * * tempHeight = NoiseMethod.Perlin2D(point, 22) * 10;// * * } * vertives[index] = new Vector3(j * w, tempHeight, i * h); * index++; * } * } * return vertives; * }*/ private Vector3[] GetVertives()//Fractal Noise { float frequency = 5; //[Range(1, 8)] int octaves = 6;//1普通,8烟雾状 // [Range(1f, 4f)] float lacunarity = 2f; // [Range(0f, 1f)] float persistence = 0.5f; int sum = Mathf.FloorToInt((segment.x + 1) * (segment.y + 1)); float w = size.x / segment.x; float h = size.y / segment.y; int index = 0; GetUV(); GetTriangles(); vertives = new Vector3[sum]; int resolution = 256; Vector3 point00 = new Vector3(-0.5f, -0.5f); Vector3 point10 = new Vector3(0.5f, -0.5f); Vector3 point01 = new Vector3(-0.5f, 0.5f); Vector3 point11 = new Vector3(0.5f, 0.5f); float stepSize = 1f / resolution; Random.seed = 42; for (int i = 0; i < segment.y + 1; i++) { Vector3 point0 = Vector3.Lerp(point00, point01, (i + 0.5f) * stepSize); Vector3 point1 = Vector3.Lerp(point10, point11, (i + 0.5f) * stepSize); for (int j = 0; j < segment.x + 1; j++) { float tempHeight = 0; // if ((i+j)%5 == 0) { Vector3 point = Vector3.Lerp(point0, point1, (j + 0.5f) * stepSize); float sample = NoiseMethod.Sum(1, point, frequency, octaves, lacunarity, persistence); tempHeight = sample * 20;// } vertives[index] = new Vector3(j * w, tempHeight, i * h); index++; } } return(vertives); }
public static void FillTexture(this Texture2D texture, Transform transform, int resolution = 256, NoiseMethodType type = NoiseMethodType.Perlin, int dimensions = 3, float frequency = 1f, int octaves = 1, float lacunarity = 2f, float persistence = 0.5f, Gradient gradient = null) { if (gradient == null) { gradient = new Gradient(); // Populate the color keys at the relative time 0 and 1 (0 and 100%) var colorKey = new GradientColorKey[3]; colorKey[0].color = Color.red; colorKey[0].time = 0.0f; colorKey[1].color = Color.blue; colorKey[1].time = 0.5f; colorKey[2].color = Color.green; colorKey[2].time = 1.0f; // Populate the alpha keys at relative time 0 and 1 (0 and 100%) var alphaKey = new GradientAlphaKey[3]; alphaKey[0].alpha = 1.0f; alphaKey[0].time = 0.0f; alphaKey[1].alpha = 1.0f; alphaKey[1].time = 0.5f; alphaKey[2].alpha = 1.0f; alphaKey[2].time = 1.0f; gradient.SetKeys(colorKey, alphaKey); } 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)); NoiseMethod method = Noise.methods[(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 = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence); if (type != NoiseMethodType.Value) { sample = sample * 0.5f + 0.5f; } texture.SetPixel(x, y, gradient.Evaluate(sample)); } } texture.Apply(); }
// Start is called before the first frame update void Start() { surfaceTexture = new Texture2D(resolution, resolution, TextureFormat.ARGB32, false); mRender = gameObject.GetComponent <MeshRenderer>(); method = Noise.methods[(int)type][dimensions - 1]; DrawBiomeMask(); }
public Perlin() { pTypes = new NoiseMethod[] { Perlin2D, Perlin3D }; hash = new int[0]; }
public NoiseCommand(NoiseMethod method, int x, int y, int z, float frequency) { this.x = x; this.y = y; this.z = z; this.frequency = frequency; this.method = method; octaves = 1; lacunarity = 2f; persistance = 1.0f; }
public static float Sum (NoiseMethod method, Vector3 point, float frequency, int octaves, float lacunarity, float persistence) { float sum = method(point, frequency); float amplitude = 1f; float range = 1f; for (int o = 1; o < octaves; o++) { frequency *= lacunarity; amplitude *= persistence; range += amplitude; sum += method(point, frequency) * amplitude; } return sum / range; }
public static float MultiFractal(NoiseMethod noiseFunc, Vector3 point, float frequency, int octaves, float lacunarity, float persistence) { float value = 1.0f; float pwr = 1.0f; //float pwHL = Mathf.Pow(lacunarity, -H); for (int i = 0; i < octaves; i++) { value *= (pwr * noiseFunc(point, frequency) + 2.0f); pwr *= persistence; frequency *= lacunarity; } return value; /* for (i = 0; i < (int)octaves; i++) { value *= (pwr * noisefunc(x, y, z) + 1.0f); pwr *= pwHL; x *= lacunarity; y *= lacunarity; z *= lacunarity; } for (i = 0; i < (int)octaves; i++) { value *= (pwr * noisefunc(x, y, z) + 1.0f); pwr *= pwHL; x *= lacunarity; y *= lacunarity; z *= lacunarity; } rmd = octaves - floorf(octaves); if (rmd != 0.0f) value *= (rmd * noisefunc(x, y, z) * pwr + 1.0f); */ }
// aka fBm public static float fBm(NoiseMethod noiseFunc, Vector3 point, float frequency, int octaves, float lacunarity, float persistence) { float value = 0.0f; float power = 1f; float range = power; float pwHL = Mathf.Pow(lacunarity, -frequency); for (int o = 1; o < octaves; o++) { value += noiseFunc(point, frequency) * power; frequency *= lacunarity; power *= persistence; range += power; } return value/range; /* for (i = 0; i < (int)octaves; i++) { value += noisefunc(x, y, z) * pwr; pwr *= pwHL; x *= lacunarity; y *= lacunarity; z *= lacunarity; } rmd = octaves - floorf(octaves); if (rmd != 0.f) value += rmd * noisefunc(x, y, z) * pwr; */ }