-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
132 lines (123 loc) · 3.23 KB
/
App.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React, { useRef, useState, useLayoutEffect } from "react";
import { StyleSheet, View } from "react-native";
import { Canvas, useFrame, useThree } from "@react-three/fiber/native";
import {
GestureDetector,
Gesture,
} from "react-native-gesture-handler";
import { animated, useSpring } from "@react-spring/three";
function Box(props) {
const mesh = useRef(null);
const [hovered, setHover] = useState(false);
const [active, setActive] = useState(false);
useFrame((state, delta) => (mesh.current.rotation.x += 0.01));
return (
<mesh
{...props}
ref={mesh}
scale={active ? 1.5 : 1}
onClick={(event) => setActive(!active)}
onPointerOver={(event) => setHover(true)}
onPointerOut={(event) => setHover(false)}
>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color={hovered ? "hotpink" : "orange"} />
</mesh>
);
}
const Camera = (props) => {
const cameraRef = useRef();
const set = useThree(({ set }) => set);
const size = useThree(({ size }) => size);
useLayoutEffect(() => {
if (cameraRef.current) {
cameraRef.current.aspect = size.width / size.height;
cameraRef.current.updateProjectionMatrix();
}
}, [size, props]);
useLayoutEffect(() => {
set({ camera: cameraRef.current });
}, []);
return (
<animated.perspectiveCamera
ref={cameraRef}
fov={75}
// aspect={sizes.width / sizes.height}
near={0.1}
far={100}
position={props.position}
/>
);
};
export default function App() {
const [{ position }, api] = useSpring(() => ({
from: { position: [0, 0, 5] },
to: { position: [0, 0, 10] },
}));
let savedPos = [0, 0, 10];
const panGesture = Gesture.Pan()
.onUpdate((e) => {
// console.log(e.translationX, e.translationY);
api.start({
from: { position: savedPos },
to: {
position: [
savedPos[0] - e.translationX / 100,
savedPos[1] + e.translationY / 100,
savedPos[2],
],
},
immediate: true,
});
})
.onEnd(() => {
savedPos = position.goal;
});
const pinchGesture = Gesture.Pinch()
.onUpdate((e) => {
console.log(e.scale);
api.start({
from: { position: savedPos },
to: {
position: [
savedPos[0],
savedPos[1],
savedPos[2] - (e.scale - 1) * 5,
],
},
immediate: true,
});
})
.onEnd(() => {
savedPos = position.goal;
});
const composed = Gesture.Race(panGesture, pinchGesture);
return (
<View style={styles.container}>
<GestureDetector gesture={composed}>
<View style={styles.canvasContainer}>
<Canvas>
<Camera position={position} />
<ambientLight />
<pointLight position={[10, 10, 10]} />
<Box position={[-1.2, 0, 0]} />
<Box position={[1.2, 0, 0]} />
</Canvas>
</View>
</GestureDetector>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#000",
alignItems: "center",
justifyContent: "center",
},
canvasContainer: {
width: "95%",
height: "85%",
backgroundColor: "#fff",
},
});