I have made a few functions to abstract some stuff that I plan on doing over and over in many different projects. Therefore I have my current project in one directory, but also the library of functions in a different directory. For example (the details of what’s in the library aren’t very important for my question, I don’t think, but if anyone wants to see more of the code, I can include it):
In lib\library.ts
,
import { ArcRotateCamera, Color3, Engine, HemisphericLight, MeshBuilder, Scene, Vector3 } from "@babylonjs/core";
import { GridMaterial } from "@babylonjs/materials";
import { AdvancedDynamicTexture, Rectangle } from '@babylonjs/gui';
const black = new Color3(0,0,0);
export let init = function(canvasId: string, width="500px", height="500px") {
const canvas = document.querySelector<HTMLCanvasElement>(canvasId);
if (canvas == null) {throw new Error("canvas id not found");}
canvas.style.width = width;
canvas.style.height = height;
const engine = new Engine(canvas,true);
const scene = new Scene(engine);
scene.useRightHandedSystem = true;
const camera = new ArcRotateCamera("Camera", 0,0,1, Vector3.Zero(), scene);
camera.attachControl(canvas, true);
camera.setPosition( new Vector3(0,-2,20) );
camera.cameraDirection.set( 0,5,-20 );
const light = new HemisphericLight("light", new Vector3(.5,1,0), scene);
light.intensity = .7;
scene.ambientColor = new Color3(.1,.2,.3);
return {canvas, engine, scene, camera, light};
}
... (so on with more functions)
In \vite-project\src\proj.ts
import './style.css';
import {Engine, Scene, ArcRotateCamera, Vector3, Color3, HemisphericLight, MeshBuilder, AxesViewer, UniversalCamera, DynamicTexture, StandardMaterial} from "@babylonjs/core";
import {GridMaterial} from '@babylonjs/materials';
import {AdvancedDynamicTexture, GUI3DManager, HolographicSlate, Rectangle, TextBlock} from '@babylonjs/gui';
import {init, spinsphere, textbox, twodgrid} from "/Users/ax/BabylonScripts/main";
const p1bag = init("#p1");
const p1scene = p1bag.scene;
twodgrid(p1scene);
spinsphere(p1scene, 0,0);
spinsphere(p1scene, 1,1,0, 0,1,0, .2, true);
spinsphere(p1scene, 2,1,0, 0,0,1);
... (so on with making the rest of the app, making calls to functions written in library.ts)
Now I know that all the code works, since I can run the app and it looks how I intend, with no console errors. (By running yarn dev
in the terminal.)
But when I try to build it, I think understandably, NPM complains.
/Users/ax/BabylonScripts/main.ts:3:51 - error TS2307: Cannot find module '@babylonjs/gui' or its corresponding type declarations.
3 import { AdvancedDynamicTexture, Rectangle } from '@babylonjs/gui';
The problem is that the library script lives outside of any project – which is what I want, because it shouldn’t be attached to any project. But it also calls on BabylonJS packages, which it can’t find, because it’s developed outside of … whatever it is. NPM, Yarn, Vite, whichever of those are responsible for “knowing” about the BabylonJS packages.
I’m guessing that I should “manually” make the library into an NPM package (i.e. in the \lib
directory run npm init
), and then in the package.json
file, include BabylonJS as a dependency. So I did this and edited the file to look like this:
{
"name": "babylonscripts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"dependencies": {
"@babylonjs/core": "^6.19.1",
"@babylonjs/gui": "^6.19.1"
},
"license": "ISC"
}
But this didn’t resolve the issue.
One slightly funny thing is that this is only a problem for @babylonjs/gui
. I’m also using @babylonjs/core
and @babylonjs/materials
and neither of these show an error in intellisense or in the build process. Only the GUI library causes an error, both in intellisense and the build.
I guess also it only now occurs to me that I did all of this in the context of using Yarn and Vite, because I was following a YouTube walk-through. But the instructions in the documentation talk about Webpack. Maybe I should switch? I don’t know much about either of these, I’m mostly just doing what seems to be working for other people.