API

Gradient material

Screenshot

You can get the gradient material by:

Using the gradient material

The material creates a smooth gradient with two colors:

import { Color3 } from "@babylonjs/core/Maths/math.color";
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
import { GradientMaterial } from "@babylonjs/materials/gradient/gradientMaterial";
var sphere = MeshBuilder.CreateSphere("sphere", { segments: 32, diameter: 2 }, scene);
var gradientMaterial = new GradientMaterial("grad", scene);
gradientMaterial.topColor = Color3.Red(); // Set the gradient top color
gradientMaterial.bottomColor = Color3.Blue(); // Set the gradient bottom color
gradientMaterial.offset = 0.25;
sphere.material = gradientMaterial;

The gradient material can be configured with:

  • topColor and bottomColor are the two colors used in this gradient material
  • offset is a number used to offset the color on the Y axis
  • scale is a number used to scale the color on the Y axis
  • smoothness is a number (between 0 and 10) that can be used to define the speed of the color change along the Y axis (1 by default)

Playground example

You can find a live example here: PG: Gradient Material