Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use camera as texture image in Three.js

In three.js, I am trying to create a texture whose image is the current scene as viewed from a Camera. Using a CubeCamera to create a similar effect is well-documented; and with CubeCamera I have created an example of a scene to illustrate my goal at:

http://stemkoski.github.com/Three.js/Camera-Texture-Almost.html

However, I would like to use a regular Camera (rather than a CubeCamera) as the texture. How could I do this?

like image 800
Stemkoski Avatar asked Jul 04 '12 12:07

Stemkoski


2 Answers

Ideally this would work.

Init:

renderTarget = new THREE.WebGLRenderTarget( 512, 512, { format: THREE.RGBFormat } );

var planelikeGeometry = new THREE.CubeGeometry( 400, 200, 200 );
var plane = new THREE.Mesh( planelikeGeometry, new THREE.MeshBasicMaterial( { map: renderTarget } ) );
plane.position.set(0,100,-500);
scene.add(plane);

Render:

renderer.render( scene, topCamera, renderTarget, true );
renderer.render( scene, topCamera );

And it almost does, but we have some unfinished business with y-flipped textures that ruins the party here.

So the best solution at the moment is to use the intermediry quad for flipping the texture (also saves a render):

http://mrdoob.github.com/three.js/examples/webgl_rtt.html

like image 61
mrdoob Avatar answered Oct 19 '22 01:10

mrdoob


Based on mrdoob's example and suggestions, I have created a working example with very detailed comments, available at:

http://stemkoski.github.com/Three.js/Camera-Texture.html

part of my series of tutorial-style Three.js series of examples at

http://stemkoski.github.com/Three.js/

like image 4
Stemkoski Avatar answered Oct 18 '22 23:10

Stemkoski