Skip to content

Conversation

@simon-paris
Copy link

Example: https://raw.githack.com/simon-paris/three.js/occlusion-queries-with-build/examples/webgl_occlusionqueries.html

Documentation: https://raw.githack.com/simon-paris/three.js/occlusion-queries-with-build/docs/api/en/objects/OcclusionQueryMesh.html

This change integrates WebGL2 queries into the renderer, allowing occlusion querying like this:

var queryMesh = new THREE.OcclusionQueryMesh(
	new THREE.BoxBufferGeometry( 1, 1, 1 ),
	new THREE.MeshBasicMaterial( { depthWrite: false, colorWrite: false } )
);
queryMesh.frustumCulled = false;
queryMesh.renderOrder = Infinity;
var highestFrame = 0;
queryMesh.occlusionQueryCallback = function ( result, frame, camera ) {

	if ( camera === myMainCamera && frame > highestFrame ) {
		highestFrame = frame;
		doStuff( result ); // queryMesh was visible if result === true
	}

};
scene.add( queryMesh );

Hope you like it

* @author simon paris / https://github.com/simon-paris
*/

function OcclusionQueryMesh() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you introduce a new object type, it's necessary to adjust ObjectLoader and Object3D.toJSON() so it's possible to serialize/deserialize a scene with objects of type OcclusionQueryMesh.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since OcclusionQueryMesh has a callback function property, and also needs to be disposed properly, would it be acceptable to leave serialization unsupported and just print a warning?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, I see what you mean. Let me think about this...

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 20, 2018

Have you considered to choose a different API style similar to BabylonJS? Objects have a property isOccluded which indicates whether the mesh is occluded or not. If you assign a specific value to occlusiontype, an occlusion query is configured for the respective object. You can now evaluate isOccluded e.g. in your render loop and react accordingly. Check out this approach right here:

https://www.babylonjs-playground.com/#QDAZ80#5

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 20, 2018

I personally tend to prefer such an API since it feels easier to use. I'm curious how others think about this 😇

@simon-paris
Copy link
Author

simon-paris commented Dec 21, 2018

Can I instead output the result by pushing it to an array?

If I output the result as a single boolean, you wouldn't get a result for each frame. A guarantee to get one output for each frame rendered would be useful for some people. A single boolean also wouldn't work with multi-camera setups.

For the serialization problem, I could always deserialize it into a disabled state? Then programs that aren't aware of the occlusion query don't have to worry about leaks or disposal.

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 21, 2018

A guarantee to get one output for each frame rendered would be useful for some people. A single boolean also wouldn't work with multi-camera setups.

I like to keep things simple. I would prefer an API that is easy to use and covers 90% of use cases. Better than a more complex one which also regards edge cases. A single boolean value for 3d objects should be sufficient for most scenarios even for VR applications.

But like I said before, this is just my opinion. Let's see how others think about the API style.

@Ben-Mack
Copy link

Any update on this? This would be a great feature for Three.js.

@mrdoob
Copy link
Owner

mrdoob commented Jan 30, 2019

Do you mind resolving the rollup.config.js conflicts?

@mrdoob mrdoob added this to the r102 milestone Jan 30, 2019
@mrdoob mrdoob modified the milestones: r102, r103 Feb 28, 2019
@simon-paris
Copy link
Author

Sorry! I've been busy at work.

I've resolved the conflict.

@mrdoob mrdoob modified the milestones: r103, r104 Mar 27, 2019
@mrdoob mrdoob modified the milestones: r104, r105 Apr 24, 2019
@mrdoob mrdoob modified the milestones: r105, r106 May 30, 2019
@mrdoob mrdoob modified the milestones: r106, r107 Jun 26, 2019
@mrdoob mrdoob modified the milestones: r157, r158 Sep 28, 2023
@mrdoob mrdoob modified the milestones: r158, r159 Oct 27, 2023
@mrdoob mrdoob modified the milestones: r159, r160 Nov 30, 2023
@mrdoob mrdoob modified the milestones: r160, r161 Dec 22, 2023
@mrdoob mrdoob modified the milestones: r161, r162 Jan 31, 2024
@luisfonsivevo
Copy link
Contributor

Hi! I come from the future where #26335 has been merged. It'd be great to have occlusion queries for WebGL as well since WebGPU isn't widely supported yet. I haven't looked at how either PR is implemented yet, but I'm sure this PR could use some polishing to get it up to the same standard as the WebGPU version. We're willing to allocate some development time to this project if anyone's interested. @mrdoob @Mugen87

@LeviPesin
Copy link
Contributor

"WebGPURenderer" is a confusing name for the universal renderer that works for both WebGPU and WebGL2. You can try replacing WebGLRenderer in your project with it.

@mrdoob mrdoob modified the milestones: r162, r163 Feb 29, 2024
@mrdoob mrdoob modified the milestones: r163, r164 Mar 29, 2024
@mrdoob mrdoob modified the milestones: r164, r165 Apr 25, 2024
@mrdoob mrdoob modified the milestones: r165, r166 May 31, 2024
@mrdoob mrdoob modified the milestones: r166, r167 Jun 28, 2024
@mrdoob mrdoob modified the milestones: r167, r168 Jul 25, 2024
@mrdoob mrdoob modified the milestones: r168, r169 Aug 30, 2024
@mrdoob mrdoob modified the milestones: r169, r170 Sep 26, 2024
@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 24, 2024

Occlusion queries have been implemented in WebGPURenderer for the WebGPU and WebGL 2 backend.

https://threejs.org/examples/webgpu_occlusion

Since we want to focus on the development of WebGPURenderer with its new architecture and node material system, occlusion queries are not going to be implemented for WebGLRenderer.

@Mugen87 Mugen87 closed this Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.