Set mesh of MeshFilter?

I’m new to Three.js and looking for the equivelant of setting the sharedMesh of MeshFilter in script.

In Unity C# it would be

var meshFilter = GetComponent();
meshFilter.sharedMesh = myMesh;

Looks like there is no MeshFilter class provided by Needle.

Original Post on Discord

by user 427134081860501507

Hey :wave: in Three.js the sudden change is that the mesh (filter) and mesh rendering aren’t components and instead those are merged into a special game object that is called a Mesh.

This means, that you should be able to do this:

import { Behaviour, serializable } from "@needle-tools/engine";
import { Object3D, Mesh } from "three";

export class Example extends Behaviour {

    @serializable(Object3D)
    target?: Object3D;

    @serializable(Mesh)
    myMesh?: Mesh;

    awake(): void {
        if (this.target instanceof Mesh && this.myMesh) {
            this.target.geometry = this.myMesh.geometry;
        }
    }
}

Or if the object that the script is on is the one with the mesh you can replace target with this.gameobject

Here’s the relevant three.js page. As you can see the Mesh class derives from a Object3D (which is equivalent to a GameObject in Unity - so calling this.gameObject gives you the three.js Object3D object which can be of type Group, Mesh or SkinnedMesh) three.js docs

We have an implementation for the Renderer component that you have in Unity that you can use to e.g. easily access Materials but we don’t have it for the MeshFilter component that Unity has

Ah, I see. Thanks!

by user 427134081860501507