
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
@three.ez/instanced-mesh
Advanced tools
Enhanced InstancedMesh with frustum culling, fast raycasting (using BVH), sorting, visibility management and more.
Simplify your three.js application development with three.ez!
three.ez/batched-mesh-extensions - BatchedMesh
extension methods and enhancements for better performance and usability
InstancedMesh2
is an alternative version of InstancedMesh
with enhanced features for performance and usability.
const myInstancedMesh = new InstancedMesh2(geometry, material);
myInstancedMesh.addInstances(count, (obj, index) => {
obj.position.x = index;
});
Object3D
with transforms and custom data.Vanilla
Using three.ez/main
Using other libraries
Join us on Discord or open an issue on GitHub.
If you like this project, please leave a star. Thank you! ❤️
The documentation is available here.
You can install it via npm using the following command:
npm install @three.ez/instanced-mesh
Or you can import it from CDN:
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three/examples/jsm/",
"@three.ez/instanced-mesh": "https://cdn.jsdelivr.net/npm/@three.ez/instanced-mesh/build/index.js",
"bvh.js": "https://cdn.jsdelivr.net/npm/bvh.js/build/index.js"
}
}
</script>
Manage a dynamic number of instances, automatically expanding the data buffers as needed to accommodate additional instances.
If not specified, capacity
is 1000
.
const myInstancedMesh = new InstancedMesh2(geometry, material, { capacity: count });
myInstancedMesh.addInstances(count, (obj, index) => { ... }); // add instances and expand buffer if necessary
myInstancedMesh.removeInstances(id0, id1, ...);
myInstancedMesh.clearInstances(); // remove all instances
It's possible to create an array of InstancedEntity
(Object3D-like) in order to easily manipulate instances, using more memory.
const myInstancedMesh = new InstancedMesh2(geometry, material, { createEntities: true });
myInstancedMesh.instances[0].customData = {};
myInstancedMesh.instances[0].position.random();
myInstancedMesh.instances[0].rotateX(Math.PI);
myInstancedMesh.instances[0].updateMatrix(); // necessary after transformations
Avoiding rendering objects outside the camera frustum can drastically improve performance (especially for complex geometries).
Frustum culling by default is performed by iterating all instances, but it is possible to speed up this process by creating a spatial indexing data structure (BVH).
By default perObjectFrustumCulled
is true
.
To speed up raycasting and frustum culling, a spatial indexing data structure can be created to contain the boundingBoxes of all instances.
This works very well if the instances are mostly static (updating a BVH can be expensive) and scattered in world space.
Setting a margin makes BVH updating faster, but may make raycasting and frustum culling slightly slower.
myInstancedMesh.computeBVH({ margin: 0 }); // margin is optional
Sorting can be used to decrease overdraw and render transparent objects.
It's possible to improve sort performance adding a customSort
, like built-in createRadixSort
.
By default sortObjects
is false
.
import { createRadixSort } from '@three.ez/instanced-mesh';
myInstancedMesh.sortObjects = true;
myInstancedMesh.customSort = createRadixSort(myInstancedMesh);
Set the visibility status of each instance.
myInstancedMesh.setVisibilityAt(index, false);
myInstancedMesh.instances[0].visible = false; // if instances array is created
Set the opacity of each instance. It's recommended to enable instances sorting and disable the depthWriting
of the material.
myInstancedMesh.setOpacityAt(index, 0.5);
myInstancedMesh.instances[0].opacity = 0.5; // if instances array is created
Assign unique shader uniforms to each instance, working with every materials.
myInstancedMesh.initUniformsPerInstance({ fragment: { metalness: 'float', roughness: 'float', emissive: 'vec3' } });
myInstancedMesh.setUniformAt(index, 'metalness', 0.5);
myInstancedMesh.instances[0].setUniform('emissive', new Color('white')); // if instances array is created
Improve rendering performance by dynamically adjusting the detail level of instances based on their distance from the camera.
Use simplified geometries for distant objects to optimize resources.
myInstancedMesh.addLOD(geometryMid, material, 50);
myInstancedMesh.addLOD(geometryLow, material, 200);
Optimize shadow rendering by reducing the detail level of instances casting shadows based on their distance from the camera.
myInstancedMesh.addShadowLOD(geometryMid);
myInstancedMesh.addShadowLOD(geometryLow, 100);
Apply skeletal animations to instances for more complex and dynamic movements.
myInstancedMesh.initSkeleton(skeleton);
mixer.update(time);
myInstancedMesh.setBonesAt(index);
If you are not using a BVH, you can set the raycastOnlyFrustum
property to true to avoid iterating over all instances.
It's recommended to use three-mesh-bvh to create a geometry BVH.
FAQs
Enhanced InstancedMesh with frustum culling, fast raycasting (using BVH), sorting, visibility management and more.
We found that @three.ez/instanced-mesh demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.