Skip to content

Commit

Permalink
feat: outline
Browse files Browse the repository at this point in the history
  • Loading branch information
felixerdy committed Nov 11, 2024
1 parent 8cd4678 commit d00b2db
Show file tree
Hide file tree
Showing 12 changed files with 54,735 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-github-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
cname: 3d.sensebox.de
cname: 3d.sensebox.de
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ npm run dev

## License

This project is licensed under the [MIT License](./LICENSE.md).
This project is licensed under the [MIT License](./LICENSE.md).
24 changes: 12 additions & 12 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
import js from "@eslint/js";
import globals from "globals";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";
import tseslint from "typescript-eslint";

export default tseslint.config(
{ ignores: ['dist'] },
{ ignores: ["dist"] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
files: ["**/*.{ts,tsx}"],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
"react-hooks": reactHooks,
"react-refresh": reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
},
},
)
);
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"dependencies": {
"@react-three/drei": "^9.114.6",
"@react-three/fiber": "^8.17.10",
"@react-three/postprocessing": "^2.16.3",
"lamina": "^1.1.23",
"postprocessing": "^6.36.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.28.0",
Expand Down
54,572 changes: 54,571 additions & 1 deletion public/gltf/bike/senseBox_bike.gltf

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/scenes/bike/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Suspense, useRef } from "react";
import { Canvas } from "@react-three/fiber";
import { OrbitControls, Stage } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
import { Suspense, useRef } from "react";
import { Model } from "./model";

