
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
@three.ez/instanced-mesh-test
Advanced tools
Simplified and enhanced InstancedMesh with frustum culling, fast raycasting (using BVH), sorting, visibility management and more.
InstancedMesh2
is an alternative version of InstancedMesh
that offers advantages:
Object3D
to simplify its useimport { InstancedMesh2 } from '@three.ez/instanced-mesh';
const myInstancedMesh = new InstancedMesh2(renderer, count, geometry, material);
myInstancedMesh.updateInstances((obj, index) => {
obj.position.z = index;
obj.rotateY(Math.PI);
});
myInstancedMesh.computeBVH();
This library has only one dependency: three.js r159+
.
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.
Sorting should be used to decrease overdraw and render transparent objects.
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 like this:
myInstancedMesh.setVisibilityAt(false, 0);
myInstancedMesh.instances[0].visible = false; // if instances array is created
It is possible to create an array of InstancedEntity (Object3D-like) in order to easily change the visibility, apply transformations and add custom data to each instance, using more memory.
myInstancedMesh.createInstances((obj, index) => {
obj.position.random();
});
myInstancedMesh.instances[0].visible = false;
myInstancedMesh.instances[1].userData = {};
myInstancedMesh.instances[2].position.random();
myInstancedMesh.instances[2].quaternion.random();
myInstancedMesh.instances[2].scale.random();
myInstancedMesh.instances[2].updateMatrix(); // necessary after transformations
myInstancedMesh.instances[3].rotateX(Math.PI);
myInstancedMesh.instances[3].updateMatrix(); // necessary after transformations
To speed up raycasting and frustum culling, it is possible to create a spatial indexing data structure, in this case a dynamic BVH.
This works very well if the instances are mostly static (updating a BVH can be expensive) and scattered in world space.
// calls this function after valuing all instances
myInstancedMesh.computeBVH({ margin: 0, highPrecision: false });
If all instances are static set the margin to 0.
Setting a margin makes BVH updating faster, but may make raycasting and frustum culling slightly slower.
export type Entity<T> = InstancedEntity & T;
export type UpdateEntityCallback<T> = (obj: Entity<T>, index: number) => void;
export interface BVHParams {
margin?: number;
highPrecision?: boolean;
}
export declare class InstancedMesh2<TCustomData = {}, TGeometry extends BufferGeometry = BufferGeometry, TMaterial extends Material | Material[] = Material, TEventMap extends Object3DEventMap = Object3DEventMap> extends Mesh<TGeometry, TMaterial, TEventMap> {
type: 'InstancedMesh2';
isInstancedMesh2: true;
instances: Entity<TCustomData>[];
instanceIndex: GLInstancedBufferAttribute;
matricesTexture: DataTexture;
colorsTexture: DataTexture;
morphTexture: DataTexture;
boundingBox: Box3;
boundingSphere: Sphere;
instancesCount: number;
bvh: InstancedMeshBVH;
perObjectFrustumCulled: boolean;
sortObjects: boolean;
customSort: any;
raycastOnlyFrustum: boolean;
visibilityArray: boolean[];
customDepthMaterial: MeshDepthMaterial;
customDistanceMaterial: MeshDistanceMaterial;
get count(): number;
get maxCount(): number;
get material(): TMaterial;
set material(value: TMaterial);
/** THIS MATERIAL AND GEOMETRY CANNOT BE SHARED */
constructor(renderer: WebGLRenderer, count: number, geometry: TGeometry, material?: TMaterial);
updateInstances(onUpdate: UpdateEntityCallback<Entity<TCustomData>>): void;
createInstances(onInstanceCreation?: UpdateEntityCallback<Entity<TCustomData>>): void;
computeBVH(config?: BVHParams): void;
setMatrixAt(id: number, matrix: Matrix4): void;
getMatrixAt(id: number, matrix?: Matrix4): Matrix4;
setVisibilityAt(id: number, visible: boolean): void;
getVisibilityAt(id: number): boolean;
setColorAt(id: number, color: Color): void;
getColorAt(id: number, color?: Color): Color;
setUniformAt(id: number, name: string, value: UniformValue): void;
getMorphAt(index: number, object: Mesh): void;
setMorphAt(index: number, object: Mesh): void;
raycast(raycaster: Raycaster, result: Intersection[]): void;
computeBoundingBox(): void;
computeBoundingSphere(): void;
copy(source: InstancedMesh2, recursive?: boolean): this;
dispose(): this;
}
export type UniformValueNoNumber = Vector2 | Vector3 | Vector4 | Matrix3 | Matrix4;
export type UniformValue = number | UniformValueNoNumber;
export declare class InstancedEntity {
isInstanceEntity: true;
readonly id: number;
readonly owner: InstancedMesh2;
position: Vector3;
scale: Vector3;
quaternion: Quaternion;
get visible(): boolean;
set visible(value: boolean);
get color(): Color;
set color(value: Color);
get matrix(): Matrix4;
get matrixWorld(): Matrix4;
constructor(owner: InstancedMesh2<any, any, any>, index: number);
updateMatrix(): void;
setUniform(name: string, value: UniformValue): void;
copyTo(target: Mesh): void;
applyMatrix4(m: Matrix4): this;
applyQuaternion(q: Quaternion): this;
rotateOnAxis(axis: Vector3, angle: number): this;
rotateOnWorldAxis(axis: Vector3, angle: number): this;
rotateX(angle: number): this;
rotateY(angle: number): this;
rotateZ(angle: number): this;
translateOnAxis(axis: Vector3, distance: number): this;
translateX(distance: number): this;
translateY(distance: number): this;
translateZ(distance: number): this;
}
export declare function patchShader(shader: string): string;
export declare function createRadixSort(target: InstancedMesh2): typeof radixSort<InstancedRenderItem>;
export declare function createTexture_float(count: number): DataTexture;
export declare function createTexture_vec2(count: number): DataTexture;
export declare function createTexture_vec3(count: number): DataTexture;
export declare function createTexture_vec4(count: number): DataTexture;
export declare function createTexture_mat3(count: number): DataTexture;
export declare function createTexture_mat4(count: number): DataTexture;
It works similarly to BatchedMesh
: matrices, colors, etc. are stored in Texture
instead of InstancedAttribute
.
The only InstancedAttribute
is used to store the indices of the instances to be rendered.
If you create a custom material, you will need to use Texture
instead of InstancedBufferAttribute
.
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@0.167.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.167.0/examples/jsm/",
"@three.ez/instanced-mesh": "https://cdn.jsdelivr.net/npm/@three.ez/instanced-mesh/index.js",
"bvh.js": "https://cdn.jsdelivr.net/npm/bvh.js/index.js"
}
}
</script>
These examples use vite
, and some mobile devices may run out of memory.
If you have questions or need assistance, you can ask on our discord server.
If you find this project helpful, I would greatly appreciate it if you could leave a star on this repository!
This helps me know that you appreciate my work and encourages me to continue improving it.
Thank you so much for your support! 🌟
FAQs
Simplified and enhanced InstancedMesh with frustum culling, fast raycasting (using BVH), sorting, visibility management and more.
The npm package @three.ez/instanced-mesh-test receives a total of 0 weekly downloads. As such, @three.ez/instanced-mesh-test popularity was classified as not popular.
We found that @three.ez/instanced-mesh-test demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.