Where should imperative creation like `new Mesh()` be done?
See original GitHub issueConsider this component:
const Thing = () => {
const skinnedMesh = new THREE.SkinnedMesh(geometry, material);
return <primitive object={skinnedMesh} />;
};
Every time this component renders, a new SkinnedMesh will be created (and I’m not sure if it will be cleaned up?).
What’s the “right way” to minimize extra mesh creation here?
Should it be in in an effect?
const Thing = () => {
const ref = useRef();
useEffect(() => {
const skinnedMesh = new THREE.SkinnedMesh(geometry, material);
ref.current.add(skinnedMesh);
}, []);
return <primitive ref={ref} />;
};
I think this will cause one frame of a blank primitive?
So should it be in in a useLayoutEffect() to insert before the scene renders? (Will that even work with r3f?)
const Thing = () => {
const ref = useRef();
useLayoutEffect(() => {
const skinnedMesh = new THREE.SkinnedMesh(geometry, material);
ref.current.add(skinnedMesh);
}, []);
return <primitive ref={ref} />;
};
Or should the Mesh itself be memoized? And any side effects done in the useMemo callback too, like adding more children to the mesh?
const Thing = () => {
const skinnedMesh = useMemo(() => new THREE.SkinnedMesh(geometry, material), []);
return <primitive object={skinnedMesh} />;
};
Or something else?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Creating a Mesh - Catlike Coding
This is done by creating a new Mesh object. Also name it Procedural Mesh by settings its name property.
Read more >Configure 3D models with react-three-fiber - LogRocket Blog
You can either drop the newly created file into your project's src folder as is, or you can copy and paste the code...
Read more >What is Network Topology? Best Guide to Types & Diagrams
Mesh topologies are incredibly labor-intensive. Each interconnection between nodes requires a cable and configuration once deployed, so it can ...
Read more >Quickstart Blender Scripting Tutorial: The Plane and the Ball.
The mesh of the sphere object does not get updated. Everyone knows that catastrophe is imminent, we near the precipice. Let's fix this!...
Read more >The Ecosystem of Shared Value - Harvard Business Review
In the process, companies will find economic opportunities that their competitors ... as business opportunities, they suggested, is the most important new ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

yes, memoize it or put it into state. the render function shouldn’t have any side effects. useEffect happens after the component renders, so that wouldn’t be the best solution to your problem like you said.
if it never changes i would do this:
if it can change i would do
Ok, right now I’m trying this, for anyone spelunking and finds this thread. This is an (incomplete) example to implement shader-particle-engine in r3f which requires instantiating two classes at render time and adding one to the other, and disposing on unmount