3

I'm playing around with one of the three.js examples http://threejs.org/examples/#software_geometry_earth

How do I make the background transparent? The following does not work for the SoftwareRenderer (but it does work for the WebGLRenderer, but I need to use the SoftwareRenderer).

renderer = new THREE.SoftwareRenderer( { alpha: true } );
renderer.setClearColor( 0x000000, 0 );

Any ideas?

The full code is:

        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'm opening it with Sublime Text.

Agent Zebra
  • 4,410
  • 6
  • 33
  • 66

2 Answers2

0

You need to set:

renderer = new THREE.SoftwareRenderer({ alpha: true}); // just set this anyway , I didn't test this on false....

I have it working for you, you need to edit renderers/SoftwareRenderer.js replace line with:

  /*line85 :*/  context.fillRect( 0, 0, 0, 0 ); 

stops for the bg to be rendered

 /*line596 :*/  data[ poffset ++ ] = 0; 

Stops like a redraw filling the blocks back...

Tested with a scene and a plane..and css html image background.. I used this http://threejs.org/examples/#software_geometry_earth with out the earth and just simplified it for quick test

This is a non static scene

enter image description here

Careen
  • 567
  • 1
  • 6
  • 26
  • Thanks Careen, unfortunately that didn't work, for some reason this does not render at all using the WebGLRenderer. I checked the css and there's no color on the background of the element. I still have the same problem. – Agent Zebra May 05 '15 at 19:06
  • @ WestLangley, @Agent Zebra Tested and working updated answer – Careen May 06 '15 at 05:50
  • Did you test it on a non-static scene? http://threejs.org/examples/software_sandbox.html What does `antialias` have to do with `SoftwareRenderer`? – WestLangley May 06 '15 at 23:56
  • antialias has nothing to do with it, the scene was not static hence here! data[ poffset ++ ] = 0; – Careen May 07 '15 at 03:17
  • Thanks @Careen, I'm just getting back to this one. It's still not working for me :( Can you check those line numbers for the edits to renderers/SoftwareRenderer.js ? You mention to replace code, but there's no code at those line numbers in my version of that file (I'll edit the question and add a link to the file). – Agent Zebra Jun 01 '15 at 05:20
  • i used http://threejs.org/examples/js/renderers/SoftwareRenderer.js use this file and replace those lines – Careen Jun 04 '15 at 14:56
  • Thanks @Careen, just seeing your reply. I just checked that file (in sublime text) and do not see any code to edit on those line numbers referenced above. See screenshots of the code as I am seeing it with line numbers [here](http://imgur.com/xpgC4XZ,mgQYMaB) and [here](http://imgur.com/xpgC4XZ,mgQYMaB#1). Would you mind please rechecking your line numbers and writing the actual content of the lines that need replacing? Thanks. – Agent Zebra Jun 12 '15 at 01:25
  • they updated possibly, moved a few lines away line85 is now "line 96", line 596 is around the same on threejs.org/examples/js/renderers/SoftwareRenderer.js , if you cant find it search for the words described above in answer for example : fillRect it will take to all the possible matches and change the appropriate one that i had listed above... i reformatted pc – Careen Jun 23 '15 at 16:29
0

in the constructor add

var alpha = parameters.alpha;

replace

context.fillStyle = clearColor.getStyle();

with

context.fillStyle = alpha ? "rgba(0, 0, 0, 0)" : clearColor.getStyle();

in all 3 occurrences; Replace

data[ i + 3 ] = 255;

with

data[ i + 3 ] = alpha ? 0 : 255;

and

data[ poffset ++ ] = 255;

with

data[ poffset ++ ] = alpha ? 0 : 255;

View Demo - the third one from the top uses SoftwareRenderer

ecivic
  • 1