查看: 2841|回复: 0
打印 上一主题 下一主题

编写表面着色器:表面着色器光照示例

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-2-24 09:58:17 |只看该作者 |倒序浏览
Here are some examples of custom lighting models in Surface Shaders. General Surface Shader examples are in this page.
下面的这些例子是表面着色器(Surface Shaders)中的自定义光照模式(custom lighting models)。普通光照模式的表面着色器在这页。
Because Deferred Lighting does not play well with some custom per-material lighting models, in most examples below we make the shaders compile to Forward Rendering only.
因为延时光照(Deferred lighting)不能与一些自定义 per-material 光照模式(lighting model)很好的运行,在下面大部分例子中我们只在着色器的正向渲染(Forward Rendering)中编译。



[Diffuse 漫反射]
We'll start with a shader that uses built-in Lambert lighting model:
我们从内置的Lambert光照模式(lighting model)开始:
  Shader "Example/Diffuse Texture" {

    Properties {

      _MainTex ("Texture", 2D) = "white" {}

    }

    SubShader {

      Tags { "RenderType" = "Opaque" }

      CGPROGRAM

      #pragma surface surf Lambert

      s***ct Input {

          float2 uv_MainTex;

      };

      sampler2D _MainTex;

      void surf (Input IN, inout SurfaceOutput o) {

          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;

      }

      ENDCG

    }

    Fallback "Diffuse"

  }
Here's how it looks like with a texture and without an actual texture (one directional light is in the scene):
下面是效果一个有纹理(texture)和一个没有真实的纹理(texture)。(在场景中有一个方向光):




Now, let's do exactly the same, but write out our own lighting model instead of using built-in Lambert one. Surface Shader Lighting Models are just some functions that we need to write. Here's a simple Lambert one. Note that the "shader part" itself did not change at all (grayed out):
现在,让我们来做一个完全一样的。但,是我们自己编写的光照模式(lighting model)而不是使用内置的Lambert。 表面着色器光照模式(Surface Shader Lighting Models)仅仅是需要我们编写的一些函数。下面是一个简单的Lambert。注意:"着色器部分"自身并没有改变(即下面的灰色部分)。
  Shader "Example/Diffuse Texture" {

    Properties {

      _MainTex ("Texture", 2D) = "white" {}

    }

    SubShader {

      Tags { "RenderType" = "Opaque" }

      CGPROGRAM

      #pragma surface surf SimpleLambert
      half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {

          half NdotL = dot (s.Normal, lightDir);

          half4 c;

          c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten * 2);

          c.a = s.Alpha;

          return c;

      }
      s***ct Input {

          float2 uv_MainTex;

      };

      sampler2D _MainTex;

      void surf (Input IN, inout SurfaceOutput o) {

          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;

      }

      ENDCG

    }

    Fallback "Diffuse"

  }
So our simple Diffuse lighting model is LightingSimpleLambert function. It computes lighting by doing a dot product between surface normal and light direction, and then applies light attenuation and color.
就这样我们这个点单的漫反射光照模式(Diffuse lighting model)是一个名叫LightingSimpleLambert的函数。它计算的是表面法线(surface normal)与灯光方向(light direction)的点积。然后应用于光线衰减和颜色。



[Diffuse Wrap 漫反射遮蔽]
Here's Wrapped Diffuse - a modification of Diffuse lighting, where illumination "wraps around" the edges of objects. It's useful for faking subsurface scattering effect. Again, the surface shader itself did not change at all, we're just using different lighting function.
下面是遮蔽的漫反射-漫反射光照的一种改进。照明"环绕(wraps around)"在物体的边缘。它对于假冒子表面(subsurface)散射效果(scattering effect)非常有用。同样的"着色器部分"自身并没有改变(即下面的灰色部分)。我们仅仅用了不同的光照函数(黑体字部分)。
  Shader "Example/Diffuse Wrapped" {

    Properties {

      _MainTex ("Texture", 2D) = "white" {}

    }

    SubShader {

      Tags { "RenderType" = "Opaque" }

      CGPROGRAM

      #pragma surface surf WrapLambert
      half4 LightingWrapLambert (SurfaceOutput s, half3 lightDir, half atten) {

          half NdotL = dot (s.Normal, lightDir);

          half diff = NdotL * 0.5 + 0.5;

          half4 c;

          c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);

          c.a = s.Alpha;

          return c;

      }
      s***ct Input {

          float2 uv_MainTex;

      };

      sampler2D _MainTex;

      void surf (Input IN, inout SurfaceOutput o) {

          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;

      }

      ENDCG

    }

    Fallback "Diffuse"

  }






[Toon Ramp 卡通渐变]
Here's a "Ramp" lighting model that uses a texture ramp to define how surface responds to angle between light and the normal. This can be used for variety of effects, including Toon lighting.
下面是一个"渐变(Ramp)"光照模式。他使用一个纹理(texture)定义渐变怎样在表面做出反应,反应角度通过光照方向和法线求得。导入卡通光照(Toon lighting)可以实现多种效果。
  Shader "Example/Toon Ramp" {

    Properties {

      _MainTex ("Texture", 2D) = "white" {}

      _Ramp ("Shading Ramp", 2D) = "gray" {}

    }

    SubShader {

      Tags { "RenderType" = "Opaque" }

      CGPROGRAM

      #pragma surface surf Ramp
      sampler2D _Ramp;
      half4 LightingRamp (SurfaceOutput s, half3 lightDir, half atten) {

          half NdotL = dot (s.Normal, lightDir);

          half diff = NdotL * 0.5 + 0.5;

          half3 ramp = tex2D (_Ramp, float2(diff)).rgb;

          half4 c;

          c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);

          c.a = s.Alpha;

          return c;

      }
      s***ct Input {

          float2 uv_MainTex;

      };

      sampler2D _MainTex;

      void surf (Input IN, inout SurfaceOutput o) {

          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;

      }

      ENDCG

    }

    Fallback "Diffuse"

  }






[Simple Specular 简单的高光]
Here's a simple specular lighting model. It's quite simple to what built-in BlinnPhong actually does; we just put it here to illustrate how it's done.
下面是一个简单的高光光照模式(specular lighting model)。它是内置的BlinnPhong,实际上做起来非常简单。放在这里仅仅是为了说明它是如何工作的。
  Shader "Example/Simple Specular" {

    Properties {

      _MainTex ("Texture", 2D) = "white" {}

    }

    SubShader {

      Tags { "RenderType" = "Opaque" }

      CGPROGRAM

      #pragma surface surf SimpleSpecular
      half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {

          half3 h = normalize (lightDir + viewDir);
          half diff = max (0, dot (s.Normal, lightDir));
          float nh = max (0, dot (s.Normal, h));

          float spec = pow (nh, 48.0);
          half4 c;

          c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten * 2);

          c.a = s.Alpha;

          return c;

      }
      s***ct Input {

          float2 uv_MainTex;

      };

      sampler2D _MainTex;

      void surf (Input IN, inout SurfaceOutput o) {

          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;

      }

      ENDCG

    }

    Fallback "Diffuse"

  }



【来源:互联网】

更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

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

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

GMT+8, 2024-11-12 04:52 , Processed in 0.108288 second(s), 33 queries .

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

© 2008-2019 Narkii Inc.

回顶部