Skip to content

Commit

Permalink
feat: multimaterial
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Oct 24, 2024
1 parent 21f7f4d commit 33ac785
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/core/MultiMaterial.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Original idea by https://x.com/verekia
*/

import * as React from 'react'

export function MultiMaterial(props: JSX.IntrinsicElements['group']) {
const group = React.useRef(null!)
React.useLayoutEffect(() => {
const parent = (group.current as any)?.parent
const geometry = parent?.geometry
if (geometry) {
const oldMaterial = parent.material
parent.material = (group.current as any).__r3f.objects
const oldGroups = [...geometry.groups]
geometry.clearGroups()
parent.material.forEach((material, index) => {
if (index < parent.material.length - 1) material.depthWrite = false
geometry.addGroup(0, Infinity, index)
})
return () => {
parent.material = oldMaterial
geometry.groups = oldGroups
}
}
})
return <group ref={group} {...props} />
}
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export * from './MeshReflectorMaterial'
export * from './MeshRefractionMaterial'
export * from './MeshTransmissionMaterial'
export * from './MeshDiscardMaterial'
export * from './MultiMaterial'
export * from './PointMaterial'
export * from './shaderMaterial'
export * from './softShadows'
Expand Down

0 comments on commit 33ac785

Please sign in to comment.