-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(environment): add new model for 3d environment props
- Loading branch information
Showing
6 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/components/Models/EnvironmentModel/EnvironmentModel.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { FC } from 'react'; | ||
import { useFallback, useGltfLoader, CustomNode, Transform } from 'src/services'; | ||
import { useGraph } from '@react-three/fiber'; | ||
import { BaseModelProps } from 'src/types'; | ||
|
||
export interface EnvironmentModelProps extends BaseModelProps { | ||
modelSrc: string | Blob; | ||
scale?: number; | ||
} | ||
|
||
export const EnvironmentModel: FC<EnvironmentModelProps> = ({ modelSrc, scale = 1, setModelFallback }) => { | ||
const { scene } = useGltfLoader(modelSrc); | ||
const { nodes } = useGraph(scene); | ||
|
||
useFallback(nodes, setModelFallback); | ||
const transform = new Transform(); | ||
|
||
return ( | ||
<group> | ||
{Object.keys(nodes).map((key) => { | ||
const node = nodes[key] as CustomNode; | ||
if (node.type === 'Mesh') { | ||
return ( | ||
<mesh | ||
receiveShadow | ||
key={node.name} | ||
scale={scale} | ||
position={transform.position} | ||
rotation={transform.rotation} | ||
geometry={node.geometry} | ||
material={node.material} | ||
morphTargetInfluences={node.morphTargetInfluences || []} | ||
/> | ||
); | ||
} | ||
|
||
return null; | ||
})} | ||
</group> | ||
); | ||
}; |
16 changes: 16 additions & 0 deletions
16
src/components/Models/EnvironmentModel/EnvironmentModel.container.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React, { FC, Suspense, useState } from 'react'; | ||
import { EnvironmentModel, EnvironmentModelProps } from './EnvironmentModel.component'; | ||
|
||
/** | ||
* Contains model to handle suspense fallback. | ||
*/ | ||
export const EnvironmentModelContainer: FC<EnvironmentModelProps> = (props) => { | ||
/* eslint-disable-next-line react/jsx-no-useless-fragment */ | ||
const [fallback, setFallback] = useState<JSX.Element>(<></>); | ||
|
||
return ( | ||
<Suspense fallback={fallback}> | ||
<EnvironmentModel setModelFallback={setFallback} {...props} /> | ||
</Suspense> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { EnvironmentModelContainer as EnvironmentModel } from './EnvironmentModel.container'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters