05-09 03:24
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebGL] Render Texture 본문

Programming/WebGL(ThreeJS)

[WebGL] Render Texture

cinema4dr12 2015. 8. 6. 23:46

이번 예제는 Render Texture에 대한 것입니다. Render Texture란 가상 카메라를 통해 렌더링 된 이미지를 오브젝트의 texture로 맵핑하는 기법을 의미합니다.

Download Project

05-03-render-texture.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
// MAIN
 
// standard global variables
var container, scene, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
 
// custom global variables
var player = new THREE.Object3D();
var textureCamera, mainCamera;
 
// intermediate scene for reflecting the reflection
var screenScene, screenCamera, firstRenderTarget, finalRenderTarget;
 
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( 050120 );
  dae.scale.x = dae.scale.y = dae.scale.z = 8;
  dae.updateMatrix();
 
  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,200,500);
    mainCamera.lookAt(scene.position);
    // camera 2
    textureCamera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
 
    // 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, mainCamera);
    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 dirLight = new THREE.DirectionalLight(0xffffff);
    dirLight.position.set(100,250,0);
    dirLight.intensity = 2;
    var lightTarget = new THREE.Object3D();
    var targetPosition = new Array();
    targetPosition[0= -100;
    targetPosition[1= -10;
    targetPosition[2= 200;
    lightTarget.position.set(targetPosition[0], targetPosition[1], targetPosition[2]);
    scene.add(lightTarget);
    dirLight.target = lightTarget;
    scene.add(dirLight);
 
    // 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 );
    
    ////////////
    // CUSTOM //
    ////////////
    // put the collada camera model into "player" and then
    // put "player" into the scene
    player.position.set(025.10);
    player.add( dae );
    scene.add( player );
    
    // 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  / -2window.innerWidth  /  2
        window.innerHeight /  2window.innerHeight / -2
        -1000010000 );
                
    var screenGeometry = new THREE.PlaneGeometry( window.innerWidth, window.innerHeight );
    
    firstRenderTarget = new THREE.WebGLRenderTarget( 10241024, { format: THREE.RGBFormat } );    
    var screenMaterial = new THREE.MeshBasicMaterial( { map: firstRenderTarget } );
    
    var quad = new THREE.Mesh( screenGeometry, screenMaterial );
    screenScene.add( quad );
 
    // Final version of camera texture, used in the scene.
    var planeGeometry = new THREE.CubeGeometry( 40020011 );
    finalRenderTarget = new THREE.WebGLRenderTarget( 10241024, { 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(Frame) for plane, to make it easier to see
    var planeGeometry = new THREE.CubeGeometry( 42022011 );
    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") )
        player.translateZ( -moveDistance );
    if ( keyboard.pressed("S") )
        player.translateZ(  moveDistance );
    if ( keyboard.pressed("Q") )
        player.translateX( -moveDistance );
    if ( keyboard.pressed("E") )
        player.translateX(  moveDistance );    
 
    // rotate left/right/up/down
    var rotation_matrix = new THREE.Matrix4().identity();
    if ( keyboard.pressed("A") )
        player.rotateOnAxis( new THREE.Vector3(0,1,0), rotateAngle);
    if ( keyboard.pressed("D") )
        player.rotateOnAxis( new THREE.Vector3(0,1,0), -rotateAngle);
    if ( keyboard.pressed("R") )
        player.rotateOnAxis( new THREE.Vector3(1,0,0), rotateAngle);
    if ( keyboard.pressed("F") )
        player.rotateOnAxis( new THREE.Vector3(1,0,0), -rotateAngle);
    
    if ( keyboard.pressed("Z") )
    {
        player.position.set(0,25.1,0);
        player.rotation.set(0,0,0);
    }
    
    // Update the texture camera's position and look direction
    var relativeCameraOffset = new THREE.Vector3(0,0,1);
    var cameraOffset = player.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( player.matrixWorld );
    textureCamera.lookAt( cameraLookOffset );
                
    stats.update();
}
 
function render() 
{
    // textureCamera is located at the position of player
    //   (and therefore is contained within it)
    // put the result of textureCamera into the first texture.
    renderer.render( scene, textureCamera, firstRenderTarget, true );
 
    // 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 );
}
cs


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

[WebGL] Texture Canvas  (0) 2015.08.06
[WebGL] Video Texture  (0) 2015.08.06
[WebGL] Multiple Cameras  (0) 2015.08.06
[WebGL] Texture Basic  (0) 2015.08.06
[WebGL] Shadow Basic  (0) 2015.08.06
Comments