Three.js SoftwareRenderer - transparent background
I am playing with one of the three.js examples http://threejs.org/examples/#software_geometry_earth
How do I make the background transparent? Doesn't work for SoftwareRenderer (but it works for WebGLRenderer, but I need to use SoftwareRenderer).
renderer = new THREE.SoftwareRenderer( { alpha: true } );
renderer.setClearColor( 0x000000, 0 );
Any ideas?
Complete code:
var container, stats;
var camera, scene, renderer;
var group;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
init();
animate();
function init() {
container = document.getElementById( 'container' );
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 500;
scene = new THREE.Scene();
group = new THREE.Group();
scene.add( group );
// earth
var loader = new THREE.TextureLoader();
loader.load( 'textures/land_ocean_ice_cloud_2048.jpg', function ( texture ) {
var geometry = new THREE.SphereGeometry( 200, 20, 20 );
var material = new THREE.MeshLambertMaterial( { map: texture, overdraw: 0.5 } );
var mesh = new THREE.Mesh( geometry, material );
group.add( mesh );
} );
// shadow
var canvas = document.createElement( 'canvas' );
canvas.width = 128;
canvas.height = 128;
var texture = new THREE.Texture( canvas );
texture.needsUpdate = true;
var geometry = new THREE.PlaneBufferGeometry( 0, 0, 0, 0 );
var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: 0.5 } );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.y = - 250;
mesh.rotation.x = - Math.PI / 2;
group.add( mesh );
renderer = new THREE.SoftwareRenderer( { alpha: true } );
renderer.setClearColor( 0x000000, 0 );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseMove( event ) {
mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );
}
//
function animate() {
requestAnimationFrame( animate );
render();
// stats.update();
}
function render() {
camera.position.x += ( mouseX - camera.position.x ) * 0.05;
camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
camera.lookAt( scene.position );
group.rotation.y -= 0.005;
renderer.render( scene, camera );
}
** edit: here's a link to the softwareRenderer.js file I used. I am opening it with Sublime Text.
source to share
You need to install:
renderer = new THREE.SoftwareRenderer({ alpha: true}); // just set this anyway , I didn't test this on false....
It works for me for you, you need to edit renderers / SoftwareRenderer.js replace the line with:
/*line85 :*/ context.fillRect( 0, 0, 0, 0 );
stops for the bg to be rendered
/*line596 :*/ data[ poffset ++ ] = 0;
Stops like redrawing, filling blocks back ...
Tested by scene and plane .. and css html image background .. I used this http://threejs.org/examples/#software_geometry_earth without ground and just simplified it for quick testing
This is not a static scene
source to share
in the constructor add
var alpha = parameters.alpha;
replace
context.fillStyle = clearColor.getStyle();
from
context.fillStyle = alpha ? "rgba(0, 0, 0, 0)" : clearColor.getStyle();
in all three cases; Replace
data[ i + 3 ] = 255;
from
data[ i + 3 ] = alpha ? 0 : 255;
and
data[ poffset ++ ] = 255;
from
data[ poffset ++ ] = alpha ? 0 : 255;
source to share