Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreeJS: Creating a background image that exactly fits the window?

Tags:

three.js

In ThreeJS (JavaScript/ WebGL) how can I create a static background image that is part of the scene but resizes to fit the full inner browser window exactly? It works when I use CSS but then the background image won't show when making a "screenshot" via renderer.domElement.toDataURL('image/png'), which I want to. Thanks!

like image 372
Philipp Lenssen Avatar asked Jun 24 '12 15:06

Philipp Lenssen


1 Answers

You can use a separate background scene to render the background image.

var bg = new THREE.Mesh(
  new THREE.PlaneGeometry(2, 2, 0),
  new THREE.MeshBasicMaterial({map: backgroundTexture})
);

// The bg plane shouldn't care about the z-buffer.
bg.material.depthTest = false;
bg.material.depthWrite = false;

var bgScene = new THREE.Scene();
var bgCam = new THREE.Camera();
bgScene.add(bgCam);
bgScene.add(bg);

Then in your render loop, draw bgScene before your real scene.

renderer.autoClear = false;
renderer.clear();
renderer.render(bgScene, bgCam);
renderer.render(scene, cam);
like image 191
Ilmari Heikkinen Avatar answered Oct 22 '22 04:10

Ilmari Heikkinen