@adatechnology/game-shapes-three 0.0.0-stage → 0.1.1
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 +44 -2
- package/dist/index.d.ts +69 -0
- package/dist/index.global.js +1798 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +302 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anderson Fernandes
|
|
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
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @adatechnology/game-shapes-three
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The three.js side of [`@adatechnology/game-shapes`](../game-shapes): buffers become geometries, meshes, part groups, `InstancedMesh` and `LOD`.
|
|
4
|
+
three.js is passed in by the host; the package never imports it.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
pnpm add @adatechnology/game-shapes @adatechnology/game-shapes-three three
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Browser without a bundler: `dist/index.global.js` exposes `GameShapesThree` (game-shapes is bundled in; three.js is yours).
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import * as THREE from 'three'
|
|
18
|
+
import { generate } from '@adatechnology/game-shapes'
|
|
19
|
+
import { shapeGeometries, toGeometry, toObject3D, mergeByMaterial } from '@adatechnology/game-shapes-three'
|
|
20
|
+
|
|
21
|
+
// a whole shape: one mesh per (part, material), part groups at their pivots, instances, a LOD when there are levels
|
|
22
|
+
const { object, parts, meshes, instanced, points, materials } = toObject3D(THREE, generate(schema), { materials: { wood: myWood } })
|
|
23
|
+
scene.add(object)
|
|
24
|
+
parts.door.rotation.y = 1.2
|
|
25
|
+
|
|
26
|
+
// three's constructors, made by game-shapes (bit-identical, instances of three's classes, template cache)
|
|
27
|
+
const T = shapeGeometries(THREE)
|
|
28
|
+
const handle = new T.CylinderGeometry(0.05, 0.065, 1.1, 10) // instanceof THREE.CylinderGeometry, type 'CylinderGeometry'
|
|
29
|
+
|
|
30
|
+
// one primitive or one batch
|
|
31
|
+
const geometry = toGeometry(THREE, buildPrimitive({ kind: 'torus', radius: 0.3, tube: 0.02 }))
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
| Export | What it does |
|
|
35
|
+
|---|---|
|
|
36
|
+
| `toGeometry(THREE, primitiveOrBatch, { copy })` | A `BufferGeometry`; a primitive gets three's `type`, `parameters` and the prototype of the class it reproduces, its attributes in three's order, its index and groups. `copy: false` shares the arrays (read-only) |
|
|
37
|
+
| `shapeGeometries(THREE, { cache })` | A namespace equal to `THREE` except `BoxGeometry`, `CylinderGeometry`, `ConeGeometry`, `SphereGeometry`, `PlaneGeometry`, `CircleGeometry`, `RingGeometry`, `LatheGeometry`, `TorusGeometry` and `ExtrudeGeometry` (one straight-edged shape; curves, several shapes, a path or a custom uv generator fall back to three). Hand it as `T` to code ported from the teams |
|
|
38
|
+
| `toObject3D(THREE, buffers, { materials, materialFor, shadows, copy, name })` | `{ object, near, parts, meshes, instanced, points, materials }`; meshes are named `static:<material>` / `part:<id>` with `userData.material`, `userData.part` and `userData.ranges`; levels after the first are static (parts baked at rest), and `cullDistance` ends the `LOD` |
|
|
39
|
+
| `createMaterials(THREE, hints, ids)` | `MeshStandardMaterial` per id from the schema's material hints (vertex colours on) |
|
|
40
|
+
| `mergeByMaterial(THREE, root, space)` | Every visible mesh under `root` baked into `space` and joined per material (non-indexed), on game-shapes' merge; the same bytes `game-props3d`'s version gave |
|
|
41
|
+
|
|
42
|
+
## Português
|
|
43
|
+
|
|
44
|
+
Desenho three.js das formas do `game-shapes`: geometria com o tipo e o protótipo da classe do three, malha por lote, grupos de peça, instâncias e
|
|
45
|
+
LOD; o `shapeGeometries(THREE)` é o `T` que o código portado dos times recebe para continuar escrevendo `new T.CylinderGeometry(...)`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { PrimitiveBuffers, ShapeBatch, ShapePoint, MaterialHint, ShapeBuffers } from '@adatechnology/game-shapes';
|
|
2
|
+
import * as THREE_NS from 'three';
|
|
3
|
+
|
|
4
|
+
type Three = typeof THREE_NS;
|
|
5
|
+
interface ToGeometryOptions {
|
|
6
|
+
/** Copy the arrays into the attributes (default true). Without a copy the geometry shares the buffers (a cached result is read-only: do not deform it). */
|
|
7
|
+
readonly copy?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A `BufferGeometry` from game-shapes buffers. A primitive becomes an instance of the three class it reproduces (`type`, `parameters`, prototype:
|
|
11
|
+
* `instanceof THREE.CylinderGeometry`, `clone()` and `toJSON()` behave as three's), with the same attributes, index and groups. A batch gets
|
|
12
|
+
* `position`, `normal`, `uv`, `color` (when present) and its index.
|
|
13
|
+
*/
|
|
14
|
+
declare function toGeometry(THREE: Three, source: PrimitiveBuffers | ShapeBatch, options?: ToGeometryOptions): THREE_NS.BufferGeometry;
|
|
15
|
+
|
|
16
|
+
/** The geometry constructors `shapeGeometries` replaces. */
|
|
17
|
+
declare const SHAPE_GEOMETRY_TYPES: readonly ["BoxGeometry", "CylinderGeometry", "ConeGeometry", "SphereGeometry", "PlaneGeometry", "CircleGeometry", "RingGeometry", "LatheGeometry", "TorusGeometry", "ExtrudeGeometry"];
|
|
18
|
+
interface ShapeGeometriesOptions {
|
|
19
|
+
/** Go through the template cache of game-shapes (default true): the same primitive is generated once per process. */
|
|
20
|
+
readonly cache?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A three namespace whose `BoxGeometry`, `CylinderGeometry`, `ConeGeometry`, `SphereGeometry`, `PlaneGeometry`, `CircleGeometry`, `RingGeometry`,
|
|
24
|
+
* `LatheGeometry`, `TorusGeometry` and (straight-edged) `ExtrudeGeometry` are made by game-shapes, bit-identical to three's and instances of three's
|
|
25
|
+
* classes; everything else is three's own. Hand it to code ported from the teams as `T`, so it keeps reading `new T.CylinderGeometry(...)`.
|
|
26
|
+
* An extrusion with curves, several shapes, a path or a custom uv generator falls back to three.
|
|
27
|
+
*/
|
|
28
|
+
declare function shapeGeometries(THREE: Three, options?: ShapeGeometriesOptions): Three;
|
|
29
|
+
|
|
30
|
+
/** A `MeshStandardMaterial` per material id from the schema's hints (vertex colours on unless the hint says otherwise). */
|
|
31
|
+
declare function createMaterials(THREE: Three, hints?: Readonly<Record<string, MaterialHint>>, ids?: Iterable<string>): Record<string, THREE_NS.Material>;
|
|
32
|
+
interface ToObjectOptions {
|
|
33
|
+
/** Materials by id; missing ids get one from the schema hints (`createMaterials`). */
|
|
34
|
+
readonly materials?: Readonly<Record<string, THREE_NS.Material>>;
|
|
35
|
+
/** The material of one draw (wins over `materials`): per material id and part (`null` for the static body). */
|
|
36
|
+
readonly materialFor?: (material: string, part: string | null) => THREE_NS.Material | undefined;
|
|
37
|
+
/** Cast and receive shadows (default true). */
|
|
38
|
+
readonly shadows?: boolean;
|
|
39
|
+
/** Copy the arrays into the attributes (default true; false shares them, read-only). */
|
|
40
|
+
readonly copy?: boolean;
|
|
41
|
+
/** Name of the root. */
|
|
42
|
+
readonly name?: string;
|
|
43
|
+
}
|
|
44
|
+
interface ShapeObject {
|
|
45
|
+
/** A `Group`, or a `LOD` when the shape has levels or a cull distance. */
|
|
46
|
+
readonly object: THREE_NS.Object3D;
|
|
47
|
+
/** The level 0 group (the root itself without LOD). */
|
|
48
|
+
readonly near: THREE_NS.Group;
|
|
49
|
+
/** Part groups of level 0 by id, at their pivots; animate these. */
|
|
50
|
+
readonly parts: Record<string, THREE_NS.Group>;
|
|
51
|
+
readonly meshes: THREE_NS.Mesh[];
|
|
52
|
+
readonly instanced: THREE_NS.InstancedMesh[];
|
|
53
|
+
readonly points: readonly ShapePoint[];
|
|
54
|
+
readonly materials: Record<string, THREE_NS.Material>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The three objects of a shape: one mesh per batch (`static:<material>` or `part:<id>`, `userData.material`, `userData.part`), part groups at their
|
|
58
|
+
* pivots, an `InstancedMesh` per instanced batch, and a `LOD` (levels at their distances, nothing from `cullDistance`) when there are levels.
|
|
59
|
+
*/
|
|
60
|
+
declare function toObject3D(THREE: Three, buffers: ShapeBuffers, options?: ToObjectOptions): ShapeObject;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Every visible mesh under `root` (whose world matrix must be up to date), baked into the space of `space` and joined by material: one non-indexed
|
|
64
|
+
* mesh per material (position, normal, uv), no shadow cast. The bake is game-shapes' `mergeTransformed`, bit-identical to three's
|
|
65
|
+
* `toNonIndexed` + `applyMatrix4` + join, so the result is the one `game-props3d`'s `mergeByMaterial` always gave.
|
|
66
|
+
*/
|
|
67
|
+
declare function mergeByMaterial(THREE: Three, root: THREE_NS.Object3D, space: THREE_NS.Object3D): THREE_NS.Mesh[];
|
|
68
|
+
|
|
69
|
+
export { SHAPE_GEOMETRY_TYPES, type ShapeGeometriesOptions, type ShapeObject, type Three, type ToGeometryOptions, type ToObjectOptions, createMaterials, mergeByMaterial, shapeGeometries, toGeometry, toObject3D };
|