-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera-view.html
executable file
·327 lines (280 loc) · 11.5 KB
/
camera-view.html
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
<!doctype html>
<html lang="en">
<head>
<title>Three.js -- Template</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body
{
font-family: Monospace;
font-weight: bold;
background-color: #ccccff;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<div id="message"></div>
<script>
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.getUserMedia;
window.URL = window.URL || window.webkitURL;
var camvideo = document.getElementById('monitor');
if (!navigator.getUserMedia)
{
document.getElementById('errorMessage').innerHTML =
'Sorry. <code>navigator.getUserMedia()</code> is not available.';
return;
}
navigator.getUserMedia({video: true}, gotStream, noStream);
function gotStream(stream)
{
if (window.URL)
{ camvideo.src = window.URL.createObjectURL(stream); }
else // Opera
{ camvideo.src = stream; }
camvideo.onerror = function(e)
{ stream.stop(); };
stream.onended = noStream;
}
function noStream(e)
{
var msg = 'No camera available.';
if (e.code == 1)
{ msg = 'User denied access to use camera.'; }
document.getElementById('errorMessage').textContent = msg;
}
</script>
<script src="js/Three.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script src="js/TrackballControls.js"></script>
<script src="js/THREEx.KeyboardState.js"></script>
<script src="js/THREEx.FullScreen.js"></script>
<script src="js/THREEx.WindowResize.js"></script>
<script>
/*
Three.js "tutorials by example"
Author: Lee Stemkoski
Date: March 2013 (three.js v56)
*/
// MAIN
// standard global variables
var container, scene, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var MovingCube;
var textureCamera, mainCamera;
// custom global variables
var video, videoImage, videoImageContext, videoTexture;
// intermediate scene for reflecting the reflection
var screenScene, screenCamera, firstRenderTarget, finalRenderTarget;
init();
animate();
// FUNCTIONS
function init()
{
// SCENE
scene = new THREE.Scene();
// CAMERAS
var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
// camera 1
mainCamera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
scene.add(mainCamera);
mainCamera.position.set(0,150,700);
mainCamera.lookAt(scene.position);
// camera 2
textureCamera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
scene.add(textureCamera);
// RENDERER
if ( Detector.webgl )
renderer = new THREE.WebGLRenderer( {antialias:true} );
else
renderer = new THREE.CanvasRenderer();
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
container = document.createElement( 'div' );
document.body.appendChild( container );
container.appendChild( renderer.domElement );
// EVENTS
THREEx.WindowResize(renderer, mainCamera);
THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild( stats.domElement );
// LIGHT
var light = new THREE.PointLight(0xffffff);
light.position.set(0,250,0);
scene.add(light);
// FLOOR
var floorTexture = new THREE.ImageUtils.loadTexture( 'custom/yugioh1.jpg' );
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat.set( 1, 1 );
var floorMaterial = new THREE.MeshBasicMaterial( { map: floorTexture, side: THREE.DoubleSide } );
var floorGeometry = new THREE.PlaneGeometry(1000, 1000, 10, 10);
var floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.position.y = -0.5;
floor.rotation.x = Math.PI / 2;
scene.add(floor);
// SKYBOX/FOG
var materialArray = [];
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-xpos.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-xneg.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-ypos.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-yneg.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-zpos.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-zneg.png' ) }));
for (var i = 0; i < 6; i++)
materialArray[i].side = THREE.BackSide;
var skyboxMaterial = new THREE.MeshFaceMaterial( materialArray );
var skyboxGeom = new THREE.CubeGeometry( 5000, 5000, 5000, 1, 1, 1 );
var skybox = new THREE.Mesh( skyboxGeom, skyboxMaterial );
scene.add( skybox );
////////////
// CUSTOM //
////////////
// create an array with six textures for a cool cube
var materialArray = [];
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/xpos.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/xneg.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/ypos.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/yneg.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/zpos.png' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/zneg.png' ) }));
var MovingCubeMat = new THREE.MeshFaceMaterial(materialArray);
var MovingCubeGeom = new THREE.CubeGeometry( 50, 50, 50, 1, 1, 1, materialArray );
MovingCube = new THREE.Mesh( MovingCubeGeom, MovingCubeMat );
MovingCube.position.set(0, 25.1, 0);
scene.add( MovingCube );
// a little bit of scenery...
var ambientlight = new THREE.AmbientLight(0x111111);
scene.add( ambientlight );
var wireMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
// torus knot
var colorMaterial = new THREE.MeshPhongMaterial( { color: 0xff3333 } );
var shape = THREE.SceneUtils.createMultiMaterialObject(
new THREE.TorusKnotGeometry( 30, 6, 160, 10, 2, 5 ), [ colorMaterial, wireMaterial ] );
shape.position.set(-200, 50, -200);
scene.add( shape );
// torus knot
var colorMaterial = new THREE.MeshPhongMaterial( { color: 0x33ff33 } );
var shape = THREE.SceneUtils.createMultiMaterialObject(
new THREE.TorusKnotGeometry( 30, 6, 160, 10, 3, 2 ), [ colorMaterial, wireMaterial ] );
shape.position.set(200, 50, -200);
scene.add( shape );
// torus knot
var colorMaterial = new THREE.MeshPhongMaterial( { color: 0xffff33 } );
var shape = THREE.SceneUtils.createMultiMaterialObject(
new THREE.TorusKnotGeometry( 30, 6, 160, 10, 4, 3 ), [ colorMaterial, wireMaterial ] );
shape.position.set(200, 50, 200);
scene.add( shape );
// torus knot
var colorMaterial = new THREE.MeshPhongMaterial( { color: 0x3333ff } );
var shape = THREE.SceneUtils.createMultiMaterialObject(
new THREE.TorusKnotGeometry( 30, 6, 160, 10, 3, 4 ), [ colorMaterial, wireMaterial ] );
shape.position.set(-200, 50, 200);
scene.add( shape );
// intermediate scene.
// this solves the problem of the mirrored texture by mirroring it again.
// consists of a camera looking at a plane with the mirrored texture on it.
screenScene = new THREE.Scene();
screenCamera = new THREE.OrthographicCamera(
window.innerWidth / -2, window.innerWidth / 2,
window.innerHeight / 2, window.innerHeight / -2,
-10000, 10000 );
screenCamera.position.z = 1;
screenScene.add( screenCamera );
var screenGeometry = new THREE.PlaneGeometry( window.innerWidth, window.innerHeight );
firstRenderTarget = new THREE.WebGLRenderTarget( 512, 512, { format: THREE.RGBFormat } );
var screenMaterial = new THREE.MeshBasicMaterial( { map: firstRenderTarget } );
var quad = new THREE.Mesh( screenGeometry, screenMaterial );
// quad.rotation.x = Math.PI / 2;
screenScene.add( quad );
// final version of camera texture, used in scene.
var planeGeometry = new THREE.CubeGeometry( 600, 400, 1, 1 );
finalRenderTarget = new THREE.WebGLRenderTarget( 600, 600, { format: THREE.RGBFormat } );
var planeMaterial = new THREE.MeshBasicMaterial( { map: finalRenderTarget } );
var plane = new THREE.Mesh( planeGeometry, planeMaterial );
plane.position.set(0,100,-500);
scene.add(plane);
// pseudo-border for plane, to make it easier to see
var planeGeometry = new THREE.CubeGeometry( 620, 420, 1, 1 );
var planeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000 } );
var plane = new THREE.Mesh( planeGeometry, planeMaterial );
plane.position.set(0,100,-502);
scene.add(plane);
}
function animate()
{
requestAnimationFrame( animate );
render();
update();
}
function update()
{
var delta = clock.getDelta(); // seconds.
var moveDistance = 200 * delta; // 200 pixels per second
var rotateAngle = Math.PI / 2 * delta; // pi/2 radians (90 degrees) per second
// local transformations
// move forwards/backwards/left/right
if ( keyboard.pressed("W") )
MovingCube.translateZ( -moveDistance );
if ( keyboard.pressed("S") )
MovingCube.translateZ( moveDistance );
if ( keyboard.pressed("Q") )
MovingCube.translateX( -moveDistance );
if ( keyboard.pressed("E") )
MovingCube.translateX( moveDistance );
// rotate left/right/up/down
var rotation_matrix = new THREE.Matrix4().identity();
if ( keyboard.pressed("A") )
rotation_matrix = new THREE.Matrix4().makeRotationY(rotateAngle);
if ( keyboard.pressed("D") )
rotation_matrix = new THREE.Matrix4().makeRotationY(-rotateAngle);
if ( keyboard.pressed("R") )
rotation_matrix = new THREE.Matrix4().makeRotationX(rotateAngle);
if ( keyboard.pressed("F") )
rotation_matrix = new THREE.Matrix4().makeRotationX(-rotateAngle);
if ( keyboard.pressed("A") || keyboard.pressed("D") ||
keyboard.pressed("R") || keyboard.pressed("F") )
{
MovingCube.matrix.multiply(rotation_matrix);
MovingCube.rotation.setEulerFromRotationMatrix(MovingCube.matrix);
}
// update the texture camera's position and look direction
var relativeCameraOffset = new THREE.Vector3(0,0,1);
var cameraOffset = MovingCube.matrixWorld.multiplyVector3( relativeCameraOffset );
textureCamera.position.x = cameraOffset.x;
textureCamera.position.y = cameraOffset.y;
textureCamera.position.z = cameraOffset.z;
var relativeCameraLookOffset = new THREE.Vector3(0,0,-1);
var cameraLookOffset = relativeCameraLookOffset.applyMatrix4( MovingCube.matrixWorld );
textureCamera.lookAt( cameraLookOffset );
stats.update();
}
function render()
{
// textureCamera is located at the position of MovingCube
// (and therefore is contained within it)
// Thus, we temporarily hide MovingCube
// so that it does not obscure the view from the camera.
MovingCube.visible = false;
// put the result of textureCamera into the first texture.
renderer.render( scene, textureCamera, firstRenderTarget, true );
MovingCube.visible = false;
// slight problem: texture is mirrored.
// solve problem by rendering (and hence mirroring) the texture again
// render another scene containing just a quad with the texture
// and put the result into the final texture
renderer.render( screenScene, screenCamera, finalRenderTarget, true );
// render the main scene
renderer.render( scene, mainCamera );
}
</script>
</body>
</html>