Advertisement
Guest User

shader code help

a guest
Nov 3rd, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. Shader "Custom/LightMap Blend Diffuse Specular Transparency" {
  2. Properties {
  3. _Color ("MainColor", Color) = (1, 1, 1, 1)
  4. _SpecColor ("Specular Color", Color) = (1, 1, 1, 1)
  5. _Shininess ("Shininess", Float) = 10
  6. _MainTex ("Base (RGB)", 2D) = "white" {}
  7. _LightMap01 ( "LightMap 1", 2D ) = "white" {}
  8. _LightMap02 ( "LightMap 2", 2D ) = "white" {}
  9. _Trans ("Transparency", Range(0, 1)) = 0
  10. }
  11. SubShader {
  12. Pass
  13. {
  14. Tags { "LightMode" = "ForwardBase" }
  15.  
  16. CGPROGRAM
  17. #pragma multi_compile_fwdbase
  18. #pragma vertex vert
  19. #pragma fragment frag
  20. #include "UnityCG.cginc"
  21.  
  22. uniform float4 _LightColor0;
  23.  
  24. float4 _Color;
  25. float4 _SpecColor;
  26. float _Shininess;
  27.  
  28. struct vertexIn
  29. {
  30. float4 pos : POSITION;
  31. float3 norm : normal;
  32. };
  33.  
  34. struct vertexOut
  35. {
  36. float4 pos : SV_POSITION;
  37. float3 posWorld : TEXCOORD0;
  38. float3 norm : TEXCOORD1;
  39. float3 vertexLighting : TEXCOORD2;
  40. };
  41.  
  42. vertexOut vert(vertexIn i)
  43. {
  44. vertexOut o;
  45.  
  46. float4x4 modelMatrix = _Object2World;
  47. float4x4 modelMatrixInverse = _World2Object;
  48.  
  49. o.posWorld = mul(modelMatrix, i.pos);
  50. o.norm = normalize( mul(float4(i.norm, 0), modelMatrixInverse).xyz );
  51. o.pos = mul( UNITY_MATRIX_MVP, i.pos);
  52.  
  53. o.vertexLighting = float3(0, 0, 0);
  54. for (int i = 0; i < 8; i++)
  55. {
  56. float4 lightPosition = unity_LightPosition[i];
  57.  
  58. float3 vertexToLightSource = lightPosition.xyz - o.posWorld.xyz;
  59.  
  60. float3 lightDirection = normalize(vertexToLightSource);
  61. float squaredDistance = dot(vertexToLightSource, vertexToLightSource);
  62. float attenuation = 1.0 / (1.0 + unity_LightAtten[i] * squaredDistance);
  63. float3 diffuseReflection = attenuation
  64. * unity_LightColor[i].rgb * _Color.rgb
  65. * max(0.0, dot(o.norm, lightDirection));
  66.  
  67. o.vertexLighting += diffuseReflection;
  68. }
  69.  
  70.  
  71.  
  72. return o;
  73. }
  74.  
  75. float4 frag(vertexOut o) : COLOR
  76. {
  77. float3 normalDirection = normalize(o.norm);
  78. float3 viewDirection = normalize(_WorldSpaceCameraPos - o.posWorld.xyz);
  79. float3 lightDirection = float3(0, 0, 0);
  80. float attenuation;
  81.  
  82. lightDirection = normalize(_WorldSpaceLightPos0.xyz) * (1 - _WorldSpaceLightPos0.w);
  83.  
  84. float3 PointLightDirection = _WorldSpaceLightPos0.xyz - o.posWorld.xyz;
  85. float PointLightLength = length(PointLightDirection);
  86.  
  87. lightDirection += normalize(PointLightDirection) * _WorldSpaceLightPos0.w;
  88.  
  89. attenuation = 1.0 / PointLightLength;
  90.  
  91. float3 diffuseColor = lerp(_LightColor0.rgb * _Color.rgb * max(0, dot(normalDirection, lightDirection)),
  92. _LightColor0.rgb * _Color.rgb * max(0, dot(normalDirection, lightDirection) * attenuation), _WorldSpaceLightPos0.w);
  93.  
  94. float3 specularColor = (_LightColor0.rgb * _Color.rgb * max(0, dot(reflect(-lightDirection, normalDirection), viewDirection)))
  95. * max(0, dot (normalDirection, lightDirection));
  96.  
  97. return float4(o.vertexLighting, 1);
  98. }
  99. ENDCG
  100. }
  101.  
  102. Pass
  103. {
  104. Tags { "LightMode" = "ForwardAdd" }
  105.  
  106. Blend One One
  107.  
  108. CGPROGRAM
  109. #pragma vertex vert
  110. #pragma fragment frag
  111. #include "UnityCG.cginc"
  112.  
  113. uniform float4 _LightColor0;
  114.  
  115. sampler2D _MainTex;
  116.  
  117. float4 _Color;
  118. float4 _SpecColor;
  119. float _Shininess;
  120.  
  121. struct vertexIn
  122. {
  123. float4 pos : POSITION;
  124. float3 norm : normal;
  125. };
  126.  
  127. struct vertexOut
  128. {
  129. float4 pos : SV_POSITION;
  130. float4 col : COLOR;
  131. float3 posWorld : TEXCOORD0;
  132. float3 norm : TEXCOORD1;
  133. };
  134.  
  135. vertexOut vert(vertexIn i)
  136. {
  137. vertexOut o;
  138.  
  139. float4x4 modelMatrix = _Object2World;
  140. float4x4 modelMatrixInverse = _World2Object;
  141.  
  142. o.posWorld = mul(modelMatrix, i.pos);
  143. o.norm = normalize( mul(float4(i.norm, 0), modelMatrixInverse).xyz );
  144. o.pos = mul( UNITY_MATRIX_MVP, i.pos);
  145.  
  146. return o;
  147. }
  148.  
  149. float4 frag(vertexOut o) : COLOR
  150. {
  151. float3 normalDirection = normalize(o.norm);
  152. float3 viewDirection = normalize(_WorldSpaceCameraPos - o.posWorld.xyz);
  153. float3 lightDirection = float3(0, 0, 0);
  154. float attenuation;
  155.  
  156. lightDirection = normalize(_WorldSpaceLightPos0.xyz) * (1 - _WorldSpaceLightPos0.w);
  157.  
  158. float3 PointLightDirection = _WorldSpaceLightPos0.xyz - o.posWorld.xyz;
  159. float PointLightLength = length(PointLightDirection);
  160.  
  161. lightDirection += normalize(PointLightDirection) * _WorldSpaceLightPos0.w;
  162.  
  163. attenuation = 1.0 / PointLightLength;
  164.  
  165. float3 diffuseColor = lerp( _LightColor0.rgb * _Color.rgb * max(0, dot(normalDirection, lightDirection)),
  166. _LightColor0.rgb * _Color.rgb * max(0, dot(normalDirection, lightDirection) * attenuation), _WorldSpaceLightPos0.w );
  167.  
  168. float3 specularColor = ( _LightColor0.rgb * _Color.rgb * max(0, dot(reflect(-lightDirection, normalDirection), viewDirection)) )
  169. * max(0, dot (normalDirection, lightDirection));
  170.  
  171. return float4(diffuseColor, 0);
  172. }
  173. ENDCG
  174. }
  175. }
  176. FallBack "Diffuse"
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement