URP Lit shader渲染面上的问题,两者都不受光线影响。

huangapple go评论50阅读模式
英文:

URP Lit shader render face on both doesn't react to light

问题

我有一个URP Lit着色器的问题,我正在使用一个spotLight作为手电筒,它对材质的正面有效,但我无法让灯光与背面交互,这是我设置材质的方式:

英文:

I'm having a problem with URP Lit shader, I'm using a spotLight as a flashlight, it works for the front face of the material, but I can't get the lights to react with the back face, here is how I'm setting the material :

URP Lit shader渲染面上的问题,两者都不受光线影响。

答案1

得分: 1

默认情况下,Unity的光照系统不希望照亮背面,因为它们通常会被默认剔除。

您可以尝试在光照贴图设置中将背面容差设置为0,这可能有所帮助,但您可能需要编写自定义着色器来处理这种情况。

真正的问题是,为什么您需要渲染和照亮背面?如果您想要看到背面,建议简单地在您想要看到背面的对象上添加额外的正面,甚至可以翻转纹理,如果需要的话。在材质级别启用背面渲染可能会对游戏中的大量几何体产生相当大的性能影响。

英文:

By defualt unity's lighting system doesn't want to light up backfaces because they are usually culled by defualt.

You could try set the Backface Tolerance to 0 in your lightmap settings which may help, but you may need to write a custom shader to handle this use case.

The real question is why do you need to render and light backfaces? It would be likely be advisable to simply add additional front-faces to your objects you want to see the backfaces of, you can even flip the texture if this behaviour is desirable. Enabling backfaces on the material level can have some pretty significant performance impacts if its used on a lot of geometry thoughout your game.

答案2

得分: 1

通常情况下,在典型的基于物理的着色器中,背面不会被照亮,因为面法线指向光源的反方向,因此表面的光照计算结果为负值,而一个面法线不能同时指向两个方向。

选项1:如果您的用例类似于“仅纹理”的围栏或墙,带有一些孔(基本上是带有纹理的四边形),那么最简单的解决方案是为模型/对象的每一侧都启用背面剔除的附加四边形。

选项2:您还可以创建一个Shader Graph自定义着色器(使用PBR Master和“Is Front Face Node”),并使用if条件(分支)来检查照亮的面是正面还是背面,类似于这个关于Shader Graph的问题(由“cxode”回答):Shader Graph - Lighting Back Face。如果是背面,则面法线乘以-1(完全相反)。请注意,材质选项中Master节点的“Two Sided”值需要设置为true才能使此方法生效。

选项3:另一种方式(通常用于类似草四边形或广告牌的情况)是编写自定义着色器,从标准的光照着色器开始。在这里,您更改光照计算,使面法线被忽略,面的光照仅基于与光源的距离,但这会更加复杂。

英文:

Usually, a back face is not lit in a typical physical based shader as the face normal points away from the light source so that the result of the light calculation for the effect on the surface is below zero, and a face normal can not point in 2 directions at once.

Option 1: If your use case is something like a "texture only" fence or wall with some holes (basically a quad with a texture) then the easiest solution is to have additional quads for each side of the model/object with backface culling enabled.

Option 2: You could also create a Shader Graph custom shader (with the PBR Master and the "Is Front Face Node") and an if condition (branch) to check if the lit face is a front or back face, similar as in this question about Shader Graph (answer by "cxode"): Shader Graph - Lighting Back Face.
If it is a backface, the face normal is multiplied by -1 (exact opposite). Note that the Master node's "Two Sided" value in the Material options needs to be set to true for this to work.

Option 3: Another way (typically done for things like grass quads or billboards) is to write a custom shader, starting from a standard lit one. Here, you change the lighting calculation so that the face normal is ignored and the lighting of the face is based on the distance to the light source only, but this is a bit more complicated.

huangapple
  • 本文由 发表于 2023年4月20日 00:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定