简体   繁体   English

Threejs PlaneGeometry不会接收阴影或反射

[英]Threejs PlaneGeometry doesn't receive shadows or reflections

I'm pretty new to 3d and to threejs and I can't figure out how I can get a PlaneGeometry to show individually illuminated polygons ie receive shadows or show reflection. 我对3d和threejs很新,我无法弄清楚如何让PlaneGeometry显示单独照明的多边形,即接收阴影或显示反射。 What I basically do is taking a PlaneGeometry applying some noise to every z value of the vertices. 我基本上做的是使用PlaneGeometry将一些噪声应用到顶点的每个z值。 Then I have a simple directional light in my scene which is supposed to make the emerging noise pattern on the plane visible. 然后我在我的场景中有一个简单的定向光,它应该使平面上出现的噪声模式可见。 I tried different things like plane.castShadow = true or renderer.shadowMapEnabled = true without success. 我尝试了不同的东西,比如plane.castShadow = true或者renderer.shadowMapEnabled = true没有成功。 Am I just missing a simple option or is this way more complicated than I think? 我只是错过了一个简单的选项,还是比我想的更复杂?

Here's are the relevant pieces of my code 这是我的代码的相关部分

renderer.setSize(width, height);
renderer.setClearColor(0x111111, 1);

...

var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.9);
directionalLight.position.set(10, 2, 20);
directionalLight.castShadow = true;
directionalLight.shadowCameraVisible = true;

scene.add( directionalLight );


var geometry = new THREE.PlaneGeometry(20, 20, segments, segments);

var index = 0;
for(var i=0; i < segments + 1; i++) {
    for(var j=0; j < segments + 1; j++) {

        zOffset = simplex.noise2D(i * xNoiseScale, j * yNoiseScale) * 5;
        geometry.vertices[index].z = zOffset;

        index++;
    }
}

var material = new THREE.MeshLambertMaterial({
    side: THREE.DoubleSide,
    color: 0xf50066
});

var plane = new THREE.Mesh(geometry, material);
plane.rotation.x = -Math.PI / 2.35;
plane.castShadow = true;
plane.receiveShadow = true;
scene.add(plane);

This is the output I get. 这是我得到的输出。 Obviously the plane is aware of the light because the bottom side is darker than the upper side but there is no sign of any individual polygons receiving individual lightening and no 3d structure is visible. 显然,飞机知道光线,因为底部比上侧更暗,但没有任何单个多边形接收个别闪电的迹象,并且没有可见的3d结构。 Interestingly when I put in a different geometry like a BoxGeometry individual polygons are illuminated individually (see 2nd image). 有趣的是,当我放入像BoxGeometry这样的不同几何体时,单独的多边形被单独照亮(见第2张图像)。 Any ideas? 有任何想法吗?

在此输入图像描述

在此输入图像描述

Ok I figured it out thanks to this post . 好的,我想通过这篇文章了解它 The trick is to use the THREE.FlatShading shader on the material. 诀窍是在材质上使用THREE.FlatShading着色器。 Important to note is that after every update of the vertices two things need to be done. 需要注意的是,每次更新顶点后,都需要做两件事。 Before rendering geometry.normalsNeedUpdate must be set to true so the renderer also incorporates the newly oriented vertices. 在渲染geometry.normalsNeedUpdate之前,必须将其设置为true,以便渲染器还包含新定向的顶点。 Also geometry.computeFaceNormals() needs to be called before rendering because when you alter the vertices the normals are not the same anymore. 此外, geometry.computeFaceNormals()需要在渲染之前调用,因为当您更改顶点时,法线不再相同。

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

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