# How to alternate red/green color for a line chart based on segment direction?

I am working on a project to create procedural line charts (e.g. share market charts):

Here's how I did it:

1. I took a plane and deleted three edges.
2. Next I subdivided the remaining edge 10-15 times to get some detail.
3. Then I used a displace modifier with a clouds texture.
4. Then I slapped on a skin modifier.
5. Now I can manipulate the chart by changing the strength of displace modifier and size, depth attributes of clouds texture.

Modifiers:

Texture:

Topology:

Problem: I want to shade the chart such that if the Y-axis value of a vertex is less than the vertex before it then the area between it and the vertex before appears red else it appears green(The color should also change if the chart changes). So that I get something like this:

I've decided to use normals, as they are perpendicular to the direction of the line, so from them you can read the direction of the line:

Above red and blue vectors (arrows) mark the rising line, while cyan and violet vectors mark the falling line. Problem is, those are the normals of the faces that aren't actually visible by the camera - the camera only sees faces facing the camera, which all point in one direction:

Fortunately, by enabling Smooth Shading in Skin modifier, you can make those information bleed to the front and back faces:

Smooth shading is effectively faking more geometry - we could achieve a similar effect using Subdivision Surface modifier, so let's do that (and disable smooth shading for the time being):

In this case it should be apparent that the visible part of the shape no longer faces just one direction - because the geometry is smoothed. A single level of subdivision surface actually completely eliminates faces parallel to the camera.

Now, by separating XYZ, we can separately investigate Y:

From that we can see that the top half of the curve always has positive (approaching white) Y component of the normal, while the bottom half of the curve always has negative (clamped to black) component.

...And separately investigate X:

When the top part is rising, it has negative (black) X component, which gets positive (white) when it falls. For the bottom part it's the opposite. Our logic can therefore be:

If Y > 0: [top]

• if X < 0: green [rising]
• if X > 0: red [falling]

If Y < 0: [bottom]

• if X < 0: red [falling]
• if X > 0: green [rising]

Or in the nodes language:

• You are a legend You saved me a lot of trouble Mar 25, 2021 at 5:18