Unity3D Shader官方教程翻译(十九)----Shader语法,编写表面着色器
Writing Surface Shaders
Writing shaders that interact with lighting is complex. There are different light types, different shadow options, different rendering paths (forward and deferred rendering), and the shader should somehow handle all that complexity.
编写光照交互的着色器是十分复杂的事情。它们有不同类型的光照,不同的阴影选项,不同的渲染路径(正向渲染和延时渲染),着色器应该以某种合适的方式处理这些的复杂的东西。
Surface Shaders in Unity is a code generation approach that makes it much easier to write lit shaders than using low level vertex/pixel shader programs. Note that there is no custom languages, magic or ninjas involved in Surface Shaders; it just generates all the repetitive code that would have to be written by hand. You still write shader code in Cg / HLSL.
Unity3D的表面着色器编写的办法只有通过编写代码产生,这样就使得编写光照着色器比使用低级顶点/像素着色器编程更简单。请注意编写表面着色器没有任何自定义的语言和捷径。它仅仅是通过手动书写来产生一些重复的代码。
你依然可以用CG/HLSL语言来编写它。
For some examples, take a look at Surface Shader Examples and Surface Shader Custom Lighting Examples.
这有一些例子,你可以看看:表面着色器 和 自定义照明的表面着色器。
How it works 工作原理
You define a "surface function" that takes any UVs or data you need as input, and fills in output structureSurfaceOutput. SurfaceOutput basically describes properties of the surface (it's albedo color, normal, emission, specularity etc.). You write this code in Cg / HLSL.
你定义一个“surface function”,将UV或数据作为输入参数,并填充SurfaceOutput作为输出结构体。SurfaceOutput描述了基础的表面属性(光照的颜色反射率、法线、自发光、镜面 等)。你可以用Cg/HLSL编写它。
Surface Shader compiler then figures out what inputs are needed, what outputs are filled and so on, and generates actual vertex&pixel shaders, as well as rendering passes to handle forward and deferred rendering.
表面着色器的编译器,指出需要输入什么,在输出中填充什么等等,并产生真实的顶点&像素着色器,并且处理渲染路径为正向渲染或延时渲染。
Standard output structure of surface shaders is this: 标准的表面着色器输出结构体如下:
struct SurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half Specular;
half Gloss;
half Alpha;
};
Samples 例子
See Surface Shader Examples and Surface Shader Custom Lighting Examples pages.
参考 Surface Shader Examples 和 Surface Shader Custom Lighting Examples 。
Surface Shader compile directives 表面着色器编译指令
Surface shader is placed inside CGPROGRAM..ENDCG block, just like any other shader. The differences are:
表面着色器必须放在CGPROGRAM..ENDCG块中,就像其他着色器一样。区别在于:
It must be placed inside SubShader block, not inside Pass. Surface shader will compile into multiple passes itself.
它必须放在SubShader块中,不能放在Pass中。表面着色器将在多个通道内编译自己。
It uses #pragma surface ... directive to indicate it's a surface shader.
它使用#pragma surface 指令来表明自己是表明着色器。
The #pragma surface directive is: #pragma surface指令使用办法:
#pragma surface surfaceFunction lightModel [optionalparams]
Required parameters: 必须的参数
surfaceFunction - which Cg function has surface shader code. The function should have the form of void surf (Input IN, inout SurfaceOutput o), where Input is a structure you have defined. Input should contain any texture coordinates and extra automatic variables needed by surface function.表明哪些Cg函数中有表面着色器的代码。这个函数的格式如下:void surf (Input IN,inout SurfaceOutput o), Input是你自定义的结构。Input结构中包含所有纹理坐标和surfaceFunction所需的额外的自动变量。
lightModel - lighting model to use. Built-in ones are Lambert (diffuse) and BlinnPhong (specular). See Custom Lighting Models page for how to write your own.
在光照模式中使用。内置的光照模式是Lambert (diffuse)和 BlinnPhong (specular)。请参考:Custom Lighting Models 。
Optional parameters: 可选参数
alpha - Alpha blending mode. Use this for semitransparent shaders.
Alpha混合模式。它可写以被用作半透明的着色处理。
alphatest:VariableName - Alpha testing mode. Use this for transparent-cutout shaders. Cutoff value is in float variable with VariableName.
Alpha测试模式。使用它可以写出透明的剪影效果。剪影的值是用一个float型的变量表示。
vertex:VertexFunction - Custom vertex modification function. See Tree Bark shader for example.
自定义的顶点修改函数。请参考范例:树皮着色器(Tree Bark shader)。
finalcolor:ColorFunction - Custom final color modification function. See Surface Shader Examples.
自定义的最终颜色修改函数。 请参考:表面着色器例子Surface Shader Examples
exclude_path:prepass or exclude_path:forward - Do not generate passes for given rendering path.
prepass(上一通道) 或者 exclude_path:forward - 不要为给定的渲染路径生成通道。
addshadow - Add shadow caster & collector passes. Commonly used with custom vertex modification, so that shadow casting also gets any procedural vertex animation.
添加阴影投射并且集合通道。通常用于自定义顶点的修改,使阴影也能投射在任何过程的顶点动画上。
dualforward - Use dual lightmaps in forward rendering path.
在正向渲染路径中使用双重光照贴图
fullforwardshadows - Support all shadow types in Forward rendering path.
在正向渲染路径中支持所有阴影类型。
decal:add - Additive decal shader (e.g. terrain AddPass).
添加印花着色器 (例如: 地形 AddPass)。
decal:blend - Semitransparent decal shader.
混合-半透明的印花着色器(Semitransparent decal shader)。
softvegetation - Makes the surface shader only be rendered when Soft Vegetation is on.
仅在Soft Vegetation打开时使用表面着色器
noambient - Do not apply any ambient lighting or spherical harmonics lights.
并不适用于任何环境照明或球面调和照明。
novertexlights - Do not apply any spherical harmonics or per-vertex lights in Forward rendering.
在正向渲染中,不适用于球面调和照明或逐顶点光照。
nolightmap - Disables lightmap support in this shader (makes a shader smaller).
在这个着色器上禁用光照贴图。(使着色器变小)
nodirlightmap - Disables directional lightmaps support in this shader (makes a shader smaller).
在这个着色器上禁用方向光照贴图。 (使着色器变小)。
noforwardadd - Disables Forward rendering additive pass. This makes the shader support one full directional light, with all other lights computed per-vertex/SH. Makes shaders smaller as well.
禁用正向渲染添加通道。这会使这个着色器支持一个完整的方向光和所有逐顶点/SH计算的光照,使着色器变小。
approxview - Computes normalized view direction per-vertex instead of per-pixel, for shaders that need it. This is faster, but view direction is not entirely correct when camera gets close to surface.
当着色器有需要的时候,计算每个顶点,而不是每个像素的方向。 这样更快,但是当摄像机接近表面的时候,视图方向不完全正确。
halfasview - Pass half-direction vector into the lighting function instead of view-direction. Half-direction will be computed and normalized per vertex. This is faster, but not entirely correct.
在光照函数中传递进来的是half-direction向量,而不是视图方向向量。 Half-direction计算且将每个顶点标准化。这样更快,但不完全正确。
Additionally, you can write #pragma debug inside CGPROGRAM block, and then surface compiler will spit out a lot of comments of the generated code. You can view that using Open Compiled Shader in shader inspector.
另外,你可以在CGPROGRAM 块中使用#pragma debug指令,然后编译器会产生许多注释的代码。你可以在着色器面板中打开这些已经编译好的着色器中查看它们。
Surface Shader input structure 表明着色器输入结构
The input structure Input generally has any texture coordinates needed by the shader. Texture coordinates must be named "uv" followed by texture name (or start it with "uv2" to use second texture coordinate set).
输入结构体Input 包含有着色所需要的纹理坐标。纹理坐标必须在纹理名称后加上"uv"。(或者以"uv2"作为头来命名来使用第二个纹理坐标集)
Additional values that can be put into Input structure:
下列这些附加值 也可放在Input结构中。
float3 viewDir - will contain view direction, for computing Parallax effects, rim lighting etc.
为了计算视差、边缘光照等效果,Input需要包含视图方向。
float4 with COLOR semantic - will contain interpolated per-vertex color.
float4的色彩语义:每个顶点颜色的插值
float4 screenPos - will contain screen space position for reflection effects. Used by WetStreet shader in Dark Unity for example.
屏幕坐标。 为了获得反射效果,需要包含屏幕坐标。比如在Dark Unity例子中所使用的 WetStreet着色器。
float3 worldPos - will contain world space position.
世界坐标。
float3 worldRefl - will contain world reflection vector if surface shader does not write to o.Normal. See Reflect-Diffuse shader for example.
世界中的反射向量。如果表面着色不写入o.Normal, 将包含世界反射向量。 请参考这个例子:Reflect-Diffuse 着色器。
float3 worldNormal - will contain world normal vector if surface shader does not write to o.Normal.
世界中的法线向量。如果表面着色器不写入法线(o.Normal)参数,将包含这个参数。
float3 worldRefl; INTERNAL_DATA - will contain world reflection vector if surface shader writes to o.Normal. To get the reflection vector based on per-pixel normal map, use WorldReflectionVector (IN, o.Normal). See Reflect-Bumped shader for example.
内部数据。如果表面着色写入到o.Normal,将包含世界反射向量 。为了得到基于每个像素的法线贴图的反射向量, 使用WorldReflectionVector的(IN输入, o.Normal)。参照凹凸反射着色器。
float3 worldNormal; INTERNAL_DATA - will contain world normal vector if surface shader writes to o.Normal. To get the normal vector based on per-pixel normal map, use WorldNormalVector (IN, o.Normal).
内部数据。如果表面着色写入到o.Normal, 将包含世界法线向量。为了得到基于每个像素的法线贴图的法线向量,请使用世界法线向量((IN输入, o.Normal)
Further Documentation 更多参考文档
Surface Shader Examples
Custom Lighting models in Surface Shaders
Surface Shader Lighting Examples
由www.J2meGame.com原创,转载请说明。
Unity3D Shader官方教程翻译(十九)----Shader语法,编写表面着色器的更多相关文章
- 【译】Unity3D Shader 新手教程(4/6) —— 卡通shader(入门版)
本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 暗黑系 动机 如果你满足以下条件,我建议你阅读这篇教程: 你想了解更多有关表面着色器的细节知识. 你想实现一个入门 ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(十九):MessageHandler 的未知类型消息处理
这是<微信开发深度解析:微信公众号.小程序高效开发秘籍>出版之后写的第一篇微信相关的文章.从这一篇开始,将介绍第一版出版之后添加或修改的功能,或者对书上内容需要做的补充. MP v14.8 ...
- iOS 11开发教程(十九)iOS11应用视图美化按钮之设置按钮的外观
iOS 11开发教程(十九)iOS11应用视图美化按钮之设置按钮的外观 美化按钮说白了就是对按钮的属性进行设置,设置按钮的属性有两种方法:一种是使用编辑界面中的属性检查器:另一种是使用代码进行设置.以 ...
- 第十九章——使用资源调控器管理资源(1)——使用SQLServer Management Studio 配置资源调控器
原文:第十九章--使用资源调控器管理资源(1)--使用SQLServer Management Studio 配置资源调控器 本系列包含: 1. 使用SQLServer Management Stud ...
- 第十九章——使用资源调控器管理资源(2)——使用T-SQL配置资源调控器
原文:第十九章--使用资源调控器管理资源(2)--使用T-SQL配置资源调控器 前言: 在前一章已经演示了如何使用SSMS来配置资源调控器.但是作为DBA,总有需要写脚本的时候,因为它可以重用及扩展. ...
- 【译】Unity3D Shader 新手教程(2/6) —— 积雪Shader
本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 如果你是一个shader编程的新手,并且你想学到下面这些酷炫的技术,我觉得你可以看看这篇教程: 实现一个积雪效果的 ...
- Flask 教程 第十九章:Docker容器上的部署
本文翻译自The Flask Mega-Tutorial Part XIX: Deployment on Docker Containers 这是Flask Mega-Tutorial系列的第十九部分 ...
- NeHe OpenGL教程 第二十九课:Blt函数
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第十九课:粒子系统
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
随机推荐
- 摄像头驱动0V7725学习笔记连载(三):0V7725 SCCB时序的实现
上一篇博客主要是讲解了关于需要配置的重要寄存器,那么接下来就是要通过SCCB接口实现对OV7725的配置.参考<OmniVision Serial Camera Control Bus (SCC ...
- 【转】【WPF】WPF MVVM 简单实例
1 新建WPF 应用程序WPFMVVMExample 程序结构如下图所示. 2 Model实现 在Model文件夹下新建业务类StudentModel(类文件StudentModel.cs),类的详细 ...
- python epoll
需要用python实现中断的功能,所以用epoll监听gpio文件的变化.写个demo测试一下. 参考: http://www.cnblogs.com/coser/archive/2012/01/06 ...
- Eclipse初次java开发问题总结-4-Maven使用问题汇总
Non-resolvable parent POM [INFO] Scanning for projects... [ERROR] The build could not read 1 project ...
- e813. 获得当前选择的菜单或菜单项
The currently selected menu or menu item in a JMenu or JPopupMenu is tracked by MenuSelectionManager ...
- SQL Server CLR 使用 C# 自定义函数
一.简介 Microsoft SQL Server 2005之后,实现了对 Microsoft .NET Framework 的公共语言运行时(CLR)的集成.CLR 集成使得现在可以使用 .NET ...
- EayRadius 于 2013-7-19 进行体验度更新,增加用户体验度
EasyRadius于2013-7-19进行更新,此次更新并没有更新通讯接口,通讯接口将统一更新,包括对其他路由的支持 下面我将主要更新的地方向大家描述一下 如果你有疑问或者建议,可以致电137799 ...
- ★Wireshark基本介绍和学习TCP三次握手
之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇文章介绍另一个好用的抓包工具wireshark, 用来获取网络数据封包,包括http,TCP,UDP,等网络协议包. 记得大学的 ...
- 软件设计模式之单例模式(JAVA)
什么是单例模式? 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系 ...
- 《构建高性能web站点》读书笔记:CPU/IO并发策略
服务器并发处理能力:单位时间内处理的请求数,吞吐率,reqs/s apache的mod_status,显示的 requests/sec,从启动开始的平均计算值.lighttpd的mod_status显 ...