-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPostProcessing.tsx
More file actions
89 lines (72 loc) · 2.61 KB
/
PostProcessing.tsx
File metadata and controls
89 lines (72 loc) · 2.61 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as THREE from "three";
import type { CanvasRef } from "react-native-webgpu";
import { Canvas } from "react-native-webgpu";
import { PixelRatio, Text, View, StyleSheet } from "react-native";
import { useEffect, useRef } from "react";
import { color, pass } from "three/tsl";
import { bloom } from "three/addons/tsl/display/BloomNode";
import { useGLTF } from "./assets/AssetManager";
import { makeWebGPURenderer } from "./components/makeWebGPURenderer";
export const PostProcessing = () => {
const gltf = useGLTF(require("./assets/PrimaryIonDrive.glb"));
const ref = useRef<CanvasRef>(null);
useEffect(() => {
if (!gltf) {
return;
}
const context = ref.current?.getContext("webgpu")!;
const canvas = context.canvas as HTMLCanvasElement;
canvas.width = canvas.clientWidth * PixelRatio.get();
canvas.height = canvas.clientHeight * PixelRatio.get();
const { width, height } = context.canvas;
const camera = new THREE.PerspectiveCamera(40, width / height, 1, 100);
camera.position.set(-5, -8, -3.5);
camera.lookAt(0, 0, 0);
const scene = new THREE.Scene();
scene.backgroundNode = color(0);
camera.lookAt(0, 1, 0);
const clock = new THREE.Clock();
//lights
const light = new THREE.SpotLight(0xffffff, 1);
light.power = 2000;
camera.add(light);
scene.add(camera);
const object = gltf.scene;
const mixer = new THREE.AnimationMixer(object);
const action = mixer.clipAction(gltf.animations[0]);
action.play();
scene.add(object);
// portals
//renderer
const renderer = makeWebGPURenderer(context, { antialias: false });
renderer.setAnimationLoop(animate);
renderer.toneMapping = THREE.NeutralToneMapping;
renderer.toneMappingExposure = 0.3;
const postProcessing = new THREE.PostProcessing(renderer);
const scenePass = pass(scene, camera);
const scenePassColor = scenePass.getTextureNode("output");
const bloomPass = bloom(scenePassColor);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
postProcessing.outputNode = scenePassColor.add(bloomPass);
function animate() {
const delta = clock.getDelta();
if (mixer) {
mixer.update(delta);
}
postProcessing.render();
context!.present();
}
return () => {
renderer.setAnimationLoop(null);
};
}, [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>
);
};