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