export default function BikeScene() {
Expand Down
7 changes: 1 addition & 6 deletions src/scenes/bike/model/hooks/useToggleAnimation.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { useEffect, useState } from "react";
import { useState } from "react";
import * as THREE from "three";

export function useToggleAnimation(action: THREE.AnimationAction | null) {
const [isReversed, setIsReversed] = useState(false);

useEffect(() => {
console.log("useToggleAnimation", action);
}, [action]);

const toggleAnimation = () => {
if (action) {
// Set up the animation to play once and stop at the last frame
Expand All @@ -27,7 +23,6 @@ export function useToggleAnimation(action: THREE.AnimationAction | null) {
action.play();
setIsReversed(!isReversed);
}
console.log("toggleAnimation", action);
};

return toggleAnimation;
Expand Down
86 changes: 56 additions & 30 deletions src/scenes/bike/model/index.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,57 @@
// path: src/scenes/bike/model/index.tsx
import { useAnimations, useGLTF } from "@react-three/drei";
import { useEffect, useRef, useState } from "react";
import * as THREE from "three";
import { useRef } from "react";
import { useGLTF, useAnimations } from "@react-three/drei";
import { GLTF } from "three-stdlib";
import { Nodes } from "./types/nodes";
import { Materials } from "./types/materials";
import { Lid } from "./lid";
import { Mcu } from "./mcu";
import { Hdc1080 } from "./hdc1080";
import { Battery } from "./battery";
import { Ble } from "./ble";
import { Enclosure } from "./enclosure";
import { Fan } from "./fan";
import { Battery } from "./battery";
import { FanEnclosure } from "./fanEnclosure";
import { Hdc1080 } from "./hdc1080";
import { useToggleAnimation } from "./hooks/useToggleAnimation";
import { LevelBooster } from "./levelBooster";
import { Lid } from "./lid";
import { LipoMeshBoard } from "./lipoMeshBoard";
import { Mcu } from "./mcu";
import { ToF } from "./tof";
import { FanEnclosure } from "./fanEnclosure";
import { Enclosure } from "./enclosure";
import { useToggleAnimation } from "./hooks/useToggleAnimation";
import { Materials } from "./types/materials";
import { Nodes } from "./types/nodes";
import { OutlineEffect } from "./utils/outline";

type ActionName = "open lid" | "explode tof" | "explode hdc";
interface GLTFAction extends THREE.AnimationClip {
name: ActionName;
}

type GLTFResult = GLTF & {
nodes: Nodes;
materials: Materials;
animations: GLTFAction[];
};

export function Model(props: JSX.IntrinsicElements["group"]) {
const group = useRef<THREE.Group>(null);
const { nodes, materials, animations } = useGLTF(
"/gltf/bike/senseBox_bike.gltf",
) as GLTFResult;

const { actions } = useAnimations(animations, group);
const [myActions, setMyActions] = useState<typeof actions>();

// Verwende den Hook für die "open lid"-Animation
const toggleLidAnimation = useToggleAnimation(actions["open lid"]);
const toggleTofAnimation = useToggleAnimation(actions["explode tof"]);
const toggleHdcAnimation = useToggleAnimation(actions["explode hdc"]);
useEffect(() => {
setMyActions(actions);
}, [actions]);

const toggleLidAnimation = useToggleAnimation(
myActions?.["open lid"] ?? null,
);
const toggleTofAnimation = useToggleAnimation(
myActions?.["explode tof"] ?? null,
);
const toggleHdcAnimation = useToggleAnimation(
myActions?.["explode hdc"] ?? null,
);

return (
<group ref={group} {...props} dispose={null}>
Expand All @@ -45,28 +63,36 @@ export function Model(props: JSX.IntrinsicElements["group"]) {
userData={{ name: "BikeBox+Mini+S2 (1)" }}
>
<Mcu nodes={nodes} materials={materials} />
<group onClick={() => toggleHdcAnimation()}>
<Hdc1080 nodes={nodes} materials={materials} />
<Fan nodes={nodes} materials={materials} />
<FanEnclosure nodes={nodes} materials={materials} />
</group>
<OutlineEffect enabled={true} auraColor={0x00ff00}>
<group onClick={() => toggleHdcAnimation()}>
<Hdc1080 nodes={nodes} materials={materials} />
<Fan nodes={nodes} materials={materials} />
<FanEnclosure nodes={nodes} materials={materials} />
</group>
</OutlineEffect>

<Ble nodes={nodes} materials={materials} />

<Battery nodes={nodes} materials={materials} />
<Lid
nodes={nodes}
materials={materials}
onClick={() => toggleLidAnimation()}
/>

<LevelBooster nodes={nodes} materials={materials} />
<LipoMeshBoard nodes={nodes} materials={materials} />
<ToF
nodes={nodes}
materials={materials}
onClick={() => toggleTofAnimation()}
/>
<OutlineEffect enabled={true} auraColor={0xff0000}>
<ToF
nodes={nodes}
materials={materials}
onClick={() => toggleTofAnimation()}
/>
</OutlineEffect>
<Enclosure nodes={nodes} materials={materials} />

<OutlineEffect enabled={true} auraColor={0x0000ff}>
<Lid
nodes={nodes}
materials={materials}
onClick={() => toggleLidAnimation()}
/>
</OutlineEffect>
</group>
</group>
</group>
Expand Down
39 changes: 39 additions & 0 deletions src/scenes/bike/model/utils/outline.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
EffectComposer,
Outline,
Select,
Selection,
} from "@react-three/postprocessing";
import { KernelSize, BlendFunction, Resolution } from "postprocessing";

export const OutlineEffect = ({
children,
enabled,
auraColor,
}: {
children: JSX.Element;
enabled: boolean;
auraColor: number;
}) => {
return (
<Selection enabled>
<EffectComposer autoClear={false}>
<Outline
selectionLayer={10} // selection layer
edgeStrength={100} // the edge strength
pulseSpeed={0.5} // a pulse speed. A value of zero disables the pulse effect
visibleEdgeColor={auraColor} // the color of visible edges
hiddenEdgeColor={0x00ff00} // the color of hidden edges
blendFunction={BlendFunction.SCREEN} // set this to BlendFunction.ALPHA for dark outlines
width={Resolution.AUTO_SIZE} // render width
height={Resolution.AUTO_SIZE} // render height
kernelSize={KernelSize.LARGE} // blur kernel size
blur={true} // whether the outline should be blurred
xRay={true} // indicates whether X-Ray outlines are enabled
/>
</EffectComposer>

<Select enabled={enabled}>{children}</Select>
</Selection>
);
};
8 changes: 4 additions & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
base: '/',
})
base: "/",
});

0 comments on commit d00b2db

Please sign in to comment.