05-09 14:04
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebGL] Multiple Cameras 본문

Programming/WebGL(ThreeJS)

[WebGL] Multiple Cameras

cinema4dr12 2015. 8. 6. 23:34

이번 예제에서는 화면을 2분할 하고 우측 화면에는 가상 카메라의 움직임을, 좌측 화면에는 가상 카메라를 통해 입력되는 영상을 표시합니다.

Download Project

05-02-multiple-cameras.zip


Click here to view with full screen mode


Operations

W: Move forward

S: Move backward

Q: Move to the left

E: Move to the right

A: Rotate to the left

D: Rotate to the right

main.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
// MAIN
 
// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
 
// custom global variables
var video, videoImage, videoImageContext, videoTexture;
 
// mirror mesh / mirror camera material
var mirrorSphere, mirrorSphereCamera;
 
var player = new THREE.Object3D();
 
var dae, skin;
 
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load( 'models/CamCoder.dae'function ( collada ) {
  dae = collada.scene;
  skin = collada.skins[ 0 ];
 
  dae.position.set( 0070 );
  dae.scale.x = dae.scale.y = dae.scale.z = 10;
  dae.rotation.y = Math.PI;
  dae.castShadow = true;
  dae.updateMatrix();
 
  init();
  animate();
} );
 
// FUNCTIONS         
function init() 
{
    // SCENE
    scene = new THREE.Scene();
 
    // CAMERA
    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
    camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
    scene.add(camera);
    camera.position.set(0,0,400);
    camera.lookAt(scene.position);    
 
    // 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' );
 
    // CSS added so the hidden HTML elements do not reposition this one
    container.style.cssText = "position:absolute;top:0px;left:0px;";
    document.body.appendChild( container );
    container.appendChild( renderer.domElement );
 
    // EVENTS
    THREEx.WindowResize(renderer, camera);
    THREEx.FullScreen.bindKey({ charCode : 'f'.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 );
 
    // Enables Shadow Map
    renderer.shadowMapEnabled = true;
 
    // Point Light
    var pointLight = new THREE.PointLight(0xffffff);
    pointLight.position.set(0,250,0);
    scene.add(pointLight);
 
    // Directional Light
    var dirLight = new THREE.DirectionalLight(0xffffff);
    dirLight.position.set(0,250,0);
    dirLight.shadowCameraVisible = false;
    dirLight.shadowDarkness = 0.5;
    dirLight.intensity = 2;
    dirLight.castShadow = true;
    var lightTarget = new THREE.Object3D();
    var targetPosition = new Array();
    targetPosition[0= 0;
    targetPosition[1= -10;
    targetPosition[2= 200;
    lightTarget.position.set(targetPosition[0], targetPosition[1], targetPosition[2]);
    scene.add(lightTarget);
    dirLight.target = lightTarget;
    scene.add(dirLight);
 
    // FLOOR
    var floorTexture = new THREE.ImageUtils.loadTexture( 'img/Wood_floor.jpg' );
    floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping; 
    floorTexture.repeat.set( 11 );
    var floorMaterial = new THREE.MeshBasicMaterial( { map: floorTexture, side: THREE.DoubleSide } );
    var floorGeometry = new THREE.CircleGeometry(50020);
 
    var floor = new THREE.Mesh(floorGeometry, floorMaterial);
    floor.position.y = -0.5;
    floor.rotation.x = Math.PI / 2;
    floor.receiveShadow = true;
    scene.add(floor);
 
    // SKYBOX/FOG
    var materialArray = [];
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'img/px.jpg' ) }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'img/nx.jpg' ) }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'img/py.jpg' ) }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'img/ny.jpg' ) }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'img/pz.jpg' ) }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'img/nz.jpg' ) }));
    for (var i = 0; i < 6; i++)
       materialArray[i].side = THREE.BackSide;
    var skyboxMaterial = new THREE.MeshFaceMaterial( materialArray );
    var skyboxGeom = new THREE.CubeGeometry( 500050005000111 );
    var skybox = new THREE.Mesh( skyboxGeom, skyboxMaterial );
    scene.add( skybox );    
    
    scene.add( player );
    
    ///////////
    // VIDEO //
    ///////////
    video = document.getElementById'monitor' );
    
    videoImage = document.getElementById'videoImage' );
    videoImageContext = videoImage.getContext( '2d' );
    // background color if no video present
    videoImageContext.fillStyle = '#000000';
    videoImageContext.fillRect( 00, videoImage.width, videoImage.height );
 
    videoTexture = new THREE.Texture( videoImage );
    videoTexture.minFilter = THREE.LinearFilter;
    videoTexture.magFilter = THREE.LinearFilter;
    
    var movieMaterial = new THREE.MeshBasicMaterial( { map: videoTexture, overdraw: true, side:THREE.DoubleSide } );
    // the geometry on which the movie will be displayed;
    var movieGeometry = new THREE.PlaneGeometry( 10010011 );
    // attach video to a mesh that will move with the camera
    this.movieScreen = new THREE.Mesh( movieGeometry, movieMaterial );
 
    // add a frame to the image.
    var frameGeo = new THREE.CubeGeometry(12012020);
    var frameMat = new THREE.MeshLambertMaterial( {color:0x888888, emissive:0x000011} );
    this.frameMesh = new THREE.Mesh( frameGeo, frameMat );
    this.frameMesh.castShadow = true;
    
    // "attach" player to camera
    player.position = camera.position;
    player.rotation = camera.rotation;
 
    // Add a collada(DAE) mesh
    player.add( dae );
    
    ///////////////////
    // Mirror Sphere //
    ///////////////////
    var sphereGeom =  new THREE.SphereGeometry( 506432 ); // radius, segmentsWidth, segmentsHeight
    mirrorSphereCamera = new THREE.CubeCamera( 0.15000256 );
    mirrorSphereCamera.renderTarget.minFilter = THREE.LinearMipMapLinearFilter;
    scene.add( mirrorSphereCamera );
    var mirrorSphereMaterial = new THREE.MeshPhongMaterial( { emissive: 0x8888aa, envMap: mirrorSphereCamera.renderTarget } );
    mirrorSphere = new THREE.Mesh( sphereGeom, mirrorSphereMaterial );
    mirrorSphere.position.set(0500);
    mirrorSphereCamera.position = mirrorSphere.position;
    mirrorSphere.castShadow  = true;
    scene.add(mirrorSphere);
 
    camera.position.set(114,50,-61);
    camera.rotation.set(3.140.8753.14);
    
    //////////////////////
    // Secondary camera //
    //////////////////////
    this.topCamera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
    scene.add(topCamera);
    topCamera.position.set(0,200+50,550+200);
    topCamera.lookAt(scene.position);
    
    // other camera stuff
    renderer.setSize( window.innerWidth, window.innerHeight );
    renderer.setClearColor( 0x0000001 );
    renderer.autoClear = false;
}
 
