Skip to content

Commit

Permalink
remove react-three dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Feb 6, 2024
1 parent c1fd92b commit d412307
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 508 deletions.
1 change: 0 additions & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ COPY ./package*.json .
COPY ./yarn.lock .
ENV NODE_ENV='development'
RUN yarn install --production=false --frozen-lockfile
RUN yarn patch-package

COPY . .

Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"build": "tsc --project tsconfig.json && vite build",
"format": "prettier --ignore-path .prettierignore -w .",
"analyze": "npx vite-bundle-visualizer -o ./stats.html",
"build-icons": "node ./scripts/build-icons.mjs",
"postinstall": "patch-package"
"build-icons": "node ./scripts/build-icons.mjs"
},
"dependencies": {
"@cashu/cashu-ts": "^0.9.0",
Expand All @@ -32,8 +31,6 @@
"@noble/curves": "^1.3.0",
"@noble/hashes": "^1.3.2",
"@noble/secp256k1": "^1.7.0",
"@react-three/drei": "^9.92.5",
"@react-three/fiber": "^8.15.12",
"@webscopeio/react-textarea-autocomplete": "^4.9.2",
"bech32": "^2.0.0",
"blurhash": "^2.0.5",
Expand Down Expand Up @@ -74,6 +71,7 @@
"react-virtualized-auto-sizer": "^1.0.20",
"three": "^0.160.0",
"three-spritetext": "^1.8.1",
"three-stdlib": "^2.29.4",
"webln": "^0.3.2",
"yet-another-react-lightbox": "^3.15.6"
},
Expand All @@ -93,7 +91,6 @@
"@types/webscopeio__react-textarea-autocomplete": "^4.7.5",
"@vitejs/plugin-react": "^4.2.1",
"camelcase": "^8.0.0",
"patch-package": "^8.0.0",
"prettier": "^3.1.1",
"typescript": "^5.3.3",
"vite": "^5.0.10",
Expand Down
21 changes: 0 additions & 21 deletions patches/@react-three+fiber+8.15.15.patch

This file was deleted.

