Fragment
Class representing a fragment of a 3D model. Fragments are just a simple wrapper around THREE.InstancedMesh. Each fragment can contain Items (identified by ItemID) which are mapped to one or many instances inside this THREE.InstancedMesh. Fragments also implement features like instance buffer resizing and hiding out of the box.
Constructors
new Fragment()
new Fragment(
geometry
,material
,count
):Fragment
Constructs a new Fragment.
Parameters
Parameter | Type | Description |
---|---|---|
geometry | BufferGeometry <NormalBufferAttributes > | The geometry of the fragment. |
material | Material | Material [] | The material(s) of the fragment. |
count | number | The initial number of instances in the fragment. |
Returns
Properties
capacity
capacity:
number
=0
The amount of instances that this fragment can contain.
capacityOffset
capacityOffset:
number
=10
The amount by which to increase the capacity when necessary.
group?
optional
group:FragmentsGroup
The group of fragments to which this fragment belongs.
hiddenItems
hiddenItems:
Set
<number
>
A set of item IDs of instances that are currently hidden.
id
id:
string
The unique identifier of this fragment.
ids
ids:
Set
<number
>
A set of unique item IDs associated with this fragment.
instanceToItem
instanceToItem:
Map
<number
,number
>
A map of instance IDs to item IDs.
itemToInstances
itemToInstances:
Map
<number
,Set
<number
>>
A map of item IDs to sets of instance IDs.
mesh
mesh:
FragmentMesh
The mesh associated with this fragment.
Accessors
uniqueVertices
get
uniqueVertices():Vector3
[]
A getter property that returns the unique vertices of the fragment's geometry. The unique vertices are determined by comparing the vertex positions.
Returns
Vector3
[]
An array of unique vertices.
Methods
add()
add(
items
):void
Adds items to the fragment.
Parameters
Parameter | Type | Description |
---|---|---|
items | Item [] | An array of items to be added. Each item contains an ID, an array of transform matrices, and an optional array of colors. If the necessary capacity to accommodate the new items exceeds the current capacity, |
a new mesh with a larger capacity is created, and the old mesh is disposed. The transform matrices and colors of the items are added to the respective attributes of the mesh. The instance IDs, item IDs, and associations between instance IDs and item IDs are updated accordingly. The instance color and matrix attributes of the mesh are updated. |
Returns
void
applyTransform()
applyTransform(
itemIDs
,transform
):void
Applies a transformation matrix to instances associated with given item IDs.
Parameters
Parameter | Type | Description |
---|---|---|
itemIDs | Iterable <number > | An iterable of item IDs to be affected. |
transform | Matrix4 | The transformation matrix to be applied. |
Returns
void
Remarks
This method applies the provided transformation matrix to the instances associated with the given item IDs.
Example
fragment.applyTransform([1, 2, 3], new THREE.Matrix4().makeTranslation(1, 0, 0)); // Applies a translation of (1, 0, 0) to instances with IDs 1, 2, and 3.
clear()
clear():
void
Clears the fragment by resetting the hidden items, item IDs, instance-to-item associations, instance-to-item map, and the count of instances in the fragment's mesh.
Returns
void
Remarks
This method is used to reset the fragment to its initial state.
Example
fragment.clear();
clone()
clone(
itemIDs
):Fragment
Creates a copy of the whole fragment or a part of it. It shares the geometry with the original fragment, but has its own InstancedMesh data, so it also needs to be disposed.
Parameters
Parameter | Type | Description |
---|---|---|
itemIDs | Iterable <number > | An iterable of item IDs to be included in the clone. |
Returns
dispose()
dispose(
disposeResources
):void
Disposes of the fragment and its associated resources.
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
disposeResources | boolean | true | If true, disposes geometries and materials associated with the fragment. If false, only disposes of the fragment itself. |
Returns
void
exportData()
exportData():
object
Exports the fragment's geometry and associated data.
Returns
object
An object containing the exported geometry, an array of IDs associated with the fragment, and the fragment's ID.
colors
colors:
number
[]