-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
RuntimeAssets.js
162 lines (154 loc) · 3.84 KB
/
RuntimeAssets.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React from 'react';
import {
ActivityIndicator,
View,
StyleSheet,
TouchableOpacity,
Text,
} from 'react-native';
import ModelView from 'react-native-gl-model-view';
import {Buffer} from 'buffer';
import axios from 'axios';
// XXX: This is the standard content header returned for a blob.
const octetStreamHeader = 'data:application/octet-stream;base64,';
class RuntimeAssets extends React.Component {
state = {
model: null,
texture: null,
error: null,
loading: false,
};
getContentFromUrl(url, decode = false) {
return axios({
method: 'get',
url,
responseType: 'blob',
}).then(
res =>
new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onloadend = () =>
resolve(
decode
? new Buffer(fileReader.result, 'base64')
: fileReader.result,
);
fileReader.onerror = reject;
fileReader.readAsDataURL(res.data);
}),
);
}
// XXX: The underlying application needs to know the file type of the model.
// Therefore, we must change the header returned by axios to something
// more indicative of the type.
formatContent(uri, header) {
return `${header}${uri.substring(octetStreamHeader.length)}`;
}
fetchDemonFromNetwork = () => {
this.setState({
loading: true,
error: null,
});
return Promise.all([
this.getContentFromUrl(
'https://github.com/rastapasta/react-native-gl-model-view/raw/master/example/data/demon.model',
).then(content =>
this.formatContent(content, 'data:geometry/model;base64,'),
),
this.getContentFromUrl(
'https://github.com/rastapasta/react-native-gl-model-view/raw/master/example/data/demon.png',
),
])
.then(binaries => {
const model = binaries[0];
const texture = binaries[1];
this.setState({
model,
texture,
loading: false,
error: null,
});
})
.catch(e =>
this.setState({
loading: false,
error: e || new Error('Something unexpected has happened.'),
}),
);
};
renderModel(nextProps, nextState) {
const {model, texture} = nextState;
const textureSrc = {
uri: texture,
};
const modelSrc = {
uri: model,
};
return (
<ModelView
style={{flex: 1}}
model={modelSrc}
texture={textureSrc}
scale={0.01}
translateZ={-2.5}
rotateX={270}
rotateY={0}
rotateZ={0}
animate
/>
);
}
renderControls(nextProps, nextState) {
const {error, loading} = nextState;
return (
<View style={styles.controls}>
{!!loading && <ActivityIndicator />}
{!loading && (
<TouchableOpacity
style={styles.controlBox}
disabled={loading}
onPress={this.fetchDemonFromNetwork}>
<Text style={error ? styles.controlTextError : styles.controlText}>
{error ? 'Retry' : 'Load'}
</Text>
</TouchableOpacity>
)}
</View>
);
}
render() {
const {model, texture} = this.state;
return (
<View style={styles.container}>
{model && texture
? this.renderModel(this.props, this.state)
: this.renderControls(this.props, this.state)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
controls: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
controlBox: {
justifyContent: 'center',
alignItems: 'center',
padding: 15,
borderRadius: 5,
},
controlTextError: {
color: 'red',
fontSize: 30,
},
controlText: {
color: 'black',
fontSize: 30,
},
});
export default RuntimeAssets;