纳金网

标题: Unity 3 technology – Surface Shaders(一) [打印本页]

作者: 会飞的鱼    时间: 2011-11-25 15:19
标题: Unity 3 technology – Surface Shaders(一)


           In Unity you can write your own custom shaders, but it’s no secret that writing them is hard, especially when you need shaders that interact with per-pixel lights & shadows. In Unity 3, that would be even harder because in addition to all the old stuff, your shaders would have to support the new Deferred Lighting renderer. We decided it’s time to make shaders somewhat easier to write.
         

           Warning: a technical post ahead with almost no pictures!
         

           Over a year ago I had a thought that “Shaders must die” (part 1, part 2, part 3). And what do you know – turns out we’re doing this in Unity 3. We call this Surface Shaders cause I’ve a suspicion “shaders must die” as a feature name wouldn’t have flied very far.
         

            
         

           Idea
         

           The main idea is that 90% of the time I just want to declare surface properties. This is what I want to say:
         

           Hey, albedo comes from this texture mixed with this texture, and normal comes from this normal map. Use Blinn-Phong lighting model please, and don’t bother me again!
         

           With the above, I don’t have to care whether this will be used in a forward or deferred rendering, or how various light types will be handled, or how many lights per pass will be done in a forward renderer, or how some indirect illumination SH probes will come in, etc. I’m not interested in all that! These dirty bits are job of rendering programmers, just make it work dammit!
         

           This is not a new idea. Most graphical shader editors that make sense do not have “pixel color” as the final output node; instead they have some node that basically describes surface parameters (diffuse, specularity, normal, …), and all the lighting code is usually not expressed in the shader graph itself. OpenShadingLanguage is a similar idea as well (but because it’s targeted at offline rendering for movies, it’s much richer & more complex).
         

           Example
         

           Here’s a simple – but full & complete – Unity 3.0 shader that does diffuse lighting with a texture & a normal map.
         

           Shader "Example/Diffuse Bump" {
           

           Properties {
           




            _
           
           MainTex ("Texture", 2D) = "white" {}
           


            _
           
           BumpMap ("Bumpmap", 2D) = "bump" {}
           

           }
           

           SubShader {
           

           Tags { "RenderType" = "Opaque" }
           

           CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv
           
            _
           
           MainTex; float2 uv
           
            _
           
           BumpMap; }; 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
           
            _
           
           BumpMap)); } ENDCG
           

           }
           

           Fallback "Diffuse"
           

           }
           










           Given pretty model & textures, it can produce pretty pictures! How cool is that?
         

           I grayed out bits that are not really interesting (declaration of serialized shader properties & their UI names, shader fallback for older machines etc.). What’s left is Cg/HLSL code, which is then augmented by tons of auto-generated code that deals with lighting & whatnot.
         

           This surface shader dissected into pieces:
         

           ■#pragma surface surf Lambert: this is a surface shader with main function “surf”, and a Lambert lighting model. Lambert is one of predefined lighting models, but you can write your own.
           

           ■struct Input: input data for the surface shader. This can have various predefined inputs that will be computed per-vertex & passed into your surface function per-pixel. In this case, it’s two texture coordinates.
           

           ■surf function: actual surface shader code. It takes Input, and writes into SurfaceOutput (a predefined structure). It is possible to write into custom structures, provided you use lighting models that operate on those structures. The actual code just writes Albedo and Normal to the output.
           



           转自官方英文blog
         

           http://blogs.unity3d.com/2010/07/17/unity-3-technology-surface-shaders/
         

作者: 奇    时间: 2012-2-3 23:25
頂。。。

作者: 奇    时间: 2012-2-24 23:22
非常感谢,管理员设置了需要对新回复进行审核,您的帖子通过审核后将被显示出来,现在将转入主题

作者: 菜刀吻电线    时间: 2012-3-20 23:30
读铁系缘分,顶铁系友情

作者: 奇    时间: 2012-5-13 23:21
好,真棒!!

作者: tc    时间: 2012-6-17 23:18
很经典,很实用,学习了!

作者: 晃晃    时间: 2012-6-22 23:25
再看一看,再顶楼主

作者: 菜刀吻电线    时间: 2012-7-1 23:25
不错哦,顶一下......

作者: 晃晃    时间: 2012-9-19 09:45
佩服,好多阿 ,哈哈

作者: 菜刀吻电线    时间: 2012-10-7 23:25
呵呵,真得不错哦!!

作者: tc    时间: 2013-2-13 23:32
“再次路过……”我造一个-----特别路过





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