10
$\begingroup$

I want to draw sin, cos, tan and other mathematical functions with the shader editor. How do I do this?

enter image description here

$\endgroup$

4 Answers 4

11
$\begingroup$

you can use this node setup:

enter image description here

enter image description here

You can now add any math node between the two multiplier nodes.

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ +1 but... Hehe.... the challenge is to get a stroke with even thickness. A true (perpendicular-distance-from-sine) function is a bit of a nightmare.... $\endgroup$
    – Robin Betts
    May 14, 2022 at 10:28
  • $\begingroup$ I am sure you can easily do that!!! $\endgroup$
    – Chris
    May 14, 2022 at 10:41
10
$\begingroup$

This aspect may not matter that much to you.. ( if it did, it might be better to go to a vector-graphics program and use its internal rasterization, or implement the graph as geometry in Blender).

As always, open to challenge, but stroking a sine-wave, or other trig. functions, with an even thickness, turns out to be a hard problem. See, for example, this article. And you would need different distance-functions for every graphed function.

You could let the compositor in Blender approximate it for you with edge-detection..

First graph f(X) < Y:

enter image description here

And then put the result (on the left) through edge-detection and dilation in the compositor:

enter image description here

but I wouldn't expect it to be worth the hack, since even that is still an approximation..

$\endgroup$
10
  • 1
    $\begingroup$ Isn’t pixel based always just an approximation….?😇 of course +1 for this nice solution $\endgroup$
    – Chris
    May 14, 2022 at 13:47
  • $\begingroup$ @Chris true. But I think spline rasterisation, as in vector graphics, is a bit cleverer than Dilate? Finding curve-stroking algorithms is harder than I thought. $\endgroup$
    – Robin Betts
    May 14, 2022 at 13:51
  • $\begingroup$ I love watching perfectionist doing their work - but I am not one 🙃😀 $\endgroup$
    – Chris
    May 14, 2022 at 13:52
  • 2
    $\begingroup$ you won't believe it, but i don't "see" myself as an artist. Don't know why i have a problem with "artist" but i see myself as a digital creator..... ;) that's more neutral ...and art is misused to much...i think...but yeah...don't know.... ;) $\endgroup$
    – Chris
    May 14, 2022 at 14:05
  • 1
    $\begingroup$ It has been [redacted] years since I taught vector graphics, but IIRC, we solved curve width problems using mean weighted dithering. I can't find the papers now, but it doesn't really matter because you can't do that with Blender shaders anyway, AFAICT $\endgroup$ May 14, 2022 at 15:20
5
$\begingroup$

A really simple node setup that gives you a gradient as well, which of course can be changed to a solid line if you want.

enter image description here

Using a MixRGB node set to difference mode, comparing sin(x) to y just like an equation. You can do this to plot any equation, really.

enter image description here

Here it is with a custom color, thickness, and gradient.

$\endgroup$
2
$\begingroup$

Well, I can't offer a uniform line thickness, but I can give you procedural axis grid lines

Math Graph - cubic Math Graph node setup

You can download a copy here: https://blenderartists.org/t/math-graph-shader/1380100

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .