babylonbridge 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +1 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# Babylonbridge
|
|
4
|
+
|
|
5
|
+
### A small, explicit bridge from portable 3D descriptions to Babylon.js.
|
|
6
|
+
|
|
7
|
+
[](https://www.typescriptlang.org/) [](package.json) [](https://www.babylonjs.com/) [](https://shaderlab.dev)
|
|
8
|
+
|
|
9
|
+
[shaderlab.dev](https://shaderlab.dev)
|
|
10
|
+
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
Babylonbridge materializes Web3D Kit descriptors as Babylon.js runtime objects.
|
|
14
|
+
It keeps scene descriptions portable while leaving rendering, asset loading, and
|
|
15
|
+
resource ownership explicit.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install @babylonjs/core babylonbridge web3d-kit
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`@babylonjs/core` is a peer dependency: applications choose and own their
|
|
24
|
+
Babylon.js version. `babylonbridge` does not bundle it.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { mesh, planeGeometry, shaderMaterial } from "web3d-kit";
|
|
28
|
+
import { createBabylonObject } from "babylonbridge";
|
|
29
|
+
|
|
30
|
+
const object = createBabylonObject(
|
|
31
|
+
mesh({
|
|
32
|
+
geometry: planeGeometry({ width: 2, height: 2 }),
|
|
33
|
+
material: shaderMaterial({ vertexShader, fragmentShader }),
|
|
34
|
+
}),
|
|
35
|
+
babylonScene,
|
|
36
|
+
);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Architecture
|
|
40
|
+
|
|
41
|
+
```text
|
|
42
|
+
shader-runtime webgl-kit
|
|
43
|
+
│ │
|
|
44
|
+
└─────────┬────────┘
|
|
45
|
+
▼
|
|
46
|
+
web3d-kit
|
|
47
|
+
│
|
|
48
|
+
▼
|
|
49
|
+
babylonbridge
|
|
50
|
+
│
|
|
51
|
+
▼
|
|
52
|
+
Babylon.js
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`shader-runtime` owns shared animation scheduling and `webgl-kit` owns
|
|
56
|
+
low-level WebGL plumbing. `web3d-kit` owns portable descriptions;
|
|
57
|
+
`babylonbridge` materializes them as Babylon.js objects without taking
|
|
58
|
+
ownership of shader source or asset loading.
|
|
59
|
+
|
|
60
|
+
## Supported conversions
|
|
61
|
+
|
|
62
|
+
- vectors, transforms, bounds, scenes, groups, meshes, and cameras
|
|
63
|
+
- plane, box, sphere, and simple position/index buffer geometry
|
|
64
|
+
- basic, standard/PBR, and shader materials
|
|
65
|
+
- scalar and vector shader uniforms
|
|
66
|
+
- caller-owned URL textures, with explicit wrapping/filter options
|
|
67
|
+
|
|
68
|
+
## Lifecycle
|
|
69
|
+
|
|
70
|
+
Use `disposeGeometry`, `disposeMaterial`, `disposeTexture`, or
|
|
71
|
+
`disposeObjectTree` explicitly. `disposeObjectTree` disposes mesh geometry and
|
|
72
|
+
materials; textures remain caller-owned because they are commonly shared.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { AbstractMesh, Engine, FreeCamera, Mesh, PBRMaterial, Scene, ShaderMaterial, StandardMaterial, Texture, TransformNode, Vector2, Vector3, Vector4 } from "@babylonjs/core";
|
|
2
|
+
import type { Bounds3D, CameraDescriptor, GeometryDescriptor, MaterialDescriptor, SceneDescriptor, SceneNode, TextureDescriptor, Transform3D, UniformMap, Vec2, Vec3, Vec4 } from "web3d-kit";
|
|
3
|
+
export declare function toBabylonVector2(value: Vec2): Vector2;
|
|
4
|
+
export declare function toBabylonVector3(value: Vec3): Vector3;
|
|
5
|
+
export declare function toBabylonVector4(value: Vec4): Vector4;
|
|
6
|
+
type BabylonTransformTarget = {
|
|
7
|
+
position: Vector3;
|
|
8
|
+
rotation: Vector3;
|
|
9
|
+
scaling?: Vector3;
|
|
10
|
+
};
|
|
11
|
+
export declare function applyBabylonTransform<T extends BabylonTransformTarget>(target: T, transform: Transform3D | undefined): T;
|
|
12
|
+
export declare function toBabylonBounds(bounds: Bounds3D): {
|
|
13
|
+
min: Vector3;
|
|
14
|
+
max: Vector3;
|
|
15
|
+
};
|
|
16
|
+
export declare function toBabylonGeometry(descriptor: GeometryDescriptor, scene?: Scene | null): Mesh;
|
|
17
|
+
export declare function toBabylonUniforms(uniforms?: UniformMap): UniformMap;
|
|
18
|
+
export declare function toBabylonMaterial(descriptor: MaterialDescriptor, scene: Scene): StandardMaterial | PBRMaterial | ShaderMaterial;
|
|
19
|
+
/** Creates an unbound Babylon texture; callers own image loading and disposal. */
|
|
20
|
+
export declare function toBabylonTexture(descriptor: TextureDescriptor, scene?: Scene | null, source?: string): Texture;
|
|
21
|
+
export declare function createBabylonObject(descriptor: SceneNode, scene: Scene): TransformNode | Mesh;
|
|
22
|
+
export declare function toBabylonScene(descriptor: SceneDescriptor, engine: Engine): Scene;
|
|
23
|
+
export declare function toBabylonCamera(descriptor: CameraDescriptor, scene: Scene): FreeCamera;
|
|
24
|
+
export declare function disposeGeometry(mesh: AbstractMesh): void;
|
|
25
|
+
export declare function disposeMaterial(material: StandardMaterial | PBRMaterial | ShaderMaterial | readonly (StandardMaterial | PBRMaterial | ShaderMaterial)[]): void;
|
|
26
|
+
export declare function disposeTexture(texture: Texture): void;
|
|
27
|
+
/** Disposes mesh geometry and materials in a tree; textures remain caller-owned. */
|
|
28
|
+
export declare function disposeObjectTree(root: TransformNode): void;
|
|
29
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Camera as b,Color3 as l,Constants as s,FreeCamera as g,Mesh as u,MeshBuilder as h,PBRMaterial as w,Scene as M,ShaderMaterial as c,StandardMaterial as R,Texture as S,TransformNode as p,Vector2 as T,Vector3 as y,Vector4 as C,VertexData as A}from"@babylonjs/core";function B(e){return new T(e[0],e[1])}function i(e){return new y(e[0],e[1],e[2])}function O(e){return new C(e[0],e[1],e[2],e[3])}function r(e,n){return n&&(n.position&&(e.position=i(n.position)),n.rotation&&(e.rotation=i(n.rotation)),n.scale&&e.scaling&&(e.scaling=i(n.scale))),e}function I(e){return{min:i(e.min),max:i(e.max)}}function D(e,n=null){switch(e.type){case"plane":return h.CreatePlane("plane",{width:e.width,height:e.height},n??void 0);case"box":return h.CreateBox("box",{width:e.width,height:e.height,depth:e.depth},n??void 0);case"sphere":return h.CreateSphere("sphere",{diameter:e.radius*2,segments:Math.max(e.widthSegments??32,e.heightSegments??16)},n??void 0);case"buffer":{const t=new u("buffer",n),a=new A;return a.positions=e.positions.flatMap(o=>[...o]),e.indices&&(a.indices=[...e.indices]),a.applyToMesh(t),t}}}function d(e){if(e!==void 0)return typeof e=="string"?l.FromHexString(e):typeof e=="number"?l.FromInts(e>>16&255,e>>8&255,e&255):new l(e[0],e[1],e[2])}function m(e,n){n.opacity!==void 0&&(e.alpha=n.opacity),n.transparent!==void 0?e.transparencyMode=n.transparent?2:0:n.opacity!==void 0&&n.opacity<1&&(e.transparencyMode=2),n.side==="back"&&(e.backFaceCulling=!1,e.sideOrientation=u.BACKSIDE),n.side==="double"&&(e.backFaceCulling=!1),n.side==="front"&&(e.backFaceCulling=!0,e.sideOrientation=u.FRONTSIDE)}function V(e,n,t){typeof t=="boolean"?e.setInt(n,t?1:0):typeof t=="number"?e.setFloat(n,t):t.length===2?e.setVector2(n,B(t)):t.length===3?e.setVector3(n,i(t)):e.setVector4(n,O(t))}function U(e={}){return{...e}}function _(e,n){if(e.type==="shader"){const o=new c("shader",n,{vertex:e.vertexShader,fragment:e.fragmentShader},{attributes:["position","normal","uv"],uniforms:Object.keys(e.uniforms??{})});for(const[f,E]of Object.entries(e.uniforms??{}))V(o,f,E);return m(o,e),o}if(e.type==="standard"){const o=d(e.color),f=new w("standard",n??void 0);return o&&(f.albedoColor=o),e.roughness!==void 0&&(f.roughness=e.roughness),e.metalness!==void 0&&(f.metallic=e.metalness),m(f,e),f}const t=d(e.color),a=new R("basic",n??void 0);return t&&(a.diffuseColor=t),m(a,e),a}function P(e,n=null,t){const a=new S(t??"",n??void 0,void 0,!1);return a.hasAlpha=e.flipY===!0,a.vScale=e.flipY===!1?1:-1,a.wrapU=e.wrap==="repeat"?s.TEXTURE_WRAP_ADDRESSMODE:e.wrap==="mirror"?s.TEXTURE_MIRROR_ADDRESSMODE:s.TEXTURE_CLAMP_ADDRESSMODE,a.wrapV=a.wrapU,a.updateSamplingMode(e.minFilter==="nearest"||e.magFilter==="nearest"?s.TEXTURE_NEAREST_SAMPLINGMODE:s.TEXTURE_LINEAR_LINEAR),a}function x(e,n){if(e.type==="group"){const a=new p(e.name??"group",n);e.visible!==void 0&&a.setEnabled(e.visible),e.metadata&&(a.metadata={...e.metadata}),r(a,e.transform);for(const o of e.children??[])x(o,n).parent=a;return a}const t=D(e.geometry,n);return t.name=e.name??t.name,t.material=_(e.material,n),e.visible!==void 0&&t.setEnabled(e.visible),e.metadata&&(t.metadata={...e.metadata}),r(t,e.transform),t}function N(e,n){const t=new M(n);e.metadata&&(t.metadata={...e.metadata});for(const a of e.children??[])x(a,t);return t}function k(e,n){const t=new g("camera",y.Zero(),n);return t.mode=e.type==="orthographic"?b.ORTHOGRAPHIC_CAMERA:b.PERSPECTIVE_CAMERA,t.minZ=e.near,t.maxZ=e.far,e.type==="perspective"?t.fov=e.fov:(t.orthoLeft=e.left,t.orthoRight=e.right,t.orthoTop=e.top,t.orthoBottom=e.bottom),r(t,e.transform)}function L(e){e.dispose(!1,!1)}function X(e){for(const n of Array.isArray(e)?e:[e])n.dispose()}function j(e){e.dispose()}function G(e){for(const n of e.getChildMeshes(!1)){const t=n.material;n.dispose(!1,!1),t&&t.dispose()}e.dispose(!1,!1)}export{r as applyBabylonTransform,x as createBabylonObject,L as disposeGeometry,X as disposeMaterial,G as disposeObjectTree,j as disposeTexture,I as toBabylonBounds,k as toBabylonCamera,D as toBabylonGeometry,_ as toBabylonMaterial,N as toBabylonScene,P as toBabylonTexture,U as toBabylonUniforms,B as toBabylonVector2,i as toBabylonVector3,O as toBabylonVector4};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "babylonbridge",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Small adapters between Mosa WebGL / 3D primitives and Babylon.js.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc --project tsconfig.json",
|
|
22
|
+
"postbuild": "npm run dist --prefix \"$npm_config_local_prefix\" -- \"$PWD\"",
|
|
23
|
+
"prepack": "npm run build",
|
|
24
|
+
"prepublishOnly": "npm run build",
|
|
25
|
+
"pretest": "npm run build",
|
|
26
|
+
"test": "node --test test/*.test.mjs"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"babylonjs",
|
|
30
|
+
"webgl",
|
|
31
|
+
"3d",
|
|
32
|
+
"shaderlab"
|
|
33
|
+
],
|
|
34
|
+
"homepage": "https://shaderlab.dev",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/mosanic/mosa-platform.git",
|
|
38
|
+
"directory": "packages/babylonbridge"
|
|
39
|
+
},
|
|
40
|
+
"author": "Mosanic <dev@mosanic.io>",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=20"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"web3d-kit": "^0.1.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@babylonjs/core": ">=7.0.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@babylonjs/core": "^8.0.0",
|
|
56
|
+
"typescript": "^5.9.3"
|
|
57
|
+
}
|
|
58
|
+
}
|