-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathHelmet.tsx
More file actions
68 lines (54 loc) · 1.95 KB
/
Helmet.tsx
File metadata and controls
68 lines (54 loc) · 1.95 KB
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
import * as THREE from "three";
import type { CanvasRef } from "react-native-webgpu";
import { Canvas } from "react-native-webgpu";
import { StyleSheet, Text, View } from "react-native";
import { useEffect, useRef } from "react";
import { useGLTF, useRGBE } from "./assets/AssetManager";
import { makeWebGPURenderer } from "./components/makeWebGPURenderer";
export const Helmet = () => {
const texture = useRGBE(require("./assets/helmet/royal_esplanade_1k.hdr"));
const gltf = useGLTF(require("./assets/helmet/DamagedHelmet.gltf"));
const ref = useRef<CanvasRef>(null);
useEffect(() => {
if (!texture || !gltf) {
return;
}
const context = ref.current?.getContext("webgpu")!;
const { width, height } = context.canvas;
const clock = new THREE.Clock();
const camera = new THREE.PerspectiveCamera(45, width / height, 0.25, 20);
camera.position.set(-1.8, 0.6, 2.7);
const scene = new THREE.Scene();
const renderer = makeWebGPURenderer(context);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
texture.mapping = THREE.EquirectangularReflectionMapping;
scene.background = texture;
scene.environment = texture;
scene.add(gltf.scene);
renderer.setAnimationLoop(animate);
//
function animateCamera() {
const elapsed = clock.getElapsedTime();
const distance = 5;
camera.position.x = Math.sin(elapsed) * distance;
camera.position.z = Math.cos(elapsed) * distance;
camera.lookAt(new THREE.Vector3(0, 0, 0));
}
function animate() {
animateCamera();
renderer.render(scene, camera);
context!.present();
}
return () => {
renderer.setAnimationLoop(null);
};
}, [texture, gltf, ref]);
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Loading assets...</Text>
<View style={StyleSheet.absoluteFill}>
<Canvas ref={ref} style={{ flex: 1 }} />
</View>
</View>
);
};