I've been struggling for hours to understand why the text inside the cube is not visible, and I can't seem to figure out what's causing the issue.
There are two meshes, one nested inside the other, but it appears invisible.
If I comment out the line adding the cube mesh to my group, the text suddenly becomes visible (line 34). I am able to see the text if I remove the "transparent: true" from its material, but a background shows up instead (line 52).
The text is applied as a canvas texture, which seemed like the simplest way to dynamically add 2D text.
All I want is to insert some white text inside my cube without any background color showing through.
I came across this question, but it doesn't seem to be directly related to my current problem: THREE.JS: Seeing geometry when inside mesh
var renderer, scene, camera, cubeGroup;
var rotationX = 0;
var rotationY = 0;
var percentX, percentY;
var container = document.getElementById('container');
init();
animate();
function init(){
renderer = new THREE.WebGLRenderer({alpha: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor( 0x000000, 1);
document.getElementById('container').appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.set(0, 0, 1000);
scene.add(camera);
// group
cubeGroup = new THREE.Group();
// cube
var geometry = new THREE.BoxGeometry(200, 200, 200);
var material = new THREE.MeshPhongMaterial({
color: 0x11111f,
side: THREE.DoubleSide,
opacity: .5,
transparent: true
});
var mesh = new THREE.Mesh(geometry, material);
cubeGroup.add(mesh); // Comment this and the text appears with transparency
var ambientLight = new THREE.AmbientLight(0x999999, 0.8);
scene.add(ambientLight);
// text
var bitmap = document.createElement('canvas');
var g = bitmap.getContext('2d');
bitmap.width = 256;
bitmap.height = 256;
g.font = '80px Arial';
g.fillStyle = 'white';
g.fillText('text', 20, 80);
var geometry = new THREE.PlaneGeometry(180, 180);
var texture = new THREE.CanvasTexture(bitmap);
var material = new THREE.MeshStandardMaterial({
map: texture,
transparent: true // Comment this and the text appears - but with background
});
var mesh2 = new THREE.Mesh(geometry, material);
cubeGroup.add(mesh2);
// add group to scene
scene.add(cubeGroup);
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 animate(now){
requestAnimationFrame(animate);
cubeGroup.rotation.y += (rotationX - cubeGroup.rotation.y) * 0.05;
cubeGroup.rotation.x += (rotationY - cubeGroup.rotation.x) * 0.05;
renderer.render(scene, camera);
}
function findViewCoords(mouseEvent)
{
var xpos;
var ypos;
var w = window.innerWidth;
var h = window.innerHeight;
var centerX = w/2;
var centerY = h/2;
if (mouseEvent)
{
xpos = mouseEvent.pageX - document.body.scrollLeft;
ypos = mouseEvent.pageY - document.body.scrollTop;
}
else
{
xpos = window.event.x + 2;
ypos = window.event.y + 2;
}
var diffX = xpos - centerX;
var diffY = ypos - centerY;
percentX = diffX / centerX;
percentY = diffY / centerY;
rotationX = percentX/2;
rotationY = percentY/2;
}
container.addEventListener("mousemove", findViewCoords);
body {
margin: 0;
padding: 0;
overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.js"></script>
<div id="container"></div>