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. maven创建多模块项目

    在eclipse下构建maven项目,该项目由多个子模块组成. 1.创建一个父项目 NEW -->project-->maven-->maven Project,点击下一步,进入ne ...

  2. fopen()和fclose()

    1.fopen()函数的用法fopen函数用于打开文件, 其调用格式为:FILE *fopen(char *filename, *type);fopen()函数中第一个形式参数表示文件名, 可以包含路 ...

  3. python模块--config

    一.创建文件 ##-----------------创建数据表-------------------------- import configparser config = configparser. ...

  4. FastAdmin 中 Bootstrap-Table 列宽控制

    FastAdmin 中 Bootstrap-Table 列宽控制 使用 css 控制 1 使用 formatter 处理 2 http://issues.wenzhixin.net.cn/bootst ...

  5. PHP如何实现网址伪静态(转)

    Apache的 mod_rewrite是比较强大的,在进行网站建设时,可以通过这个模块来实现伪静态.主要步骤如下: 1.检测Apache是否开启mod_rewrite功能     可以通过php提供的 ...

  6. 搭建基于hyperledger fabric的联盟社区(九) --检索状态数据库

    一.启动elasticsearch服务 官网下载压缩包解压,进入bin目录启动: ./elasticsearch 通过ip访问 localhost:9200,可以看到如下信息 { name: &quo ...

  7. SQL语句操作全集

    SQL语句操作全集 下列语句部分是MySQL语句 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDAT ...

  8. Unit01: Spring简介 、 Spring容器 、 Spring IOC

    Unit01: Spring简介 . Spring容器 . Spring IOC Spring (1)Spring是什么? Spring是一个开源的用来简化应用开发的框架. (2)Spring的特点? ...

  9. Unit04: JavaScript 概述 、 JavaScript 基础语法 、 流程控制

    Unit04: JavaScript 概述 . JavaScript 基础语法 . 流程控制 my.js function f3() { alert("唐胜伟"); } demo1 ...

  10. Git操作行

    基础层:-----------------#初始化一个版本仓库git init #复制远程版本库git clone url #添加远程版本库origingit remote add origin ur ...