https://docs.unity3d.com/Manual/SL-SurfaceShaders.html


说明:
注意下surfaceshader相关开关选项,input结构体全部可用参数
google搜不到surface shader alpha:blend的用法,就当没这个参数吧

注意下Code generation options 中说明了prepass就是legacy Deffred


#pragma target 3.0    :
定义Shader模型为Shader Model 3.0,

2.0, Direct3D 9 (默认缺省值)。支持32张贴图 + 64个计算

3.0, Direct3D 9。支持512张贴图 + 512个计算

4.0, 只支持DirectX 11。

5.0, 只支持DirectX 11。









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 are 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 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 structure SurfaceOutput. SurfaceOutput basically describes properties
of the surface
 (it’s albedo color, normal, emission, specularity etc.). You write this code in 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:

  1. struct SurfaceOutput
  2. {
  3. fixed3 Albedo; // diffuse color
  4. fixed3 Normal; // tangent space normal, if written
  5. fixed3 Emission;
  6. half Specular; // specular power in 0..1 range
  7. fixed Gloss; // specular intensity
  8. fixed Alpha; // alpha for transparencies
  9. };

In Unity 5, surface shaders can also use physically based lighting models. Built-in Standard and StandardSpecular lighting models (see below) use these output structures respectively:

  1. struct SurfaceOutputStandard
  2. {
  3. fixed3 Albedo; // base (diffuse or specular) color
  4. fixed3 Normal; // tangent space normal, if written
  5. half3 Emission;
  6. half Metallic; // 0=non-metal, 1=metal
  7. half Smoothness; // 0=rough, 1=smooth
  8. half Occlusion; // occlusion (default 1)
  9. fixed Alpha; // alpha for transparencies
  10. };
  11. struct SurfaceOutputStandardSpecular
  12. {
  13. fixed3 Albedo; // diffuse color
  14. fixed3 Specular; // specular color
  15. fixed3 Normal; // tangent space normal, if written
  16. half3 Emission;
  17. half Smoothness; // 0=rough, 1=smooth
  18. half Occlusion; // occlusion (default 1)
  19. fixed Alpha; // alpha for transparencies
  20. };

Samples

See Surface Shader ExamplesSurface
Shader Custom Lighting Examples
 and Surface Shader Tessellation pages.

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:

  1. #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.
  • lightModel - lighting model to use. Built-in ones are physically based Standard and StandardSpecular,
    as well as simple non-physically based Lambert (diffuse) and BlinnPhong(specular).
    See Custom Lighting Models page for how to write your own.

    • Standard lighting model uses SurfaceOutputStandard output struct, and matches the
      Standard (metallic workflow) shader in Unity.
    • StandardSpecular lighting model uses SurfaceOutputStandardSpecular output struct,
      and matches the Standard (specular setup) shader in Unity.
    • Lambert and BlinnPhong lighting models are not physically based (coming from Unity
      4.x), but the shaders using them can be faster to render on low-end hardware.

Optional parameters

Transparency and alpha testing is controlled by alpha and alphatest directives.
Transparency can typically be of two kinds: traditional alpha blending (used for fading objects out) or more physically plausible “premultiplied blending” (which allows semitransparent surfaces to retain proper specular reflections). Enabling semitransparency
makes the generated surface shader code contain blendingcommands; whereas enabling alpha cutout will do a fragment discard
in the generated pixel shader, based on the given variable.

  • alpha or alpha:auto - Will pick fade-transparency (same as alpha:fade)
    for simple lighting functions, and premultiplied transparency (same as alpha:premul) for physically based lighting functions.
  • alpha:blend - Enable alpha blending.
  • alpha:fade - Enable traditional fade-transparency.
  • alpha:premul - Enable premultiplied alpha transparency.
  • alphatest:VariableName - Enable alpha cutout transparency. Cutoff value is in a float variable with VariableName. You’ll likely also want to use addshadow directive
    to generate proper shadow caster pass.
  • keepalpha - By default opaque surface shaders write 1.0 (white) into alpha channel, no matter what’s output in the Alpha of output struct or what’s returned by the lighting
    function. Using this option allows keeping lighting function’s alpha value even for opaque surface shaders.
  • decal:add - Additive decal shader (e.g. terrain AddPass). This is meant for objects that lie atop of other surfaces, and use additive blending. See Surface
    Shader Examples
  • decal:blend - Semitransparent decal shader. This is meant for objects that lie atop of other surfaces, and use alpha blending. See Surface
    Shader Examples

