UNPKG

glodrei

Version:

useful add-ons for react-three-fiber

60 lines (48 loc) 1.94 kB
--- title: Clone sourcecode: src/core/Clone.tsx --- <Grid cols={4}> <li> <Codesandbox id="42glz0" /> </li> </Grid> Declarative abstraction around THREE.Object3D.clone. This is useful when you want to create a shallow copy of an existing fragment (and Object3D, Groups, etc) into your scene, for instance a group from a loaded GLTF. This clone is now re-usable, but it will still refer to the original geometries and materials. ```ts <Clone /** Any pre-existing THREE.Object3D (groups, meshes, ...), or an array of objects */ object: THREE.Object3D | THREE.Object3D[] /** Children will be placed within the object, or within the group that holds arrayed objects */ children?: React.ReactNode /** Can clone materials and/or geometries deeply (default: false) */ deep?: boolean | 'materialsOnly' | 'geometriesOnly' /** The property keys it will shallow-clone (material, geometry, visible, ...) */ keys?: string[] /** Can either spread over props or fill in JSX children, applies to every mesh within */ inject?: MeshProps | React.ReactNode | ((object: THREE.Object3D) => React.ReactNode) /** Short access castShadow, applied to every mesh within */ castShadow?: boolean /** Short access receiveShadow, applied to every mesh within */ receiveShadow?: boolean /> ``` You create a shallow clone by passing a pre-existing object to the `object` prop. ```jsx const { nodes } = useGLTF(url) return ( <Clone object={nodes.table} /> ``` Or, multiple objects: ```jsx <Clone object={[nodes.foo, nodes.bar]} /> ``` You can dynamically insert objects, these will apply to anything that isn't a group or a plain object3d (meshes, lines, etc): ```jsx <Clone object={nodes.table} inject={<meshStandardMaterial color="green" />} /> ``` Or make inserts conditional: ```jsx <Clone object={nodes.table} inject={ {(object) => (object.name === 'table' ? <meshStandardMaterial color="green" /> : null)} } /> ```