简体   繁体   English

即使属性设置为true,在Three.js中加载的OBJ也不接收阴影

[英]Loaded OBJ in Three.js doesn't receive shadows even though the property is set to true

I'm currently diving into Three.js and came across my first real issue when switching from native primitives to imported OBJ objects. 我目前正在进入Three.js,并在从本机基元切换到导入的OBJ对象时遇到了我的第一个实际问题。

I have a simple model of a dodecahedron that I UV-mapped in Cinema4D r15 and exported as OBJ file. 我有一个十二面体的简单模型,该模型在Cinema4D r15中进行了UV映射并导出为OBJ文件。 When using this model over a DodecahedronGeometry, the geometry is not lit by my directional light anymore, but it is if I use the primitive. 在DodecahedronGeometry上使用此模型时,该几何不再由我的定向光照亮,但是如果我使用图元,则是这样。

See this JSFiddle: https://jsfiddle.net/xm3ttmxw/1/ 看到这个JSFiddle: https ://jsfiddle.net/xm3ttmxw/1/

See desired result here (using a primitive): https://jsfiddle.net/84hbs7ed/1/ 在此处(使用基元)查看所需结果: https : //jsfiddle.net/84hbs7ed/1/

As you can see, I'm setting the receiveShadow property to true for all meshes in the OBJ. 如您所见,我将OBJ中的所有网格物体的receiveShadow属性设置为true I activated shadow maps for the light and renderer. 我激活了灯光和渲染器的阴影贴图。 The directional light is following the camera and pointing towards the origin (center of the dodecahedron). 定向光跟随摄像机并指向原点(十二面体的中心)。 The ambient light seems to work fine. 环境光似乎工作正常。

Any idea? 任何想法? Thanks in advance 提前致谢

After some more research, it appears that the problem comes from a lack of vertex normals (OBJLoader doesn't compute those). 经过更多研究后,问题似乎出在缺少顶点法线(OBJLoader不会计算那些顶点法线)。 The solution is to compute the vertex normals on the fly like shown here: https://stackoverflow.com/a/28568522/3446439 解决方案是快速计算顶点法线,如下所示: https : //stackoverflow.com/a/28568522/3446439

object.traverse( function( child ) {
    if ( child instanceof THREE.Mesh ) {
        child.geometry.computeVertexNormals(); //add this
    }
} );

Thanks Sebastian Baltes! 感谢Sebastian Baltes!

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

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