# How to create a 3D graphing calculator in Unity?

This isn’t a question that is particularly related to Unity, but a discussion on how to create a 3D graphing calculator. I have been trying to tackle this problem for a few days, and it seems the algorithms are beyond me.

I managed to simplify and evaluate algebraic equations from strings using other peoples libraries.
My method takes about 2 minutes to get all the data points for the mesh. Not all points are calculated by the library, and some points shouldn’t exist. I rounded the data points to the interval they were parsed through, and added these to a dictionary so that next when I used marching cubes algorithm, I could quickly check if a z value existed at the corner. (umm IDK if I lost values because of floating point precision, I did round the keys down, but I also drew out gizmos which visually matched the mesh.)

I managed to get the shape, with holes in it, and I am not satisfied with this one bit. The shape is larger on the negative side than on the positive side. Nonetheless, this got a fairly close shape to what I wanted and wasn’t complete gibberish.

So, can someone help me break down the problem so that I can quickly generate a seamless mesh, similar to how commercial 3D graphing calculators do it?

I like to post videos of stuff on my youtube channel with WARNING random music , anyway here is what I managed …and it is not pretty.

1 Like

Thanks for that. Fortunately I had my headphones off or I would have been deafened.

This is the kind of problem that if you have to ask, you’d better off finding some library to do it for you. It is just a lot of work.

You can start by learning how to generate a plane mesh, given a domain and resolution, i.e. z=0 . There’s plenty of tutorials on this.

Then generating a surface for a function is just the same thing, but with z = f(x,y)

Sometime there are multiple solutions for z, you’ll have to generate a separate surface for each.

Then you’ll probably want to adaptively sample the mesh instead of just sampling on an uniform grid. This is a massive can of worm. The simplest is probably to split each quads into 4 equal quads until the error is acceptable.

If your function are analytically differentiable, you can also calculate the derivative and thus surface normal to get a smoother looking mesh.

This is just the broad stroke, you’ll have to solve many problems that you don’t sound like you’re ready to solve: dealing with discontinuites, singularities, aliasing, numerical precision and stability. That’s just what I can immediately think of.

2 Likes

I went ahead and made it with plane meshes, thanks for suggesting this idea. I initially did not think it will work, but couldn’t think of any other way. Here is what it looks like https://www.youtube.com/watch?v=Orfc-HsCI_s
I am still wondering about better and quicker ways. The code to generate a single side plane mesh is here Front Plane (only) Mesh Generation for 3D Graphing calculator in Unity · GitHub

You seems to be generating 6 surfaces. You should just generates 1 for each branch of the solution. For example, -x^2 +y^2 +z^2=0 have 2 solutions for z, so only 2 planes.

You’re probably doing it because this particular equation have no discontinuities when sampled on the yz plane instead of xy. As I said before, handling discontinuities is one of the many challenges you’ll have to overcome. Changing the sampling plane only hide the problem.

The artifact in the middle is due to the singularity there, which is also 1 of the challenges I’ve mentioned.

Sampling is the general method, which have its drawbacks. Commercial graphing calculators most certainly detect common shapes, such as the cone in this example, and have dedicated code path for each of them. But that is infinitely more works.

This is not the kind of problem you can have solved by asking on general forum. You’ll have to dig through code of existing opensource solutions and read a lot of research papers on the topic.