查看: 3122|回复: 6
打印 上一主题 下一主题

Unity 3 technology – Surface Shaders(二)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-25 15:22:00 |只看该作者 |倒序浏览




           What is generated
         

           Unity’s “surface shader code generator” would take this, generate actual vertex & pixel shaders, and compile them to various target platforms. With default settings in Unity 3.0, it would make this shader support:
         

           ■Forward renderer and Deferred Lighting (Light Pre-Pass) renderer.
           

           ■Objects with precomputed lightmaps and without.
           

           ■Directional, Point and Spot lights; with projected light cookies or without; with shadowmaps or without. Well ok, this is only for forward renderer because in Deferred Lighting the lighting happens elsewhere.
           

           ■For Forward renderer, it would compile in support for lights computed per-vertex and spherical harmonics lights computed per-object. It would also generate extra additive blended pass if needed for the case when additional per-pixel lights have to be rendered in separate passes.
           

           ■For Deferred Lighting, it would generate base pass that outputs normals & specular power; and a final pass that combines albedo with lighting, adds in any lightmaps or emissive lighting etc.
           

           ■It can optionally generate a shadow caster rendering pass (needed if custom vertex position modifiers are used for vertex shader based animation; or some complex alpha-test effects are done).
           

           For example, here’s code that would be compiled for a forward-rendered base pass with one directional light, 4 per-vertex point lights, 3rd order SH lights; optional lightmaps (I suggest just scrolling down):
         

           #pragma vertex vert
           


            _
           
           surf
         

           #pragma fragment frag
           
            _
           
           surf
         

           #pragma fragmentoption ARB
           
            _
           
           fog
           
            _
           
           exp2
         

           #pragma fragmentoption ARB
           
            _
           
           precision
           
            _
           
           hint
           
            _
           
           fastest
         

           #pragma multi
           
            _
           
           compile
           
            _
           
           fwdbase
         

           #include "HLSLSupport.cginc"
         

           #include "UnityCG.cginc"
         

           #include "Lighting.cginc"
         

           #include "AutoLight.cginc"
         

           s***ct Input {
         

               float2 uv
           
            _
           
           MainTex : TEXCOORD0;
         

           };
         

           sampler2D
           
            _
           
           MainTex;
         

           sampler2D
           
            _
           
           BumpMap;
         

           void surf (Input IN, inout SurfaceOutput o)
         

           {
         

               o.Albedo = tex2D (
           
            _
           
           MainTex, IN.uv
           
            _
           
           MainTex).rgb;
         

               o.Normal = UnpackNormal (tex2D (
           
            _
           
           BumpMap, IN.uv
           
            _
           
           MainTex));
         

           }
         

           s***ct v2f
           
            _
           
           surf {
         

             V2F
           
            _
           
           POS
           
            _
           
           FOG;
         

             float2 hip
           
            _
           
           pack0 : TEXCOORD0;
         

             #ifndef LIGHTMAP
           
            _
           
           OFF
         

             float2 hip
           
            _
           
           lmap : TEXCOORD1;
         

             #else
         

             float3 lightDir : TEXCOORD1;
         

             float3 vlight : TEXCOORD2;
         

             #endif
         

             LIGHTING
           
            _
           
           COORDS(3,4)
         

           };
         

           #ifndef LIGHTMAP
           
            _
           
           OFF
         

           float4 unity
           
            _
           
           LightmapST;
         

           #endif
         

           float4
           
            _
           
           MainTex
           
            _
           
           ST;
         

           v2f
           
            _
           
           surf vert
           
            _
           
           surf (appdata
           
            _
           
           full v) {
         

             v2f
           
            _
           
           surf o;
         

             PositionFog( v.vertex, o.pos, o.fog );
         

             o.hip
           
            _
           
           pack0.xy = TRANSFORM
           
            _
           
           TEX(v.texcoord,
           
            _
           
           MainTex);
         

             #ifndef LIGHTMAP
           
            _
           
           OFF
         

             o.hip
           
            _
           
           lmap.xy = v.texcoord1.xy * unity
           
            _
           
           LightmapST.xy + unity
           
            _
           
           LightmapST.zw;
         

             #endif
         

             float3 worldN = mul((float3x3)
           
            _
           
           Object2World, SCALED
           
            _
           
           NORMAL);
         

             TANGENT
           
            _
           
           SPACE
           
            _
           
           ROTATION;
         

             #ifdef LIGHTMAP
           
            _
           
           OFF
         

             o.lightDir = mul (rotation, ObjSpaceLightDir(v.vertex));
         

             #endif
         

             #ifdef LIGHTMAP
           
            _
           
           OFF
         

             float3 shlight = ShadeSH9 (float4(worldN,1.0));
         

             o.vlight = shlight;
         

             #ifdef VERTEXLIGHT
           
            _
           
           ON
         

             float3 worldPos = mul(
           
            _
           
           Object2World, v.vertex).xyz;
         

             o.vlight += Shade4PointLights (
         

               unity
           
            _
           
           4LightPosX0, unity
           
            _
           
           4LightPosY0, unity
           
            _
           
           4LightPosZ0,
         

               unity
           
            _
           
           LightColor0, unity
           
            _
           
           LightColor1, unity
           
            _
           
           LightColor2, unity
           
            _
           
           LightColor3,
         

               unity
           
            _
           
           4LightAtten0, worldPos, worldN );
         

             #endif // VERTEXLIGHT
           
            _
           
           ON
         

             #endif // LIGHTMAP
           
            _
           
           OFF
         

             TRANSFER
           
            _
           
           VERTEX
           
            _
           
           TO
           
            _
           
           FRAGMENT(o);
         

             return o;
         

           }
         

           #ifndef LIGHTMAP
           
            _
           
           OFF
         

           sampler2D unity
           
            _
           
           Lightmap;
         

           #endif
         

           half4 frag
           
            _
           
           surf (v2f
           
            _
           
           surf IN) : COLOR {
         

             Input surfIN;
         

             surfIN.uv
           
            _
           
           MainTex = IN.hip
           
            _
           
           pack0.xy;
         

             SurfaceOutput o;
         

             o.Albedo = 0.0;
         

             o.Emission = 0.0;
         

             o.Specular = 0.0;
         

             o.Alpha = 0.0;
         

             o.Gloss = 0.0;
         

             surf (surfIN, o);
         

             half atten = LIGHT
           
            _
           
           ATTENUATION(IN);
         

             half4 c;
         

             #ifdef LIGHTMAP
           
            _
           
           OFF
         

             c = LightingLambert (o, IN.lightDir, atten);
         

             c.rgb += o.Albedo * IN.vlight;
         

             #else // LIGHTMAP
           
            _
           
           OFF
         

             half3 lmFull = DecodeLightmap (tex2D(unity
           
            _
           
           Lightmap, IN.hip
           
            _
           
           lmap.xy));
         

             #ifdef SHADOWS
           
            _
           
           SCREEN
         

             c.rgb = o.Albedo * min(lmFull, atten*2);
         

             #else
         

             c.rgb = o.Albedo * lmFull;
         

             #endif
         

             c.a = o.Alpha;
         

             #endif // LIGHTMAP
           
            _
           
           OFF
         

             return c;
         

           }
         

            
         

            
         

           Of those 90 lines of code, 10 are your original surface shader code; the remaining 80 would have to be pretty much written by hand in Unity 2.x days (well ok, less code would have to be written because 2.x had less rendering features). But wait, that was only base pass of the forward renderer! It also generates code for additive pass, for deferred base pass, deferred final pass, optionally for shadow caster pass and so on.
         

           So this should be an easier to write lit shaders (it is for me at least). I hope this will also increase the number of Unity users who can write shaders at least 3 times (i.e. to 30 up from 10!). It should be more future proof to accomodate changes to the lighting pipeline we’ll do in Unity next.
           



           转自官方英文blog
         

           http://blogs.unity3d.com/2010/07/17/unity-3-technology-surface-shaders/
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-1-19 23:00:45 |只看该作者
鉴于你今年的良好表现,新年将至,别人都在祝福你新年快乐,我觉得换一种新颖的方式祝福你,那就是:天天开心,事事顺利,雷打不动的祝福属于你!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-4-18 23:21:24 |只看该作者
凡系斑竹滴话要听;凡系朋友滴帖要顶!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-5-7 23:18:13 |只看该作者
佩服,好多阿 ,哈哈
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-11-4 23:23:09 |只看该作者
真不错,全存下来了.
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2013-2-20 23:35:54 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2013-2-28 23:18:23 |只看该作者
发了那么多,我都不知道该用哪个给你回帖了,呵呵
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-29 22:00 , Processed in 0.091685 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部