Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPURenderer: Instance Sprite Example #29503

Merged
merged 5 commits into from
Sep 28, 2024

Conversation

RenaudRohlinger
Copy link
Collaborator

@RenaudRohlinger RenaudRohlinger commented Sep 26, 2024

Related comment: #29491 (comment)

Description

Third attempt to provide a fallback for THREE.Points with sizeAttenuation support. This PR demonstrates how to combine Sprite Meshes with instanceBufferAttribute. /cc @WestLangley

image

This contribution is funded by Segments.AI & Utsubo

Copy link

github-actions bot commented Sep 26, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 687.15
170.18
687.15
170.18
+0 B
+0 B
WebGPU 805.7
216.91
805.73
216.91
+26 B
+10 B
WebGPU Nodes 805.21
216.77
805.24
216.78
+26 B
+11 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 462.85
111.79
462.85
111.79
+0 B
+0 B
WebGPU 535.49
144.49
535.52
144.5
+26 B
+9 B
WebGPU Nodes 491.67
134.26
491.67
134.26
+0 B
+0 B

@RenaudRohlinger RenaudRohlinger added this to the r169 milestone Sep 26, 2024
@mrdoob mrdoob modified the milestones: r169, r170 Sep 26, 2024
@WestLangley
Copy link
Collaborator

This is a thing of beauty. ❤️

I have hacked your example to test some scaling issues...

A Plane and a Sprite of unit size render the same size by design. So should a single instanced Sprite.

Currently the instanced Sprite is too big by a factor of two. This is because the geometry is twice as big. I temporarily fix it by scaling the uniform, but you will have to scale the geometry instead.

Sprites

@RenaudRohlinger
Copy link
Collaborator Author

Thanks! 😊 I’ve scaled the geometry accordingly, so it should be now fixed.

@WestLangley
Copy link
Collaborator

Do you think InstancedSprites should have a .center property, like Sprite does.

Granted, you can add the property and it works.

const sprites = new InstancedSprites( geometry, material );
sprites.center = new THREE.Vector2( 0.5, 0 );

@sunag
Copy link
Collaborator

sunag commented Sep 27, 2024

We can already make instanced sprites, this would be an additional redundancy in the library, more code, documentation and maintenance, object.count should be intended to instantiate any object

const material = new THREE.SpriteNodeMaterial( { sizeAttenuation: true, map: sprite, alphaMap: sprite, alphaTest: 0.1, transparent: true } );
material.positionNode = instancedBufferAttribute( new THREE.InstancedBufferAttribute( new Float32Array( positions ), 3 ) );

const particles = new THREE.Sprite( material );
particles.count = 10000;
scene.add( particles );

@sunag
Copy link
Collaborator

sunag commented Sep 27, 2024

Could we use the object.count approach instead of creating new objects and materials? This example would be very useful.

@RenaudRohlinger
Copy link
Collaborator Author

Ahh, we all forgot about this obvious one. That's why you're the boss 😄.

Updated the PR consequently.

@RenaudRohlinger RenaudRohlinger changed the title WebGPURenderer: Introduce InstancedSprites WebGPURenderer: Instance Sprite Example Sep 28, 2024
@sunag
Copy link
Collaborator

sunag commented Sep 28, 2024

The nodes allow so many combinations. It also took me a while to process this possibility, luckily the implementation has evolved this far 🙏.

@sunag sunag merged commit 64072a1 into mrdoob:dev Sep 28, 2024
12 checks passed
@WestLangley
Copy link
Collaborator

The original purpose of this PR was to add InstanceSprite -- just as we have InstanceMesh. Both are convenience classes.

But InstanceSprite was removed from this PR, and the PR was merged.

What is the plan for:

  1. computing the correct bounding volumes necessary for frustum culling,

  2. handling raycasting,

  3. updating the position of one of the instances.

@RenaudRohlinger
Copy link
Collaborator Author

My initial idea was just rendering multiples sprites but I understand these elements are importants.

I can still cherry pick my previous commit if we want to add a new Class in the future with all these functions included just like InstancedMesh.

@WestLangley
Copy link
Collaborator

@RenaudRohlinger I do think your original PR was excellent.

@sunag
Copy link
Collaborator

sunag commented Sep 28, 2024

@WestLangley These possibilities also came to my mind, but I think we should upgrade these apporachs based on GPU-Compute and abandon approchs using CPU. We could handle some internal rules using object.count > 1 as well, working with GPU on this should give us more performance and compatibility with procedural stuff, and the unification will reduce maintenance, documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants