three.js SoftwareRenderer - transparent background

时间:2015-05-04 19:40:51

标签: javascript three.js background-image alpha alpha-transparency

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.

2 个答案:

答案 0 :(得分:0)

您需要设置:

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

我有它为你工作, 您需要编辑渲染器/ SoftwareRenderer.js 替换为:

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

stops for the bg to be rendered

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

像重新填充块一样停止......

使用场景和飞机测试..和css html图像背景.. 我在地球上使用了这个http://threejs.org/examples/#software_geometry_earth并将其简化为快速测试

这是非静态场景

enter image description here

答案 1 :(得分:0)

在构造函数中添加

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

替换

data[ i + 3 ] = 255;

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

在所有3次出现中;替换

data[ poffset ++ ] = 255;

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

var gulp = require('gulp');

var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();

gulp.task('sass', function(){
    return gulp.src('app/scss/style.scss')
    .pipe(sass())
    .pipe(gulp.dest('app/css'))
    .pipe(browserSync.reload({
         stream: true
     }));
});

gulp.task('watch', ['browserSync', 'sass'], function (){
    gulp.watch('app/scss/**/*.scss', ['sass']);
    // Reloads the browser whenever HTML or JS files change
    gulp.watch('app/*.html', browserSync.reload());
    gulp.watch('app/js/**/*.js', browserSync.reload());
});

gulp.task('browserSync', function(){
    browserSync.init({
        server: {
            baseDir: 'app'
        }
    });
});

m_requests::where('id', '=', Auth::user()->id)
            ->where('created_at', 'LIKE', '%'.date("Y-m-d").'%');

View Demo - the third one from the top uses SoftwareRenderer