Guest User

Untitled

a guest
Dec 10th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
  3.  
  4. //basic lambert CG shader with rim light
  5.  
  6. Shader "CG Shaders/Lambert/Vertex Lit Rim Light"
  7. {
  8. Properties
  9. {
  10. _diffuseColor("Diffuse Color", Color) = (1,1,1,1)
  11. _diffuseMap("Diffuse", 2D) = "white" {}
  12. //The Frensel Rim Light properties
  13. _FrenselPower("Rim Power", Range(1.0, 10.0)) = 2.5
  14. //properties can be declared twice to allow sliders and numbers to be used on the same attribute
  15. //the second one i usually leave a space in the name, to keep the box from expanding
  16. _FrenselPower(" ", Float) = 2.5
  17. _rimColor("Rim Color", Color) = (1,1,1,1)
  18.  
  19. }
  20. SubShader
  21. {
  22. Pass
  23. {
  24. Tags{ "LightMode" = "ForwardBase" }
  25.  
  26.  
  27. CGPROGRAM
  28.  
  29. #pragma vertex vShader
  30. #pragma fragment pShader
  31. #include "UnityCG.cginc"
  32. #pragma multi_compile_fwdbase
  33.  
  34. uniform fixed3 _diffuseColor;
  35. uniform sampler2D _diffuseMap;
  36. uniform half4 _diffuseMap_ST;
  37. uniform fixed4 _LightColor0;
  38. //The power of the frensel Rim Light
  39. uniform half _FrenselPower;
  40. uniform fixed4 _rimColor;
  41.  
  42. struct app2vert {
  43. float4 vertex : POSITION;
  44. fixed2 texCoord : TEXCOORD0;
  45. fixed4 normal : NORMAL;
  46. };
  47. struct vert2Pixel
  48. {
  49. float4 pos : SV_POSITION;
  50. fixed2 uvs : TEXCOORD0;
  51. fixed3 lighting : TEXCOORD1;
  52. };
  53.  
  54. fixed lambert(fixed3 N, fixed3 L)
  55. {
  56. return saturate(dot(N, L));
  57. }
  58. //frensel function
  59. fixed frensel(fixed3 V, fixed3 N)
  60. {
  61. //similar to the lambert function, we run a dotProduct on the View and Normal Vectors
  62. //We then saturate it to return a value between 1 and 0 and invert that via 1 - value
  63. //We power this to control the falloff
  64. return pow(1 - saturate(dot(V,N)), _FrenselPower);
  65. }
  66. vert2Pixel vShader(app2vert IN)
  67. {
  68. vert2Pixel OUT;
  69. float4x4 WorldViewProjection = UNITY_MATRIX_MVP;
  70. float4x4 WorldInverseTranspose = unity_WorldToObject;
  71. float4x4 World = unity_ObjectToWorld;
  72.  
  73. OUT.pos = mul(WorldViewProjection, IN.vertex);
  74. OUT.uvs = IN.texCoord.xy;
  75.  
  76. fixed3 normalDir = normalize(mul(IN.normal, WorldInverseTranspose).xyz);
  77. fixed3 posWorld = mul(World, IN.vertex).xyz;
  78.  
  79. //vertex lights
  80. fixed3 vertexLighting = fixed3(0.0, 0.0, 0.0);
  81. #ifdef VERTEXLIGHT_ON
  82. for (int index = 0; index < 4; index++)
  83. {
  84. half3 vertexToLightSource = half3(unity_4LightPosX0[index], unity_4LightPosY0[index], unity_4LightPosZ0[index]) - posWorld;
  85. fixed attenuation = (1.0 / length(vertexToLightSource)) *.5;
  86. fixed3 diffuse = unity_LightColor[index].xyz * lambert(normalDir, normalize(vertexToLightSource)) * attenuation;
  87. vertexLighting = vertexLighting + diffuse;
  88. }
  89. vertexLighting = saturate(vertexLighting);
  90. #endif
  91.  
  92. //Main Light calculation - includes directional lights
  93. half3 vertexToLightSource = _WorldSpaceLightPos0.xyz - (posWorld*_WorldSpaceLightPos0.w);
  94. fixed attenuation = lerp(1.0, 1.0 / length(vertexToLightSource), _WorldSpaceLightPos0.w);
  95. fixed3 lightDirection = normalize(vertexToLightSource);
  96. fixed3 ambientL = UNITY_LIGHTMODEL_AMBIENT.xyz;
  97.  
  98. fixed diffuseL = lambert(normalDir, lightDirection);
  99. fixed3 diffuse = _LightColor0.xyz* diffuseL * attenuation;
  100.  
  101. //At this point we can calculate the rim light
  102. //calculate the direction to the camera from the world position
  103. //camera position comes in via "UnityCG.cginc"
  104. fixed3 viewDir = normalize(_WorldSpaceCameraPos - posWorld);
  105. //run the frensel function to get a base for the rim light
  106. fixed rim = frensel(normalDir, viewDir);
  107. //get a dot product mask of the normals vs the up vector, wrapped slightly
  108. fixed3 upV = fixed3(0,1,0);
  109. fixed mask = saturate(dot(upV,normalDir)* 0.5 + 0.5);
  110. //multiply the rimlight by up vector mask
  111. //this means the light will seem to come from above
  112. rim *= mask;
  113. //mask via view vector and negative up vector, with a lot of wrapping
  114. //This is to reduce obviously fake lighting when looking from above
  115. mask = saturate(dot(upV,-viewDir) + 1.75);
  116. //multiply the rimLight by the mask
  117. rim *= mask;
  118. //we can add some bloom along the edges that the key light comes from
  119. //this just looks nice
  120. diffuse += diffuse * rim;
  121. //finally multiply the rimlight by the inverse of the key light
  122. //this means the rim light will only be visible in shadows and the light will seem to come from above
  123. //99% of 3 point lighting rigs have a back/rim light opposite the key coming from above, which we attempt to approximate
  124. //need to convert to fixed 3 for color multiplication
  125.  
  126. fixed3 rimLight = rim *(1 - diffuseL)* _rimColor.xyz;
  127. OUT.lighting = saturate(ambientL + vertexLighting + diffuse + rimLight);
  128.  
  129. return OUT;
  130. }
  131.  
  132. fixed4 pShader(vert2Pixel IN) : COLOR
  133. {
  134. fixed4 outColor;
  135. half2 diffuseUVs = TRANSFORM_TEX(IN.uvs, _diffuseMap);
  136. fixed3 texSample = tex2D(_diffuseMap, diffuseUVs);
  137. outColor = fixed4(IN.lighting * texSample * _diffuseColor,1.0);
  138. return outColor;
  139. }
  140.  
  141. ENDCG
  142. }
  143.  
  144. //the second pass for additional lights
  145. Pass
  146. {
  147. Tags{ "LightMode" = "ForwardAdd" }
  148. Blend One One
  149.  
  150. CGPROGRAM
  151. #pragma vertex vShader
  152. #pragma fragment pShader
  153. #include "UnityCG.cginc"
  154.  
  155. uniform fixed3 _diffuseColor;
  156. uniform sampler2D _diffuseMap;
  157. uniform half4 _diffuseMap_ST;
  158.  
  159. uniform fixed4 _LightColor0;
  160.  
  161. struct app2vert {
  162. float4 vertex : POSITION;
  163. fixed2 texCoord : TEXCOORD0;
  164. fixed4 normal : NORMAL;
  165. };
  166. struct vert2Pixel
  167. {
  168. float4 pos : SV_POSITION;
  169. fixed2 uvs : TEXCOORD0;
  170. fixed3 lighting : TEXCOORD1;
  171. };
  172.  
  173. fixed lambert(fixed3 N, fixed3 L)
  174. {
  175. return saturate(dot(N, L));
  176. }
  177. vert2Pixel vShader(app2vert IN)
  178. {
  179. vert2Pixel OUT;
  180. float4x4 WorldViewProjection = UNITY_MATRIX_MVP;
  181. float4x4 WorldInverseTranspose = unity_WorldToObject;
  182. float4x4 World = unity_ObjectToWorld;
  183.  
  184. OUT.pos = mul(WorldViewProjection, IN.vertex);
  185. OUT.uvs = IN.texCoord.xy;
  186.  
  187. fixed3 normalDir = normalize(mul(IN.normal, WorldInverseTranspose).xyz);
  188. half3 vertexToLightSource = _WorldSpaceLightPos0.xyz - (mul(World, IN.vertex).xyz *_WorldSpaceLightPos0.w);
  189. fixed attenuation = lerp(1.0, 1.0 / length(vertexToLightSource), _WorldSpaceLightPos0.w);
  190. fixed3 lightDirection = normalize(vertexToLightSource);
  191.  
  192. fixed diffuseL = lambert(normalDir, lightDirection);
  193. fixed3 diffuse = _LightColor0.xyz* diffuseL * attenuation;
  194.  
  195. OUT.lighting = diffuse;
  196.  
  197. return OUT;
  198. }
  199. fixed4 pShader(vert2Pixel IN) : COLOR
  200. {
  201. fixed4 outColor;
  202. half2 diffuseUVs = TRANSFORM_TEX(IN.uvs, _diffuseMap);
  203. fixed3 texSample = tex2D(_diffuseMap, diffuseUVs);
  204. outColor = fixed4(IN.lighting * texSample * _diffuseColor,1.0);
  205. return outColor;
  206. }
  207.  
  208. ENDCG
  209. }
  210.  
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment