I want to draw sin, cos, tan and other mathematical functions with the shader editor. How do I do this?
4 Answers
-
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
-
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:
And then put the result (on the left) through edge-detection and dilation in the compositor:
but I wouldn't expect it to be worth the hack, since even that is still an approximation..
-
1$\begingroup$ Isn’t pixel based always just an approximation….?😇 of course +1 for this nice solution $\endgroup$– ChrisMay 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$– ChrisMay 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$– ChrisMay 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
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.
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.
Here it is with a custom color, thickness, and gradient.
Well, I can't offer a uniform line thickness, but I can give you procedural axis grid lines
You can download a copy here: https://blenderartists.org/t/math-graph-shader/1380100