Advertisement
bbcqx

Untitled

Apr 7th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. To create a React component using TypeScript and Next.js that displays a 3D model in GLB format, you can use the three.js library and @react-three/fiber for better integration with React. Here's a simple example of how to create such a component:
  2.  
  3. First, install the necessary dependencies:
  4. bash
  5. Copy code
  6. npm install three @react-three/fiber @react-three/drei react-gesture-responder
  7. Create a new file named ModelViewer.tsx and add the following code:
  8. tsx
  9. Copy code
  10. import React, { Suspense } from "react";
  11. import { Canvas } from "@react-three/fiber";
  12. import { OrbitControls, PerspectiveCamera, useGLTF } from "@react-three/drei";
  13. import { GestureResponderView } from "react-gesture-responder";
  14.  
  15. type ModelViewerProps = {
  16. url: string;
  17. };
  18.  
  19. const Model: React.FC<ModelViewerProps> = ({ url }) => {
  20. const { nodes, materials } = useGLTF(url);
  21.  
  22. return (
  23. <group>
  24. <mesh geometry={nodes.Mesh_0.geometry} material={materials.Material_0} />
  25. </group>
  26. );
  27. };
  28.  
  29. const ModelViewer: React.FC<ModelViewerProps> = ({ url }) => {
  30. return (
  31. <GestureResponderView style={{ width: "100%", height: "100%" }}>
  32. <Canvas>
  33. <PerspectiveCamera makeDefault position={[0, 0, 5]} />
  34. <Suspense fallback={null}>
  35. <Model url={url} />
  36. </Suspense>
  37. <OrbitControls />
  38. </Canvas>
  39. </GestureResponderView>
  40. );
  41. };
  42.  
  43. export default ModelViewer;
  44. Now you can use the ModelViewer component in your Next.js application. Make sure to pass the URL of the GLB model to the url prop. Here's an example of how to use the component in a Next.js page:
  45. tsx
  46. Copy code
  47. // pages/index.tsx
  48. import React from "react";
  49. import ModelViewer from "../components/ModelViewer";
  50.  
  51. const IndexPage: React.FC = () => {
  52. return (
  53. <div style={{ width: "100vw", height: "100vh" }}>
  54. <ModelViewer url="/path/to/your/model.glb" />
  55. </div>
  56. );
  57. };
  58.  
  59. export default IndexPage;
  60. This will create a simple 3D model viewer that allows the user to rotate, zoom in, and zoom out using the mouse or touch gestures. The model will be loaded from the provided URL. Note that you should adjust the geometry and materials in the Model component to match your specific GLB model structure.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement