bbmodel-viewer 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/LICENSE +21 -0
- package/README.md +148 -0
- package/dist/index.cjs +606 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +221 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.js +581 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Renard
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# bbmodel-viewer
|
|
2
|
+
|
|
3
|
+
Render and view [Blockbench](https://blockbench.net) `.bbmodel` files in the
|
|
4
|
+
browser with [three.js](https://threejs.org). Drop a container element in,
|
|
5
|
+
point it at a file (URL, `File`, or parsed object) and get an orbit-controlled
|
|
6
|
+
3D preview — cubes **and** meshes, per-face UVs, textures, and the full bone
|
|
7
|
+
(outliner) hierarchy with correct pivots and rotations.
|
|
8
|
+
|
|
9
|
+
Built for exactly this kind of use case: **previewing model files stored on a
|
|
10
|
+
drive / CDN** without opening Blockbench.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- ✅ Cube elements with per-face UVs, `inflate`, per-face UV rotation
|
|
15
|
+
- ✅ Mesh (poly) elements (triangles + quads)
|
|
16
|
+
- ✅ Embedded textures (`data:` sources) with crisp nearest-neighbour filtering
|
|
17
|
+
- ✅ Full outliner hierarchy: groups/bones with pivots and `ZYX` Euler rotation
|
|
18
|
+
- ✅ Transparent textures (alpha cutout)
|
|
19
|
+
- ✅ Orbit / zoom / pan controls, optional auto-rotate and ground grid
|
|
20
|
+
- ✅ Auto camera framing, container resize handling, clean `dispose()`
|
|
21
|
+
- ✅ TypeScript types, ESM + CJS builds, three.js as a peer dependency
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install bbmodel-viewer three
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`three` is a **peer dependency** (`>=0.150.0`) so your app controls the version.
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { BBModelViewer } from "bbmodel-viewer";
|
|
35
|
+
|
|
36
|
+
const container = document.getElementById("viewer")!; // give it a size in CSS
|
|
37
|
+
const viewer = new BBModelViewer(container, {
|
|
38
|
+
background: "#20232a",
|
|
39
|
+
autoRotate: true,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await viewer.load("/models/creeper.bbmodel");
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The container should have a non-zero size (e.g. `width: 100%; height: 480px`).
|
|
46
|
+
The viewer fills it and follows resizes automatically.
|
|
47
|
+
|
|
48
|
+
### From a file input or drag & drop (drive viewer)
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
input.addEventListener("change", (e) => {
|
|
52
|
+
const file = (e.target as HTMLInputElement).files?.[0];
|
|
53
|
+
if (file) viewer.loadFile(file);
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### From an already-parsed object
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const data = await fetch(url).then((r) => r.json());
|
|
61
|
+
await viewer.loadModel(data);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Options
|
|
65
|
+
|
|
66
|
+
All options are optional.
|
|
67
|
+
|
|
68
|
+
| Option | Type | Default | Description |
|
|
69
|
+
| ----------------------- | --------------------------- | -------------- | ------------------------------------------------------- |
|
|
70
|
+
| `background` | `string \| number \| null` | `"#20232a"` | Scene background. `null`/`"transparent"` for see-through |
|
|
71
|
+
| `grid` | `boolean` | `true` | Ground grid aligned to the model base |
|
|
72
|
+
| `controls` | `boolean` | `true` | Orbit / zoom / pan |
|
|
73
|
+
| `autoRotate` | `boolean` | `false` | Spin the model |
|
|
74
|
+
| `autoRotateSpeed` | `number` | `1.0` | Spin speed |
|
|
75
|
+
| `fov` | `number` | `45` | Camera field of view (degrees) |
|
|
76
|
+
| `ambientIntensity` | `number` | `0.8` | Ambient light |
|
|
77
|
+
| `directionalIntensity` | `number` | `0.55` | Key light |
|
|
78
|
+
| `doubleSided` | `boolean` | `true` | Render both sides of every polygon |
|
|
79
|
+
| `alphaTest` | `number` | `0.02` | Alpha cutoff for transparent texels |
|
|
80
|
+
| `antialias` | `boolean` | `true` | MSAA |
|
|
81
|
+
| `pixelRatio` | `number` | `min(dpr, 2)` | Device pixel ratio cap |
|
|
82
|
+
| `onLoad` | `(model, object) => void` | — | Fires after a model is added to the scene |
|
|
83
|
+
| `onError` | `(error) => void` | — | Fires on load/parse failure |
|
|
84
|
+
|
|
85
|
+
## API
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
const viewer = new BBModelViewer(container, options);
|
|
89
|
+
|
|
90
|
+
await viewer.load(url, init?); // fetch + render a .bbmodel URL
|
|
91
|
+
await viewer.loadFile(file); // render a File/Blob
|
|
92
|
+
await viewer.loadModel(data); // render a parsed bbmodel object
|
|
93
|
+
viewer.frameModel(); // re-frame the camera on the model
|
|
94
|
+
viewer.resize(); // re-measure the container (also automatic)
|
|
95
|
+
viewer.clear(); // remove the model + free GPU memory
|
|
96
|
+
viewer.dispose(); // tear everything down
|
|
97
|
+
|
|
98
|
+
// Escape hatches for advanced use:
|
|
99
|
+
viewer.scene; // THREE.Scene
|
|
100
|
+
viewer.camera; // THREE.PerspectiveCamera
|
|
101
|
+
viewer.renderer; // THREE.WebGLRenderer
|
|
102
|
+
viewer.controls; // OrbitControls | undefined
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Lower-level building blocks
|
|
106
|
+
|
|
107
|
+
If you just want the geometry (e.g. to drop into your own scene):
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { MaterialLibrary, buildModelObject } from "bbmodel-viewer";
|
|
111
|
+
|
|
112
|
+
const materials = new MaterialLibrary(model, { alphaTest: 0.02, side: THREE.DoubleSide });
|
|
113
|
+
await materials.load();
|
|
114
|
+
const object3d = buildModelObject(model, materials); // THREE.Group
|
|
115
|
+
myScene.add(object3d);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Coordinate system & conventions
|
|
119
|
+
|
|
120
|
+
- Units are Blockbench units (16 = one Minecraft block); the grid draws one cell
|
|
121
|
+
per block.
|
|
122
|
+
- Rotations use Euler order `ZYX` (matching Blockbench's `Rz·Ry·Rx`).
|
|
123
|
+
- Per-face UVs are read as `[u_min, v_min, u_max, v_max]` in texture pixels and
|
|
124
|
+
normalized by each texture's `uv_width`/`uv_height` (falling back to the
|
|
125
|
+
project `resolution`).
|
|
126
|
+
- Textures use `NearestFilter` and no mipmaps for pixel-perfect art.
|
|
127
|
+
|
|
128
|
+
## Notes & limitations
|
|
129
|
+
|
|
130
|
+
- **Animations** are not played yet (static pose only). The bone hierarchy is
|
|
131
|
+
built, so this is a planned addition.
|
|
132
|
+
- **`box_uv`**: files store computed per-face UVs, which are used directly; a
|
|
133
|
+
best-effort standard unwrap is used only if a face's `uv` is missing.
|
|
134
|
+
- Cross-origin textures/URLs need appropriate CORS headers when loaded from a
|
|
135
|
+
remote drive.
|
|
136
|
+
|
|
137
|
+
## Development
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
npm install
|
|
141
|
+
npm run build # bundle to dist/ (ESM + CJS + d.ts)
|
|
142
|
+
npm run typecheck
|
|
143
|
+
npm run example # serve examples/ with Vite
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
MIT
|