This repository was archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathPano.js
executable file
·363 lines (340 loc) · 12.6 KB
/
Pano.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/**
* RCTPano: runtime implementation of the <Pano source={{uri:URL}}>
* creates a 1000 m radius globe that as a child of the view
* view responds to layout and transforms with pivot point the center
* of the view
* @class RCTPano
* @extends RCTBaseView
*/
import RCTBaseView from './BaseView';
import merge from '../Utils/merge';
import StereoOffsetRepeats from '../Utils/StereoOffsetRepeats';
import {HPanoBufferGeometry} from '../Utils/HPano';
import {CubePanoBufferGeometry} from '../Utils/CubePano';
import {RCTBindedResource} from '../Utils/RCTBindedResource';
import * as OVRUI from 'ovrui';
import * as THREE from 'three';
import * as Yoga from '../Utils/Yoga.bundle';
import Prefetch from './Prefetch';
const panoRayCast = (function() {
// avoid create temp objects;
const inverseMatrix = new THREE.Matrix4();
const ray = new THREE.Ray();
const sphere = new THREE.Sphere(new THREE.Vector3(0, 0, 0), 1000);
const intersectionPoint = new THREE.Vector3();
const intersectionPointWorld = new THREE.Vector3();
return function(raycaster, intersects) {
// transform the ray into the space of the sphere
inverseMatrix.getInverse(this.matrixWorld);
ray.copy(raycaster.ray).applyMatrix4(inverseMatrix);
const intersect = ray.intersectSphere(sphere, intersectionPoint);
if (intersect === null) {
return;
}
// determine hit location in world space
intersectionPointWorld.copy(intersectionPoint);
intersectionPointWorld.applyMatrix4(this.matrixWorld);
const distance = raycaster.ray.origin.distanceTo(intersectionPointWorld);
if (distance < raycaster.near || distance > raycaster.far) {
return;
}
intersects.push({
distance: distance,
point: intersectionPointWorld.clone(),
object: this,
});
};
})();
let sphereGeometry = undefined;
let cubeGeometry = undefined;
// Only dispose certain textures, not those that is from video.
// A texture manager would be useful to help track the lifetime of textures.
const tryDisposeTexture = texture => {
if (texture._needsDispose) {
texture.dispose();
} else if (Prefetch.isCachedTexture(texture)) {
Prefetch.removeTextureFromCache(texture);
}
};
export default class RCTPano extends RCTBaseView {
/**
* constructor: allocates the required resources and sets defaults
*/
constructor(guiSys, rnctx) {
super();
sphereGeometry = sphereGeometry || new THREE.SphereGeometry(1000, 50, 50);
cubeGeometry = cubeGeometry || new CubePanoBufferGeometry(2000, 3, 2, 1.01);
this._tintOpacity = 1.0;
this._styleOpacity = 1.0;
this._sphereGeometry = sphereGeometry;
this._cubeGeometry = cubeGeometry;
this._material = new OVRUI.StereoBasicTextureMaterial({
color: 'white',
side: THREE.DoubleSide,
});
this._globe = new THREE.Mesh(this._sphereGeometry, this._material);
this._globe.onBeforeRender = function(renderer, scene, camera, geometry, material, group) {
if (camera.viewID === 1 && material.stereoOffsetRepeats[1]) {
material.uniforms.stereoOffsetRepeat.value = material.stereoOffsetRepeats[1];
} else {
material.uniforms.stereoOffsetRepeat.value = material.stereoOffsetRepeats[0];
}
if (material._rightEnvMap) {
if (camera.viewID === 1) {
material.envMap = material._rightEnvMap;
} else {
material.envMap = material._leftEnvMap;
}
material.needsUpdate = true;
}
};
this._globe.raycast = panoRayCast.bind(this._globe);
this._globe.rotation.y = -Math.PI / 2;
this.view = new OVRUI.UIView(guiSys);
// set zOffset to be the radius of the sphere. This helps prevent the pano's
// transparency from affecting other views as a result of rendering order.
this.view.zOffset = 1000;
this.view.add(this._globe);
this._localResource = new RCTBindedResource(rnctx.RCTResourceManager);
this.globeOnUpdate = this.globeOnUpdate.bind(this);
Object.defineProperty(this.props, 'source', {
set: value => this.setSource(value),
});
// register a setter for the backgroundColor so the globe can be tinted
Object.defineProperty(this.style, 'opacity', {
set: value => {
this._styleOpacity = value;
this._material.opacity = this._styleOpacity * this._tintOpacity;
this._material.transparent = this._material.opacity < 1;
},
});
// register a setter for the backgroundColor so the globe can be tinted
Object.defineProperty(this.style, 'tintColor', {
set: value => {
const opacity = parseInt(value.toString(16).slice(0, 2), 16) / 255;
this._material.color.set(value);
this._tintOpacity = opacity;
this._material.opacity = this._styleOpacity * this._tintOpacity;
this._material.transparent = this._material.opacity < 1;
},
});
}
globeOnUpdate(scene, camera) {
const projScreenMatrix = new THREE.Matrix4();
const modelViewMatrix = new THREE.Matrix4();
modelViewMatrix.multiplyMatrices(camera.matrixWorldInverse, this._globe.matrixWorld);
projScreenMatrix.multiplyMatrices(camera.projectionMatrix, modelViewMatrix);
this._globe.geometry.update(this.maxDepth, projScreenMatrix);
this._globe.material = this._globe.geometry.material;
}
setSource(value) {
if (value && value.tile) {
// use tile renderer
this._globe.geometry.dispose();
this.maxDepth = value.maxDepth || 2;
this._globe.geometry = new HPanoBufferGeometry(1000, this.maxDepth, value.tile);
this._globe.onUpdate = this.globeOnUpdate;
} else {
// use sphere renderer
this._globe.geometry.dispose();
if (value && value.layout === 'CUBEMAP_32') {
this._globe.geometry = this._cubeGeometry;
this._globe.scale.z = -1;
this._material.useUV = 1;
} else {
this._globe.geometry = this._sphereGeometry;
this._globe.scale.z = 1;
this._material.useUV = 0;
}
this._globe.onUpdate = null;
// call onLoadStart in React
this.UIManager._rnctx.callFunction('RCTEventEmitter', 'receiveEvent', [
this.getTag(),
'topLoadStart',
[],
]);
const loadRemoteTexture = (url, onLoad, viewID) => {
// When a url is null or undefined, send undefined to onLoad callback
const onError = () => onLoad(undefined, viewID);
const onLoadDisposable = texture => {
texture._needsDispose = true;
onLoad(texture, viewID);
};
// No progress indication for now.
const onProgress = undefined;
if (url == null) {
onError();
} else if (Prefetch.isCached(url)) {
// First Check if the texture hasn't already been prefetched
const cachedTexture = Prefetch.getFromCache(url);
onLoad(cachedTexture, viewID);
} else if (Array.isArray(url)) {
const loader = new THREE.CubeTextureLoader();
loader.setCrossOrigin('Access-Control-Allow-Origin');
loader.load(url, onLoadDisposable, onProgress, onError);
} else {
const loader = new THREE.TextureLoader();
loader.setCrossOrigin('Access-Control-Allow-Origin');
loader.load(url, onLoadDisposable, onProgress, onError);
}
};
const onLoadOrChange = (texture, viewID) => {
// ignore a old request result
if (value !== this._currentSource) {
return;
}
this._globe.scale.x = -1;
if (this._material.map) {
tryDisposeTexture(this._material.map);
}
if (
this._material.envMap &&
this._material.envMap !== this._material._leftEnvMap &&
this._material.envMap !== this._material._rightEnvMap
) {
tryDisposeTexture(this._material.envMap);
}
if (texture === undefined) {
this._material.map = undefined;
this._material.envMap = undefined;
} else if (texture.type === 'MonoTextureInfo') {
this._material.map = texture.texture;
this._material.envMap = undefined;
} else {
if (!value.enableMipmaps) {
texture.generateMipmaps = false;
texture.minFilter = THREE.LinearFilter;
}
texture.wrapS = THREE.ClampToEdgeWrapping;
texture.wrapT = THREE.ClampToEdgeWrapping;
const cubeTexture = texture.isCubeTexture ? texture : null;
const flatTexture = texture.isCubeTexture ? null : texture;
if (texture.isCubeTexture) {
this._globe.scale.x = 1;
}
this._material.map = flatTexture;
this._material.envMap = cubeTexture;
if (viewID === 1) {
this._nextRightEnvMap = cubeTexture;
} else {
this._nextLeftEnvMap = cubeTexture;
}
}
const stereoFormat = value && value.stereo ? value.stereo : '2D';
this._material.stereoOffsetRepeats = StereoOffsetRepeats[stereoFormat];
if (!this._material.stereoOffsetRepeats) {
console.warn(`Pano: stereo format '${stereoFormat}' not supported.`);
// fallback to 2D
this._material.stereoOffsetRepeats = StereoOffsetRepeats['2D'];
}
this._material.needsUpdate = true;
this._numTexturesToLoad--;
if (this._numTexturesToLoad === 0) {
if (this._material._leftEnvMap) {
tryDisposeTexture(this._material._leftEnvMap);
}
if (this._material._rightEnvMap) {
tryDisposeTexture(this._material._rightEnvMap);
}
this._material._leftEnvMap = this._nextLeftEnvMap;
this._material._rightEnvMap = this._nextRightEnvMap;
// call onLoad in React
if (texture !== undefined) {
this.UIManager._rnctx.callFunction('RCTEventEmitter', 'receiveEvent', [
this.getTag(),
'topLoad',
[],
]);
}
// call onLoadEvent in React
this.UIManager._rnctx.callFunction('RCTEventEmitter', 'receiveEvent', [
this.getTag(),
'topLoadEnd',
[],
]);
}
};
this._currentSource = value;
this._numTexturesToLoad = 1;
this._nextLeftEnvMap = undefined;
this._nextRightEnvMap = undefined;
if (Array.isArray(value)) {
if ((value.length !== 6 && value.length !== 12) || !value[0].uri) {
console.warn(
'Pano expected cubemap source in format [{uri: http..}, {uri: http..}, ... ]' +
'with length of 6 (or 12 for stereo)'
);
return;
}
const urls = value.map(function(x) {
return x.uri;
});
this._localResource.unregister();
if (urls.length === 12) {
this._numTexturesToLoad = 2;
loadRemoteTexture(urls.slice(0, 6), onLoadOrChange, 0);
loadRemoteTexture(urls.slice(6, 12), onLoadOrChange, 1);
} else {
loadRemoteTexture(urls, onLoadOrChange, 0);
}
} else {
const url = value ? value.uri : null;
if (this._localResource.isValidUrl(url)) {
this._localResource.load(url, texture => onLoadOrChange(texture, 0));
} else {
this._localResource.unregister();
loadRemoteTexture(url, onLoadOrChange, 0);
}
}
}
}
presentLayout() {
super.presentLayout();
this._globe.visible = this.YGNode.getDisplay() !== Yoga.DISPLAY_NONE;
}
/**
* Dispose of any associated resources
*/
dispose() {
if (this._material.map) {
tryDisposeTexture(this._material.map);
}
if (
this._material.envMap &&
this._material.envMap !== this._material._leftEnvMap &&
this._material.envMap !== this._material._rightEnvMap
) {
tryDisposeTexture(this._material.envMap);
}
if (this._material._leftEnvMap) {
tryDisposeTexture(this._material._leftEnvMap);
}
if (this._material._rightEnvMap) {
tryDisposeTexture(this._material._rightEnvMap);
}
if (this._localResource) {
this._localResource.dispose();
}
super.dispose();
}
/**
* Describes the properties representable by this view type and merges
* with super type
*/
static describe() {
return merge(super.describe(), {
// declare the native props sent from react to runtime
NativeProps: {
source: 'string',
},
});
}
}