I’m trying to put objects in front of CSS3DObjects with the THREE.NoBlending hack. But I only see the black plane without the CSS3DObject in the newest revisions (tried r65 and r66). A small example I made looks like this:
index.html:
JavaScript
x
69
69
1
<!doctype html>
2
<script src="lib/three58.js"></script>
3
<script src="lib/CSS3dRenderer.js"></script>
4
<body>
5
<script>
6
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 555);
7
camera.position.z += 20;
8
9
var rendererCSS = new THREE.CSS3DRenderer();
10
rendererCSS.setSize(window.innerWidth, window.innerHeight);
11
rendererCSS.domElement.style.position = 'absolute';
12
document.body.appendChild(rendererCSS.domElement);
13
14
var rendererMain = new THREE.WebGLRenderer();
15
rendererMain.setSize(window.innerWidth, window.innerHeight);
16
rendererMain.setClearColor("white", 0);
17
rendererMain.domElement.style.position = 'absolute';
18
document.body.appendChild(rendererMain.domElement);
19
20
var scene = new THREE.Scene();
21
var sceneCSS = new THREE.Scene();
22
23
var planeWidth = 15;
24
var planeHeight = 15;
25
26
var planeMaterial = new THREE.MeshBasicMaterial({
27
blending: THREE.NoBlending,
28
opacity : 0.0,
29
transparent : true,
30
color : 0x000000,
31
side : THREE.DoubleSide
32
});
33
var planeGeometry = new THREE.PlaneGeometry(planeWidth, planeHeight);
34
var planeMesh = new THREE.Mesh(planeGeometry, planeMaterial);
35
scene.add(planeMesh);
36
37
var element = document.createElement('iframe')
38
var aspectRatio = planeHeight / planeWidth;
39
var elementWidth = 1024;
40
var elementHeight = elementWidth * aspectRatio;
41
element.src = 'moo.html';
42
element.style.width = elementWidth + "px";
43
element.style.height = elementHeight + "px";
44
45
var objectCSS = new THREE.CSS3DObject(element);
46
objectCSS.scale.x = planeWidth / elementWidth;
47
objectCSS.scale.y = planeHeight / elementHeight;
48
objectCSS.position = planeMesh.position;
49
objectCSS.rotation = planeMesh.rotation;
50
sceneCSS.add(objectCSS);
51
52
var material = new THREE.MeshBasicMaterial({color: 0xff0000});
53
var mesh = new THREE.Mesh(new THREE.SphereGeometry(5.0, 20.0, 20.0), material);
54
var mesh2 = new THREE.Mesh(new THREE.SphereGeometry(5.0, 20.0, 20.0), material);
55
mesh.position.set(5, 0, -1);
56
mesh2.position.set(-8, 0, -6);
57
scene.add(mesh); // in front
58
scene.add(mesh2); // behind
59
60
animate();
61
62
function animate() {
63
requestAnimationFrame(animate);
64
rendererMain.render(scene, camera);
65
rendererCSS.render(sceneCSS, camera);
66
}
67
</script>
68
</body>
69
moo.html:
JavaScript
1
7
1
<!DOCTYPE html>
2
<html>
3
<body style="background-color: purple">
4
<p align="center"><font color="lime" size="150">hello world</font> </p>
5
</body>
6
</html>
7
This is the result:
Revision 58: http://i.imgur.com/tCszJ8X.jpg
Revision 66: http://i.imgur.com/kfppVwF.jpg
I’ve tried to understand why, it’s probably something very simple. I can’t find anything in migration that would change the behavior in this way.
Thanks in advance.
Advertisement
Answer
If you want a transparent background with WebGLRenderer
, you need to set alpha = true
in the WebGLRenderer
constructor. You can then set the clear color.
JavaScript
1
4
1
var renderer = new THREE.WebGLRenderer( { alpha: true } );
2
3
renderer.setClearColor( 0x00ff00, 0.5 );
4
three.js r.66