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.
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.
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:
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.
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: