u3d_Shader_effects笔记2 自定义surfaceDiffuseLight

1.前面的心情

今晚7点半睡着后,9点半左右被吵醒。醒来后非常失落,感觉人生到底在追求什么,我又在追求什么。昨晚梦到妈妈了。最近不时会想到爷爷的去世。人世的险恶,良心的缺失。不过一切总要向前看,至少我有一个快乐幸福的家庭。有支持我的父母姐妹。

但自己到底想要什么,追求什么。

近期工作也不太顺利,业余时间也比较堕落。

感情方面还是空白,而且经常想性事。

不管怎样,为了活着的人和自己,每天都要开心、进步、阳光。

2.本文主要内容

本文主要是18页到20页内容。 自定义diffuse光照处理。

3.参考内容:

(1)书

(2)参照中文,速度看看整个流程; http://blog.csdn.net/candycat1992/article/details/17440101  

(3)查看shader内置函数:http://www.cnblogs.com/rainstorm/archive/2013/05/04/3057444.html 

           微软官网也有:https://msdn.microsoft.com/en-us/library/windows/desktop/ff471376(v=vs.85).aspx

4.代码

Shader "Custom/NewShader" {
	Properties {
	  _EmissiveColor("Emissive Color",Color)=(1,1,1,1)	
	  _AmbientColor("Ambient Color",Color)=(1,1,1,1)
	  _MySliderValue("This is a Slider",Range(0,10))=2.5	
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf BasicDiffuse

		inline float4 LightingBasicDiffuse(SurfaceOutput s,fixed3 lightDir,fixed atten)
		{
			float difLight = max(0,dot(s.Normal,lightDir));
			float4 col;
			col.rgb = s.Albedo*_LightColor0.rgb*(difLight*atten*2);
			col.a = s.Alpha;
			return col;
		}

		float4 _EmissiveColor;
		float4 _AmbientColor;
		float _MySliderValue;

		struct Input {
		 float2 uv_MainTex;  
		};

		void surf (Input IN, inout SurfaceOutput o) {
			float4 c;
			c = pow((_EmissiveColor+_AmbientColor),_MySliderValue);

			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

  

 

5.学到什么

(1).正确设置自定义的light函数:

#pragma surface surf BasicDiffuse //#pragma surface"将直接告诉Shader使用哪个光照模型用于计算

  然后定义Ligthing+编译的surf后函数名;

inline float4 LightingBasicDiffuse(SurfaceOutput s,fixed3 lightDir,fixed atten)//

 (2).surface shader下三种类型的lighting mode可以使用:

  主要区分点:1.是否延迟light(此不熟悉)   2.是否和viewDir有关(上篇讲的光源种类,specular和viewDir相关)

   

(3)基本的光照公式:

即 dot vertexNormal和ligthDir;然后加个atten;

(4)此diffuseLight计算处于的渲染阶段:

首先show generated coder,找lightingBasicDiffuse():

在 fragment shader里:

  // realtime lighting: call lighting function
  #ifdef LIGHTMAP_OFF
  c = LightingBasicDiffuse (o, _WorldSpaceLightPos0.xyz, atten);//自定义的light计算
  #endif // LIGHTMAP_OFF || DIRLIGHTMAP_OFF
  #ifdef LIGHTMAP_OFF
  c.rgb += o.Albedo * IN.vlight;//是否有vertexLight?有的话也添加计算
  #endif // LIGHTMAP_OFF

  即本文lightDiffuse是在片元shader里进行的,像素级的light计算

(5)用到的内置函数

1.dot , Returns the dot product of two vectors.

2.max ,Selects the greater of x and y.

书中提到saturate:Clamps x to the range [0, 1]

注意max和saturate传入的参数可以为scalarvector, or matrix;

3.step;是避免if...else的一种方式!

 

6.疑问,待学习

1.max 和saturate防止负值用哪个比较好?  

  更新:据群里bluebit大神言,如我没理解错,当saturate和dot结合使用时,会编译成一条指令,效率高;其他情况使用max...

2.deferred概念不清楚

3.第5节(2)中三种光照模式没找到在哪里定义的。。.EditorDataCGIncludes没找到设定

改变自己
原文地址:https://www.cnblogs.com/sun-shadow/p/4893540.html