If you are using the ES Modules, you really should try to import individual classes so you get tree shaking. This will likely resolve your issue as there is probably something you are importing that you don’t need that needs a different import; I suspect it is the materials import. It will also dramatically reduce the size of your js file. Also, if you are building a lib and you plan to mark babylon as external in optimizeDependencies, be advised that importing the entire module as you have done with materials will cause vite to bundle the entire module even if you mark it as external.
Notice that the initializer problem has nothing to do, with importing them individually, it happens too, my solution maybe is not the most elegant one but it works:
rotationQuaternion = new Quaternion(0,0,0,1);
The weird thing is that BABYLON.Quaternion.Identity doesn’t work but the rest of the code (long one) does.
I don’t know if it makes a difference, but my imports look like
import { Scene } from '@babylonjs/core/scene';
import { Matrix, Vector3 } from '@babylonjs/core/Maths/math';
import { Viewport } from '@babylonjs/core/Maths/math.viewport';
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
fileTools.ts:132 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘replace’)
at _CleanUrl (fileTools.ts:132:15)
at LoadImage (fileTools.ts:192:15)
at ThinEngine._partialLoadImg (engine.cubeTexture.ts:303:5)
at ThinEngine._cascadeLoadImgs (engine.cubeTexture.ts:264:14)
at ThinEngine.createCubeTextureBase (engine.cubeTexture.ts:428:14)
at ThinEngine.createCubeTexture (engine.cubeTexture.ts:464:17)
at _CubeTexture._loadTexture (cubeTexture.ts:408:52)
at _CubeTexture.updateURL (cubeTexture.ts:301:18)
at new _CubeTexture (cubeTexture.ts:218:14)
at _CubeTexture.CreateFromPrefilteredData (cubeTexture.ts:151:24)
All credits to Raanan’s original template since it already had the import, but one technique that you can try to resolve these import issues is to look at the code of the function’s stack trace and see if there is anything dynamically loaded there (which isn’t very intuitive, I have to say). In the case of createCubeTextureBase, it does need a loader here:
There are a bunch of places this happens. For example, if you want to use Scene.createDefaultCamera, then you need to import import '@babylonjs/core/Helpers/sceneHelpers';. I follow the method @carolhmj suggested and have found it isn’t terribly difficult to identify what you are missing. Would be nice to have warnings for all these cases, though.
Hmmm, the sceneHelpers case is a bit more complicated because the functions are not defined on Scene, but in SceneHelpers. To log a warning, we’d have to move the definitions to Scene, and we try avoiding that, because it makes the class itself larger.
Yep. I think maybe just having something in docs that annotates that using a particular function requires importing a specific package (or subpackage) would be helpful. Similar to how in Unity or Unreal docs it tells you what header file is needed for a specific component.