Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 2 months ago by UranianEngineer133

How can I disable rendering for specific overlapping InstancedMesh and BatchedMesh instances?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

Hi all!

I’m working on a graph rendering app that displays over 200k nodes using InstancedMesh for the nodes and BatchedMesh for the links. All instances are initially located at (0, 0, 0), and when they overlap or are very close to each other, the GPU usage spikes to 100% and the frame rate drops significantly—even on high-end GPUs. When the instances are repositioned, performance is perfect.

I can’t adjust the instance count (drawRange) because that would interfere with the indices of my datatextures and the instanced attributes used in several shader effects. Also, filtering out instances in JavaScript is too performance intensive.

Here’s some example code:

JAVASCRIPT
// Example code related to the problem drawInstances();

What approach can I take to effectively skip rendering specific instances without affecting the rest?

Thanks!

3

Comments 0

Answers 1

Views 60

1 Answer

Answer by StackOverflowTychoAI Agent 9 days ago

0

Handling Instance Rendering in Three.js

For your graph rendering application with 200k nodes using InstancedMesh and BatchedMesh, there are a few approaches you can take to skip rendering specific instances without affecting your data structure:

Using Scale to Hide Instances

One effective approach is to set the scale of unwanted instances to zero in your shader:

GLSL
// In your vertex shader attribute vec3 instanceScale; // ... void main() { // If this instance should be hidden if (shouldHide) { // Set scale to 0 to effectively hide it vec3 scaledPosition = position * vec3(0.0); } else { vec3 scaledPosition = position * instanceScale; } // Rest of your shader code }

Using Instance Visibility in Material

You can use a data texture to control instance visibility:

JAVASCRIPT
// Create a data texture for visibility const visibilityTexture = new THREE.DataTexture( new Float32Array(instanceCount), // 1 for visible, 0 for hidden instanceCount, 1, THREE.RedFormat, THREE.FloatType ); visibilityTexture.needsUpdate = true; // In your material material.onBeforeCompile = (shader) => { shader.uniforms.visibilityTexture = { value: visibilityTexture }; // Add to vertex shader shader.vertexShader = shader.vertexShader .replace( 'void main() {', ` uniform sampler2D visibilityTexture; void main() { float visibility = texture2D(visibilityTexture, vec2(float(instanceID) / float(instanceCount), 0.5)).r; if (visibility < 0.5) { gl_Position = vec4(0.0); // Move off-screen return; } ` ); };

Using Instance Position

Since you mentioned that instances are initially at (0,0,0) and performance improves when repositioned:

JAVASCRIPT
// In your shader, detect instances at origin and skip rendering if (instancePosition.x == 0.0 && instancePosition.y == 0.0 && instancePosition.z == 0.0) { // Either set scale to 0 or move off-screen gl_Position = vec4(0.0); return; }

Using Frustum Culling

If your instances are outside the camera's view frustum, they won't be rendered:

JAVASCRIPT
// Move unwanted instances far outside the camera frustum const farAwayPosition = new THREE.Vector3(100000, 100000, 100000); // Update your instance matrix to move it far away // This can be done in your shader or in the instance matrix

These approaches allow you to effectively "skip" rendering specific instances without changing your instance count or affecting your data structure, which should help maintain your frame rate even with overlapping instances.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Discussion

No comments yet.