This repository was archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathResetProps.tsx
99 lines (86 loc) · 3.29 KB
/
ResetProps.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { OrbitControls } from "@react-three/drei";
import { Canvas, useFrame, useThree } from "@react-three/fiber";
import React, { useEffect, useRef, useState } from "react";
import * as THREE from "three";
function AdaptivePixelRatio() {
const gl = useThree(state => state.gl);
const current = useThree(state => state.performance.current);
const initialDpr = useThree(state => state.viewport.initialDpr);
const setDpr = useThree(state => state.setDpr);
// Restore initial pixelratio on unmount
useEffect(() => {
const domElement = gl.domElement;
return () => {
setDpr(initialDpr);
domElement.style.imageRendering = "auto";
};
}, []);
// Set adaptive pixelratio
useEffect(() => {
setDpr(current * initialDpr);
gl.domElement.style.imageRendering = current === 1 ? "auto" : "pixelated";
}, [current]);
return null;
}
function AdaptiveEvents() {
const get = useThree(state => state.get);
const current = useThree(state => state.performance.current);
useEffect(() => {
const enabled = get().events.enabled;
return () => void (get().events.enabled = enabled);
}, []);
useEffect(() => void (get().events.enabled = current === 1), [current]);
return null;
}
function Scene() {
const group = useRef<THREE.Group>(null!);
const [showCube, setShowCube] = useState(false);
const [hovered, setHovered] = useState(false);
const [color, setColor] = useState("pink");
useEffect(() => {
const interval = setInterval(() => setShowCube(showCube => !showCube), 1000);
return () => clearInterval(interval);
}, []);
useFrame((
{
clock
}
) => group.current?.rotation.set(Math.sin(clock.elapsedTime), 0, 0));
return (<>
<ambientLight intensity={0.5} />
<pointLight position={[10, 10, 10]} intensity={2.2} />
<pointLight position={[-10, -10, -10]} color="rgb(137, 48, 48)" intensity={6.6} />
<mesh scale={hovered ? 1.25 : 1} onPointerOver={() => setHovered(true)} onPointerOut={() => setHovered(false)} onClick={() => setColor(color === "pink" ? "peachpuff" : "pink")}>
<sphereGeometry args={[0.5, 32, 32]} />
<meshStandardMaterial color={showCube ? "white" : "red"} />
</mesh>
<group ref={group}>
{showCube ? (<mesh position={[1.5, 0, 0]}>
<boxGeometry args={[1, 1]} />
<meshNormalMaterial transparent opacity={0.5} />
</mesh>) : (<mesh>
<icosahedronGeometry args={[1]} />
<meshStandardMaterial color="orange" transparent opacity={0.5} />
</mesh>)}
<mesh position={[-2, -2, 0]}>
<sphereGeometry args={[0.2, 32, 32]} />
<meshPhongMaterial>
{showCube ? (<color attach="color" args={[0, 0, 1]} />) : (<color attach="color" args={[1, 0, 0]} />)}
</meshPhongMaterial>
</mesh>
</group>
<OrbitControls regress />
<AdaptivePixelRatio />
<AdaptiveEvents />
</>);
}
export default function App() {
return (<Canvas
dpr={[1, 2]}
frameloop="always"
performance={{
min: 0.1
}}>
<Scene />
</Canvas>);
}