Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 16 additions & 32 deletions blocks/file-blocks/3d-files.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
import { Suspense, useRef, useState, useEffect } from "react";
import { tw } from "twind";
import { PerspectiveCamera, useGLTF } from "@react-three/drei";
import { Canvas, useLoader, useStore } from "@react-three/fiber";
import { Suspense, useLayoutEffect } from "react";
import { Stage, useGLTF } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
import { OrbitControls } from "@react-three/drei";
import { FileBlockProps } from "@githubnext/blocks";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";

const LControl = () => {
// @ts-ignore
const dom = useStore((state) => state.dom);
const control = useRef(null);

// @ts-ignore
return <OrbitControls ref={control} domElement={dom.current} />;
};

function Model({ url }: { url: string }) {
// need to load gltf model this way instead of with hook
// or you get an infinite loop on the production sandbox side
const { scene } = useLoader(GLTFLoader, url);
const { scene } = useGLTF(url);
useLayoutEffect(() => {
scene.traverse((object) => {
if (object.type === "Mesh") {
object.receiveShadow = object.castShadow = true;
}
});
}, []);
return <primitive object={scene} dispose={null} />;
}

export default function (props: FileBlockProps) {
const { context } = props;

const url = `https://raw.githubusercontent.com/${context.owner}/${context.repo}/${context.sha}/${context.path}`;

return (
<Canvas style={{ height: "100vh" }}>
{/*
// @ts-ignore */}
<PerspectiveCamera
makeDefault
position={[0, 0, 0.3]}
near={0.01}
far={1000}
/>
<ambientLight />
<LControl />
<pointLight position={[10, 10, 10]} />
<Canvas shadows style={{ height: "100vh" }}>
<Suspense fallback={null}>
<Model url={url} />
<Stage shadows="accumulative">
<Model url={url} />
</Stage>
</Suspense>
<OrbitControls makeDefault />
</Canvas>
);
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@primer/octicons-react": "^16.0.0",
"@primer/primitives": "^7.8.3",
"@primer/react": "^35.2.0",
"@react-three/drei": "^7.20.5",
"@react-three/fiber": "^7.0.19",
"@react-three/drei": "^9.43.3",
"@react-three/fiber": "^8.9.1",
"@replit/codemirror-indentation-markers": "^6.0.0",
"@replit/codemirror-interact": "^6.0.1",
"@replit/codemirror-vim": "^6.0.2",
Expand Down Expand Up @@ -69,7 +69,7 @@
"recharts": "^2.1.6",
"rlayers": "^1.3.2",
"styled-components": "^5.3.5",
"three": "^0.134.0",
"three": "^0.146.0",
"twind": "^0.16.17",
"unidiff": "^1.0.2",
"use-debounce": "^8.0.3"
Expand All @@ -83,7 +83,7 @@
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-syntax-highlighter": "^13.5.2",
"@types/three": "^0.134.0",
"@types/three": "^0.146.0",
"husky": "^7.0.4",
"lint-staged": "^12.3.5",
"prettier": "2.5.1",
Expand Down
Loading