MaterialManager

Manage materials.

Creation

To create a material, first retrieve the class associated to the pipeline using getTemplate:

const PhongMaterial = engine.materials.getTemplate('Phong Opaque');

Creating a material is then done using the constructor:

const material = new PhongMaterial();
material.setDiffuseColor([1.0, 0.0, 0.0, 1.0]);

Methods


Methods

._wrapInstance(instance: Material) ⇒ Material

Wrap a material instance.

@todo: Remove at 2.0.0.

Note: Wrapping should only be called once per instance.

Returns: The new material, wrapped in a proxy.

ParamTypeDescription
instanceMaterialThe material instance.

.getTemplate<T>(pipeline: string) ⇒ MaterialConstructor<T>

Get the material class with the given pipeline name.

Usage

const PhongMaterial = engine.materials.getTemplate('Phong Opaque');
const material = new PhongMaterial();
material.setDiffuseColor([1.0, 0.0, 0.0, 1.0]);

TypeScript

This method provide a simple way to cast the constructor returned by getTemplate:

interface Phong {
    getAmbientColor(out?: Float32Array): Float32Array;
    setAmbientColor(value: NumberArray): void;
}
const PhongMaterial = engine.materials.getTemplate<Phong>('Phong Opaque');
const mat = new PhongMaterial(); // `mat` is of type `Phong`

However, this means manually writing types for each pipeline.

Fortunately, The Wonderland Editor can automatically generate material definitions (.d.ts) from the project pipelines.

To enable the generation, go to the Project Settings > JavaScript panel and set materialDefinitions to a path, e.g., materials.d.ts.

Material constructors will then be typed automatically when using a string literal pipeline name:

// Note the `.js` instead of `.d.ts`
import {PhongOpaque} from './materials.js';

const PhongMaterial = engine.materials.getTemplate('Phong Opaque');
const mat = new PhongMaterial(); // `mat` is of type `PhongOpaque`

Returns: The material class.

Throws: Error if the material class doesn’t exist.

ParamTypeDescription
pipelinestringThe pipeline name to search for.
Template ParamType Definition
Textends Material<T>

.wrap(index: number) ⇒ null | Material

ParamTypeDescription
indexnumber