4 changes: 2 additions & 2 deletions src/components/embed-types/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function EmbeddedStlFile({ src }: { src: string }) {
</ButtonGroup>
</CardHeader>
{preview.isOpen && (
<CardBody px="2" pt="0" pb="2">
<CardBody px="2" pt="0" pb="2" overflow="hidden">
<Suspense
fallback={
<Text>
Expand All @@ -45,7 +45,7 @@ function EmbeddedStlFile({ src }: { src: string }) {
}
>
<ErrorBoundary>
<STLViewer aspectRatio={16 / 10} url={src} />
<STLViewer aspectRatio={16 / 10} width={1920} height={1080} w="full" h="auto" url={src} />
</ErrorBoundary>
</Suspense>
</CardBody>
Expand Down
220 changes: 156 additions & 64 deletions src/components/stl-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,74 +1,166 @@
// @ts-nocheck
/**
* @react-three/fiber extends the global JSX.IntrinsicElements with 100+ extra elements which overloads typescripts type checking.
* To fix this I pulled in the patch-package tool to comment out the offending code in @react-three/fiber
*/
import { forwardRef } from "react";
import { useEffect, useRef, useState } from "react";
import { Box, BoxProps } from "@chakra-ui/react";
import { Color, Fog, Vector3 } from "three";
import { Canvas, useLoader } from "@react-three/fiber";
import { OrbitControls } from "@react-three/drei/core/OrbitControls";
import { STLLoader } from "three/examples/jsm/loaders/STLLoader";
import {
AmbientLight,
BufferGeometry,
Color,
DirectionalLight,
Fog,
GridHelper,
HemisphereLight,
Mesh,
MeshPhongMaterial,
NormalBufferAttributes,
PerspectiveCamera,
PlaneGeometry,
Scene,
Vector3,
WebGLRenderer,
} from "three";
import { OrbitControls, STLLoader } from "three-stdlib";
import { useAsync, useMount } from "react-use";

const STLViewer = forwardRef<HTMLCanvasElement, Omit<BoxProps, "children"> & { url: string }>(
({ url, ...props }, ref) => {
const geometry = useLoader(STLLoader, url);
function createSTLWorld(canvas: HTMLCanvasElement) {
const renderer = new WebGLRenderer({
canvas,
antialias: true,
preserveDrawingBuffer: true,
alpha: true,
});
renderer.shadowMap.enabled = true;

const scene = new Scene();
scene.background = new Color(0xa0a0a0);
scene.fog = new Fog(0xa0a0a0, 4, 20);

const camera = new PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);
camera.position.set(-2, 2, -2.5);
scene.add(camera);

const hemiLight = new HemisphereLight(0xffffff, 0x444444, 3);
hemiLight.position.set(0, 20, 0);
scene.add(hemiLight);

const ambientLight = new AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);

const dirLight = new DirectionalLight(0xffffff);
dirLight.position.set(-5, 15, 10);
dirLight.castShadow = true;
scene.add(dirLight);

const floor = new Mesh(new PlaneGeometry(40, 40), new MeshPhongMaterial({ color: 0xbbbbbb, depthWrite: false }));
floor.rotation.set(-Math.PI / 2, 0, 0);
floor.receiveShadow = true;
scene.add(floor);

const grid = new GridHelper(40, 40, 0x000000, 0x000000);
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);

const object = new Mesh(
new BufferGeometry(),
new MeshPhongMaterial({ color: 0x1a5fb4, shininess: 60, flatShading: true }),
);
object.castShadow = true;
object.receiveShadow = true;
scene.add(object);

const controls = new OrbitControls(camera, renderer.domElement);
controls.update();

controls.enableDamping = true;
controls.enablePan = true;
controls.enableRotate = true;
controls.enableZoom = true;

function setSTLGeometry(geometry: BufferGeometry<NormalBufferAttributes>) {
if (!geometry.boundingBox) geometry.computeBoundingBox();
if (!geometry.boundingSphere) geometry.computeBoundingSphere();

const objectScale = 2 / geometry.boundingSphere!.radius;
const bb = geometry.boundingBox!;
const center = bb.getCenter(new Vector3()).multiplyScalar(objectScale);

return (
<Box {...props} position="relative">
<Canvas
shadows
gl={{
antialias: true,
shadowMapEnabled: true,
preserveDrawingBuffer: true,
}}
style={{ width: "100%", height: "100%" }}
scene={{ background: new Color(0xa0a0a0), fog: new Fog(0xa0a0a0, 4, 20) }}
camera={{ position: [-2, 2, -2.5] }}
ref={ref}
>
<OrbitControls enableDamping enablePan enableRotate enableZoom />
<hemisphereLight color={0xffffff} groundColor={0x444444} intensity={3} position={[0, 20, 0]} />
<ambientLight color={0xffffff} intensity={0.5} />
<directionalLight color={0xffffff} position={[-5, 15, 10]} castShadow>
<orthographicCamera attach="shadow-camera" args={[-2, 2, 2, -2]} />
</directionalLight>
<mesh
rotation={[-Math.PI / 2, 0, 0]}
receiveShadow
position={[0, ((bb.min.z - bb.max.z) / 2) * objectScale, 0]}
>
<planeGeometry args={[40, 40]} />
<meshPhongMaterial color={0xbbbbbb} depthWrite={false} />
</mesh>
<gridHelper
args={[40, 40, 0x000000, 0x000000]}
material-opacity={0.2}
material-transparent={true}
position={[0, ((bb.min.z - bb.max.z) / 2) * objectScale, 0]}
/>
<mesh
geometry={geometry}
scale={objectScale}
rotation={[Math.PI * -0.5, 0, 0]}
castShadow
receiveShadow
position={[-center.x, -center.z, center.y]}
>
<meshPhongMaterial color={0x1a5fb4} shininess={60} flatShading />
</mesh>
</Canvas>
</Box>
);
},
);

export default STLViewer;
// update object
object.geometry = geometry;
object.scale.set(objectScale, objectScale, objectScale);
object.rotation.set(Math.PI * -0.5, 0, 0);
object.position.set(-center.x, -center.z, center.y);

// update floor
grid.position.set(0, ((bb.min.z - bb.max.z) / 2) * objectScale, 0);
floor.position.set(0, ((bb.min.z - bb.max.z) / 2) * objectScale, 0);

console.log(object);
}

function resize() {
camera.aspect = canvas.width / canvas.height;
camera.updateProjectionMatrix();
}
function animate() {
controls.update();
renderer.render(scene, camera);
}

return {
renderer,
scene,
camera,
object,
grid,
floor,
dirLight,
ambientLight,
hemiLight,
controls,
animate,
resize,
setSTLGeometry,
};
}

export default function STLViewer({
url,
width,
height,
...props
}: Omit<BoxProps, "children" | "width" | "height"> & { url: string; width: number; height: number }) {
const ref = useRef<HTMLCanvasElement | null>(null);

const [world, setWorld] = useState<ReturnType<typeof createSTLWorld>>();
const { value: geometry } = useAsync(async () => {
const loader = new STLLoader();
return await loader.loadAsync(url);
}, [url]);

useMount(() => {
if (!ref.current) return;
ref.current.width = width;
ref.current.height = height;
setWorld(createSTLWorld(ref.current));
});

useEffect(() => {
if (!world) return;
let running = true;
const animate = () => {
if (running) {
world.animate();
requestAnimationFrame(animate);
}
};
animate();
return () => {
running = false;
};
}, [world]);

useEffect(() => {
if (geometry && world) world.setSTLGeometry(geometry);
}, [world, geometry]);

return <Box as="canvas" ref={ref} {...props} />;
}
2 changes: 0 additions & 2 deletions src/views/launchpad/components/feeds-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ export default function FeedsCard({ ...props }: Omit<CardProps, "children">) {
const myLists = useUserLists(account?.pubkey).filter((list) => list.kind === PEOPLE_LIST_KIND);
const { lists: favoriteLists } = useFavoriteLists(account?.pubkey);

console.log(favoriteLists);

const { recent: recentFeeds, useThing: useFeed } = useRecentIds("feeds", 4);

const sortedFeeds = [...myLists, ...favoriteLists].sort((a, b) => {
Expand Down
Loading

0 comments on commit d412307

Please sign in to comment.