Unity shader中的法线详解
来源:互联网 发布:天蝎网络第三季百度云 编辑:程序博客网 时间:2024/04/24 19:48
一次写shader的时候,根据法线调整视觉效果,却发现坐标不同的情况下,会有颜色突变的情况。不解。故寻找计算法线部分的知识。
首先看一下大神文章了解下大概:http://blog.csdn.net/candycat1992/article/details/41605257
1.
is the transformation from object coordinates to world coordinates.
- _Object2World
is the transformation from world coordinates to object coordinates.
- _World2Object * unity_Scale.w
Unity sometimes (for non-uniform scale factors) scales the vertex coordinates before handing them to the shader. (Supposedly such that _Object2World is an orthogonal matrix.) For uniform scale factors, however, it doesn't do this vertex scaling but integrates the scaling into _Object2World (making it a scaled orthogonal matrix). However, for some strange reason (and this is probably an inconsistency that just never got fixed and now too much code relies on it) this scaling was never included in _World2Object. Thus, you have to scale _World2Object with unity_Scale.w (if scale is important) but _Object2World is already scaled with the reciprocal factor.
No the space is the same, but in a general case (not unity) a non orthogonal matrix doesn't transform normals correctly.
If you want to perform a correct normal transformation in the general case you need to use theinverse transpose matrix. (In unity shaders this is equivalent tomul(normal,_World2Object)
.
_Object2World works like a charm :(mul(_Object2World, normal)). both rotation and scaling (uniformly) gave the expected matrix through color checking elements in rows and columns
This is a bit tricky to explain (I hope to remember it correctly).You can assumescale is always uniform becauseUnity
(at least until 4.x version. I think it will change in 5.x) doesn't apply non-uniform scale in the vertex shader but pre-transform the mesh CPU side.
Following uniforms are provided to shaders:
_Object2World
: contains the world matrix including the scaleunity_Scale
: the w component contains the inverse of the uniform scale factor (w = 1/scale
)_World2Object
: contains the inverse world matrix without scale
In order to correctly transform the normal from object to world space, you have 3 possibility:
- transform the scaled normal :
float3 worldN = mul((float3x3)_Object2World, SCALED_NORMAL);
- you can avoid to use the scaled normal if you normalize the normal after the transform (probably this is your case otherwise AFAIK the transformation shouldn't be 100% correct)
- use the inverse transpose :
mul(normal,_World2Object)
SCALED_NORMAL
is defined this way:
#define SCALED_NORMAL (v.normal * unity_Scale.w)
这个算是一个小知识点,虽然和法线本身无关,但却对写shader有所帮助。
2.o.normal本身是模型坐标系的,通过转化成世界坐标系,可以进行正确的插值。
- Unity shader中的法线详解
- Unity Shader 什么是法线贴图
- Unity Shader中法线变换
- Unity Shader 在Shader中使用法线贴图
- Shader以及Unity中的Shader
- Unity编写添加法线贴图的Shader - 二
- Unity 法线贴图、高光贴图、Cube Map shader
- Unity Shader-法线贴图(Normal)及其原理
- Unity Shader-法线贴图(Normal)及其原理
- Unity Shader入门精要笔记(十三):法线纹理
- Unity Shader-法线贴图(Normal)及其原理
- Unity Shader 学习笔记(24) 深度纹理、法线纹理
- Unity Shader 学习笔记 (七) 根据切线和法线方向设置模型颜色shader
- <Shader>Unity Shader 中的 alpha测试
- Unity Shader:Unity网格(1)---顶点,三角形朝向,法线,uv,以及双面渲染三角形
- 聚光灯+法线贴图 shader
- 聚光灯+法线贴图 shader
- 【Unity Shaders】Shader中的光照
- 并行计算-学习笔记1
- java过滤器---一异步处理
- 用Adobe Reader的“朗读”功能实现“少林修女GPS”语音提示
- 黑马程序员日记-14
- OO技术三大特点
- Unity shader中的法线详解
- 黑马程序员日记-15
- 单链表的链式表示和实现(含代码)
- map 遍历第二种方法 MapEntry
- jsp语法
- Android之ListView长按列表项弹出菜单 给菜单项添加事件 获取上下文
- Oracle监听器启动出错:OracleOraDb10g home1TNSListener服务启动后又停止
- 利用ssh传输文件
- 设计模式之装饰设计模式(一)类别