Mesh.onBeforeRender (edit: no issue, ignore)

I basically have:

let mesh = new Mesh(geometry, material);
mesh.onBeforeRender = () => console.log("in onBeforeRender function");

but the log line is only printed once when the mesh first gets into the camera viewing frustrum.

I expect this to be called every time I do renderer.render(scene, camera). Or does “render” mean something else? (I guess if I want to do something before renderer.render(), I don’t need Three’s help.)

onBeforeRender is definitely called each time an object is rendered:

And onAfterRender is also called each time after the object is rendered:

Are you sure your object is still visible?

Thanks for the reply. Yes the object is still visible. The objects in my scene do not move normally but the camera does. When I change some property of the object (like .color), the onBeforeRender gets called again.

(I am now just keeping track of tasks that need to be called before rendering in my own code.)

@jancellor we won’t be able to help you further without seeing your code. Can you share a minimal live example that demonstrates your problem? e.g. using Codepen or JSfiddle.

Multiple consecutive prints of the same message may have been collapsed in the console.

1 Like

Facepalm.

So I had a text element that I wanted to hide depending on the zoom level, as is typical in a mapping application. So just set mesh.visible accordingly in the onBeforeRender function right? Except an invisible mesh doesn’t render, obviously.

Sorry to waste your time.

1 Like

So I was not completely mistaken :wink:

1 Like

How could my brain respond directly with this lie?! I think I interpreted “visible” as “within the frustrum”.

Actually, this would have been the same thing: if an object is rejected (at render time) because not in the frustum, onBeforeRender / onAfterRender won’t be called for it.