Skip to content

Commit

Permalink
移动设备着色器适配
Browse files Browse the repository at this point in the history
  • Loading branch information
linkliu committed Mar 21, 2023
1 parent a7ecb0e commit 3fa02bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion _posts/shader_book/2023-02-26-移动设备着色器适配.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,30 @@ tags: [U3D, Shader,Cookbook,中文版]
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_NormalMap));
}
```
```
现在我们优化了我们的变量,接下来我们将利用Unity内建的 **光线函数变量(lighting function variable)**,这样我们就可以控制光线该如何被着色器处理。通过这样做,我们极大的减少着色器需要处理的光线数量。按照下面的代码修改着色器中的 **#pragma** 声明:
`#pragma surface surf SimpleLambert noforwardadd`
<br>
我们之后还会通过让法线贴图和漫反射纹理共享UV来继续优化着色器。为了这个优化,我们简单的通过在 **UnpackNormal()** 函数中用 **_MainTex** 的UV替换掉 **_NormalMap** 的UV,从而修改着色器的 **UV查找(UV lookup)**
```c#
void surf (Input IN, inout SurfaceOutput o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
}
```
4. 由于我们已经不再需要法线贴图的UV了,所以我们需要确保在 **输入结构体(Input struct)** 中移除掉跟法线贴图UV相关的代码:
``` c#
struct Input
{
half2 uv_MainTex;
};
```
5. 最后,我们通过告诉着色器只需要工作在一些特定的渲染器中从而进一步优化我们的着色器:
`#pragma surface surf SimpleLambert exclude_path:prepass noforwardadd`
<br>
优化的最终结果显示我们几乎注意不到视效质量上的不同,但我们却减少了这个着色器在屏幕上绘制的次数。我们在下一个知识点将会学习如何确定着色器的绘制次数,这里我们关心的是我们通过更少的数据消耗而取得了相同的效果。所以当你在创建自己的着色器的时候也要留意这一点。下图向我们展示了我们的着色器的最终效果:
![diagram](https://linkliu.github.io/game-tech-post/assets/img/shader_book/diagram74.png){: .shadow width = "90%" }
Binary file added assets/img/shader_book/diagram74.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3fa02bb

Please sign in to comment.