function animate() 
{
    requestAnimationFrame( animate );
    render();        
    update();
}
 
function update()
{        
    if ( keyboard.pressed("p") ) // pause webcam
        video.pause();
    if ( keyboard.pressed("r") ) // resume webcam
        video.play();
        
    var delta = clock.getDelta(); // seconds.
    var moveDistance = 100 * delta; // 100 pixels per second
    var rotateAngle = Math.PI / 2 * delta;   // pi/2 radians (90 degrees) per second
 
    var previousPosition = camera.position.clone();
    var previousRotation = camera.rotation.clone();
    
    // move forwards/backwards/left/right (local coordinates)
    if ( keyboard.pressed("W") )
        camera.translateZ( -moveDistance );
    if ( keyboard.pressed("S") )
        camera.translateZ(  moveDistance );
    if ( keyboard.pressed("Q") )
        camera.translateX( -moveDistance );
    if ( keyboard.pressed("E") )
        camera.translateX(  moveDistance );    
 
    // rotate left/right/up/down
    if ( keyboard.pressed("A") )
        camera.rotateOnAxis( new THREE.Vector3(0,1,0), rotateAngle);
    if ( keyboard.pressed("D") )
        camera.rotateOnAxis( new THREE.Vector3(0,1,0), -rotateAngle);
    
    var d = camera.position.distanceTo( mirrorSphere.position );
    if ( d > 500 || d < 60 )
    {
        camera.position = previousPosition;
        camera.rotation = previousRotation;
        player.position = camera.position;
        player.rotation = camera.rotation;
    }
    
    stats.update();
}
 
function render() 
{    
    // update image from webcam
    if ( video.readyState === video.HAVE_ENOUGH_DATA ) 
    {
        videoImageContext.drawImage( video, 00, videoImage.width, videoImage.height );
        if ( videoTexture ) 
            videoTexture.needsUpdate = true;
    }
 
    // update cameras
    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
    camera.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT;
    camera.updateProjectionMatrix();
    topCamera.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT;
    topCamera.updateProjectionMatrix();
 
    // setViewport parameters:
    //  lower_left_x, lower_left_y, viewport_width, viewport_height
    renderer.setViewport( 00, SCREEN_WIDTH, SCREEN_HEIGHT );
    //renderer.clear();
    
    // left side
    renderer.setViewport( 11,   0.5 * SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2 );
    
    // move the CubeCamera to the position of the object
    //    that has a reflective surface, "take a picture" in each direction
    //    and apply it to the surface.
    // need to hide mirror surface before and after so that it does not
    //    "get in the way" of the camera
    movieScreen.visible = true;
    mirrorSphere.visible = false;
    
    renderer.autoClear = true;        
    mirrorSphereCamera.updateCubeMap( renderer, scene );
    renderer.autoClear = false;
    
    mirrorSphere.visible = true;
    movieScreen.visible = false;
    renderer.render( scene, camera );
 
    // right side
    movieScreen.visible = true;
    mirrorSphere.visible = true;
    renderer.setViewport( 0.5 * SCREEN_WIDTH + 11,   0.5 * SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2 );
    renderer.render( scene, topCamera );    
}
cs


'Programming > WebGL(ThreeJS)' 카테고리의 다른 글

[WebGL] Video Texture  (0) 2015.08.06
[WebGL] Render Texture  (0) 2015.08.06
[WebGL] Texture Basic  (0) 2015.08.06
[WebGL] Shadow Basic  (0) 2015.08.06
[WebGL] Refraction  (0) 2015.08.06
Comments