简体   繁体   English

three.js-绘制两个重叠的透明球体并隐藏交叉点

[英]three.js - drawing two overlapping transparent spheres and hiding intersection

I have two transparent, overlapping spheres in a webgl context. 我在webgl上下文中有两个透明的重叠球体。 During overlap, I would like to control how the spheres are drawn. 在重叠期间,我想控制如何绘制球体。 You can see my problem at this page: 您可以在此页面上看到我的问题:

http://andrewray.me/stuff/circle-clip.html http://andrewray.me/stuff/circle-clip.html

When the red and blue sphere overlap, I would like for the blue sphere to be hidden behind the red sphere. 当红色和蓝色球体重叠时,我希望将蓝色球体隐藏在红色球体后面。

If I simply change the order in which the spheres are added to the scene (if I add the big sphere first ), I get the desired behavior : http://andrewray.me/stuff/circle-clip-correct.html 如果我只是简单地更改将球体添加到场景中的顺序(如果我先添加大球体 ),则会得到所需的行为http : //andrewray.me/stuff/circle-clip-correct.html

However, things are different in my project. 但是,在我的项目中情况有所不同。 I see the intersection even though I add the big sphere first. 即使先添加大球体,我也能看到交点。

The closest I have gotten is moving the smaller spheres closer to the camera. 我最近得到的是将较小的球体移近相机。 in this example, I have moved the smaller spheres 10 units closer : http://andrewray.me/stuff/b-test/ 在此示例中,我将较小的球体移近了10个单位http : //andrewray.me/stuff/b-test/

However, if you move around with the arrow keys, you can see the intersection flash every now and then, and sometimes flat out rudely stay visible (especially with bubbles coming in from the side) 但是,如果您使用箭头键来回走动,您会时不时地看到交叉路口闪烁,有时会平缓地保持平整,以保持可见状态(尤其是气泡从侧面进入)

I have tried setting depwthWrite: false on both materials but that doesn't have any effect. 我尝试在两种材质上设置depwthWrite: false ,但这没有任何效果。

I have messed a bit with renderer.sortObjects = false but I think this is removing the "natural" draw order that I want to occur with the rest of the scene. 我对renderer.sortObjects = false感到有些困惑,但是我认为这消除了我希望在场景其余部分中出现的“自然”绘制顺序。 It is just these spheres I am at issue with. 我只是在讨论这些领域。

  1. Can I force the draw order of transparent objects in webgl / three.js? 我可以在webgl / three.js中强制透明对象的绘制顺序吗? It seems like they are z-fighting even when moving the smaller spheres 10 units closer 即使将较小的球体移近10个单位,它们似乎仍在进行Z形战斗
  2. or is there any way to force the behavior of overlapping transparent objects? 或者有什么方法可以强制重叠透明对象的行为?

After much head banging (not the good kind) I have discovered that you can force draw order with a flag on the mesh. 经过很多次头部撞击(不是很好的撞击)后,我发现可以用网格上的标志强制绘制顺序。

For Three r70 and above use renderOrder 对于三个r70及更高版本,请使用renderOrder

mesh.renderOrder = 0.5; // Value from 0 to 1

For Three r69 and below use renderDepth 对于三个r69及以下版本,请使用renderDepth

mesh.renderDepth = 0.5; // Value from 0 to 1

It draws lowest first, highest last. 它绘制最低的第一,最高的最后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM