Creating a linear gradient without knowing the size
I am trying to create a gradient to feed into Libnoise so I can create a heatmap for my procedural world game and I can't quite come up with a way to create a linear gradient when I don't know how large the gradient will be.
To use libnoise operators I need to subclass from modulebase which means I need to implement
public override double GetValue(double x, double y, double z)
{
}
which should return a value between -1 and 1. I can come up with ways to create the gradient if I know the size of the texture but I can't come up with a method if I don't know the resolution of the map. Maybe I am looking at this the wrong way and there is a better way to go about this.
Answer by johnmcmahonart · Jul 15, 2016 at 02:02 AM
So at the moment the ugly hack I am going with is to pass in the size (in one dimension) to the constructor for my gradient and then linearly interpolate between a fixed set of control points. It's ugly but quick. I'll post terrible code when I get it working.
Here is the solution I ended up writing. Not great but maybe it will help someone in the future
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibNoise;
using UnityEngine;
namespace LibNoise.Generator
{
public class gradientHack:$$anonymous$$oduleBase //the ugliest hack in the history of computer graphics
{
private double $$anonymous$$;
private double max;
private rescale rescaler;
private double domain$$anonymous$$in = -1;
private double domain$$anonymous$$ax = 1;
public gradientHack(): base(0)
{
}
public gradientHack (double $$anonymous$$, double max):base(0)
{
this.$$anonymous$$ = $$anonymous$$;
this.max = max;
this.rescaler = new rescale(this.$$anonymous$$, this.max, domain$$anonymous$$in, domain$$anonymous$$ax);
}
public override double GetValue(double x, double y, double z)
{
var currentPoint = rescaler.Scale(z);
//Debug.Log("Current point scaled:" + currentPoint);
double input1 = -0.4;
double output1 = -0.3;
double input2 = 0.0f;
double output2 = 1;
double input3 = .4f;
double output3 = -0.3;
if (currentPoint<=input1)
{
rescale newScale = new rescale (domain$$anonymous$$in,input1,domain$$anonymous$$in,output1);
return newScale.Scale(currentPoint);
}
if (currentPoint>=input1 && currentPoint<=input2)
{
rescale newScale = new rescale(input1, input2,output1, output2);
return newScale.Scale(currentPoint);
}
if (currentPoint >= input2 && currentPoint <= input3)
{
rescale newScale = new rescale(input2,input3, output2, output3);
return newScale.Scale(currentPoint);
}
if (currentPoint >= input3)
{
rescale newScale = new rescale(input3, domain$$anonymous$$ax, output3, domain$$anonymous$$in);
return newScale.Scale(currentPoint);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class rescale
{
private double range$$anonymous$$in, range$$anonymous$$ax, domain$$anonymous$$in, domain$$anonymous$$ax;
public rescale (double range$$anonymous$$in,double range$$anonymous$$ax, double domain$$anonymous$$in, double domain$$anonymous$$ax)
{
if (range$$anonymous$$ax>range$$anonymous$$in)
{
this.range$$anonymous$$ax = range$$anonymous$$ax;
}
if (range$$anonymous$$in<range$$anonymous$$ax)
{
this.range$$anonymous$$in = range$$anonymous$$in;
}
this.domain$$anonymous$$ax = domain$$anonymous$$ax;
this.domain$$anonymous$$in = domain$$anonymous$$in;
}
public double Scale (double value)
{
double scaleParm = (domain$$anonymous$$ax - domain$$anonymous$$in) / (range$$anonymous$$ax - range$$anonymous$$in);
return (domain$$anonymous$$in + ((value - range$$anonymous$$in) * scaleParm));
}
}
Your answer
Follow this Question
Related Questions
Gradient + Transparent 1 Answer
Generating Mesh using Points 1 Answer
[SOLVED]Unexpected results 0 Answers
Help with clouds -- Is it possible to procedurally generate them in a 2d game? 1 Answer