gpt4 book ai didi

javascript - 三JS : Mesh becomes invisible when inside another mesh

转载 作者:太空宇宙 更新时间:2023-11-04 15:25:22 27 4
gpt4 key购买 nike

我花了几个小时试图弄清楚为什么我看不到立方体内的文本,而且我不明白为什么它不起作用。

有两个网格,其中一个在另一个网格内部,但不可见。

当我注释将立方体网格添加到我的组中的行时,会出现文本(第 34 行)。如果我从其 Material 中删除“transparent: true”,我可以看到文本,但会出现背景(第 52 行)。

文本作为 Canvas 纹理添加,这是我发现动态添加二维文本的最简单方法。

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

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

最佳答案

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

首先渲染固体的东西..然后是透明的东西。

透明物体按照距相机的距离排序。

因此,通过设置 .transparent = true;您要让文本在第二遍期间渲染,从而渲染在其他几何体之上。

查看不屈不挠的 West Langley 的回答:

Transparent objects in Threejs

关于javascript - 三JS : Mesh becomes invisible when inside another mesh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50690473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com