Custom modifier functions can be used to alter or compute incoming vertex data, or to alter final computed fragment color.

  • vertex:VertexFunction - Custom vertex modification function. This function is invoked at start of generated vertex shader, and can modify or compute per-vertex data. See Surface
    Shader Examples
    .
  • finalcolor:ColorFunction - Custom final color modification function. See Surface
    Shader Examples
    .
  • finalgbuffer:ColorFunction - Custom deferred path for altering gbuffer content.
  • finalprepass:ColorFunction - Custom prepass base path.

Shadows and Tessellation - additional directives can be given to control how shadows and tessellation is handled.

  • addshadow - Generate a shadow caster pass. Commonly used with custom vertex modification, so that shadow casting also gets any procedural vertex animation. Often shaders
    don’t need any special shadows handling, as they can just use shadow caster pass from their fallback.
  • fullforwardshadows - Support all light shadow types in Forward rendering
    path. By default shaders only support shadows from one directional light in forward rendering (to save on internal shader variant count). If you need point or spot light shadows in forward rendering, use this directive.
  • tessellate:TessFunction - use DX11 GPU tessellation; the function computes tessellation factors. See Surface
    Shader Tessellation
     for details.

Code generation options - by default generated surface shader code tries to handle all possible lighting/shadowing/lightmap scenarios. However in some cases you know you won’t need some of them, and it is possible to adjust
generated code to skip them. This can result in smaller shaders that are faster to load.

  • exclude_path:deferredexclude_path:forwardexclude_path:prepass -
    Do not generate passes for given rendering path (Deferred ShadingForward and Legacy
    Deferred
     respectively).
  • noshadow - Disables all shadow receiving support in this shader.
  • noambient - Do not apply any ambient lighting or light probes.
  • novertexlights - Do not apply any light probes or per-vertex lights in Forward rendering.
  • nolightmap - Disables all lightmapping support in this shader.
  • nodynlightmap - Disables runtime dynamic global illumination support in this shader.
  • nodirlightmap - Disables directional lightmaps support in this shader.
  • nofog - Disables all built-in Fog support.
  • nometa - Does not generate a “meta” pass (that’s used by lightmapping & dynamic global illumination to extract surface information).
  • 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.

Miscellaneous options

  • softvegetation - Makes the surface shader only be rendered when Soft Vegetation is on.
  • interpolateview - Compute view direction in the vertex shader and interpolate it; instead of computing it in the pixel shader. This can make the pixel shader faster, but
    uses up one more texture interpolator.
  • 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.
  • approxview - Removed in Unity 5.0. Use interpolateview instead.
  • dualforward - Use dual lightmaps in forward rendering
    path.

To see what exactly is different from using different options above, it can be helpful to use “Show Generated Code” button in the Shader
Inspector
.

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).

Additional values that can be put into Input structure:

  • float3 viewDir - contains view direction, for computing Parallax effects, rim lighting etc.
  • float4 with COLOR semantic - contains interpolated per-vertex color.
  • float4 screenPos - contains screen space position for reflection or screenspace effects. Note that this is not suitable for GrabPass;
    you need to compute custom UV yourself using ComputeGrabScreenPos function.
  • float3 worldPos - contains world space position.
  • float3 worldRefl - contains world reflection vector if surface shader does not write to o.Normal. See Reflect-Diffuse shader for example.
  • float3 worldNormal - contains world normal vector if surface shader does not write to o.Normal.
  • float3 worldRefl; INTERNAL_DATA - contains 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.
  • float3 worldNormal; INTERNAL_DATA - contains 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).

Surface Shaders and DirectX 11 HLSL syntax

Currently some parts of surface shader compilation pipeline do not understand DirectX 11-specific HLSL syntax,
so if you’re using HLSL features like StructuredBuffers, RWTextures and other non-DX9 syntax, you have to wrap it into a DX11-only preprocessor macro.

