THREE.StaticMesh - Less memory, render faster

This is a lightweight scene object class saving memory and giving a better performance for large scenes. It makes several optimizations without requiring core modifications.

This is a plugin that runs out of box without core modifications, wanted to see what can be done as a plugin, THREE has a lot private routines that can’t be patched. I made a more advanced concept for the IndexedVolume.

You can archive up to 50% better performance regarding computations and culling compared to meshes with matrixAutoUpdate set to false, you need to consider a few things though, it’s not a magic bullet and the difference scales by the amount of meshes in the scene.

Test Example


Transforming a static mesh like a regular object

Notes

This isn’t about instancing or accelerating the GPU side, it is about reducing calculations and traversal in the culling phase. SkinnedMesh not supported yet.

When do i benefit?

Generally you save a lot computations and memory, you get the most benefit in a large scene with several thousand objects, with many being outside/beyond the camera frustum far plane. With all objects visible on screen you can still gain several FPS, around 5-10%.

Saving memory

The THREE.Object3D class creates a lot objects in it’s constructor, some you probably not even require such as an individual up vector or layers object, many matrices and also 2 callbacks are created to synchronize euler rotation with the quaternion property what is more of a legacy issue.

StaticMesh is reduced to a few primitives and the worldMatrix, children array only if necessary. For a large scene this has a huge impact. Especially if you have multiple maps/levels you already hold in memory.

Better performance

Culling is improved with less computations and hierarchical culling, this means if the static mesh with it’s children isn’t visible, the children are discarded already. This is specifically useful for assets consisting of sub-meshes, for example a tree with a separate trunk and crown mesh and to group some meshes such as a building with it’s interiors.

Moving static meshes

To move or rotate a static mesh or one of it’s children, you can use them like regular objects or restore the state to the source and apply the hierarchy then again (recommended for multiple changes)

staticMesh.position.x = 10;
staticMesh.rotateY( THREE.Math.DEG2RAD * 90 );

Restore and save:

// Restore hierarchy transformation to source, apply manipulations then save it back to the static mesh

const source = staticMesh.restore();

source.position.x += 100;

staticMesh.save();

To create a static mesh. Avoid using the original mesh if you want to transform any of the static meshes created from it again (use a clone if required), the source is used to restore and update the transformation.

const staticMesh = regularMesh.toStatic();

If you want to really crank it up, checkout the IndexedVolume component i introduced there, it will be available soon with auto-instancing and more.

6 Likes

Is it also possible to move the static mesh by modifying/replacing the model matrix?

Currently no, but i’ll separate the bounding sphere cache update then you only need to update this, children need the original mesh for hierarchy to get updated though.

I’ve made an update, a static mesh can be treated like a regular mesh now.

If the static mesh has no children you can just directly use it’s world matrix to transform it, that’s the most efficient case.

The position, rotation, quaternion and scale properties are now available singletons that restore the state when accessing and save it when writing to them automatically. This is for easier use of less frequent transformations like a opening or closing door. But restoring the state once and making all manipulations is best practice to only update the hierarchy and matrices once.

https://codepen.io/Fyrestar/pen/BaBVWmR


The sphere shows the bounding sphere for the static mesh.