MaterialManager

Manage materials.

Creation 

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

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

Creating a material is then done using the constructor:

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

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

Get the material class with the given pipeline name.

Usage 

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

TypeScript 

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

1interface Phong {
2    getAmbientColor(out?: Float32Array): Float32Array;
3    setAmbientColor(value: NumberArray): void;
4}
5const PhongMaterial = engine.materials.getTemplate<Phong>('Phong Opaque');
6const 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:

1// Note the `.js` instead of `.d.ts`
2import {PhongOpaque} from './materials.js';
3
4const PhongMaterial = engine.materials.getTemplate('Phong Opaque');
5const 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

.wrap(index: number) ⇒ null | Material 

ParamTypeDescription
indexnumber