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) and BlinnPhong (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).

参考:file:///C:/Program%20Files%20(x86)/Unity/Editor/Data/Documentation/html/en/Manual/SL-SurfaceShaders.html

Writing Surface Shaders的更多相关文章

  1. Unity Shader——Writing Surface Shaders(3)——Surface Shader Lighting Examples

    Surface Shader 光照例子 这里有一些自定义光照模型和Surface Shaders的例子.通常的Surface Shader例子在这里. 由于延迟光照在某些自定义的逐材质光照模型中表现得 ...

  2. Unity Shader——Writing Surface Shaders(2)——Custom Lighting models in Surface Shaders

    Surface Shader中的自定义光照模型 当你在编写 Surface Shaders 时,是在描述一个表面的属性(反射颜色.法线……),而且光的交互过程是由一个光照模型来计算的.内建的光照模型有 ...

  3. Unity Shader——Writing Surface Shaders(0)

    从今天起,开始翻译Unity关于shader的官方文档.翻译水平比较一般,目的主要是通过翻译来提升对shader的见解,也让其他人更容易的了解shader.以下开始正文内容: 编写Surface Sh ...

  4. Unity Shader——Writing Surface Shaders(1)——Surface Shader Examples

    这里有Surface Shader的一些例子.下面的这些例子关注使用内建的光照模型:关于如何使用自定义光照模型的例子参见Surface Shader Lighting Examples. 简单 我们将 ...

  5. surface shader相关参数,命令

    https://docs.unity3d.com/Manual/SL-SurfaceShaders.html 说明: 注意下surfaceshader相关开关选项,input结构体全部可用参数 goo ...

  6. 【译】Unity3D Shader 新手教程(1/6)

    本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 刚开始接触Unity3D Shader编程时,你会发现有关shader的文档相当散,这也造成初学者对Unity3D ...

  7. Unity3D Shader官方教程翻译(十九)----Shader语法,编写表面着色器

    Writing Surface Shaders Writing shaders that interact with lighting is complex. There are different ...

  8. Unity 内置Shader变量、辅助函数等

    一:标准库里的常用.cginc文件 HLSLSupport.cginc - (automatically included) Helper macros and definitions for cro ...

  9. 【Unity Shaders】Shader学习资源和Surface Shader概述

    写在前面 写这篇文章的时候,我断断续续学习Unity Shader半年了,其实还是个门外汉.我也能体会很多童鞋那种想要学好Shader却无从下手的感觉.在这个期间,我找到一些学习Shader的教程以及 ...

随机推荐

  1. java中static关键字的理解(转载)

    static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...

  2. [MEF]第03篇 MEF延迟加载导出部件及元数据

    一.演示概述此演示介绍了MEF的延迟加载及元数据相关的内容.在实际的设计开发中,存在着某些对象是不需要在系统运行或者附属对象初始化的时候进行实例化的,只需要在使用到它的时候才会进行实例化,这种方式就可 ...

  3. [MEF]第01篇 MEF使用入门

    一.演示概述 此演示初步介绍了MEF的基本使用,包括对MEF中的Export.Import和Catalog做了初步的介绍,并通过一个具体的Demo来展示MEF是如何实现高内聚.低耦合和高扩展性的软件架 ...

  4. cockpit 使用(集成docker && k8s 管理)

    1. yum 安装 sudo yum install cockpit 2. 允许启动 sudo systemctl enable --now cockpit.socket 3. 可选的插件 cockp ...

  5. 坑爹的AMH

    坑爹的 AMH的配置文件在这里: nginx 配置文件1:/usr/local/nginx-generic-1.6/conf/nginx.conf nginx 配置文件2:/home/wwwroot/ ...

  6. 各大互联网公司2014前端笔试面试题–HTML,CSS篇

    Html篇: 1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? IE: trident内核 Firefox:gecko内核 Safari:webkit内核 Opera:以前是presto ...

  7. LogUtils日志管理工具

    public class LogUtils { public static final int VERBOSE = 1; public static final int DEBUG = 2; publ ...

  8. android 网络连接判断

    Android 网络判断类,用来判断网络状态 使用方法: (1)先初始化 //初始化网络状态检测类 NetworkStateManager.instance().init(this); (2)判断是否 ...

  9. python调试方法

    之前调试python程序都是用print参数,感觉有点弱爆啊,最近发现python也有类似C语言gdb的工具pdb,记录下pdb的使用方法和心得. 先找了段简单的测试程序: 复制代码 !/usr/bi ...

  10. Django前端的文本编辑器,滑动登陆

    文本编译器 # 添加文章 url(r'^addarticle/$', views.addarticle), # 利用文本编辑器添加文章 def addarticle(request): ''' 添加文 ...