Babylon.js: MeshBuilder.CreatePolygon and PolygonMeshBuilder API is inconsistent

Created on 14 Jan 2019  路  10Comments  路  Source: BabylonJS/Babylon.js

Hello,
I'm migrating not from [email protected] to @babylonjs/[email protected] to try out the new and long waited tree shaking! (@sebavan 馃憫)

And getting the following error: ReferenceError: earcut is not defined at new PolygonMeshBuilder.

This is because MeshBuilder.CreatePolygon API doesn't support earcut injection:
Babylon.js/src/Meshes/meshBuilder.ts:
screen shot 2019-01-14 at 16 22 21

screen shot 2019-01-14 at 16 24 14

Help will be very much appreciated,
Tomer.

Most helpful comment

Thanks a lot.

If anyone struggling with the same issue here is a temporary solution you can use:

  const earcut = require('earcut');
  (window as any).earcut = earcut;
  const mesh = MeshBuilder.CreatePolygon(...);

All 10 comments

If you are relying on webpack why not using provide plugin to inject earcut globally ? (asking as I am creating the documentation)

module.exports = {
    context: __dirname,
...
    plugins: [
        new webpack.ProvidePlugin({
            'earcut': 'earcut'
        })
    ]
}

At the same time I will allow earcut to be injected through those both methods as well.

I'm using next.js and wouldn't want all my pages to include earcut for no reason.

Furthermore, I'm trying to ignore global dependencies as much as possible, I have only one call to MeshBuilder.CreatePolygon and if I will ever decide to remove it I think it will be much more obvious that earcut is no longer needed compering to global dependency.

Thanks again for your work!

Sweet that is a nice reason for the doc. You ll have it in the next version.

Thanks a lot.

If anyone struggling with the same issue here is a temporary solution you can use:

  const earcut = require('earcut');
  (window as any).earcut = earcut;
  const mesh = MeshBuilder.CreatePolygon(...);

Fixed!

  const earcut = require('earcut');
  window.earcut = earcut;

Not a nice solution, but I can say that it works...

Using Electron & Webpack & Vue. I've put it into main.js
Afaik, earcut will be injected globally (into window), which isn't nice considering that the principle of SFC's are broken this way.

... But it works

  const earcut = require('earcut');
  window.earcut = earcut;

Not a nice solution, but I can say that it works...

Using Electron & Webpack & Vue. I've put it into main.js
Afaik, earcut will be injected globally (into window), which isn't nice considering that the principle of SFC's are broken this way.

... But it works

@Fusseldieb, This was a temporary solution I used until @deltakosh solution, you can inject earcut into CreatePolygon, so there is no real reason to manually referance the global scope:

const mesh = MeshBuilder.CreatePolygon("name", { // ... }, scene, require('earcut'));

I'm using @babylonjs/[email protected] and it's working.

Add Earcut as dependency in your index HTML

I'm using next.js and wouldn't want all my pages to include earcut for no reason.

Furthermore, I'm trying to ignore global dependencies as much as possible, I have only one call to MeshBuilder.CreatePolygon and if I will ever decide to remove it I think it will be much more obvious that earcut is no longer needed compering to global dependency.

Thanks again for your work!

@hiteshsahu see this reply why I think injecting earcut it s better solution than add it as a global dependency. (your solution even bypass webpack which is even worse cause now earcut is not part of the bundle)
But do whatever works for you :)

With Webpack , install earcut via npm:
npm i --save-dev earcut

Then in your .ts or .js file:

import earcut from "earcut";

const mesh = MeshBuilder.CreatePolygon(name, options, scene, earcut)

In the documentation you can see that after scene, the next optional parameter is earcutInjection, _this is the way._
https://doc.babylonjs.com/api/classes/babylon.meshbuilder#createpolygon

Was this page helpful?
0 / 5 - 0 ratings