http://gaov666.blog.163.com/blog/static/10409531020101494828459/html
初步認識舉例:http://answers.unity3d.com/questions/529814/how-to-have-2-different-objects-at-the-same-place.html編程
http://www.unitymanual.com/2660.htmlapp
Shaders: Getting started來源網址:http://unity3d.com/support/documentation/Manual/ShaderTut1.html
Unity Manual > Advanced > Shaders > Shaders: Getting startedcurl
This tutorial will teach you how you can create your own shaders and make you game look a lot better!
此教程將指引你如何創建本身的Shaders,讓你的遊戲場景看起來更好編程語言
Unity is equipped with a powerful shading and material language called ShaderLab. In style it is similar to CgFX and Direct3D Effects languages - it describes everything needed to display a Material, not just plain vertex/pixel shaders.
Unity配備了強大的陰影和材料的語言工具稱爲ShaderLab,以程式語言來看,它相似於CgFX和Direct3D的語法,它不僅紀錄基本的端點或者映像點(vertex/pixel)資訊,也描述了材質所必要的一切資訊。ide
Shaders describe properties that are exposed in Unity's Material Inspector and multiple shader implementations (SubShaders) targeted at different graphics hardware capabilities, each describing complete graphics hardware rendering state, fixed function pipeline setup or vertex/fragment programs to use. Vertex and fragment programs are written in high-level Cg programming language or low-level shader assembly.
在unity材質檢視器中能夠看到Shaders的性質及多重shader(SubShaders)的描述,針對不一樣圖形硬件,每一個描述也都完整的說明了圖形硬件的彩現狀態,fixed function pipeline如何設定、vertex/ fragment programs如何做用。 Vertex and fragment程序可使用高階Cg程式語言或低階shader組合。工具
In this tutorial we describe how to write shaders in ShaderLab using both fixed function and programmable pipelines. We assume that the reader has a basic understanding of OpenGL or Direct3D render states, fixed function and programmable pipelines and has some knowledge of Cg, HLSL or GLSL programming languages. Some shader tutorials and documentation can be found on NVIDIA and AMD developer sites.
在這個教程中,咱們將描述如何使用fixed function與programmable pipelines兩種方式於ShaderLab中撰寫shaders,咱們假設讀者擁有基本的OpenGL或Direct3D彩現概念,並對cg有fixed function與programmable pipelines的常識,HLSL或GLSL編程語言技術,一些Shader教程與參考文件可於NVIDIA以及AMD的開發站上取得測試
Getting started
To create a new shader, either choose Assets->Create->Shader from the menubar, or duplicate an existing shader, and work from that. The new shader can be edited by double-clicking it in the Project View.
創建一個新的shader有兩種方法,能夠由菜單Assets->Create->Shader新增,或複製一個既有的shader再進行編輯,新的shader能夠透過雙擊來啓動編輯畫面(UniSciTE)ui
We'll start with a very basic shader:
下面開始介紹一個基礎的shader範例:this
view plaincopy to clipboardprint?
Shader "Tutorial/Basic" {
Properties {
_Color ("Main Color", Color) = (1,0.5,0.5,1)
}
SubShader {
Pass {
Material {
Diffuse [_Color]
}
Lighting On
}
}
}
Shader "Tutorial/Basic" {
Properties {
_Color ("Main Color", Color) = (1,0.5,0.5,1)
}
SubShader {
Pass {
Material {
Diffuse [_Color]
}
Lighting On
}
}
}
This simple shader demonstrates one of the most basic shaders possible. It defines a color property called Main Color and assigns it a default value of rose-like color (red=100% green=50% blue=50% alpha=100%). It then renders the object by invoking a Pass and in that pass setting the diffuse material component to the property _Color and turning on the vertex lighting.
這個shader範例只是衆多shader中最基本的一個,它定義了一個顏色性質,名稱爲Main Color,並指定了玫瑰色的效果(red=100% green=50% blue=50% alpha=100%),在調用時會跳過Diffuse的材質設定(_Color)並開啓頂點光源
To test this shader, create a new material, select the shader from the drop-down menu (Tutorial->Basic) and assign the Material to some object. Tweak the color in the Material Inspector and watch the changes. Time to move onto more complex things!
要測試這個shader,你能夠創建一個新的材質,並於Shader下拉菜單選擇(Tutorial->Basic),再把這個新材質指定到物件上,拖拉材質檢視器的顏色表並查看其變化。是時候研究更復雜的事情了!
Basic Vertex Lighting
If you open an existing complex shader, it can be a bit hard to get a good overview. To get you started, we will dissect the built-in VertexLit shader that ships with Unity. This shader uses fixed function pipeline to do standard per- vertex lighting.
假如你開啓一個既有的複合shader,剛開始看可能會以爲有點難,在開始之前,咱們將詳細說明unity內建的VertexLit shader。這個shader使用fixed function pipeline產生標準的per-vertex照明。
view plaincopy to clipboardprint?
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一開始的位置,你能夠定義任何性質,這些性質將可在材質檢視器中編輯
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 structure 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並且使用硬體可支持的第一個。
view plaincopy to clipboardprint?
Shader "Structure 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 runs on anything : )
}
}
Shader "Structure 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 runs 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.
此係統提供unity能夠支援現有全部的硬體並取得最佳的品質。它做到了,然而,結果是必須撰寫很長的shaders語法
Inside each SubShader block you set the rendering state shared by all passes; and define rendering passes themselves. A complete list of available commands can be found in the SubShader Reference.
在每個SubShader block,你能夠設定彩現途徑的狀態;並定義彩現途徑自己。完整的SubShader語法請參照SubShader Reference章節
Passes
Each subshader is a collection of passes. For each pass, the object geometry is rendered, so there must be at least one pass. Our VertexLit shader has just one pass:
每一個subshader等因而一個途徑集。要對幾何物件進行彩現,至少必定要有一個途徑,內定的VertexLit shader裏面僅有一個途徑:
view plaincopy to clipboardprint?
// ...snip...
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
}
}
// ...snip...
// ...snip...
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
}
}
// ...snip...
Any commands defined in a pass configures the graphics hardware to render the geometry in a specific way.
經過指令能夠定義一個特殊的方法,用來驅動繪圖硬體彩現指定的幾何物件
In the example above we have a Material block that binds our property values to the fixed function lighting material settings. The command Lighting On turns on the standard vertex lighting, and SeperateSpecular On enables the use of a separate color for the specular highlight.
例如:上方語法中有一個Material block,定義了照明時所須要幾項固定參數。而指令Lighting On用來開啓該照明設備,而SeperateSpecular On則是啓用Seperate做爲特殊鏡射效果
All of these command so far map very directly to the fixed function OpenGL/Direct3D hardware model. Consult OpenGL red book for more information on this.
到目前爲止的全部命令,皆屬於支援OpenGL/Direct3D技術硬體自己可以使用的固定功能,您能夠參考OpenGL紅皮書,能夠找到更多相關資料
The next command, SetTexture, is very important. These commands define the textures we want to use and how to mix, combine and apply them in our rendering. SetTexture command is followed by the property name of the texture we would like to use (_MainTex here) This is followed by a combiner block that defines how the texture is applied. The commands in the combiner block are executed for each pixel that is rendered on screen.
下一個命令是SetTexture,這是個很是重要的命令,這個命令能夠定義影像紋理如何混合、組合以及如何運用於咱們的彩現環境裏,SetTexture一般跟隨於紋理的屬性名稱以後(咱們在這裏使用_MainTex ),接下來的combiner block也是定義紋理的應用方式,這個combiner block的命令會在螢幕顯示每個被執行的動做
Within this block we set a constant color value, namely the Color of the Material, _Color. We'll use this constant color below.
在這個block內咱們設定了一個顏色值,並命名爲_Color,咱們會在後面使用這個顏色
In the next command we specify how to mix the texture with the color values. We do this with the Combine command that specifies how to blend the texture with another one or with a color. Generally it looks like this:
在下個命令,咱們指定如何混合紋理以及顏色值。咱們用Combine命令來混合其餘紋理或顏色,看起來像下面這樣:
Combine ColorPart, AlphaPart
Here ColorPart and AlphaPart define blending of color (RGB) and alpha components respectively. If AlphaPart is omitted, then it uses the same blending as ColorPart.
在這裏ColorPart與AlphaPart定義了混合的顏色(RGB)以及alpha值(A)個別的資料,假如AlphaPart被省略了,那它將與ColorPart的資料做混合
In our VertexLit example:
在咱們的VertexLit範例中:
Combine texture * primary DOUBLE, texture * constant
Here texture is the color coming from the current texture (here _MainTex). It is multiplied (*) with the primary vertex color. Primary color is the vertex lighting color, calculated from the Material values above. Finally, the result is multiplied by two to increase lighting intensity (DOUBLE).
這裏的texture來源是當前的紋理(_MainTex),它將與主要的顏色互相搭配(*),主色爲照明設備的顏色,它是由Material計算出來的結果。最終是這兩個倍增後的結果會增長照明強度
The alpha value (after the comma) is texture multiplied by constant value (set with constantColor above). Another often used combiner mode is called previous (not used in this shader). This is the result of any previous SetTexture step, and can be used to combine several textures and/or colors with each other. aplha值(在逗號之後)是由constantColor倍增而得的結果。另外一個經常使用的混合模式稱爲previous(在這個shader未使用),這是全部previous SetTexture的結果,而且能夠用來混合多種紋理和顏色。