-
Notifications
You must be signed in to change notification settings - Fork 1.7k
saturate和max #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
对于那些值范围在-1到1的变量,两个方法在计算结果上没有区别。不过,saturate在Unity里通常会编译成两个指令,大致是min(max(val, 0.0), 1.0),如果自己知道值的范围可以直接使用max来节省一条运算指令。 |
好的谢谢,还有一个问题,在求tangentNormal.z的时候dot(tangentNormal.xy,tangentNormal.xy) 这个操作不是很理解 |
向量自己dot自己就是对自己取平方操作,因为tangentNormal是单位向量,所以根据xy分量来求z分量。 |
哦哦好的,十分感谢 |
我在Unity里面,点Shader属性中 “Compile and show code”按钮,看到
对应的是 因为今天看到《Real-time Rendering 3rd》5.5节 115页 中提到
所以有点奇怪。 |
@alasja 我之前的回答的确说得很模糊,这里补充下。 在Unity里,同一份ShaderLab代码在不同目标平台上、甚至不同设备上编译结果都是不同的。比如同样一份fragment shader:
在DX9下,编译出来就是:
DX11下就是:
OpenGL ES2.0下是:
OpenGLES 3.0下是:
Metal下是:
Vulkan太长了我就不粘贴了…… 你可以看出来在一些平台下,比如DX9、DX11下,编译出来的确是使用真正的saturate运算操作,而在大多数移动平台上,比如ES 2.0、Metal等,是编译成了clamp,而在ES 3.0下跟设备还有关系。至于为什么要分这么多种情况,Unity应该是根据各个平台每种操作耗时的平衡,选择同等运算结果下最优的等价操作来代替saturate。 |
在基础光照那一章中,我看到计算worldNormal与worldLightDir的时候用的是saturate,而后面章节使用的是max(0,…),这两个方法在计算结果上会产生什么差异吗?
The text was updated successfully, but these errors were encountered: