private void ApplyRandomParameters() { resolution = 400; frequency = Random.Range(1, 50); octaves = Random.Range(1, 8); lacunarity = Random.Range(1f, 4f); persistence = Random.Range(0f, 1f); dimensions = Random.Range(1, 3); type = (NoiseMethodType)Random.Range((int)NoiseMethodType.Perlin, (int)NoiseMethodType.Perlin); GradientColorKey[] gradientColorKeys = new GradientColorKey[Random.Range(3, 6)]; GradientAlphaKey[] gradientAlphaKeys = new GradientAlphaKey[2]; gradientAlphaKeys[0] = new GradientAlphaKey(); gradientAlphaKeys[1] = new GradientAlphaKey(); gradientAlphaKeys[0].time = 0; gradientAlphaKeys[0].alpha = 255; gradientAlphaKeys[1].time = 1; gradientAlphaKeys[1].alpha = 255; for (int i = 0; i < gradientColorKeys.Length; i++) { GradientColorKey gck = new GradientColorKey(); gck.color = Random.ColorHSV(); gck.time = Random.Range(0f, 1f); gradientColorKeys[i] = gck; } coloring.SetKeys(gradientColorKeys, gradientAlphaKeys); }
/// <summary> /// Reset all values /// </summary> public override void Reset() { resolution = 256; frequency = 1f; octaves = 1; lacunarity = 2f; persistence = 0.5f; dimensions = 3; type = NoiseMethodType.Perlin; GradientColorKey[] gck = new GradientColorKey[2]; gck[0].color = Color.red; gck[0].time = 0.0F; gck[1].color = Color.blue; gck[1].time = 1.0F; GradientAlphaKey[] gak = new GradientAlphaKey[2]; gak[0].alpha = 1.0F; gak[0].time = 0.0F; gak[1].alpha = 0.0F; gak[1].time = 1.0F; coloring = new Gradient(); coloring.SetKeys(gck, gak); result = null; everyFrame = true; } // Reset
private void OnGUI() { EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Texture Settings", EditorStyles.boldLabel); m_resolution = EditorGUILayout.IntField("Texture Resolution", m_resolution); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Noise Settings", EditorStyles.boldLabel); m_noiseMethod = (NoiseMethodType)EditorGUILayout.EnumPopup("Method", m_noiseMethod); m_noiseDimension = (DimensionType)EditorGUILayout.EnumPopup("Dimension", m_noiseDimension); if (m_noiseMethod != NoiseMethodType.Simplex) { m_noiseInterpolate = (InterpolateMethodType)EditorGUILayout.EnumPopup("Interpolate Type", m_noiseInterpolate); } m_generateNewHash = EditorGUILayout.Toggle("Generate New Hash", m_generateNewHash); m_noiseFrequency = EditorGUILayout.FloatField("Frequency", m_noiseFrequency); m_noiseOctave = EditorGUILayout.IntField("Octave", m_noiseOctave); m_noiseLacunarity = EditorGUILayout.FloatField("Lacunarity", m_noiseLacunarity); m_noisePersistence = EditorGUILayout.FloatField("Persistence", m_noisePersistence); OnShowButtons(); EditorGUILayout.EndVertical(); }
} // 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 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(); }
public TerrainChunkSettings(int heightmapResolution, int alphamapResolution, int length, int height, NoiseMethodType noiseType, int dimensions) { HeightmapResolution = heightmapResolution; AlphamapResolution = alphamapResolution; Length = length; Height = height; this.noiseType = noiseType; this.dimensions = dimensions; }
public static Texture2D Create( int resolution = 256, float frequency = 6f, int octaves = 6, float lacunarity = 2f, float persistence = 0.5f, int dimensions = 2, NoiseMethodType type = NoiseMethodType.Perlin, Gradient coloring = null) { // This is the default gradient if the user doesn't provide one. if (coloring == null) { // From black to white. GradientColorKey[] gradientColorKeys = new GradientColorKey[2]; gradientColorKeys[0].color = Color.black; gradientColorKeys[0].time = 0f; gradientColorKeys[1].color = Color.white; gradientColorKeys[1].time = 1f; // Full alpha. GradientAlphaKey[] gradientAlphaKeys = new GradientAlphaKey[2]; gradientAlphaKeys[0].alpha = 1f; gradientAlphaKeys[0].time = 0f; gradientAlphaKeys[1].alpha = 1f; gradientAlphaKeys[1].time = 1f; coloring = new Gradient(); coloring.SetKeys(gradientColorKeys, gradientAlphaKeys); } Texture2D result = new Texture2D(resolution, resolution, TextureFormat.RGB24, true); result.name = "Procedural Texture"; result.wrapMode = TextureWrapMode.Clamp; result.filterMode = FilterMode.Trilinear; result.anisoLevel = 9; FillTextureWithNoise( result, resolution, frequency, octaves, lacunarity, persistence, dimensions, type, coloring); return(result); }
public static void FillTextureWithNoise( Texture2D texture, int resolution = 256, float frequency = 1f, int octaves = 1, float lacunarity = 2f, float persistence = 0.5f, int dimensions = 3, NoiseMethodType type = NoiseMethodType.Perlin, Gradient coloring = null) { 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); // NOTE: Removed conversion from local to world space, as it appears to be unnecessary. -Casper 2017-09-14 /* * 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).value; sample = sample * 0.5f + 0.5f; texture.SetPixel(x, y, coloring.Evaluate(sample)); } } texture.Apply(); }
// TODO: super ugly code ! public static Texture2D RandomTexture(Transform transform, int resolution = 256, FilterMode filterMode = FilterMode.Trilinear, int anisolevel = 9, NoiseMethodType type = NoiseMethodType.Perlin, int dimensions = 3, float frequency = 1f, int octaves = 1, float lacunarity = 2f, float persistence = 0.5f, Gradient gradient = null) { var texture = new Texture2D(resolution, resolution, TextureFormat.RGB24, true) { name = "Procedural Texture", wrapMode = TextureWrapMode.Clamp, filterMode = filterMode, anisoLevel = anisolevel }; texture.FillTexture(transform, resolution, type, dimensions, frequency, octaves, lacunarity, persistence, gradient); return(texture); }
public static float GetValue(Vector3 point, NoiseMethodType noiseMethodType, DimensionType dimension, InterpolateMethodType interpolate, float frequency, int octave, float lacunarity, float persistence) { float value = 0; switch (noiseMethodType) { case NoiseMethodType.Value: value = FractalSum(point, m_valueMethods[(int)dimension], m_interpolateMethods[(int)interpolate], frequency, octave, lacunarity, persistence); break; case NoiseMethodType.Perlin: value = FractalSum(point, m_perlinMethods[(int)dimension], m_interpolateMethods[(int)interpolate], frequency, octave, lacunarity, persistence); break; case NoiseMethodType.Simplex: value = FractalSum(point, m_simplexMethods[(int)dimension], m_interpolateMethods[(int)interpolate], frequency, octave, lacunarity, persistence); break; } return(value); }