Skip to content

Commit

Permalink
#3 Setting up multiple Noise Filters (need to insert Conditional scri…
Browse files Browse the repository at this point in the history
…pts for editor)
  • Loading branch information
Gizmotronn committed Nov 1, 2021
1 parent ca68efd commit 5417ce4
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class NoiseFilterFactory {
public static INoiseFilter CreateNoiseFilter(NoiseSettings settings) {
// Type of noise that will be created depends on the filter type specifed in the settings (NoiseSettings.cs main class)
switch(settings.filterType) {
case NoiseSettings.FilterType.Simple:
return new SimpleNoiseFilter(settings.simpleNoiseSettings);
case NoiseSettings.FilterType.Rigid:
return new RigidNoiseFilter(settings.rigidNoiseSettings);
}
return null;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions Assets/PCG/Planet Procedural Generation/Scripts/NoiseSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@

[System.Serializable]
public class NoiseSettings {
// Noise attributes
public float strength = 1; // Strength of the noise
[Range(1,8)]
public int numLayers = 1;
public float baseRoughness = 1;
public float roughness = 2;
public float persistence = .5; // Half the amplitude with each layer
public Vector3 centre; // Allow us to move the noise around
public float minValue;
public enum FilterType { Simple, Rigid };
public FilterType filterType;

public SimpleNoiseSettings simpleNoiseSettings;
public RigidNoiseSettings rigidNoiseSettings;

[System.Serializable]
public class SimpleNoiseSettings {
// Noise attributes
public float strength = 1; // Strength of the noise

[Range(1,8)]
public int numLayers = 1;
public float baseRoughness = 1;
public float roughness = 2;
public float persistence = .5; // Half the amplitude with each layer
public Vector3 centre; // Allow us to move the noise around
public float minValue;
}

[System.Serializable]
public class RigidNoiseSettings : SimpleNoiseSettings {
public float weightMultiplier = .8f;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RigidNoiseFilter : INoiseFilter {
NoiseSettings.RigidNoiseSettings settings;
Noise noise = new Noise();

public RigidNoiseFilter(NoiseSettings.RigidNoiseSettings settings) {
this.settings = settings;
}

public float Evaluate(Vector3 point) {
float noiseValue = 0;
float frequency = settings.baseRoughness;
float amplitude = 1;
float weight = 1;

for(int i = 0; i < settings.numLayers; i++) {
float v = 1-Mathf.Abs(noise.Evaluate(point*frequency+settings.centre));
v *= v;// Make ridges more pronounced by squaring the value
v *= weight;
weight = Mathf.Clamp01(v * settings.weightMultiplier);
noiseValue += v * amplitude;
frequency *= settings.roughness;
amplitude *= settings.persistence;
}

noiseValue = Mathf.Max(0, noiseValue - settings.minValue);
return noiseValue * settings.strength;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

public class ShapeGenerator {
ShapeSettings settings;
NoiseFilter[] noiseFilters;
INoiseFilter[] noiseFilters;

public ShapeGenerator(ShapeSettings settings)
{
this.settings = settings;
noiseFilters = new NoiseFilter[settings.noiseLayers.Length];
noiseFilters = new INoiseFilter[settings.noiseLayers.Length];
for(int i = 0; i < noiseFilters.Length; i++) {
noiseFilters[i] = new NoiseFilter(settings.noiseLayers[i].noiseSettings);
noiseFilters[i] = NoiseFilterFactory.CreateNoiseFilter(settings.noiseLayers[i].noiseSettings);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using System.Collections.Generic;
using UnityEngine;

public class NoiseFilter {
NoiseSettings settings;
public class SimpleNoiseFilter : INoiseFilter {
NoiseSettings.SimpleNoiseSettings settings;
Noise noise = new Noise();

public NoiseFilter(NoiseSettings settings) {
public SimpleeNoiseFilter(NoiseSettings.SimpleNoiseSettings settings) {
this.settings = settings;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface INoiseFilter {
// Define the method that each class has to have to qualify as a noise filter
float Evaluate(Vector3 point);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5417ce4

Please sign in to comment.