master
Colby Knox v0.12.1 eeedee4
// The BABYLON namespace the viewer hands to host pages (window.BABYLON and
// Adom3DViewerBabylon9.BABYLON).
//
// It used to be `import * as BABYLON from '@babylonjs/core'`, which pulls the
// ENTIRE engine into the bundle every page downloads (the core index registers
// every module as a side effect, so nothing can be tree-shaken): 12.4 MB of
// script at startup for a viewer that uses a fraction of it. Now the
// namespace starts with the classes host pages commonly reach for, bundled
// with the viewer, and loadBabylon() brings in the rest on demand as its own
// chunk. A name outside the starting set reads as undefined (so feature
// checks like `if (BABYLON.X)` keep working) and warns once with the fix.

import { Vector2, Vector3, Vector4, Quaternion, Matrix } from '@babylonjs/core/Maths/math.vector';
import { Color3, Color4 } from '@babylonjs/core/Maths/math.color';
import { Axis, Space } from '@babylonjs/core/Maths/math.axis';
import { Scalar } from '@babylonjs/core/Maths/math.scalar';
import { Mesh } from '@babylonjs/core/Meshes/mesh';
import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
import { MeshBuilder } from '@babylonjs/core/Meshes/meshBuilder';
import { VertexData } from '@babylonjs/core/Meshes/mesh.vertexData';
import { VertexBuffer } from '@babylonjs/core/Buffers/buffer';
import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
import { PBRMetallicRoughnessMaterial } from '@babylonjs/core/Materials/PBR/pbrMetallicRoughnessMaterial';
import { Texture } from '@babylonjs/core/Materials/Textures/texture';
import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
import { CubeTexture } from '@babylonjs/core/Materials/Textures/cubeTexture';
import { RenderTargetTexture } from '@babylonjs/core/Materials/Textures/renderTargetTexture';
import { SceneLoader } from '@babylonjs/core/Loading/sceneLoader';
import { DracoCompression } from '@babylonjs/core/Meshes/Compression/dracoCompression';
import { Animation } from '@babylonjs/core/Animations/animation';
import { AnimationGroup } from '@babylonjs/core/Animations/animationGroup';
import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
import { HemisphericLight } from '@babylonjs/core/Lights/hemisphericLight';
import { DirectionalLight } from '@babylonjs/core/Lights/directionalLight';
import { PointLight } from '@babylonjs/core/Lights/pointLight';
import { SpotLight } from '@babylonjs/core/Lights/spotLight';
import { ShadowGenerator } from '@babylonjs/core/Lights/Shadows/shadowGenerator';
import { Tools } from '@babylonjs/core/Misc/tools';
import { Observable } from '@babylonjs/core/Misc/observable';
import { PointerEventTypes } from '@babylonjs/core/Events/pointerEvents';
import { KeyboardEventTypes } from '@babylonjs/core/Events/keyboardEvents';
import { Ray } from '@babylonjs/core/Culling/ray';
import { Engine } from '@babylonjs/core/Engines/engine';
import { EngineStore } from '@babylonjs/core/Engines/engineStore';
import { Scene } from '@babylonjs/core/scene';
import { Material } from '@babylonjs/core/Materials/material';
import { ImageProcessingConfiguration } from '@babylonjs/core/Materials/imageProcessingConfiguration';
import { LoadAssetContainerAsync } from '@babylonjs/core/Loading/sceneLoader';
import { DracoDecoder } from '@babylonjs/core/Meshes/Compression/dracoDecoder';
import { PickingInfo } from '@babylonjs/core/Collisions/pickingInfo';
import { Layer } from '@babylonjs/core/Layers/layer';
import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
import { GlowLayer } from '@babylonjs/core/Layers/glowLayer';

const base: Record<string, unknown> = {
  Vector2, Vector3, Vector4, Quaternion, Matrix, Color3, Color4, Axis, Space, Scalar,
  Mesh, AbstractMesh, TransformNode, MeshBuilder, VertexData, VertexBuffer,
  Material, StandardMaterial, PBRMaterial, PBRMetallicRoughnessMaterial, ImageProcessingConfiguration,
  Texture, DynamicTexture, CubeTexture, RenderTargetTexture,
  SceneLoader, LoadAssetContainerAsync, DracoCompression, DracoDecoder, Animation, AnimationGroup,
  ArcRotateCamera, HemisphericLight, DirectionalLight, PointLight, SpotLight, ShadowGenerator,
  Layer, HighlightLayer, GlowLayer,
  Tools, Observable, PointerEventTypes, KeyboardEventTypes, Ray, PickingInfo, Engine, EngineStore, Scene,
};

let full: Record<string, unknown> | null = null;
let loading: Promise<Record<string, unknown>> | null = null;
const warned = new Set<string>();

/** The whole @babylonjs/core namespace, loaded once on first call. */
export function loadBabylon(): Promise<Record<string, unknown>> {
  if (full) return Promise.resolve(full);
  if (!loading) {
    loading = import('@babylonjs/core').then((m) => {
      full = m as unknown as Record<string, unknown>;
      for (const k of Object.keys(full)) if (!(k in base)) base[k] = full[k];
      return full;
    });
  }
  return loading;
}

/** window.BABYLON / Adom3DViewerBabylon9.BABYLON. */
export const BABYLON_NAMESPACE: Record<string, unknown> = new Proxy(base, {
  get(target, prop) {
    if (typeof prop !== 'string' || prop in target) return (target as any)[prop];
    if (prop === 'then' || prop === 'toJSON' || prop.startsWith('__')) return undefined;
    if (!full && !warned.has(prop)) {
      warned.add(prop);
      console.warn(`[adom-3d-viewer] BABYLON.${prop} is not in the viewer's bundle. ` +
        'Load the full engine first: await Adom3DViewerBabylon9.loadBabylon() (or viewer.loadBabylon()), then use BABYLON.' + prop + '.');
    }
    return undefined;
  },
});