Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Created August 1, 2022 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save whoisryosuke/7f9df402cc6b2ee6584d25939a3925a5 to your computer and use it in GitHub Desktop.
Save whoisryosuke/7f9df402cc6b2ee6584d25939a3925a5 to your computer and use it in GitHub Desktop.
R3F / ThreeJS - Multiple materials on each cube face. ThreeJS does not support stacking materials -- see instanced geometry + groups.
import * as THREE from "three";
import { MeshProps, useFrame } from "@react-three/fiber";
import { Mesh } from "three";
import { useRef } from "react";
type MultiMaterialMeshProps = MeshProps & {};
export default function MultiMaterialMesh({}: MultiMaterialMeshProps) {
const geom = useRef<Mesh>();
const materials = ["red", "green", "blue", "cyan", "magenta", "yellow"].map(
(color, index) => (
// Should work in ThreeJS-land - doesn't work for R3F
// <meshBasicMaterial key={color} attachArray="material" color={color} />
<meshBasicMaterial
key={index}
attach={`material-${index}`}
color={color}
/>
)
);
return (
<>
<mesh ref={geom} position={[0, 0, 0]}>
<boxBufferGeometry args={[1, 1, 1]} />
{materials}
</mesh>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment