3D Surface Plotter  1.0.0
Create 3D Surface Charts for Any Math Function
Demo: Custom Colormaps

The Surface3DLib library contains over 120 colormaps that let you set colors for your 3D surface plots. In case if you want to use your own colormaps, our library also allows you to do just that. This library exposes a public field called CustomeColormap. In the previous several demos, we set this field to false because we want to use the built-in colormaps. If you set this field to true, you can then create your own colormaps with the Unity's Gradient object, and use it to set colors for your surface plots.

In this demo, we will explain how to create a custom colormap and how to apply it to the built-in Peaks function.

Set Up the Scene

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

The 3D Peaks surface with a custom 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 UserColormap.cs, which exposes several properties that allow you to create your own colormap and manipulate 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 UserColormap.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 UserColormap : MonoBehaviour
{
public Gradient MyColormap;
[Range(1f, 10f)]
public float Size = 3f;
[Range(0.2f, 10f)]
public float Scaling = 1f;
[Range(0f, 1f)]
public float AspectRatio = 0.9f;
public bool IsAnimation = false;
public float AnimationSpeed = 1f;
Mesh mesh;
private SurfaceData.VertexData data;
void Start()
{
InitialCustomeColormap();
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.Scale = Scaling;
SurfaceData.Aspect = AspectRatio;
SurfaceData.UseCustomColormap = true;
SurfaceData.CustomColormap = MyColormap;
SurfaceData.IsAnimation = IsAnimation;
MathFunc.PeaksAnimation(AnimationSpeed);
data = SurfaceData.CreateData(MathFunc.Peaks, -Size, Size, -Size, Size);
mesh.Clear();
mesh.vertices = data.Vertices;
mesh.colors = data.Colors;
mesh.triangles = data.Triangles;
mesh.RecalculateNormals();
}
void InitialCustomeColormap()
{
MyColormap = new Gradient();
var colorKey = new GradientColorKey[2];
colorKey[0].color = Color.red;
colorKey[0].time = 0.0f;
colorKey[1].color = Color.blue;
colorKey[1].time = 1.0f;
var alphaKey = new GradientAlphaKey[2];
alphaKey[0].alpha = 1.0f;
alphaKey[0].time = 0.0f;
alphaKey[1].alpha = 0.0f;
alphaKey[1].time = 1.0f;
MyColormap.SetKeys(colorKey, alphaKey);
}
}
A static class MathFunc.
Definition: MathFunc.cs:14
static void PeaksAnimation(float speed)
This method is used to animate the peaks function.
Definition: MathFunc.cs:135
static Vector3 Peaks(float x, float z)
This method produces a Vector3 point on a 3D Peaks surface described by the Peaks function.
Definition: MathFunc.cs:104
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
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

You can see that the script code used in this demo is very similar to that used in our Animating Peaks Function demo, except that we define a public field called MyColormap:

public Gradient MyColormap;

Inside the CreateSurface method, we relate this public field to the CustomColormap defined in our Surface3DLib library. We also set the SurfaceData.UseCustomColormap field to true in order to use the custom colormap:

SurfaceData.UseCustomColormap = true;
SurfaceData.CustomColormap = MyColormap;

In addition, we define a new method called InitialCustomColormap that provides the initial colormap for our public field MyColormap. This initial colormap is created using a Gradient object with two colors, red and blue.

The rest of the code for this demo is the same as that used in our Animating Peaks Function demo. You can refer to that demo for the detailed explanation.

Start Play Mode

Now, let's start the Play mode by clicking on the Play button. You will get the Peaks surface with the custom red-blue colormap. Now, click on My Colormap in the Inspector to bring up the Gradient Editor window, from which you can visually add color keys and specify whatever colors you like, as shown in the following image:

If satisfying with the custom colormap, you can check the Is Animation box in the Inspector to start animation with your own colormap:

Congratulations! You have successfully created your own colormap for the 3D Peaks surface.