ThreeJS:当在另一个网格内部时,网格变得不可见

时间:2018-06-05 00:32:12

标签: javascript three.js 3d textures

我想弄清楚为什么我看不到立方体里面的文字,我不明白为什么它不起作用。

有两个网格,其中一个在另一个网格中,但是看不见。

当我评论将立方体网格添加到我的组时,显示文本(第34行)。 如果我从其材质中删除“transparent:true”,我会看到该文本,但会出现背景(第52行)。

文本被添加为画布纹理,这是我发现动态添加2d文本的最简单方法。

我只是想在我的立方体中添加一些白色文字而没有任何背景颜色。

我看到了这个问题,但看起来它与我的问题无关: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>

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是三个.js中的渲染分两个阶段进行。

第一个固体东西被渲染..然后是透明的东西。

透明物体按照与相机的距离进行分类。

通过设置你的.transparent = true;你在第二遍中渲染你的文字,然后在另一个几何体上渲染。

不屈不挠的West Langley看看这个答案:

Transparent objects in Threejs