-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
135 lines (109 loc) · 3.37 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
133
134
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
const THREE = require('three');
global.THREE = THREE;
require('./OBJLoader')
import ExpoTHREE from 'expo-three';
console.disableYellowBox = true;
export default class App extends React.Component {
state = {
loaded: false,
}
componentWillMount() {
this.preloadAssetsAsync();
}
render() {
return this.state.loaded ? (
<View style={{ flex: 1 }}>
<Expo.GLView
ref={(ref) => this._glView = ref}
style={{ flex: 1 }}
onContextCreate={this._onGLContextCreate}
/>
</View>
) : <Expo.AppLoading />;
}
async preloadAssetsAsync() {
await Promise.all([
require('./assets/venusAR.obj'),
require('./assets/gold.jpg'),
].map((module) => Expo.Asset.fromModule(module).downloadAsync()));
this.setState({ loaded: true });
}
_onGLContextCreate = async (gl) => {
const width = gl.drawingBufferWidth;
const height = gl.drawingBufferHeight;
gl.createRenderbuffer = () => {};
gl.bindRenderbuffer = () => {};
gl.renderbufferStorage = () => {};
gl.framebufferRenderbuffer = () => {};
// ar init
const arSession = await this._glView.startARSessionAsync();
// three.js init
const scene = new THREE.Scene();
const camera = ExpoTHREE.createARCamera(
arSession,
gl.drawingBufferWidth,
gl.drawingBufferHeight,
0.01,
2000
);
const renderer = ExpoTHREE.createRenderer({ gl });
renderer.setSize(gl.drawingBufferWidth, gl.drawingBufferHeight);
scene.background = ExpoTHREE.createARBackgroundTexture(arSession, renderer);
// lights
const frontLight = new THREE.DirectionalLight(0xffffff, 0.3);
frontLight.position.set(1, 1, 1);
scene.add(frontLight);
const backLight = new THREE.DirectionalLight(0xffffff, 0.1);
backLight.position.set(-1, 0, -1);
scene.add(backLight);
const ambLight = new THREE.AmbientLight(0xd3d3d3, 0.5);
scene.add(ambLight);
// yellow lights
const frontLeftLight = new THREE.DirectionalLight(0xFFAB00, 0.3);
frontLeftLight.position.set(-3, 0, -3);
scene.add(frontLeftLight);
const frontRightLight = new THREE.DirectionalLight(0xFFAB00, 0.1);
frontRightLight.position.set(-3, 0, 3);
scene.add(frontRightLight);
// venus material
const venusMaterial = new THREE.MeshPhongMaterial({
color: 0xFFFFFF,
specular: 0xf8f8f8,
shininess: 1,
reflectivity: 1,
roughness: 2.5,
});
// venus model
const modelAsset = Asset.fromModule(require('./assets/venusAR.obj'));
await modelAsset.downloadAsync();
const loader = new THREE.OBJLoader();
const model = loader.parse(
await Expo.FileSystem.readAsStringAsync(modelAsset.localUri))
model.traverse((child) => {
if (child instanceof THREE.Mesh) {
child.material = venusMaterial;
}
});
model.position.y = -0.2;
model.position.z = -0.7;
model.scale.set(0.15, 0.15, 0.15);
scene.add(model);
// animation loop
const animate = () => {
requestAnimationFrame(animate);
renderer.render(scene, camera);
gl.endFrameEXP();
}
animate();
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});