Skip to content

Instantly share code, notes, and snippets.

@HappyFaceIndustries
Created June 4, 2015 04:17
Show Gist options
  • Save HappyFaceIndustries/c1b22de4727c8a867fc0 to your computer and use it in GitHub Desktop.
Save HappyFaceIndustries/c1b22de4727c8a867fc0 to your computer and use it in GitHub Desktop.
Parser and PQSMod for PQSMod_HeightColorRamp
//// ---------- HeightColorRamp.cs ---------- ////
using System;
using UnityEngine;
using KopernicusExpansion.PQSMods;
using Kopernicus.Configuration;
namespace Kopernicus.Configuration.ModLoader
{
[RequireConfigType(ConfigType.Node)]
public class HeightColorRamp : ModLoader, IParserEventSubscriber
{
private PQSMod_HeightColorRamp _mod;
public HeightColorRamp()
{
GameObject modObject = new GameObject("HeightColorRamp");
modObject.transform.parent = Utility.Deactivator;
_mod = modObject.AddComponent<PQSMod_HeightColorRamp> ();
base.mod = _mod;
}
[ParserTarget("ColorRamp", optional = false)]
public ColorRampParser colorRamp
{
set{_mod.Ramp = value.colorRamp;}
}
[ParserTarget("Simplex", optional = true)]
public SimplexParser simplex
{
set{_mod.simplex = value.simplex;}
}
[ParserTarget("BaseColorBias", optional = true)]
public NumericParser<float> BaseColorBias
{
set{_mod.BaseColorBias = value.value;}
}
[ParserTarget("blend", optional = true)]
public NumericParser<float> blend
{
set{_mod.blend = value.value;}
}
//we're not using these, but they're required
void IParserEventSubscriber.Apply(ConfigNode node) { }
void IParserEventSubscriber.PostApply(ConfigNode node) { }
}
[RequireConfigType(ConfigType.Node)]
public class SimplexParser : IParserEventSubscriber
{
public Simplex simplex;
public SimplexParser()
{
simplex = new Simplex (0, 5, 0.5, 10);
}
[ParserTarget("seed", optional = false)]
public NumericParser<int> seed
{
set{simplex.seed = value.value;}
}
[ParserTarget("frequency", optional = true)]
public NumericParser<double> frequency
{
set{simplex.frequency = value.value;}
}
[ParserTarget("persistence", optional = true)]
public NumericParser<double> persistence
{
set{simplex.persistence = value.value;}
}
[ParserTarget("octaves", optional = true)]
public NumericParser<int> octaves
{
set{simplex.octaves = (int)value.value;}
}
//we're not using these, but they're required
void IParserEventSubscriber.Apply(ConfigNode node)
{
}
void IParserEventSubscriber.PostApply(ConfigNode node) { }
}
[RequireConfigType(ConfigType.Node)]
public class ColorRampParser : IParserEventSubscriber
{
public PQSMod_HeightColorRamp.ColorRamp colorRamp;
void IParserEventSubscriber.Apply(ConfigNode node)
{
colorRamp = new PQSMod_HeightColorRamp.ColorRamp ();
foreach (var value in node.GetValuesStartsWith("key"))
{
string[] things = value.Split (new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries);
if (things.Length < 4)
{
throw new ArgumentException ("Cannot parse a key with less than 4 numbers. Keep in mind that the numbers must be seperated by commas");
}
float height = float.Parse(things[0]);
float r = float.Parse(things[1]);
float g = float.Parse(things[2]);
float b = float.Parse(things[3]);
float rn = r;
float gn = g;
float bn = b;
if (things.Length >= 7)
{
rn = float.Parse (things [4]);
gn = float.Parse (things [5]);
bn = float.Parse (things [6]);
}
colorRamp.Add (r, g, b, rn, gn, bn, height);
Logger.Active.Log ("ColorRampKey: " + height + " : " + r + ", " + g + ", " + b + " : " + rn + ", " + gn + ", " + bn);
}
}
void IParserEventSubscriber.PostApply(ConfigNode node) { }
}
}
//// ---------- PQSMod_HeightColorRamp.cs ---------- ////
using System;
using UnityEngine;
namespace KopernicusExpansion.PQSMods
{
public class PQSMod_HeightColorRamp : PQSMod
{
public ColorRamp Ramp;
public Simplex simplex = null;
public float BaseColorBias = 0.2f;
public float blend = 1f;
public override void OnSetup ()
{
this.requirements = PQS.ModiferRequirements.MeshColorChannel;
}
bool rampReported = false;
public override void OnVertexBuild (PQS.VertexBuildData data)
{
if (Ramp == null)
{
if (!rampReported)
Debug.LogError ("ColorRamp is null in PQS " + sphere.name);
rampReported = true;
return;
}
float height = (float)(data.vertHeight - sphere.radius);
var colors = Ramp.Evaluate (height);
var color = colors [0];
var noiseColor = colors [1];
if (simplex != null)
{
double noise = simplex.noise (data.directionFromCenter);
float blendN = Mathf.Clamp01 (Mathf.Abs ((float)noise) + BaseColorBias);
data.vertColor = Color.Lerp(data.vertColor, Color.Lerp (color, noiseColor, blendN), blend);
}
else
{
data.vertColor = Color.Lerp(data.vertColor, color, blend);
}
}
public class ColorRamp
{
FloatCurve r;
FloatCurve g;
FloatCurve b;
FloatCurve rn;
FloatCurve gn;
FloatCurve bn;
public ColorRamp()
{
r = new FloatCurve();
g = new FloatCurve();
b = new FloatCurve();
rn = new FloatCurve();
gn = new FloatCurve();
bn = new FloatCurve();
}
public Color[] Evaluate(float height)
{
var color = new Color (r.Evaluate (height), g.Evaluate (height), b.Evaluate (height));
var noise = new Color (rn.Evaluate (height), gn.Evaluate (height), bn.Evaluate (height));
return new Color[]{ color, noise };
}
public void Add(float _r, float _g, float _b, float _rn, float _gn, float _bn, float height)
{
r.Add (height, _r);
g.Add (height, _g);
b.Add (height, _b);
rn.Add (height, _rn);
gn.Add (height, _gn);
bn.Add (height, _bn);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment