Writing Surface Shaders
【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.
You define a “surface function” that takes any UVs or data you need as input, and fills in output structure SurfaceOutput. SurfaceOutput basically describes properties of the surface (it’s albedo color, normal, emission, specularity etc.). You write this code in 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.

【Surface Shader compile directives】
Surface shader is placed inside CGPROGRAM..ENDCG block, just like any other shader. The differences are:
- It must be placed inside SubShader block, not inside Pass. Surface shader will compile into multiple passes itself.
- It uses
#pragma surface ...directive to indicate it’s a surface shader.
The #pragma surface directive is:

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. - lightModel - lighting model to use. Built-in ones are
Lambert(diffuse) andBlinnPhong(specular). See Custom Lighting Models page for how to write your own.
【引用变量】
1、如果在Properties中定义了:_MainTex("Base (RGB)", 2D) = "white" {}
那么想在Surface中引用,则需先定义:sampler2D _MainTex;
2、同样,如果定义了Color。也需要先在Surface中定义,如:
float4 _EmissiveColor;
【Optional parameters】
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).
Writing Surface Shaders的更多相关文章
- Unity Shader——Writing Surface Shaders(3)——Surface Shader Lighting Examples
Surface Shader 光照例子 这里有一些自定义光照模型和Surface Shaders的例子.通常的Surface Shader例子在这里. 由于延迟光照在某些自定义的逐材质光照模型中表现得 ...
- Unity Shader——Writing Surface Shaders(2)——Custom Lighting models in Surface Shaders
Surface Shader中的自定义光照模型 当你在编写 Surface Shaders 时,是在描述一个表面的属性(反射颜色.法线……),而且光的交互过程是由一个光照模型来计算的.内建的光照模型有 ...
- Unity Shader——Writing Surface Shaders(0)
从今天起,开始翻译Unity关于shader的官方文档.翻译水平比较一般,目的主要是通过翻译来提升对shader的见解,也让其他人更容易的了解shader.以下开始正文内容: 编写Surface Sh ...
- Unity Shader——Writing Surface Shaders(1)——Surface Shader Examples
这里有Surface Shader的一些例子.下面的这些例子关注使用内建的光照模型:关于如何使用自定义光照模型的例子参见Surface Shader Lighting Examples. 简单 我们将 ...
- surface shader相关参数,命令
https://docs.unity3d.com/Manual/SL-SurfaceShaders.html 说明: 注意下surfaceshader相关开关选项,input结构体全部可用参数 goo ...
- 【译】Unity3D Shader 新手教程(1/6)
本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 刚开始接触Unity3D Shader编程时,你会发现有关shader的文档相当散,这也造成初学者对Unity3D ...
- Unity3D Shader官方教程翻译(十九)----Shader语法,编写表面着色器
Writing Surface Shaders Writing shaders that interact with lighting is complex. There are different ...
- Unity 内置Shader变量、辅助函数等
一:标准库里的常用.cginc文件 HLSLSupport.cginc - (automatically included) Helper macros and definitions for cro ...
- 【Unity Shaders】Shader学习资源和Surface Shader概述
写在前面 写这篇文章的时候,我断断续续学习Unity Shader半年了,其实还是个门外汉.我也能体会很多童鞋那种想要学好Shader却无从下手的感觉.在这个期间,我找到一些学习Shader的教程以及 ...
随机推荐
- maven docker plugin 常见问题解决
1. maven 项目必须使用小写,不然会一直有500 的错误 500: HTTP 500InternalServerError 2. docker server 连接数超了 Fail ...
- netstat 命令 与 ss 命令
http://www.cnblogs.com/peida/archive/2013/03/11/2953420.html http://www.ttlsa.com/linux-command/ss-r ...
- apache编译参数详解
常用编译参数: ./configure //配置源代码树–prefix=/usr/local/apache //体系无关文件的顶级安装目录PREFIX ,也就Apache的安装目录.–e ...
- 本地tomcat调用远程接口报错:java.lang.reflect.InvocationTargetException
今天碰到一个奇怪的问题,本地Eclipse起了一个tomcat通过http去调一个外部接口,结果竟然报了一个反射的异常,先看下完整日志: , :: 下午 org.apache.catalina.sta ...
- adb命令记录
1.杀掉 adb 进程 adb kill-server 2.重启 adb 服务 adb start-server 3.重启手机 adb reboot 4.进 shell 模 ...
- Required String parameter 'id' is not present
问题详情: 简单的说,我就是通过ajax发起了一个post请求到后台,但是后台没有收到请求发过去的参数,并且还报了这样的错误. 错误描述告诉我们,请求参数里面并没有存在id.我 ...
- 列表的使用2,深COPY和浅COPY,循环列表,步长切片
name2=names.copy() 下面我看几种奇怪的现象: 首先把源列表copy以后,把源列表第2个数值修改.那么没毛病. 如果源列表里,还包含了一个子列表:这也没毛病 如果我们这个时候修改子列表 ...
- SQL判断NULL的几种常见方式
第一种 where XX ='NULL' ,XX字段存的值就是NULL这四个字符, 第二种 where XX is null ,XX字段什么也没存,这是数据库的判断语法, 第三种 where isnu ...
- py4常用模块
导入模块方式 import 单文件 from dir import file 目录下文件 如果有相同的名称,可以采用别名的方式 from dir import file as rename.file ...
- Web api help page error CS0012: Type "System.Collections.Generic.Dictionary'2错误
1.在asp.net Boilerplate项目中,Abp.0.12.0.2,.net framework4.5.2.下载后添加了webApi的helpPage功能,调试出现错误. dubug : a ...