3D Surface Plotter  1.0.0
Create 3D Surface Charts for Any Math Function
Demo: Animating Sinc Function

In this demo, we will explain how to animate the parameters used to create the built-in Sinc function, which resulting in an animated 3D Sinc surface plot.

Set Up the Scene

From the 3D Surface Plotter/Scenes folder, double-click on the scene called AnimateSinc to bring up the demo.

The 3D Sinc surface with 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 AnimateSinc.cs, which exposes several properties that allow you to manipulate the surface plot and animation.

C# Script

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

using System;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[ExecuteInEditMode]
[Serializable]
public class AnimateSinc : MonoBehaviour
{
public ColormapEnum Colormaps = ColormapEnum.jet;
public bool ColormapReverse = false;
[Range(5f, 20f)]
public float Size = 8f;
[Range(0.2f, 10f)]
public float Scaling = 1f;
[Range(0f, 1f)]
public float AspectRatio = 0.9f;
public bool IsAnimation = false;
[Range(0.1f, 5f)]
public float AnimationSpeed = 1f;
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)
{
CreateSurface();
var mf = GetComponent<MeshFilter>();
mf.sharedMesh = mesh;
}
}
#endif
if (Application.isPlaying)
{
CreateSurface();
}
}
void CreateSurface()
{
SurfaceData.ColormapName = Colormaps;
SurfaceData.Scale = Scaling;
SurfaceData.Aspect = AspectRatio;
SurfaceData.ColormapReverse = ColormapReverse;
SurfaceData.UseCustomColormap = false;
SurfaceData.IsAnimation = IsAnimation;
MathFunc.SincAnimation(AnimationSpeed);
data = SurfaceData.CreateData(MathFunc.Sinc, -Size, Size, -Size, Size);
mesh.Clear();
mesh.vertices = data.Vertices;
mesh.colors = data.Colors;
mesh.triangles = data.Triangles;
mesh.RecalculateNormals();
}
}
A static class MathFunc.
Definition: MathFunc.cs:14
static void SincAnimation(float speed)
This method is used to animate the sinc function.
Definition: MathFunc.cs:84
static Vector3 Sinc(float x, float z)
This method produces a Vector3 point on a 3D Sinc surface described by the Sinc function.
Definition: MathFunc.cs:59
A static class SurfaceData.
Definition: SurfaceData.cs:12
static VertexData CreateData(Func< float, float, Vector3 > f, float XMin, float XMax, float ZMin, float ZMax)
This method generates the surface data.
Definition: SurfaceData.cs:120
Definition: Colormap.cs:7
ColormapEnum
ColormapEnum enumeration.
Definition: Colormap.cs:1015
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 using the statement:

We then define several public fields whose values can be modified in the Inspector, including ColormapEnum, ColormapReverse, Size, Scaling, AspectRatio, IsAnimation, and AnimationSpeed. The last two fields are used to control animation.

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 MathFunc.SincAnimation method with the AnimationSpeed as its input argument to animate the parameter used in creating the built-in Sinc function inside our Surface3DLib library.

In fact, you can examine what parameter in the Sinc function to be animated by hovering over the MathFunc.Sinc function in Visual Studio, as shown in the following image:

You can see that the IntelliSense in Visual Studio gives the definition of the Sinc function with the parameter named a. The MathFunc.SincAnimation method is just used to animate this parameter. You can refer to the Reference Manual to see the detailed descriptions about the classes, methods, fields, enumerations, etc. in the Surface3DLib library.

Next, we call the SurfaceData.CreateData method to create corresponding data, including Vertices, Colormaps, UVs, and Triangles (or Indices), for the 3D Sinc surface. This method accepts the math function, x and z data ranges as its input arguments. We then assign these data to the Unity mesh object. 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 on the Play button. You will get the default Sinc surface with the jet colormap. There is an Animate Sinc (Script) section in the Inspector:

Check the Is Animation box, the animation will start. During animation, you can also change the other properties such as colormap, as shown in the following image: