Unity3D Shader官方教程翻譯(二)

xy849288321發表於2013-02-21
ShaderLab語法:Shader




Shader is the root command of a shader file. Each file must define one (and only one) Shader. It specifies how any objects whose material uses this shader are rendered.

Shader是一個shader的檔案的根命令。每個檔案必須定義一個(只有一個) Shader。它指定有哪些物體的材質使用這個shader進行渲染。

Syntax
語法


Shader "name" { [Properties] Subshaders [Fallback] } Defines a shader. It will appear in the material inspector listed under name. Shaders optionally can define a list of properties that show up as material settings. After this comes a list of SubShaders, and optionally a fallback.

Shader "name" { [Properties] Subshaders [Fallback] } 定義了一個著色器。它的名字將會出現在Unity3d材質檢索器中並以定義的name顯示。Shader可以為設定材質的列表有選擇地定義屬性顯示。在此之後可以定義一個SubShaders列表,及一個可選的返回值(Fallback指當該Shader無法工作時候,返回一個可以正常工作的備用渲染處理)。


Details

細節
Properties 屬性

Shaders can have a list of properties. Any properties declared in a shader are shown in the material inspector inside Unity. Typical properties are the object color, textures, or just arbitrary values to be used by the shader.

著色器可以有一個屬性列表。在著色器中宣告的任何屬性都將在Unity3D材質檢索器中顯示。典型的屬性是物件的顏色,紋理,或一個將要在渲染中使用的任意值。


SubShaders & Fallback 子著色器及備用渲染處理

Each shader is comprised of a list of sub-shaders. You must have at least one. When loading a shader, Unity will go through the list of subshaders, and pick the first one that is supported by the end user's machine. If no subshaders are supported, Unity will try to use fallback shader.

每個著色器由一系列的子著色器組成。你必須至少指定一個子著色器。當載入一個shader時候,Unity3D將通過subshaders列表,並在列表中選擇第一個可以被終端使用者機器所支援的子著色器。如果沒有一個子著色器被支援,Unity3D將嘗試使用備用的著色器進行渲染處理[Fallback]。

Different graphic cards have different capabilities. This raises an eternal issue for game developers; you want your game to look great on the latest hardware, but don't want it to be available only to those 3% of the population. This is where subshaders come in. Create one subshader that has all the fancy graphic effects you can dream of, then add more subshaders for older cards. These subshaders may implement the effect you want in a slower way, or they may choose not to implement some details.

不同的顯示卡具有不同的功能。這就給遊戲開發商提出了一個永恆的問題,你想你的遊戲,在最新的硬體上表現得十分出色,但不希望它僅僅提供給那些僅佔人口3%的人使用。這就是subshaders的意義。建立一個Subshader給那些牛B的顯示卡使用,同時建立更多的Subshader給老式顯示卡使用,讓你的遊戲相容最多的顯示卡。這些subshaders可以實現不同的效果在不同的顯示卡上。
Examples

Here is one of the simplest shaders possible:

這是1個最簡單的Shader例子
// colored vertex lighting
Shader "Simple colored lighting" {
// a single color property 只有1個顏色屬性
Properties {
_Color ("Main Color", Color) = (1,.5,.5,1)
}
// define one subshader 定義1個子著色器
SubShader {
Pass {
Material {
Diffuse [_Color]
}
Lighting On
}
}
}

This shader defines a color property _Color (that shows up in material inspector as Main Color) with a default value of (1, 0.5, 0.5, 1). Then a single subshader is defined. The subshader consists of one Pass that turns on vertex lighting and sets up basic material for it.

這個shader定義顏色屬性的預設值(1,0.5,0.5,1)_Color(將在Unity3D的材質檢索器中作為主要顏色屬性顯示)。然後一個單一的subshader定義。該subshader組成一個渲染通道,開啟頂點照明,並設定了它的基本材質屬性。



由www.J2meGame.com精心原創,轉載請說明。http://www.j2megame.com/html/xwzx/ty/3303.html

相關文章