简体   繁体   English

THREE.js使透明对象在后台具有其他对象

[英]THREE.js make transparent objects that have other objects in background

Hy, HY,

I want to make a room in three.js and I want the walls that have objects behind them (from the pov of the camera) to become transparent (.5 opacity) as I rotate the room. 我想在three.js中创建一个房间,并且我希望在旋转房间时,在它们后面的对象(从摄像机的视点开始)的墙壁变得透明(.5不透明)。

To clarify a little bit: 为了澄清一点:

Imagine you have a room. 假设您有一个房间。 That room has walls. 那个房间有墙。 In that room you insert furniture. 在那个房间里插入家具。 Camera looks at the room and I want the walls to be transparent only if from the pov of the camera they have other objects behind (so you can see throw walls the room). 摄像头注视着房间,我希望墙壁只有在从摄像头的角度来看后面还有其他物体时才是透明的(这样您才能看到向房间扔墙壁)。 The walls in the back should have opacity 1. So anywhere you move the camera (and look at the room) you can see all the elements (otherwise some walls will block the view) 背面的墙壁应具有不透明度1。因此,在任何地方移动摄像机(并看着房间)都可以看到所有元素(否则,某些墙壁会挡住视图) 在此处输入图片说明

You don't provide a lot of detail with regards to how you are moving the camera. 您没有提供有关如何移动相机的大量细节。 But it can be done fairly easily. 但这可以很容易地完成。 All meshes have a material property that has an opacity. 所有网格物体都具有不透明的材质属性。

Here is a jsFiddle - http://jsfiddle.net/Komsomol/xu2mjwdk/ 这是一个jsFiddle- http://jsfiddle.net/Komsomol/xu2mjwdk/

I added the entire OrbitControls.js inside and added a boolean; 我在其中添加了整个OrbitControls.js并添加了一个布尔值;

var doneMoving = false;

Which I added in the mouseup and mousedown of the OrbitControls. 我在OrbitControls的mouseup和mousedown中添加了它。 Just to capture when we are not moving the camera. 只是为了在我们不移动相机时进行捕捉。

There are some specific options that need to be added in the renderer and the object. 在渲染器和对象中需要添加一些特定的选项。

renderer = new THREE.WebGLRenderer({
    alpha:true,
    transparent: true
});

The object 物体

torusMat = new THREE.MeshPhongMaterial();
torusMat.needsUpdate = true;
torusMat.transparent = true;

And finally add some control code in the Animate method to kick off whatever changes you want. 最后,在Animate方法中添加一些控制代码以启动您想要的任何更改。

if(doneMoving){
   torusMat.opacity = 0.5;
} else {
    torusMat.opacity = 1;
}

That's about it. 就是这样 This should give you enough of an idea how to implement this. 这应该给您足够的想法如何实现此目的。

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

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