Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1.  
  2. Shader "318/BumpMapping" // shader name
  3. {
  4. Properties
  5. {
  6. _MainTex("MainTex", 2D) = "white" {}
  7. _BumpTex("BumpMap", 2D) = "white" {}
  8. _farbeF("Farbe", Color) = (0.1,0.7,1,0.8)
  9. _SpecColor("SpeckColor",Color) = (1,1,1,1)
  10. _Shiny("Shiny",Float) = 10
  11.  
  12. }
  13. CGINCLUDE
  14. //common code for all passes
  15.  
  16. #include "UnityCG.cginc" //hat mehr Globale constant values aus Unity
  17.  
  18. struct VertexInput
  19. {
  20. float4 pos : POSITION;
  21. float3 normal : NORMAL;
  22. float4 tex : TEXCOORD0;
  23. float4 tan : TANGENT;
  24. };
  25.  
  26. struct VertexOutput
  27. {
  28. float4 pos : SV_POSITION;
  29. float4 posWorld : TEXCOORD0;
  30. float4 tex : TEXCOORD1;
  31. float3 tanWorld : TEXCOORD2;
  32. float3 normalWorld : TEXCOORD3;
  33. float3 binormalWorld : TEXOORD4;
  34. };
  35.  
  36. uniform float4 _LightColor0;
  37. uniform sampler2D _MainTex;
  38. uniform float4 _MainTex_ST; //tillling and offset parameters
  39. uniform sampler2D _BumpTex;
  40. uniform float4 _BumpTex_ST; //tillling and offset parameters
  41. uniform float4 _farbeF;
  42. uniform float4 _SpecColor;
  43. uniform float _Shiny;
  44.  
  45. //VERTEX shader
  46. VertexOutput VS(VertexInput vi)
  47. {
  48. VertexOutput vo;
  49.  
  50. float4x4 modelMatrix = unity_ObjectToWorld;
  51. float4x4 modelMatrixInvers = unity_WorldToObject;
  52.  
  53. vo.tanWorld = normalize(
  54. mul(modelMatrix, float4(vi.tan.xyz, 0)).xyz);
  55. vo.normalWorld = normalize(
  56. mul(float4(vi.normal, 0), modelMatrixInvers).xyz);
  57. vo.binormalWorld = normalize(
  58. cross(vo.normalWorld, vo.tanWorld)
  59. * vi.tan.w); // tan.w is nur wegen Unity....
  60.  
  61. vo.posWorld = mul(modelMatrix, vi.pos);
  62. vo.pos = UnityObjectToClipPos(vi.pos);
  63. vo.tex = vi.tex;
  64. return vo;
  65. }
  66.  
  67. float4 PS_A(VertexOutput input) : COLOR
  68. { //mit Ambient
  69.  
  70. float4 encodedNormal = tex2D(_BumpTex,
  71. _BumpTex_ST.xy * input.tex.xy + _BumpTex_ST.zw);
  72.  
  73. float3 localCoords = float3(
  74. 2.0 *encodedNormal.a - 1,
  75. 2.0 *encodedNormal.g - 1,
  76. 0);
  77. localCoords.z = sqrt(1.0 - dot(localCoords, localCoords));
  78. //approximation von localCoords.z = 1 -0.5 * dot(localCoords,localCoords)
  79.  
  80. float3x3 local2WorldTranpose = float3x3(
  81. input.tanWorld,
  82. input.binormalWorld,
  83. input.normalWorld);
  84.  
  85. //das hier ist der grosse Unterschied!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  86. float3 normalDir = normalize(mul(localCoords, local2WorldTranpose));
  87.  
  88. float3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz);
  89.  
  90. float3 lightDir;
  91. float attenuation;
  92.  
  93. //wenn in der .w eine 0 steht -> dannn directional light
  94. if (0.0 == _WorldSpaceLightPos0.w)
  95. {
  96. attenuation = 1;
  97. lightDir = normalize(_WorldSpaceLightPos0.xyz);
  98. }
  99. else
  100. { //spot or point light
  101. float3 vertexToLight = _WorldSpaceLightPos0.xyz
  102. - input.posWorld.xyz;
  103. float distance = length(vertexToLight);
  104. attenuation = 1.0 / distance;
  105. lightDir = normalize(vertexToLight);
  106. }
  107.  
  108. float3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb * _farbeF;
  109.  
  110. float3 diffRefl = attenuation * _LightColor0.rgb * _farbeF.rgb
  111. *max(0.0, dot(normalDir, lightDir));
  112.  
  113. float3 specRefl;
  114. if (dot(normalDir, lightDir) < 0)
  115. {
  116. //licht auf falscher seite?
  117. specRefl.rgb = 0;
  118. }
  119. else
  120. {
  121. specRefl = attenuation * _LightColor0.rgb * _SpecColor.rgb *
  122. pow(max(0.0, dot(reflect(-lightDir, normalDir), viewDir)), _Shiny);
  123. }
  124.  
  125. float4 diff = tex2D(_MainTex, _MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw);
  126.  
  127. return float4(diff * (ambient + diffRefl + specRefl), 1.0);
  128.  
  129. }
  130.  
  131. float4 PS(VertexOutput input) : COLOR
  132. { //mit Ambient
  133.  
  134. float4 encodedNormal = tex2D(_BumpTex,
  135. _BumpTex_ST.xy * input.tex.xy + _BumpTex_ST.zw);
  136.  
  137. float3 localCoords = float3(
  138. 2.0 *encodedNormal.a - 1,
  139. 2.0 *encodedNormal.g - 1,
  140. 0);
  141. localCoords.z = sqrt(1.0 - dot(localCoords, localCoords));
  142. //approximation von localCoords.z = 1 -0.5 * dot(localCoords,localCoords)
  143.  
  144. float3x3 local2WorldTranpose = float3x3(
  145. input.tanWorld,
  146. input.binormalWorld,
  147. input.normalWorld);
  148.  
  149. //das hier ist der grosse Unterschied!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  150. float3 normalDir = normalize(mul(localCoords, local2WorldTranpose));
  151.  
  152. float3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz);
  153.  
  154. float3 lightDir;
  155. float attenuation;
  156.  
  157. //wenn in der .w eine 0 steht -> dannn directional light
  158. if (0.0 == _WorldSpaceLightPos0.w)
  159. {
  160. attenuation = 1;
  161. lightDir = normalize(_WorldSpaceLightPos0.xyz);
  162. }
  163. else
  164. { //spot or point light
  165. float3 vertexToLight = _WorldSpaceLightPos0.xyz
  166. - input.posWorld.xyz;
  167. float distance = length(vertexToLight);
  168. attenuation = 1.0 / distance;
  169. lightDir = normalize(vertexToLight);
  170. }
  171.  
  172. float3 diffRefl = attenuation * _LightColor0.rgb * _farbeF.rgb
  173. *max(0.0, dot(normalDir, lightDir));
  174.  
  175. float3 specRefl;
  176. if (dot(normalDir, lightDir) < 0)
  177. {
  178. //licht auf falscher seite?
  179. specRefl.rgb = 0;
  180. }
  181. else
  182. {
  183. specRefl = attenuation * _LightColor0.rgb * _SpecColor.rgb *
  184. pow(max(0.0, dot(reflect(-lightDir, normalDir), viewDir)), _Shiny);
  185. }
  186.  
  187. float4 diff = tex2D(_MainTex, _MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw);
  188.  
  189. return float4(diff * (diffRefl + specRefl), 1.0);
  190.  
  191. }
  192.  
  193. ENDCG // -------------END CG
  194.  
  195. SubShader
  196. {
  197. Pass
  198. {
  199. Tags{"LightMode" = "ForwardBase"}
  200. ///pass for ambient
  201. CGPROGRAM
  202. #pragma vertex VS
  203. #pragma fragment PS_A
  204. ENDCG
  205. }
  206. Pass
  207. {
  208. Tags{"LightMode" = "ForwardAdd"}
  209. ///pass for ambient
  210. CGPROGRAM
  211. #pragma vertex VS
  212. #pragma fragment PS
  213. ENDCG
  214. }
  215. }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement