3D Surface Plotter  1.0.0
Create 3D Surface Charts for Any Math Function
Demo: Built-in Math Functions

This demo shows how to use four built-in math functions in the Surface3DLib library to create 3D surface plots. You can select the math function from the MathFunctionEnum dropdown menu in the Inspector. You can also change the colormap and other parameters for the plots in real-time in either the Scene View or the Game View.

Set Up the Scene

The demo examples in the 3D Surface Plotter package come with eight Scenes in the 3D Surface Plotter/Scenes folder:

Double-click on the scene called BuiltinFunctions to bring up the demo for built-in math functions:

The 3D Sinc surface with the default jet colormap is displayed in the Scene view window. From the Hierarchy window, you can see that this scene contains only one GameObject named Surface. Click on the Surface to bring up the Inspector for it. From the Inspector, you can check the surface's properties. The material is specified using Surface3DMaterial. The surface is also attached to a C# script called BuilinFunctions.cs, which exposes several properties that allow you to manipulate the surface plot.

C# Script

Let's examine the C# script file used in this demo. From the 3D Surface Plotter/Scripts folder, double-click on the BuiltinFunctions.cs file:

This will open the BuiltinFunctions.cs file in Visual Studio. Here is the code list for this script:

using System;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[ExecuteInEditMode]
[Serializable]
public class BuiltinFunctions : MonoBehaviour
{
public MathFunctionEnum MathFunctions = MathFunctionEnum.Sinc;
public ColormapEnum Colormaps = ColormapEnum.jet;
public bool ColormapReverse = false;
[Range(0.2f, 10f)]
public float Scaling = 1f;
[Range(0f, 1f)]
public float AspectRatio = 0.7f;
public bool AutoRotation = true;
Mesh mesh;
private SurfaceData.VertexData data;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateSurface();
}
void Update()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
bool needsUpdate = mesh == null ||
UnityEditor.Selection.activeGameObject == gameObject;
if (needsUpdate)
{
AutoRotation = false;
CreateSurface();
var mf = GetComponent<MeshFilter>();
mf.sharedMesh = mesh;
}
}
#endif
if (Application.isPlaying)
{
CreateSurface();
if (AutoRotation)
{
}
}
}
void CreateSurface()
{
SurfaceData.BuiltinFunctionEnum = MathFunctions;
SurfaceData.ColormapName = Colormaps;
SurfaceData.Scale = Scaling;
SurfaceData.Aspect = AspectRatio;
SurfaceData.ColormapReverse = ColormapReverse;
SurfaceData.UseCustomColormap = false;
mesh.Clear();
mesh.vertices = data.Vertices;
mesh.colors = data.Colors;
mesh.triangles = data.Triangles;
mesh.RecalculateNormals();
}
}
A static class SurfaceData.
Definition: SurfaceData.cs:12
static VertexData GetBuiltinData()
This method is used to create surface data.
Definition: SurfaceData.cs:82
static void RotateObject(Transform transform)
this method is used to rotate the game object.
Definition: SurfaceData.cs:244
Definition: Colormap.cs:7
ColormapEnum
ColormapEnum enumeration.
Definition: Colormap.cs:1015
MathFunctionEnum
Built-in math functions enumeration.
Definition: MathFunc.cs:258
VertexData struct.
Definition: SurfaceData.cs:256
Color[] Colors
colormap data
Definition: SurfaceData.cs:264
int[] Triangles
triangle (or index) data
Definition: SurfaceData.cs:272
Vector3[] Vertices
vertex data
Definition: SurfaceData.cs:260

First, we introduce the Surface3DLib library with the following using statement:

We then define several public fields whose values can be modified in the Inspector, including MathFunctionEnum, ColormapEnum, ColormapReverse, Scaling, and AspectRatio. All these fields are included in the Surface3DLib. The Start and Update methods are standard for a typical Unity project.

The key method to use the Surface3DLib library is the CreateSurface method. Inside this method, we first establish the relationships between the public fields defined in the Inspector and the fields implemented in the SurfaceData class in the Surface3DLib library. Next, we call the SurfaceData.GetBuilinData method to create corresponding data, including Vertices, Colormaps, UVs, and Triangles (or Indices), for the 3D surface plot. We then assign these data to the Unity mesh object.

Note that we don't assign the UV coord data to the mesh because we don't want to map any image texture onto our surface in this demo.

Finally, we call the mesh.RecalculateNormals method to calculate the normal vector data that will be used for lighting.

Start Play Mode

Now, let's start the Play mode by clicking the Play button. You will get the default Sinc surface with the jet colormap. You can rotate, pan, and zoom the plot using your mouse. You can also see the back-side of the surface by properly rotating it:

You can see that both sides of the surface plot have lighting and colormap.

Now, clicking on the Colormaps dropdown menu, you will see a list that contains over 60 colormaps:

You can select any colormap from this list to update the 3D surface plot in real-time. For example, let's select the cool colormap from this list and you will get the following Sinc surface with the cool colormap. Now, check the Colormap Reverse box, which gives a surface plot with the reversed cool colormap:

This means that our Surface3DLib library contains over 120 colormaps (61 colormaps plus 61 reversed colormaps) that let you set colors for your 3D surface plots.

You can also create different surface plots by selecting different math functions from the Math Functions dropdown menu that contains four built-in math functions. The following picture shows the surface plots generated using the Peaks, ExpFunc, and SinCos functions with different colormaps:

You can also change the other parameters such as the scaling and aspect ratio in the Inspector to get a good-looking 3D surface plot. In particular, you can check the Auto Rotation box to make the surface plot to rotate continuously on your screen.