|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * RCTLiveEnvCamera |
| 12 | + * Displays the environment facing camera on a sphere |
| 13 | + * @class RCTLiveEnvCamera |
| 14 | + * @extends RCTBaseView |
| 15 | + */ |
| 16 | + |
| 17 | +import RCTBaseView from './BaseView'; |
| 18 | +import merge from '../Utils/merge'; |
| 19 | +import * as OVRUI from 'ovrui'; |
| 20 | +import * as THREE from 'three'; |
| 21 | +import * as Yoga from '../Utils/Yoga.bundle'; |
| 22 | + |
| 23 | +// display texture always infront of the camera |
| 24 | +const basic_vert = ` |
| 25 | + varying highp vec4 vUv; |
| 26 | + void main() |
| 27 | + { |
| 28 | + vUv = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); |
| 29 | + vUv.xy = (vUv.xy + vec2(vUv.w)) * 0.5; |
| 30 | + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); |
| 31 | + } |
| 32 | + `; |
| 33 | + |
| 34 | +const basic_frag = ` |
| 35 | + uniform sampler2D map; |
| 36 | + varying highp vec4 vUv; |
| 37 | + void main() |
| 38 | + { |
| 39 | + gl_FragColor = texture2DProj( map, vUv ); |
| 40 | + } |
| 41 | + `; |
| 42 | + |
| 43 | +const SPHERE_RADIUS = 1000; |
| 44 | + |
| 45 | +const sphereRayCast = (function() { |
| 46 | + // avoid create temp objects; |
| 47 | + const inverseMatrix = new THREE.Matrix4(); |
| 48 | + const ray = new THREE.Ray(); |
| 49 | + const sphere = new THREE.Sphere(new THREE.Vector3(0, 0, 0), SPHERE_RADIUS); |
| 50 | + const intersectionPoint = new THREE.Vector3(); |
| 51 | + const intersectionPointWorld = new THREE.Vector3(); |
| 52 | + return function(raycaster, intersects) { |
| 53 | + // transform the ray into the space of the sphere |
| 54 | + inverseMatrix.getInverse(this.matrixWorld); |
| 55 | + ray.copy(raycaster.ray).applyMatrix4(inverseMatrix); |
| 56 | + const intersect = ray.intersectSphere(sphere, intersectionPoint); |
| 57 | + if (intersect === null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // determine hit location in world space |
| 62 | + intersectionPointWorld.copy(intersectionPoint); |
| 63 | + intersectionPointWorld.applyMatrix4(this.matrixWorld); |
| 64 | + |
| 65 | + const distance = raycaster.ray.origin.distanceTo(intersectionPointWorld); |
| 66 | + if (distance < raycaster.near || distance > raycaster.far) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + intersects.push({ |
| 71 | + distance: distance, |
| 72 | + point: intersectionPointWorld.clone(), |
| 73 | + object: this, |
| 74 | + }); |
| 75 | + }; |
| 76 | +})(); |
| 77 | + |
| 78 | +export default class RCTLiveEnvCamera extends RCTBaseView { |
| 79 | + /** |
| 80 | + * constructor: allocates the required resources and sets defaults |
| 81 | + */ |
| 82 | + constructor(guiSys, rnctx) { |
| 83 | + super(); |
| 84 | + |
| 85 | + navigator.getUserMedia = |
| 86 | + navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; |
| 87 | + |
| 88 | + const constraints = { |
| 89 | + video: {facingMode: {exact: 'environment'}}, |
| 90 | + }; |
| 91 | + const video = document.createElement('video'); |
| 92 | + const videoTexture = new THREE.Texture(video); |
| 93 | + videoTexture.minFilter = THREE.LinearFilter; |
| 94 | + this._video = video; |
| 95 | + this._videoTexture = videoTexture; |
| 96 | + navigator.getUserMedia( |
| 97 | + constraints, |
| 98 | + stream => { |
| 99 | + video.src = window.URL.createObjectURL(stream); |
| 100 | + }, |
| 101 | + error => { |
| 102 | + console.log('navigator.getUserMedia error: ', error); |
| 103 | + } |
| 104 | + ); |
| 105 | + |
| 106 | + this._sphereGeometry = new THREE.SphereGeometry(SPHERE_RADIUS, 5, 5); |
| 107 | + this._material = new THREE.ShaderMaterial({ |
| 108 | + uniforms: { |
| 109 | + map: { |
| 110 | + value: videoTexture, |
| 111 | + type: 't', |
| 112 | + }, |
| 113 | + }, |
| 114 | + vertexShader: basic_vert, |
| 115 | + fragmentShader: basic_frag, |
| 116 | + side: THREE.DoubleSide, |
| 117 | + }); |
| 118 | + |
| 119 | + this._onUpdate = this._onUpdate.bind(this); |
| 120 | + |
| 121 | + this._globe = new THREE.Mesh(this._sphereGeometry, this._material); |
| 122 | + this._globe.raycast = sphereRayCast.bind(this._globe); |
| 123 | + this._globe.rotation.y = -Math.PI / 2; |
| 124 | + this._globe.onUpdate = this._onUpdate; |
| 125 | + |
| 126 | + this.view = new OVRUI.UIView(guiSys); |
| 127 | + this.view.add(this._globe); |
| 128 | + } |
| 129 | + |
| 130 | + _onUpdate(scene, camera) { |
| 131 | + if (this._video.readyState === this._video.HAVE_ENOUGH_DATA) { |
| 132 | + this._videoTexture.needsUpdate = true; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + presentLayout() { |
| 137 | + super.presentLayout(); |
| 138 | + this._globe.visible = this.YGNode.getDisplay() !== Yoga.DISPLAY_NONE; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Dispose of any associated resources |
| 143 | + */ |
| 144 | + dispose() { |
| 145 | + if (this._localResource) { |
| 146 | + this._localResource.dispose(); |
| 147 | + } |
| 148 | + super.dispose(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Describes the properties representable by this view type and merges |
| 153 | + * with super type |
| 154 | + */ |
| 155 | + static describe() { |
| 156 | + return merge(super.describe(), { |
| 157 | + // declare the native props sent from react to runtime |
| 158 | + NativeProps: {}, |
| 159 | + }); |
| 160 | + } |
| 161 | +} |
0 commit comments