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) and BlinnPhong (specular). See Custom Lighting Models page for how to write your own.

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

时间: 2024-07-30 00:39:24

Writing Surface Shaders的相关文章

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

Surface Shader 光照例子 这里有一些自定义光照模型和Surface Shaders的例子.通常的Surface Shader例子在这里. 由于延迟光照在某些自定义的逐材质光照模型中表现得并不好,在下面大多数例子中,我们让shader只编译到正向渲染中. 漫反射(Diffuse) 让我们以使用内建Lambert光照模型的shader作为开始吧! Shader "Example/Diffuse Texture" { Properties { _MainTex ("T

Unity Shader——Writing Surface Shaders(0)

从今天起,开始翻译Unity关于shader的官方文档.翻译水平比较一般,目的主要是通过翻译来提升对shader的见解,也让其他人更容易的了解shader.以下开始正文内容: 编写Surface Shaders 和光交互的shader写起来很复杂,有不同的光照类型.阴影选项.渲染路径(正向渲染和延迟渲染),有时shader需要考虑所有的复杂性. Unity中的Surface Shader是一个代码生成器,用它来写光照shader(lit shader)相比于使用低阶的顶点/像素shader(ve

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

Surface Shader中的自定义光照模型 当你在编写 Surface Shaders 时,是在描述一个表面的属性(反射颜色.法线……),而且光的交互过程是由一个光照模型来计算的.内建的光照模型有Lambert(漫反射光照)和BlinnPhong(镜面光照). 有时候,你可能想要使用一个自定义的光照模型,这在Surface Shader中是可能的.光照模型其实就是一些满足某些约定的Cg/HLSL函数.Unity内建的光照模型Lambert和BlinnPhong定义在Lighting.cgin

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

这里有Surface Shader的一些例子.下面的这些例子关注使用内建的光照模型:关于如何使用自定义光照模型的例子参见Surface Shader Lighting Examples. 简单 我们将会以一个非常简单的shader作为开始,并在此基础上逐渐完善.下面这个shader会把表面颜色置成“白色”.它使用内建的Lambert(漫反射)光照模型. Shader "Example/Diffuse Simple" { SubShader { Tags { "RenderTy

Shader编程学习笔记(八)—— Surface Shader 2

Surface Shader 上一小结主要了解了Surface Shader使用了“#pragma surface surf Standard fullforwardshadows”指令的意义,这一小节主要了解“surf”surface函数. void surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex,

Shader编程学习笔记(七)—— Surface shader 1

Surface shader 本小结对Unity的Surface Shader做一个大概的了解.主要了解在Surface Shader当中比较重要的几个部分,分别是: SurfaceOutput Input lighing shadow 首先查看一下Unity的官方手册中的Writing Surface Shaders,其中描述道:如果要编写一个shader去和光进行交互是比较复杂的,因为光照会有不同的光照类型,不同的阴影选项和不同的渲染路径(包括foward和deferred renderin

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

刚开始接触Unity3D Shader编程时,你会发现有关shader的文档相当散,这也造成初学者对Unity3D Shader编程望而却步.该系列教程的第一篇文章(译者注:即本文,后续还有5篇文章)详细介绍了Unity3D中的表面着色器(Surface Shader)的,为学习更复杂的Shader编程打下基础. 动机 如果你是刚刚接触Shader编程的新手,你可能不知道从何开始踏出Shader编程的第一步.本教程将带你一步步完成一个表面着色器(Surface Shader)和片段着色器(Fra

Unity3D组件参考手册

Refer to the information on these pages for details on working in-depth with various aspects of Unity. 这些页面的参考信息,是有关Unity深入工作的各个方面的详细信息. The Unity Manual Guide contains sections that apply only to certain platforms. Please select which platforms you

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

一:标准库里的常用.cginc文件 HLSLSupport.cginc - (automatically included) Helper macros and definitions for cross-platform shader compilation. HLSLSupport.cginc :(自动包含)跨平台着色器编译帮助宏和定义 UnityCG.cginc - commonly used global variables and helper functions. UnityCG.c