最近用Mitsuba2渲染,踩了一个坑。
本来是渲染一个形状不断变化的物体(.obj),结果忘记了在每一步循环时更新顶点法向,造成物体虽然形状改变了(.obj文件中 v 的坐标变了),但法向还是初始值(.obj文件中 vn 的坐标没变)。造成渲染的结果有黑边,如下图所示。
一开始以为是渲染的代码问题,查了一通,没找到问题。甚至给Mitsuba2的github提了issue询问解决办法。人家的回复一针见血:
“This looks like bad geometry, maybe you should fix your model first.
You can also use the twosided
BSDF so that those "interior" regions flip the shading normals. This will likely get rid of those black artefacts.”
bad geometry。后来一检查还就是法向的问题。
在 shape 中加上:
<boolean name="face_normals" value="true"/>
官方文档是这么说的:“When set to true, any existing or computed vertex normals are discarded and face normals will instead be used during rendering. This gives the rendered object a faceted appearance. (Default: false)”
得到的渲染结果没有了黑边:
但因为用的是面法向,三角形也更明显了。因此,还是得每步更新顶点法向。
更新顶点法向后发现 仍有 黑边 问题,最后通过将mesh 细分(subdivision)(可提升模型精度,提高渲染效果)解决。