So in an NPM package babylon-helper-lib
I have a function, twodgrid
. Long story short, it makes a box with the help of MeshBuilder.CreateBox(name, opts, scene)
.
export let twodgrid = function(scene: Scene, width=20, height=20, name="box") {
const box = MeshBuilder.CreateBox(name, {width:width,height:height,depth:.2}, scene);
const boxmat = new GridMaterial(`${name}_mat`, scene);
boxmat.majorUnitFrequency = 2;
boxmat.useMaxLine = false;
box.material = boxmat;
return {box, boxmat};
}
In an ObservableJS project, I can successfully place a canvas element, get it with JavaScript, and put a BabylonJS app in it, just using BJS. I’m now trying to basically reproduce it, using my library functions above, like twodgrid
.
I can successfully import babylon-helper-lib
and use some of its functions.
However, for this function twodgrid
, when I call it I don’t get any errors. However, it also doesn’t draw the box on the scene. Although I doubt it’s very important, you can see here the code that I use to do this:
{
const LIB = await import("https://esm.sh/babylon-helper-lib@0.0.17");
const bag = LIB.init("#lib"); // library function to make an engine, scene, camera, light, and canvas
const scene = bag.scene;
const planebag = LIB.twodgrid(scene); // Make the box.
// const box = BABYLON.MeshBuilder.CreateBox("box", {}, scene);
bag.engine.runRenderLoop( () => {
scene.render();
});
}
If I comment out my library function and un-comment the direct call to CreateBox
it now draws a box.
So this all seems to tell me that when I pass scene
into twodgrid
, somehow it’s not the one getting updated. I know JavaScript is pass-by-copy-of-reference. But since I don’t know exactly how CreateBox
works then I’m unsure of exactly what it does. I know if you leave out the scene then it just uses the currently active scene, but I figured by passing it in explicitly I could override that behavior.
Anyway, hopefully that was a clear enough description of the problem, and I’m asking the question to the right people (apologies if not!). Does anyone have a suggestion about whether I’m right about the issue, and either way, what might be doable here?