I’m early in my learning of all things BABYLON (both the framework and Javascript) and have spent several hours on trying to load a NodeMaterial from a locally stored json file as exported by the NodeMaterial editor.
I have tried a number of different solutions. All end in tears.
This method seems to have gotten the furthest in that all parameters to LoadNodeMaterial() have reasonable values as printed by the log lines.
Derp! No?
I don’t have knowledge of “import.” As I mentioned, I’m new to Javascript (almost a half century of C and C++ mostly). I haven’t imported anything as yet. Here is a link to the complete project (1 file).
Depending on whether you want TS or JS there are a couple reference repositories available that you can use to help get yourself going with the BJS NPM packages and such. This thread also has useful info
Here’s a link to an early, simple branch of a JS application. See the develop Branch for a more complex app, and if you want bleeding edge, this is the latest and greatest (as of today).
index.zip (1.2 KB)
(see attachment with working file)
There are several more scripts which could be included (or required for some tasks) with Babylon.js. Below is the script list as it is when one downloads Playground example.
While NPM building is great, the old way of including scripts is still simple and usable.
Thank you very much!
I was concerned I would have a great deal more to learn all up-front but your advice makes the added burden unnecessary! I hadn’t picked up on the need for additional includes. I figured all of BJS was included in the babylon.js file.
Thank you again!
Seems sometimes the need for the additional scripts is not as evident in the documentation as it could be
Below there are short explanations:
dat.gui.min.js - Only if you use DAT GUI ammo.js, cannon.js, Oimo.js - Only if you use one of these physics engines earcut.min.js - Only if you need irregular polygons babylonjs.materials.min.js - If you use Babylon.js materials library or Node materials postProcess.min.js - If you use postprocesses babylonjs.loaders.js - If you load any 3D models - GLTF, OBJ etc (no need if you use native .babylon format) babylonjs.serializers.min.js - If you need to export scene or meshes to different formats babylon.gui.min.js - If you use Babylon GUI babylon.inspector.bundle.js - Recommended to use at development stage, call Inspector with scene.debugLayer.show();
It’s no burden - thinking through your question has caused me to recall all the difficulties I had getting it working myself, which I’d mostly forgotten!
import * as Ammo from "ammo.js";
export let ammoModule;
export const ammoReadyPromise = new Promise((resolve) => {
new Ammo().then((res) => {
ammoModule = res;
resolve(res);
});
});
Then you can await the ammoReadyPromise which resolves with the actual ammo module
How about like this at the top of your script? Await needs to be used from within an async function, is the issue I think. I can confirm that below is working for me without error, for example:
window.addEventListener("DOMContentLoaded", async () => {
const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);
const scene = new BABYLON.Scene(engine);
await Ammo();
scene.enablePhysics(new BABYLON.Vector3(0,-9.81, 0), new BABYLON.AmmoJSPlugin());
// other stuff to setup the scene, etc.
});
Edit, you can also create and call an async setup function if you don’t want to use the DOMContentLoaded event handler:
const setup = async () => {
const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);
const scene = new BABYLON.Scene(engine);
await Ammo();
scene.enablePhysics(new BABYLON.Vector3(0,-9.81, 0), new BABYLON.AmmoJSPlugin());
// other stuff to setup the scene, etc.
};
await setup();
Awesome, and PS I just included the other version for completeness, just in case, but I would use the DOMContentLoaded event handler to ensure that the canvas element is created and ready before trying to use it from javascript, for example.