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的教程以及 ...
随机推荐
- python 之 list,tuple,dict,set
基本类型, 其中set好像提到的很少 list : [] 根据字面, 是一个列表, 所以有序, 可以通过序号方位 tuple: () 是不变的list, 通过序号访问 dict: {}, 字 ...
- Elixir's keyword lists as option parameters
备注: 文章转自:https://www.djm.org.uk/posts/writing-extensible-elixir-with-behaviours-adapters-pluggable-b ...
- quick code ide设置分辨率
默认是640*960的分辨率,但是我的设计分辨率是480*800 所以得改改 在config.lua里的改成 -- design resolutionCONFIG_SCREEN_WIDTH = 480 ...
- (转)Android studio 使用心得(五)—代码混淆和破解apk
这篇文章等是跟大家分享一在Android studio 进行代码混淆配置.之前大家在eclipse上也弄过代码混淆配置,其实一样,大家可以把之前在eclipse上的配置文件直接拿过来用.不管是.cfg ...
- Exception in thread "main" java.lang.UnsupportedClassVersionError: com/crack
执行一个jar文件的时候抛异常了 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/cr ...
- html基础1(环境准备、标签)
学习目的 1,能改前端的模板 2,自己装修页面 3.前后端交互多个技术 4.能操作网页元素 5.能和前端开发人员沟通 开发工具: pycharm/webStorm EditPlus(适合初学) sub ...
- tensorboard的使用
# -*- coding: utf-8 -*- """ Created on: 2017/10/29 @author : Shawn function : "& ...
- mysql的三种安装方式(详细)
安装MySQL的方式常见的有三种: rpm包形式 通用二进制形式 源码编译 1,rpm包形式 (1) 操作系统发行商提供的 (2) MySQL官方提供的(版本更新,修复了更多常见BUG)www.mys ...
- 嵌入ARM硬核的FPGA
目前,在FPGA上嵌入ARM硬核的包括Xilinx的zynq系列以及Intel 的CYCLONEV系列. Zynq出来有一定市场,但是这个市场不是传统FPGA的主流市场,而是为了和微处理抢一些控制领域 ...
- linux tcp server demo
#include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <ne ...