简体   繁体   English

three.js透明对象遮挡

[英]three.js transparent object occlusion

In a three.js scene, I would like to have an object that's not visible, but still occludes other objects in the scene as if it was visible. 在一个three.js场景中,我希望有一个不可见的对象,但仍会遮挡场景中的其他对象,就好像它是可见的一样。

Is this possible with the three.js library? 这可能与three.js库有关吗? Here is an example: 这是一个例子:

Suppose I have a three.js scene that contains 3 objects: object a, object b and object c and a camera. 假设我有一个包含3个对象的three.js场景:对象a,对象b和对象c以及摄像机。 I would like object c to be invisible to the camera, but still occlude object b... Scenario 1: 我希望对象c对相机不可见,但仍会遮挡对象b ...场景1: 方案1概述 In scenario 1, here is what I would like the camera to see: 在方案1中,这是我希望相机看到的内容: 场景1摄像机视图

Scenario 2: 场景2: 在此输入图像描述 In scenario 2, here is what I would like the camera to see: 在方案2中,以下是我希望相机看到的内容: 在此输入图像描述

Can anyone tell suggest a technique use to achieve such an effect? 任何人都可以告诉建议使用技术来达到这样的效果吗?

Yes, in three.js you can create an object that is invisible, but still occludes other objects as if it were visible. 是的,在three.js中,您可以创建一个不可见的对象,但仍然会遮挡其他对象,就好像它是可见的一样。

To do that, you need to use two new features available in three.js r.71: Object3D.renderOrder and Material.colorWrite . 为此,您需要使用three.js r.71中提供的两个新功能: Object3D.renderOrderMaterial.colorWrite

You need to make sure the invisible object is rendered prior to the object(s) it must occlude. 您需要确保不可见对象在它必须遮挡的对象之前呈现。

You control the rendering order with the renderOrder property. 您可以使用renderOrder属性控制渲染顺序。

You make the occluding object invisible by setting its material's colorWrite property to false . 通过将其材质的colorWrite属性设置为false可以使遮挡对象不可见。

// material
var material = new THREE.MeshPhongMaterial();

// mesh a
var geometry = new THREE.PlaneGeometry( 10, 10, 4, 4 );
mesh = new THREE.Mesh( geometry, material.clone() );
mesh.material.color.set( 0xff0000 );
mesh.renderOrder = 0; // <=================== new
mesh.position.z = - 10;
scene.add( mesh );

// mesh b
var geometry = new THREE.BoxGeometry( 2, 2, 2 );
mesh = new THREE.Mesh( geometry, material.clone() );
mesh.material.color.set( 0x606060 );
mesh.renderOrder = 3;
mesh.position.z = 0;
scene.add( mesh );

// mesh c
var geometry = new THREE.BoxGeometry( 3, 3, 3 );
mesh = new THREE.Mesh( geometry, material.clone() );
mesh.material.color.set( 0x0000ff );
mesh.material.colorWrite = false; // <================= new
mesh.renderOrder = 2;
mesh.position.z = 10;
scene.add( mesh );

fiddle: http://jsfiddle.net/4vnsbdz6/1/ 小提琴: http//jsfiddle.net/4vnsbdz6/1/

another fiddle: http://jsfiddle.net/4vnsbdz6/4/ 另一个小提琴: http//jsfiddle.net/4vnsbdz6/4/

three.js r.71 three.js r.71

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

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