See Platform Specific Differences and Shading
Language
 pages for details.

surface shader相关参数,命令的更多相关文章

  1. 21.pyinstaller相关参数

    pyinstaller相关参数                  命令                                                          描述  -F, ...

  2. Surface Shader

    Surface Shader: (1)必须放在SubShdader块,不能放在Pass内部: (2)#pragma sufrace surfaceFunction lightModel [option ...

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

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

  4. 【Unity Shaders】Diffuse Shading——在Surface Shader中使用properties

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  5. unity surface shader 1

    Unity ShaderLib :  CGPROGRAM  ENDCG之间是CG代码,之外的代码功能都由ShaderLib提供,CG中的一些方法比如tex2D(...)也是ShaderLib对CG进行 ...

  6. iftop 安装以及相关参数及说明(转载自csdn)

      转载自http://blog.csdn.net/cqinter/article/details/6250211 关于 Iftop iftop 是类似于top的实时流量监控工具.主要用来显示本机网络 ...

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

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

  8. Linux中mod相关的命令 内核模块化 mod相关命令都是用来动态加载内核模块/驱动程序模块

    Linux中mod相关的命令 内核模块化   mod相关命令都是用来动态加载内核模块/驱动程序模块 http://baike.baidu.com/link?url=lxiKxFvYm-UfJIxMjz ...

  9. 【Unity Shaders】初探Surface Shader背后的机制

    转载请注明出处:http://blog.csdn.net/candycat1992/article/details/39994049 写在前面 一直以来,Unity Surface Shader背后的 ...

随机推荐

  1. Java for LeetCode 133 Clone Graph

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  2. HttpWebRequest的timeout和ReadWriteTimeout(转载)

    公司[1]一牛人看我的代码,说我设置的timeout有误,还应该设置ReadWriteTimeout.本人很不服,于是上网查看了相关说明. HttpWebRequest httpWebRequest ...

  3. Java基础教程:多线程基础(4)——Lock的使用

    Java基础教程:多线程基础(4)——Lock的使用 快速开始 Java 5中Lock对象的也能实现同步的效果,而且在使用上更加方便. 本节重点的2个知识点是:ReentrantLock类的使用和Re ...

  4. MySQL 中事务的实现

    在关系型数据库中,事务的重要性不言而喻,只要对数据库稍有了解的人都知道事务具有 ACID 四个基本属性,而我们不知道的可能就是数据库是如何实现这四个属性的: 在这篇文章中,我们将对事务的实现进行分析, ...

  5. 初学OpenMP

    这两天在看多核计算的书,就要用到openmp,因为我使用vs2015,从微软可以看到是支持openmp2.0版本的 具体使用: 在vs里创造一个控制台项目,然后打开属性管理器,在属性管理器里找到配置属 ...

  6. Spring Boot2.0之整合多数据源

    一般公司分两个数据库: 一个放共同配置文件, 一个数据库垂直业务数据库 垂直拆分和水平拆分: 垂直是根据业务划分具体数据库 在一个项目中有多个数据源(不同库jdbc) 无限个的哈~ 根据包名 或者 注 ...

  7. 分享知识-快乐自己:Hibernate框架常用API详解

    1):Configuration配置对象 Configuration用于加载配置文件. 1): 调用configure()方法,加载src下的hibernate.cfg.xml文件 Configura ...

  8. 网站桌面端和手机端不同url的设置

    你的网站在搜索引擎中表现怎样很大程度上依赖于你的你的网站对于不同设备上的设计. 下面介绍了怎样基于URL构造来优化你的网站对于搜索引擎的支持. 决定你网页的URL构造 Determine the UR ...

  9. oracle中导出sql的几个常见词语的意思

    set feedback off不显示反馈信息  “1行已插入”,大量数据装入时,显示这个也是很浪费资源和时间的. set define off 如果你某个字段里面有&字符,插入数据会出错,设 ...

  10. hashlib加密

    一.hashlib的基本组成: 1.hashlib库是python3的标准库,主要用于数据的加密,以下是hashlib的方法及属性. >>> import hashlib>&g ...