Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize grid cell and section size based on scene bounding box size #4410

Merged
merged 4 commits into from
May 22, 2024
Merged
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
45 changes: 35 additions & 10 deletions app/packages/looker-3d/src/fo3d/Gizmos.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GizmoHelper, GizmoViewport, Grid, Line } from "@react-three/drei";
import { useMemo } from "react";
import { useRecoilValue } from "recoil";
import { useEffect, useMemo } from "react";
import { useRecoilState, useRecoilValue } from "recoil";
import { DoubleSide, Vector3 } from "three";
import {
gridCellSizeAtom,
Expand All @@ -18,7 +18,7 @@ const AXIS_GREEN_COLOR = "#21DF80";
const AXIS_BLUE_COLOR = "#2280FF";

const GRID_CELL_COLOR = "#6f6f6f";
const GRID_SECTION_COLOR = "#a79d9d";
const GRID_SECTION_COLOR = "#736f73";

const FoAxesHelper = ({
maxInOrthonormalPlane,
Expand Down Expand Up @@ -89,12 +89,18 @@ export const Gizmos = () => {
[upVector]
);

const maxInOrthonormalPlane = useMemo(() => {
const sceneSize = useMemo(() => {
if (!sceneBoundingBox) {
return 0;
}

const sceneSize = sceneBoundingBox.getSize(new Vector3());
return sceneBoundingBox.getSize(new Vector3());
}, [sceneBoundingBox]);

const maxInOrthonormalPlane = useMemo(() => {
if (!sceneSize) {
return 0;
}

if (upVector.x === 1) {
return Math.max(sceneSize.y, sceneSize.z);
Expand All @@ -105,19 +111,38 @@ export const Gizmos = () => {
}

return Math.max(sceneSize.x, sceneSize.y);
}, [sceneBoundingBox, upVector]);
}, [sceneSize, upVector]);

const cellSize = useRecoilValue(gridCellSizeAtom);
const sectionSize = useRecoilValue(gridSectionSizeAtom);
const [cellSize, setCellSize] = useRecoilState(gridCellSizeAtom);
const [sectionSize, setSectionSize] = useRecoilState(gridSectionSizeAtom);
const isGridInfinitelyLarge = useRecoilValue(isGridInfinitelyLargeAtom);
const shouldFade = useRecoilValue(shouldGridFadeAtom);
const gridSize = useRecoilValue(gridSizeAtom);

// This effect dynamically sets initial cell and section size based on the scene size
useEffect(() => {
if (!sceneSize) {
return;
}

const maxDim = Math.max(sceneSize.x, sceneSize.y, sceneSize.z);

const nominalCellSize = maxDim / 10;

// round to the nearest 10, but for small values, just use 1
const roundedCellSize =
nominalCellSize <= 1 ? 1 : Math.ceil(nominalCellSize / 10) * 10;
const roundedSectionSize = roundedCellSize * 10;

setCellSize(roundedCellSize);
setSectionSize(roundedSectionSize);
}, [sceneSize]);

// The fade distance is the distance at which the grid will start to fade out
// the multipliers and offset are arbitrary
const fadeDistance = useMemo(() => {
return maxInOrthonormalPlane * 10 + 100;
}, [maxInOrthonormalPlane, gridSize, isGridInfinitelyLarge]);
}, [maxInOrthonormalPlane]);

return (
<>
Expand All @@ -137,7 +162,7 @@ export const Gizmos = () => {
followCamera={shouldFade}
fadeFrom={0.5}
cellThickness={0.5}
sectionThickness={0.8}
sectionThickness={0.4}
/>
<FoAxesHelper maxInOrthonormalPlane={maxInOrthonormalPlane} />
</>
Expand Down
2 changes: 0 additions & 2 deletions app/packages/looker-3d/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ export const isGridOnAtom = atom<boolean>({
export const gridCellSizeAtom = atom<number>({
key: "fo3d-gridCellSize",
default: 1,
effects: [getBrowserStorageEffectForKey("fo3d-gridCellSize")],
});

export const gridSectionSizeAtom = atom<number>({
key: "fo3d-gridSectionSize",
default: 10,
effects: [getBrowserStorageEffectForKey("fo3d-gridSectionSize")],
});

export const isGridInfinitelyLargeAtom = atom<boolean>({
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading