question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Where should imperative creation like `new Mesh()` be done?

See original GitHub issue

Consider 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drcmdacommented, Jun 4, 2019

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:

const [mesh] = useState(() => new THREE.SkinnedMesh(geometry, material))

if it can change i would do

const mesh = useMemo(() => new THREE.SkinnedMesh(geometry, material), [geometry])
0reactions
AndrewRayCodecommented, Jul 13, 2019

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

const ParticleEmitter = () => {
  const speEmitter = useMemo(
    () => new SPE.Emitter({ options }),
    [] // eslint-disable-line react-hooks/exhaustive-deps
  ); 

  const speGroup = useMemo(() => {
    const group = new SPE.Group({ ...options });
    group.addEmitter(speEmitter);
    return group;
  }, []); // eslint-disable-line react-hooks/exhaustive-deps

  useEffect(() => () => speGroup.dispose());

  return <primitive object={speGroup.mesh} />;
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found