# Coloring a 3D Surface

This topic is 4054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a 3D like the following: http://tabishfayyaz.googlepages.com/blackGraph.JPG How do I color them to give a picture like the following: http://tabishfayyaz.googlepages.com/colorGraph.JPG I create a mesh from a list of vertices. I heard coloring has something to do with normals but haven't been able to get my head working around that.

##### Share on other sites
Quote:
 Original post by tfmI create a mesh from a list of vertices. I heard coloring has something to do with normals but haven't been able to get my head working around that.
The normals only affect lighting, which affects colouring. For the effect in that image, you just want to set a vertex colour for each vertex, and the video card with blend the vertex volours across the triangle.

##### Share on other sites
You don't have to use normals here.

I would create a similar affect by defining three colors for the dimensions of the graph space, and then linearly interpolating the colors to determine the color at a certain vertex.

for example to determine a color at a certain vertex (0.5, 0.6, 0.2):

0.5 * red + 0.6 * green + 0.2 * yellow = the color at this vertex.

You might have to play around a little with that, it is hard to see from your colored graph exactly how the color changes according to the position it is at.

##### Share on other sites
It looks like it's just using a gradient from one colour to another along the x and y axes (independent of z).

ie: -x = blue, +x = green, -y = yellow, +y = magenta

GraphCalc for Windows has something very similar within its 3D graphing functionality.

My personal favourite method is to use Perlin noise to make a cow texture that is independent of all axes. It doesn't help with visualization, but it looks cool.

##### Share on other sites
@taby I would second your opinion.

the formula given by stevenmarky would most likely help in achieving that.

However as I'm using Mesh for my vertices and in the Mesh I use a Material object what property should I set for Material or I shouldn't just use it ?

and in order to tell me Mesh that I have vertices which store color information as well I would need to use the Diffuse tag along with Position in the following line ?

mesh = new Mesh(indices.Length, this.verticesFromMaple.Count, 0, VertexFormats.Position | VertexFormats.Diffuse , d3dManager.device);

##### Share on other sites
Based on the previous discussion I get the following. The regions are very well defined now which is not desired:

The code which is used is this:

if(x<0 && y<0)
vertexPosColor = new CustomVertex.PositionColored(x, y, (float)z, Color.Blue.ToArgb() * Color.Magenta.ToArgb());
else if(x<0 && y>0)
vertexPosColor = new CustomVertex.PositionColored(x, y, (float)z, Color.Blue.ToArgb() * Color.Pink.ToArgb());
else if(x>0 && y<0)
vertexPosColor = new CustomVertex.PositionColored(x, y, (float)z, Color.Green.ToArgb() * Color.Yellow.ToArgb());
else if(x>0 && y>0)
vertexPosColor = new CustomVertex.PositionColored(x, y, (float)z, Color.Green.ToArgb() * Color.Magenta.ToArgb());

##### Share on other sites
At the moment you are just setting the different quadrants of the graph to different colors.
You need to multiply the colors by the position of the vertex.

I think your code should look more like the following:
vertexPosColor = new CustomVertex.PositionColored(x, y, (float)z, Color.Blue.ToArgb()*x + Color.Magenta.ToArgb()*y + Color.Green.ToArgb()*z);

This code is only correct if your graph is in the range 0-1 (In all axis). If not then you need to do some scaling and offsetting on the values you multiply the colors with.

I hope that helps.

##### Share on other sites
I'm just checking for negativity, why do you say its only correct for (0-1) ?
What you are saying I tried that already before trying the one which I showed. However using your technique the results are following:

using only the following line of code for all points:
vertexPosColor = new CustomVertex.PositionColored(x, y, (float)z, (int)(Color.Blue.ToArgb() * x) + (int)(Color.Magenta.ToArgb() * y) + (int)(Color.Green.ToArgb() * z));

##### Share on other sites
You need to multiply the (r,g,b,a) components of each color seperately.
I'm not very familiar with C# so I'm not sure what the best way to do that is.
I hope somebody else can help with that.

I said it would only work in the range 0-1 because greater than 1 you would end up with a color component that is greater than full color and smaller than 0 you would end up with 'less than black', but all you have to do is scale appropriately and that isn't a problem.

##### Share on other sites
"You need to multiply the (r,g,b,a) components of each color seperately."

Can you elaborate more on that, I mean I get get the (r,g,b,a) of Color.Blue but what would be the operators involved in the equation among each other ?

Thank you very much

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 12
• 16
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632658
• Total Posts
3007681
• ### Who's Online (See full list)

There are no registered users currently online

×