05-09 18:17
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebGL] Viewport 4분할 본문

Programming/WebGL(ThreeJS)

[WebGL] Viewport 4분할

cinema4dr12 2015. 8. 7. 20:48

이번 예제는 뷰포트를 네 개로 분할하는 방법에 관한 것이다. 즉, Front View, Top View, Left View, Right View로 뷰포트를 분할한다.

Download Project

09-01-viewports-quad.zip


Click here to view with full screen mode


Operations

W: Move Forward

S: Move Backward

A: Turn Left

D: Turn 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
// 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 perspectiveCamera, topCamera, frontCamera, sideCamera;
 
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;
 
    // perspective cameras
    perspectiveCamera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
    perspectiveCamera.position.set(0,200,550);
    perspectiveCamera.lookAt(scene.position);
    scene.add(perspectiveCamera);
 
    // orthographic cameras
    topCamera = new THREE.OrthographicCamera(
    window.innerWidth / -4,        // Left
    window.innerWidth / 4,        // Right
    window.innerHeight / 4,        // Top
    window.innerHeight / -4,    // Bottom
    -5000,                        // Near
    10000 );                       // Far -- enough to see the skybox
    topCamera.up = new THREE.Vector3(0,0,-1);
    topCamera.lookAt( new THREE.Vector3(0,-1,0) );
    scene.add(topCamera);
    
    frontCamera = new THREE.OrthographicCamera(
    window.innerWidth / -4,    window.innerWidth / 4,
    window.innerHeight / 4,    window.innerHeight / -4,
    -500010000 );
    frontCamera.lookAt( new THREE.Vector3(0,0,-1) );
    scene.add(frontCamera);
    
    sideCamera = new THREE.OrthographicCamera(
    window.innerWidth / -4,    window.innerWidth / 4,
    window.innerHeight / 4,    window.innerHeight / -4,
    -500010000 );
    sideCamera.lookAt( new THREE.Vector3(1,0,0) );
    scene.add(sideCamera);
    
    // RENDERER
    if ( Detector.webgl )
        renderer = new THREE.WebGLRenderer( {antialias:true} );
    else
        renderer = new THREE.CanvasRenderer();
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    container = document.getElementById'ThreeJS' );
    container.appendChild( renderer.domElement );
 
    // EVENTS
    THREEx.WindowResize(renderer, topCamera);
    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 );
 
    // LIGHT
    var light = new THREE.PointLight(0xffffff);
    light.position.set(0,250,0);
    scene.add(light);
 
    // 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.PlaneGeometry(100010001010);
    var floor = new THREE.Mesh(floorGeometry, floorMaterial);
    floor.position.y = -0.5;
    floor.rotation.x = Math.PI / 2;
    scene.add(floor);
 
    // SKYBOX/FOG
    var skyBoxGeometry = new THREE.CubeGeometry( 100001000010000 );
    var skyBoxMaterial = new THREE.MeshBasicMaterial( { color: 0x9999ff, side: THREE.BackSide } );
    var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
    scene.add(skyBox);
    
    ////////////
    // CUSTOM //
    ////////////
    
    scene.add( new THREE.AxisHelper(100) );
    
    // create an array with six textures for a cool cube
    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' ) }));
    var MovingCubeMat = new THREE.MeshFaceMaterial(materialArray);
    var MovingCubeGeom = new THREE.CubeGeometry( 505050111, materialArray );
    MovingCube = new THREE.Mesh( MovingCubeGeom, MovingCubeMat );
    MovingCube.position.set(025.10);
    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( 3061601025 ), [ colorMaterial, wireMaterial ] );
    shape.position.set(-20050-200);
    scene.add( shape );
    // torus knot
    var colorMaterial = new THREE.MeshPhongMaterial( { color: 0x33ff33 } );
    var shape = THREE.SceneUtils.createMultiMaterialObject( 
        new THREE.TorusKnotGeometry( 3061601032 ), [ colorMaterial, wireMaterial ] );
    shape.position.set(20050-200);
    scene.add( shape );
    // torus knot
    var colorMaterial = new THREE.MeshPhongMaterial( { color: 0xffff33 } );
    var shape = THREE.SceneUtils.createMultiMaterialObject( 
        new THREE.TorusKnotGeometry( 3061601043 ), [ colorMaterial, wireMaterial ] );
    shape.position.set(20050200);
    scene.add( shape );
    // torus knot
    var colorMaterial = new THREE.MeshPhongMaterial( { color: 0x3333ff } );
    var shape = THREE.SceneUtils.createMultiMaterialObject( 
        new THREE.TorusKnotGeometry( 3061601034 ), [ colorMaterial, wireMaterial ] );
    shape.position.set(-20050200);
    scene.add( shape );
    
    renderer.setSize( window.innerWidth, window.innerHeight );
    renderer.setClearColorHex( 0x0000001 );
    renderer.autoClear = false;
}
 
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") )
        MovingCube.rotateOnAxis( new THREE.Vector3(0,1,0), rotateAngle);
    if ( keyboard.pressed("D") )
        MovingCube.rotateOnAxis( new THREE.Vector3(0,1,0), -rotateAngle);
    if ( keyboard.pressed("R") )
        MovingCube.rotateOnAxis( new THREE.Vector3(1,0,0), rotateAngle);
    if ( keyboard.pressed("F") )
        MovingCube.rotateOnAxis( new THREE.Vector3(1,0,0), -rotateAngle);
    
    if ( keyboard.pressed("Z") )
    {
        MovingCube.position.set(0,25.1,0);
        MovingCube.rotation.set(0,0,0);
    }
 
    stats.update();
}
 
function render()
{
    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
 
    // setViewport parameters:
    //  lower_left_x, lower_left_y, viewport_width, viewport_height
    renderer.setViewport( 00, SCREEN_WIDTH, SCREEN_HEIGHT );
    renderer.clear();
    
    // upper left corner
    renderer.setViewport( 10.5 * SCREEN_HEIGHT + 10.5 * SCREEN_WIDTH - 20.5 * SCREEN_HEIGHT - 2 );
    renderer.render( scene, perspectiveCamera );
    
    // upper right corner
    renderer.setViewport( 0.5 * SCREEN_WIDTH + 10.5 * SCREEN_HEIGHT + 10.5 * SCREEN_WIDTH - 20.5 * SCREEN_HEIGHT - 2 );
    renderer.render( scene, topCamera );
    
    // lower left corner
    renderer.setViewport( 11,   0.5 * SCREEN_WIDTH - 20.5 * SCREEN_HEIGHT - 2 );
    renderer.render( scene, sideCamera );
    
    // lower right corner
    renderer.setViewport( 0.5 * SCREEN_WIDTH + 11,   0.5 * SCREEN_WIDTH - 20.5 * SCREEN_HEIGHT - 2 );
    renderer.render( scene, frontCamera );
}
cs


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

[WebGL] PhysiJS Freefall  (0) 2015.08.07
[WebGL] Collision Detection  (0) 2015.08.07
[WebGL] Model Animation Control  (0) 2015.08.07
[WebGL] Tween Animation  (0) 2015.08.07
[WebGL] Blender Animation Scene 로딩하기  (0) 2015.08.07
Comments