Es6 typing issue with DynamicTexture

Hello! I am using es6 npm imports to bring use the TerrainMaterial like so:

import { TerrainMaterial } from “@babylonjs/materials/terrain/terrainMaterial”

Then later I try using it:
var newMat = new TerrainMaterial(“terrain”, scene);
newMat.mixTexture = new DynamicTexture(“terrainTex”, { width: 128, height: 128 }, scene, false);

For one thing, typescript is complaining that the “scene” type doesn’t match that required by the TerrainMaterial class.

Then it also insists that the DynamicTexture that I just assigned to newMat is a BaseTexture, even though it should be a subclass. What am I doing wrong with my import?

Have you tried just importing TerrainMaterial, without the ‘{ ‘ brackets?

newMat.mixTexture = new DynamicTexture(“terrainTex”, { width: 128, height: 128 }, scene, false);

The type of newMat.mixTexture will always be a BaseTexture. In order to keep a reference to the DynamicTexture, you should write:

var dynamicTexture = new DynamicTexture(“terrainTex”, { width: 128, height: 128 }, scene, false);
newMat.mixTexture = dynamicTexture;

It’s hard to know what’s going on with the scene parameter without seeing the rest of your code.

1 Like

You should also ensure in your package.json that @babylonjs/materials and @babylonjs/core have the same version