-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3d4379
commit 77d334a
Showing
7 changed files
with
410 additions
and
1 deletion.
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,20 @@ | ||
--- | ||
nav: | ||
title: 模型组 | ||
path: /guide | ||
--- | ||
|
||
## 加载 模型组 | ||
|
||
Demo: | ||
|
||
```tsx | ||
import React from 'react'; | ||
import Model from 'react-3dmodelx'; | ||
|
||
export default () => ( | ||
<div style={{ maxWidth: 800, width: '100%', height: 400, margin: 'auto' }}> | ||
<Model.Group list={['./chair1.gltf', './chair.gltf']} /> | ||
</div> | ||
); | ||
``` |
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 @@ | ||
|
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,54 @@ | ||
import React, { useRef, useEffect, useImperativeHandle, forwardRef } from 'react'; | ||
import * as THREE from 'three'; | ||
import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader'; | ||
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; | ||
import useScene from '../useScene'; | ||
|
||
function Group( | ||
{ list, backgroundColor }: { list: string[]; backgroundColor: string }, | ||
ref?: React.Ref<unknown>, | ||
) { | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
|
||
const { add2Scene, scene, animate, renderer, render } = useScene(canvasRef, backgroundColor); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
getSnapshot: () => { | ||
render(); | ||
return renderer.current?.domElement.toDataURL('image/png', 1); | ||
}, | ||
})); | ||
|
||
useEffect(() => { | ||
const ambientLight = new THREE.AmbientLight(0xcccccc, 0.4); | ||
scene.current?.add(ambientLight); | ||
|
||
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); | ||
directionalLight.position.set(1, 1, 0).normalize(); | ||
|
||
scene.current?.add(directionalLight); | ||
|
||
list.forEach((url) => { | ||
const lowerUrl = url.toLowerCase(); | ||
|
||
if (lowerUrl.endsWith('fbx')) { | ||
const loader = new FBXLoader(); | ||
loader.load(url, function (data: any) { | ||
add2Scene(data); | ||
animate(); | ||
}); | ||
} | ||
if (lowerUrl.endsWith('gltf') || lowerUrl.endsWith('glb')) { | ||
const loader = new GLTFLoader(); | ||
loader.load(url, function (gltf: any) { | ||
add2Scene(gltf.scene); | ||
render(); | ||
}); | ||
} | ||
}); | ||
}, []); | ||
|
||
return <canvas ref={canvasRef} style={{ width: '100%', height: '100%' }} />; | ||
} | ||
|
||
export default forwardRef(Group); |
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