The code work, but I’m having a problem setting transparent background to the canvas with three.js. I use:
JavaScript
x
2
1
Background.renderer.setClearColor(0xffffff, 0);
2
But then the background gets black. How do I change it to be transparent?
The code:
JavaScript
1
100
100
1
var camera, scene, renderer;
2
var mouseX = 0, mouseY = 0;
3
var p;
4
5
var windowHalfX = site.Width / 2;
6
var windowHalfY = site.Height / 2;
7
8
Background.camera = new THREE.PerspectiveCamera( 35, site.Width / site.Height, 1, 2000 );
9
Background.camera.position.z = 300;
10
//camera.position.y = 200;
11
12
// scene
13
Background.scene = new THREE.Scene();
14
15
// texture
16
var manager = new THREE.LoadingManager();
17
manager.onProgress = function ( item, loaded, total ) {
18
console.log('webgl, twice??');
19
//console.log( item, loaded, total );
20
};
21
22
23
// particles
24
var p_geom = new THREE.Geometry();
25
var p_material = new THREE.ParticleBasicMaterial({
26
color: 0xFFFFFF,
27
size: 1
28
});
29
30
// model
31
var loader = new THREE.OBJLoader( manager );
32
loader.load( site.base_url + '/assets/models/head.obj', function ( object ) {
33
34
object.traverse( function ( child ) {
35
36
if ( child instanceof THREE.Mesh ) {
37
38
// child.material.map = texture;
39
40
var scale = 6;
41
42
$(child.geometry.vertices).each(function() {
43
p_geom.vertices.push(new THREE.Vector3(this.x * scale, this.y * scale, this.z * scale));
44
})
45
}
46
});
47
48
Background.scene.add(p)
49
});
50
51
p = new THREE.ParticleSystem(
52
p_geom,
53
p_material
54
);
55
56
Background.renderer = new THREE.WebGLRenderer();
57
Background.renderer.setSize( site.Width, site.Height );
58
Background.renderer.setClearColor(0xffffff, 0);
59
60
$('.particlehead').append(Background.renderer.domElement);
61
$('#content').on('mousemove', onDocumentMouseMove);
62
site.window.on('resize', onWindowResize);
63
64
function onWindowResize() {
65
windowHalfX = site.Width / 2;
66
windowHalfY = site.Height / 2;
67
//console.log(windowHalfX);
68
69
Background.camera.aspect = site.Width / site.Height;
70
Background.camera.updateProjectionMatrix();
71
72
Background.renderer.setSize( site.Width, site.Height );
73
}
74
75
function onDocumentMouseMove( event ) {
76
mouseX = ( event.clientX - windowHalfX ) / 2;
77
mouseY = ( event.clientY - windowHalfY ) / 2;
78
//console.log(mouseX)
79
}
80
81
Background.animate = function() {
82
83
//console.log('animate2');
84
Background.ticker = TweenMax.ticker;
85
Background.ticker.addEventListener("tick", Background.animate);
86
87
render();
88
}
89
90
function render() {
91
Background.camera.position.x += ( (mouseX * .5) - Background.camera.position.x ) * .05;
92
Background.camera.position.y += ( -(mouseY * .5) - Background.camera.position.y ) * .05;
93
94
Background.camera.lookAt( Background.scene.position );
95
96
Background.renderer.render( Background.scene, Background.camera );
97
}
98
99
render();
100
Advertisement
Answer
If you want a transparent background in three.js, you need pass in the alpha
parameter to the WebGLRenderer
constructor.
JavaScript
1
2
1
var renderer = new THREE.WebGLRenderer( { alpha: true } );
2
You can leave the clear color at the default value.
JavaScript
1
2
1
renderer.setClearColor( 0x000000, 0 ); // the default
2
three.js r.71