Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. Shader "Cg per-pixel lighting with vertex lights" {
  2. Properties {
  3. _Color ("Diffuse Material Color", Color) = (1,1,1,1)
  4. _SpecColor ("Specular Material Color", Color) = (1,1,1,1)
  5. _Shininess ("Shininess", Float) = 10
  6. }
  7. SubShader {
  8. Pass {
  9. Tags { "LightMode" = "ForwardBase" } // pass for
  10. // 4 vertex lights, ambient light & first pixel light
  11.  
  12. CGPROGRAM
  13. #pragma multi_compile_fwdbase
  14. #pragma vertex vert
  15. #pragma fragment frag
  16.  
  17. #include "UnityCG.cginc"
  18. uniform float4 _LightColor0;
  19. // color of light source (from "Lighting.cginc")
  20.  
  21. // User-specified properties
  22. uniform float4 _Color;
  23. uniform float4 _SpecColor;
  24. uniform float _Shininess;
  25.  
  26. struct vertexInput {
  27. float4 vertex : POSITION;
  28. float3 normal : NORMAL;
  29. };
  30. struct vertexOutput {
  31. float4 pos : SV_POSITION;
  32. float4 posWorld : TEXCOORD0;
  33. float3 normalDir : TEXCOORD1;
  34. float3 vertexLighting : TEXCOORD2;
  35. };
  36.  
  37. vertexOutput vert(vertexInput input)
  38. {
  39. vertexOutput output;
  40.  
  41. float4x4 modelMatrix = unity_ObjectToWorld;
  42. float4x4 modelMatrixInverse = unity_WorldToObject;
  43.  
  44. output.posWorld = mul(modelMatrix, input.vertex);
  45. output.normalDir = normalize(
  46. mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);
  47. output.pos = UnityObjectToClipPos(input.vertex);
  48.  
  49. // Diffuse reflection by four "vertex lights"
  50. output.vertexLighting = float3(0.0, 0.0, 0.0);
  51. #ifdef VERTEXLIGHT_ON
  52. for (int index = 0; index < 4; index++)
  53. {
  54. float4 lightPosition = float4(unity_4LightPosX0[index],
  55. unity_4LightPosY0[index],
  56. unity_4LightPosZ0[index], 1.0);
  57.  
  58. float3 vertexToLightSource =
  59. lightPosition.xyz - output.posWorld.xyz;
  60. float3 lightDirection = normalize(vertexToLightSource);
  61. float squaredDistance =
  62. dot(vertexToLightSource, vertexToLightSource);
  63. float attenuation = 1.0 / (1.0 +
  64. unity_4LightAtten0[index] * squaredDistance);
  65. float3 diffuseReflection = attenuation
  66. * unity_LightColor[index].rgb * _Color.rgb
  67. * max(0.0, dot(output.normalDir, lightDirection));
  68.  
  69. output.vertexLighting =
  70. output.vertexLighting + diffuseReflection;
  71. }
  72. #endif
  73. return output;
  74. }
  75.  
  76. float4 frag(vertexOutput input) : COLOR
  77. {
  78. float3 normalDirection = normalize(input.normalDir);
  79. float3 viewDirection = normalize(
  80. _WorldSpaceCameraPos - input.posWorld.xyz);
  81. float3 lightDirection;
  82. float attenuation;
  83.  
  84. if (0.0 == _WorldSpaceLightPos0.w) // directional light?
  85. {
  86. attenuation = 1.0; // no attenuation
  87. lightDirection =
  88. normalize(_WorldSpaceLightPos0.xyz);
  89. }
  90. else // point or spot light
  91. {
  92. float3 vertexToLightSource =
  93. _WorldSpaceLightPos0.xyz - input.posWorld.xyz;
  94. float distance = length(vertexToLightSource);
  95. attenuation = 1.0 / distance; // linear attenuation
  96. lightDirection = normalize(vertexToLightSource);
  97. }
  98.  
  99. float3 ambientLighting =
  100. UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;
  101.  
  102. float3 diffuseReflection =
  103. attenuation * _LightColor0.rgb * _Color.rgb
  104. * max(0.0, dot(normalDirection, lightDirection));
  105.  
  106. float3 specularReflection;
  107. if (dot(normalDirection, lightDirection) < 0.0)
  108. // light source on the wrong side?
  109. {
  110. specularReflection = float3(0.0, 0.0, 0.0);
  111. // no specular reflection
  112. }
  113. else // light source on the right side
  114. {
  115. specularReflection = attenuation * _LightColor0.rgb
  116. * _SpecColor.rgb * pow(max(0.0, dot(
  117. reflect(-lightDirection, normalDirection),
  118. viewDirection)), _Shininess);
  119. }
  120.  
  121. return float4(input.vertexLighting + ambientLighting
  122. + diffuseReflection + specularReflection, 1.0);
  123. }
  124. ENDCG
  125. }
  126.  
  127. Pass {
  128. Tags { "LightMode" = "ForwardAdd" }
  129. // pass for additional light sources
  130. Blend One One // additive blending
  131.  
  132. CGPROGRAM
  133.  
  134. #pragma vertex vert
  135. #pragma fragment frag
  136.  
  137. #include "UnityCG.cginc"
  138. uniform float4 _LightColor0;
  139. // color of light source (from "Lighting.cginc")
  140.  
  141. // User-specified properties
  142. uniform float4 _Color;
  143. uniform float4 _SpecColor;
  144. uniform float _Shininess;
  145.  
  146. struct vertexInput {
  147. float4 vertex : POSITION;
  148. float3 normal : NORMAL;
  149. };
  150. struct vertexOutput {
  151. float4 pos : SV_POSITION;
  152. float4 posWorld : TEXCOORD0;
  153. float3 normalDir : TEXCOORD1;
  154. };
  155.  
  156. vertexOutput vert(vertexInput input)
  157. {
  158. vertexOutput output;
  159.  
  160. float4x4 modelMatrix = unity_ObjectToWorld;
  161. float4x4 modelMatrixInverse = unity_WorldToObject;
  162.  
  163. output.posWorld = mul(modelMatrix, input.vertex);
  164. output.normalDir = normalize(
  165. mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);
  166. output.pos = UnityObjectToClipPos(input.vertex);
  167. return output;
  168. }
  169.  
  170. float4 frag(vertexOutput input) : COLOR
  171. {
  172. float3 normalDirection = normalize(input.normalDir);
  173.  
  174. float3 viewDirection = normalize(
  175. _WorldSpaceCameraPos.xyz - input.posWorld.xyz);
  176. float3 lightDirection;
  177. float attenuation;
  178.  
  179. if (0.0 == _WorldSpaceLightPos0.w) // directional light?
  180. {
  181. attenuation = 1.0; // no attenuation
  182. lightDirection =
  183. normalize(_WorldSpaceLightPos0.xyz);
  184. }
  185. else // point or spot light
  186. {
  187. float3 vertexToLightSource =
  188. _WorldSpaceLightPos0.xyz - input.posWorld.xyz;
  189. float distance = length(vertexToLightSource);
  190. attenuation = 1.0 / distance; // linear attenuation
  191. lightDirection = normalize(vertexToLightSource);
  192. }
  193.  
  194. float3 diffuseReflection =
  195. attenuation * _LightColor0.rgb * _Color.rgb
  196. * max(0.0, dot(normalDirection, lightDirection));
  197.  
  198. float3 specularReflection;
  199. if (dot(normalDirection, lightDirection) < 0.0)
  200. // light source on the wrong side?
  201. {
  202. specularReflection = float3(0.0, 0.0, 0.0);
  203. // no specular reflection
  204. }
  205. else // light source on the right side
  206. {
  207. specularReflection = attenuation * _LightColor0.rgb
  208. * _SpecColor.rgb * pow(max(0.0, dot(
  209. reflect(-lightDirection, normalDirection),
  210. viewDirection)), _Shininess);
  211. }
  212.  
  213. return float4(diffuseReflection
  214. + specularReflection, 1.0);
  215. // no ambient lighting in this pass
  216. }
  217.  
  218. ENDCG
  219. }
  220.  
  221. }
  222. Fallback "Specular"
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement