Skip to content

How to add material to OBJ-loaded primitive? #962

Answered by TeosVerdi
TeosVerdi asked this question in Q&A
Discussion options

You must be logged in to vote

Aaand as soon as i posted this, i've found a solution. Not the best one, but it works.

Now this is a mesh creator

const Figures = () => {
  const mat = new THREE.MeshNormalMaterial()
  return (
    <mesh position={[0, 0, 0]}>
      {ObjToPrimitive({ url: "/models/bishop.obj", mat })}
    </mesh>
  );
};

and this is a ObjToPrimitive function:

import React, { useMemo, useState } from "react";
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";

import { Mesh } from "three";

export default function ObjToPrimitive({ url, mat }) {
  const [obj, setObj] = useState();
  useMemo(() => new OBJLoader().load(url, setObj), [url]);
  if (obj) {
    obj.traverse((child) => {
      if (child

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by TeosVerdi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant