Unity固定管线shader总结

Unity的固定管线shader可以通过Inspectot中的Show generated code来查看对应的cg shader,这是很好的研究方法。

以下所诉的固定管线命令都不会在Vertex Fragment Shader中起作用(至少在最新版本的Unity中是这样的)

以下所述也许会有所偏差,有疑惑就去Show generated code看看对应得Veretx&Fragment Shader


第一步:计算顶点颜色和顶点光照

注意,这一步都是在顶点上计算的。

(1)Color (1,0,0,0)或Color [_Color]   

     指定顶点颜色

(2)Material

{

 Diffuse (1,1,1,1)

 Ambient (1,1,1,1)

}

Lighting On             

打开光照(Lighting On)之后,Color命令不起作用,Material中定义的材质属性开始起作用,这些属性用来计算光照。

Material中的材质属性如果不写的话,默认为黑色(0,0,0,1)。甚至Material{}都可以不写,只要Lighting On,依然默认材质属性为黑色

通常使Diffuse和Ambient颜色相同(所有的Unity内置shader都是这样做的)

光照公式如下,其中括号内,对于每个照射到物体上的灯光都要计算然后相加

Ambient * Lighting Window’s Ambient Intensity setting + (Light Color * Diffuse + Light Color *Specular) +Emission

(3)SeparateSpecular On | Off

如果不SeparateSpecular On,那么是不会计算高光的。

如果SeparateSpecular On,计算高光,但是高光是单独计算的。即计算漫反射环境光(但不包括高光)等相加起来得到的光照,跟纹理混合。混合之后的颜色再加上我们单独计算的高光,这样得到最终的颜色。要起作用,必须Lighting On

(4)ColorMaterial AmbientAndDiffuse : 使用顶点的颜色来计算ambient和diffuse(两者颜色通常相同),而不是用Materail中指定的颜色

   ColorMaterial Emission : 使用顶点的颜色来计算emission,而不是用Materail中指定的颜色

第二步:计算纹理

SetTexture只能放在一个pass的末尾

SetTexture内使用Combine指令和constantColor指令

constantColor命令指定一个颜色可以用于Combine指令。

Combine的来源只能是以下几种

previous(上一个SetTexture的结果), constant(constantColor中指定的颜色值),primary(光照计算结果(即第一步的结果,类似于顶点shader输出给片段shader的颜色值)或顶点颜色(如果使用BindChannels绑定了顶点颜色)) ortexture(SetTexture中指定的纹理颜色值)

可以在后面加上Double或Quad表示乘以2倍或4倍

可以在后面加上alpha表示只取该颜色的alpha值

可以在前面加上one-表示1减去该值


注意,可以有多个SetTexture,下一个SetTexture可以使用上一个SetTexture的结果进行相加相乘等操作,每一个Texture Stage的结果可能不会clamp到0和1之间(视平台而定),因此Texture Stage可能产生大于1.0的值。

还可以这样写

SetTexture [_MainTex] { combine previous * texture, previous + texture }
使previous和texture的rgb相乘,alpha值相加,得到的结果就是最终颜色的rgb值和alpha值。

硬件支持:

Modern graphics cards with fragment shader support (“shader model 2.0” on desktop, OpenGL ES 2.0 on mobile) support allSetTexture modes and at least 4 texture stages (many of them support 8). If you’re running on really old hardware (made before 2003 on PC, or before iPhone3GS on mobile), you might have as low as two texture stages. The shader author should write separateSubShaders for the cards he or she wants to support.

Shader "Examples/Self-Illumination" {
    Properties {
        _MainTex ("Base (RGB) Self-Illumination (A)", 2D) = "white" {}
    }
    SubShader {
        Pass {
            // Set up basic white vertex lighting
            Material {
                Diffuse (1,1,1,1)
                Ambient (1,1,1,1)
            }
            Lighting On

            // Use texture alpha to blend up to white (= full illumination)
            SetTexture [_MainTex] {
                constantColor (1,1,1,1)
                combine constant lerp(texture) previous//注意这里,由于是第一个SetTexture,previous的值是光照计算的结果值
            }
            // Multiply in texture
            SetTexture [_MainTex] {
                combine previous * texture
            }
        }
    }
}

三.BindChannels

用来指定顶点数据与哪个硬件进行绑定。

默认情况下,Unity会帮我们绑定。但是有些情况下(使用顶点颜色或需要两套uv)我们需要自己绑定

BindChannels {
	Bind "Vertex", vertex
	Bind "normal", normal
//这句的意思是:在第一个SetTexture命令里使用第二套uv."texcoord1"表示顶点的第二套uv。texcoord0表示第一个TextureStage
	Bind "texcoord1", texcoord0 
//这句的意思是:在第二个SetTexture命令里使用第一套uv."texcoord"表示顶点的第一套uv。texcoord1表示第二个TextureStage
	Bind "texcoord", texcoord1 
}


   


 绑定参数时,第一个参数只能用 "texcoord"和"texcoord1",表示两套uv.第二个参数可以用texcoord0 texcoord1 texcoord2...表示对应的Texture Stage(即SetTexture命令)。第二个参数也可以用texcoord,表示将第一个参数表示的uv用于所有Texture Stage。

你可能感兴趣的:(Unity固定管线shader总结)