渲染透明外壳

IT技术 javascript three.js
2021-03-05 14:27:16

假设我正在渲染神奇女侠驾驶她的隐形喷气机。射流由多个网格组成,并且大部分是透明的。在透明网格重叠的地方,它变得更加不透明。我不想有那种重叠,这样透明部分仍然是阴影,但材料丢弃了其他碎片后面的透明碎片,就好像神奇女侠坐在透明壳内一样。

也许一个好方法是我想渲染像不透明网格这样的透明网格,但仍然允许它们是透明的。

举个例子,这是一个普通女人驾驶着她最常见的汽车:http : //jsfiddle.net/TheJim01/e6ccfo24/

我做了深度测试和写作的不同排列,并尝试了所有各种深度测试功能。我还尝试了不同的混合设置(我看到了一些关于 的建议THREE.NoBlending,但这不是我想要的,因为我失去了透明度)。如果我必须为此编写自定义着色器,我会走那条路,但我不确定从哪里开始。

HTML:

<script>
// INITIALIZE
var WIDTH = window.innerWidth,
    HEIGHT = window.innerHeight,
    FOV = 35,
    NEAR = 1,
    FAR = 1000;

var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(WIDTH, HEIGHT);
document.getElementById('host').appendChild(renderer.domElement);

var stats= new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0';
document.body.appendChild(stats.domElement);


var camera = new THREE.PerspectiveCamera(FOV, WIDTH / HEIGHT, NEAR, FAR);
camera.position.z = 250;

var trackballControl = new THREE.TrackballControls(camera, renderer.domElement);
trackballControl.rotateSpeed = 5.0; // need to speed it up a little

var scene = new THREE.Scene();

var light = new THREE.PointLight(0xffffff, 1, Infinity);
light.position.copy(camera.position);

scene.add(light);

function draw(){
    light.position.copy(camera.position);
    renderer.render(scene, camera);
  stats.update();
}
trackballControl.addEventListener('change', draw);

function navStartHandler(e) {
  renderer.domElement.addEventListener('mousemove', navMoveHandler);
  renderer.domElement.addEventListener('mouseup', navEndHandler);
}
function navMoveHandler(e) {
  trackballControl.update();
}
function navEndHandler(e) {
  renderer.domElement.removeEventListener('mousemove', navMoveHandler);
  renderer.domElement.removeEventListener('mouseup', navEndHandler);
}

renderer.domElement.addEventListener('mousedown', navStartHandler);
renderer.domElement.addEventListener('mousewheel', navMoveHandler);
</script>

CSS:

html *{
    padding: 0;
    margin: 0;
    width: 100%;
    overflow: hidden;
}

#host {
    width: 100%;
    height: 100%;
}

JavaScript:

// NOTE: To run this demo, you MUST use the HTTP protocol, not HTTPS!

var msh = new THREE.Mesh(new THREE.SphereGeometry(10, 20, 20), new THREE.MeshLambertMaterial({color: "red"}));
msh.position.set(0, 10, 0);
scene.add(msh);

var mat = new THREE.MeshLambertMaterial({
    color: "silver",
  transparent: true,
  opacity: 0.5
});

msh = new THREE.Mesh(new THREE.CylinderGeometry(15, 15, 75, 20), mat);
msh.rotation.set(0, 0, Math.PI / 2);
msh.position.set(0,10,0);
scene.add(msh);

msh = new THREE.Mesh(new THREE.CylinderGeometry(15, 15, 50, 20), mat);
msh.rotation.set(Math.PI / 2, 0, 0);
msh.position.set(-20,-10,0);
scene.add(msh);

msh = new THREE.Mesh(new THREE.CylinderGeometry(15, 15, 50, 20), mat);
msh.rotation.set(Math.PI / 2, 0, 0);
msh.position.set(20,-10,0);
scene.add(msh);

draw();
1个回答

如果你想渲染重叠的透明面,但不希望重叠区域变暗,你可以实现一个技巧:首先用

material.colorWrite = false;

然后第二次渲染它们

material.colorWrite = true;

小提琴:http : //jsfiddle.net/yrfuy4j8/

你的小提琴:http : //jsfiddle.net/5eku8k37/

三.js r.76

澄清一下,这个技巧不依赖于您将对象/组添加到场景中的顺序。只要具有 material.colorWrite = false 的对象与其真正的对应物一起出现在场景中,它就会起作用。(在两个小提琴中都进行了测试。)
2021-04-22 14:27:16