I am trying to come up with a technique to generate a height map for a meteor crater. I'm trying to keep is small and simple at the moment, and have a simple algorithm where the centre of the crater is marked, and within its radius, pixels around it are coloured depending on how close to the center they are.
The problem with this method is 开发者_JAVA技巧it is producing V shaped craters, I require more 'U' shaped results. I'm unfarmilliar with the use of sine waves and I get the feeling interpolation wont work very quickly given how many pixels could be inside the meteor radius.
Any suggestions?
I assume you have a texture or other picture format that can be indexed over width and height (x,y) and that the crater is also given in x,y with x,y inside the texture, this would produce an algorithm like this.
Texture2D texture = ...;
Vector2 craterPosition = new Vector2(50,50);
double maxDistance = Math.Sqrt(Math.Pow(texture.Width,2) + Math.Pow(texture.Height,2));
for(int x = 0; x < texture.Width; x++)
{
for(int y = 0; y < texture.Height; y++)
{
double distance = Math.Sqrt(Math.Pow(x - craterPosition.x, 2) + Math.Pow(y - craterPosition.y, 2));
//the lower distance is, the more intense the impact of the crater should be
//I don't know your color scheme, but let's assume that R=0.0f is the lowest point and R = 1.0f is the highest
distance /= maxDistance;
double height = (Math.Cos(distance * Math.Pi + Math.Pi) + 1.0) / 2.0;
texture[x,y] = new Color((float)height,0,0,1);
}
}
The most important line here is probably
double height = (Math.Cos(distance * Math.Pi + Math.Pi) + 1.0) / 2.0;
Take a look at the cosine curve http://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Cos.svg/500px-Cos.svg.png it has a nice smooth 'crater like' rounding from Pi to TwoPi. Since our distance variable is scaled from 0~1 we use distance *Pi + Pi. But this will give us a result from -1 to 1. So we add 1 to the end result and then divide by 2 to get a smooth result for the height between 0 and 1.
I hope this helps you.
精彩评论