纳金网

标题: Unity的shader,帮助手册(三) [打印本页]

作者: 会飞的鱼    时间: 2011-12-19 17:24
标题: Unity的shader,帮助手册(三)


  假如你开启一个既有的复合shader,刚开始看可能会觉得有点难,在开始以前,我们将详细说明unity内建的VertexLit shader。这个shader使用fixed function pipeline产生标准的per-vertex照明。
Shader "VertexLit" {   

     Properties {   

         _Color ("Main Color", Color) = (1,1,1,0.5)   

         _SpecColor ("Spec Color", Color) = (1,1,1,1)   

         _Emission ("Emmisive Color", Color) = (0,0,0,0)   

         _Shininess ("Shininess", Range (0.01, 1)) = 0.7   

         _MainTex ("Base (RGB)", 2D) = "white" { }   

     }   

  

     SubShader {   

         Pass {   

             Material {   

                Diffuse [_Color]   

                 Ambient [_Color]   

                Shininess [_Shininess]   

                 Specular [_SpecColor]   

                 Emission [_Emission]   

             }   

             Lighting On   

             SeperateSpecular On   

             SetTexture [_MainTex] {   

                 constantColor [_Color]   

                 Combine texture * primary DOUBLE, texture * constant   

             }   

         }   

     }   

}  

Shader "VertexLit" {

     Properties {

         _Color ("Main Color", Color) = (1,1,1,0.5)

         _SpecColor ("Spec Color", Color) = (1,1,1,1)

         _Emission ("Emmisive Color", Color) = (0,0,0,0)

         _Shininess ("Shininess", Range (0.01, 1)) = 0.7

         _MainTex ("Base (RGB)", 2D) = "white" { }

     }
     SubShader {

         Pass {

             Material {

                Diffuse [_Color]

                 Ambient [_Color]

                Shininess [_Shininess]

                 Specular [_SpecColor]

                 Emission [_Emission]

             }

             Lighting On

             SeperateSpecular On

             SetTexture [_MainTex] {

                 constantColor [_Color]

                 Combine texture * primary DOUBLE, texture * constant

             }

         }

     }

}


All shaders start with the keyword Shader followed by a string that represents the name of the shader. This is the name that is shown in the Inspector. All code for this shader must be put within the curly braces after it: { } (called a block).

  所有的shaders都必须以Shader作为开始,接着是这个shader的名称(例如:VertexLit),这个名称将会显示于检视器(Inspector)。所有的语法都必须放在{ }之内。
The name should be short and descriptive. It does not have to match the .shader file name.

  这个名称必须短且足以代表其功能,它并不会等于.shader的档案名称
To put shaders in submenus in Unity, use slashes - eg MyShaders/Test would be shown as Test in a submenu called MyShaders, or MyShaders->Test.

  如果要把shaders放在unity的submenus下面,请使用斜线,例如:MyShaders/Test,你将会看到有个submenu名为MyShaders,下面有个shader名为Test,或是像这样MyShaders->Test
The shader is composed of a Properties block followed by SubShader blocks. Each of these is described in sections below.

  在Properties block下面接着的是SubShader block,每个描述都在这个段落中

Properties
At the beginning of the shader block you can define any properties that artists can edit in the Material Inspector. In the VertexLit example the properties look like this:

properties位于shader block一开始的位置,你可以定义任何性质,这些性质将可在材质检视器中编辑,在VertexLit的个范例中,properties block看起来像这样:

图片一
The properties are listed on separate lines within the Properties block. Each property starts with the internal name (Color, MainTex). After this in parentheses comes the name shown in the inspector and the type of the property. After that, the default value for this property is listed:

   properties block内的语法都是单行的,每一个性质描述都由内名称开始(例如:Color, MainTex),在后方的括弧号中所显示的名字也会显示于inspector检视器上,在此之后,描述的是该性质的预设值

图片二
The list of possible types are in the Properties Reference. The default value depends on the property type. In the example of a color, the default value should be a four component vector.

  可用的性质类型请参考Properties Reference。预设值与性质有关,以color为例,预设值应该由四个值组成
We now have our properties defined, and are ready to start writing the actual shader.

  现在我们已经定义了四个性质,可以开始撰写实际的shader了
The Shader Body

Before we move on, let's define the basic s***cture of a shader file.

  在开始以前,先了解shader的结构是如何定义的
Different graphic hardware has different capabilities. For example, some graphics cards support fragment programs and others don't; some can lay down four textures per pass while the others can do only two or one; etc. To allow you to make full use of whatever hardware your user has, a shader can contain multiple SubShaders. When Unity renders a shader, it will go over all subshaders and use the first one that the hardware supports.

  不同的绘图卡有不同的能力,例如:有的绘图卡支援fragment programs但有些没有,有些可以一次处理四个贴图?(four textures)其他的可能只能处理两个或一个,为了要符合所有用户的硬体需求,一个shader可以包涵多个SubShaders,当unity在运算shader时,它将详细察看所有的subshaders而且使用硬体可支持的第一个。
Shader "S***cture Example" {   

     Properties { /* ...shader properties... }   

     SubShader {   

         // ...subshader that uses vertex/fragment programs...   

     }   

     SubShader {   

         // ...subshader that uses four textures per pass...   

     }   

     SubShader {   

         // ...subshader that uses two textures per pass...   

     }   

     SubShader {   

         // ...subshader that might look ugly but***ns on anything : )   

     }   

}  

Shader "S***cture Example" {

     Properties { /* ...shader properties... }

     SubShader {

         // ...subshader that uses vertex/fragment programs...

     }

     SubShader {

         // ...subshader that uses four textures per pass...

     }

     SubShader {

         // ...subshader that uses two textures per pass...

     }

     SubShader {

         // ...subshader that might look ugly but***ns on anything : )

     }

}
This system allows Unity to support all existing hardware and maximize the quality on each one. It does, however, result in some long shaders.
作者: 菜刀吻电线    时间: 2012-3-2 23:28
精典,学习了!

作者: C.R.CAN    时间: 2012-3-22 23:22
既来之,则看之!

作者: 奇    时间: 2012-4-5 23:19
读铁系缘分,顶铁系友情

作者: 晃晃    时间: 2012-4-11 23:18
读铁系缘分,顶铁系友情

作者: C.R.CAN    时间: 2012-5-12 23:24
呵呵,很好,方便罗。

作者: C.R.CAN    时间: 2012-6-8 23:25
谢谢楼主,真是太实用了

作者: tc    时间: 2012-6-24 23:26
有意思!学习了!

作者: tc    时间: 2012-8-8 01:40
其实楼主所说的这些,俺支很少用!

作者: 菜刀吻电线    时间: 2012-9-28 23:20
“再次路过……”我造一个-----特别路过

作者: C.R.CAN    时间: 2013-2-21 23:24
楼主收集的可真全哦

作者: C.R.CAN    时间: 2013-3-3 23:18
你们都躲开,我来顶





欢迎光临 纳金网 (http://course.narkii.com/club/) Powered by Discuz! X2.5