Advertisement
Guest User

IBL shader

a guest
Nov 27th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.73 KB | None | 0 0
  1. Shader "_Shader/IBLshader" {
  2. //Properties can be used as if they are public data members, but they are actually special methods called accessors
  3. Properties {
  4. _DiffuseColor ("Diffuse Color", 2D) = "white" {}
  5. _SpecularColor ("Specular Color", Color) = (1,1,1,1)
  6. _Shininess ("Shininess", range(1,20)) = 1
  7. _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
  8. _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
  9. _Glossiness ("Glossiness", Float ) = 0.5
  10. _AmbientCubemap ("Ambient Cubemap", Cube) = "_Skybox" {}
  11. _AmbientCubemapIntensity ("Ambient Cubemap Intensity", Float ) = 1
  12. _AmbientCubemapContrast ("Ambient Cubemap Contrast", Float ) = 1
  13. }
  14. // mapping of the picture
  15. SubShader {
  16. Tags { "Queue"="Transparent" }
  17. LOD 200
  18.  
  19. Pass {
  20. SetTexture[_Cube] { combine texture }
  21. }
  22. }
  23. SubShader {
  24.  
  25. Pass {
  26. Tags { "LightMode" = "ForwardBase" }
  27.  
  28. CGPROGRAM
  29. //Preprocessor compile directive
  30. #pragma vertex vert
  31. #pragma fragment frag
  32. //Forward rendering base pass (main directional light, lightmaps, SH).
  33. #define UNITY_PASS_FORWARDBASE
  34. //Commonly used global variables and helper functions.
  35. #include "UnityCG.cginc"
  36. //Lighting & shadowing functionality, e.g. surface shaders use this file internally.
  37. #include "AutoLight.cginc"
  38. //Standard surface shader lighting models; automatically included when you’re writing surface shaders.
  39. #include "Lighting.cginc"
  40. //tells the compiler needs to generate a permutation of the shader for each of those defines and will produce two shader variants Off and On
  41. #pragma multi_compile_fwdbase_fullshadows
  42. //Do not compile shader for given renderers. By default shaders are compiled for all renderers.
  43. #pragma exclude_renderers xbox360 ps3 flash
  44. //Which shader target to compile to
  45. #pragma target 3.0
  46.  
  47. /// Keywords used to create light mapping in unity - a negative of #ifdef LIGHTMAP_OFF. #ifndef checks whether the given token has been #defined earlier in the file or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement. #ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.
  48. //Using pragma multi compile to compile more than one shader
  49. #ifndef LIGHTMAP_OFF
  50. //Floating point sampler for 2D Texture
  51. sampler2D unity_Lightmap;
  52. float4 unity_LightmapST;
  53. #ifndef DIRLIGHTMAP_OFF
  54. sampler2D unity_LightmapInd;
  55. #endif
  56. #endif
  57.  
  58. //Uniforms for accessing shader properties declared in properties block
  59. uniform sampler2D _DiffuseColor;
  60. uniform float4 _DiffuseColor_ST;
  61. uniform sampler2D _SpecularColor;
  62. uniform float4 _SpecularColor_ST;
  63. uniform float _Shininess;
  64. uniform sampler2D _MainTex;
  65. uniform sampler2D _Cube;
  66. uniform float4 _Color;
  67. uniform samplerCUBE _AmbientCubemap;
  68. uniform float _AmbientCubemapIntensity;
  69. uniform float _Glossiness;
  70. uniform sampler2D _NormalMap;
  71. uniform float4 _NormalMap_ST;
  72. uniform float _AmbientCubemapContrast;
  73.  
  74. //vertex input data must be passed in a struct
  75. struct vertexInput {
  76. float4 vertex : POSITION; //a vertex position
  77. float3 normal : NORMAL; //the vertex normal
  78. float3 tangent: TANGENT; //the tangent vector (used for normal mapping)
  79. float4 uv0 : TEXCOORD0; // the first UV coordinate
  80. float4 uv1 : TEXCOORD1; // the second UV coordinate
  81. };
  82.  
  83. //vertex output data
  84. struct vertexOutput {
  85. float4 pos : SV_POSITION; // when using UV’s the output often is written as SV_
  86. float4 uv0 : TEXCOORD0; // the first UV coordinate
  87. float4 worldPos: TEXCOORD1;
  88. float3 tangentWorld : TEXCOORD2;
  89. float3 normalWorld : TEXCOORD3;
  90. float3 binormalWorld : TEXCOORD4;
  91.  
  92. LIGHTING_COORDS(5,6) // LIGHTING_COORDS needs two TEXCOORDS in the vertex shader
  93. //multi compile pragma just tells Unity to compile multiple times with mutually exclusive identifiers
  94. #ifndef LIGHTMAP_OFF
  95. float2 uvLM : TEXCOORD7;
  96. #endif
  97. };
  98. //vertex output function
  99. vertexOutput vert(vertexInput input) {
  100. vertexOutput output;
  101. output.uv0 = input.uv0;
  102.  
  103. //transformations
  104. float4x4 modelMatrix = _Object2World; //Current model matrix
  105. float4x4 viewMatrix = UNITY_MATRIX_V; //Current view matrix
  106. float4x4 projectionMatrix = UNITY_MATRIX_P; //Current projection matrix
  107.  
  108. //Find the world position
  109. output.worldPos = mul(_Object2World,input.vertex);
  110.  
  111. //Find the normals in world coordinates
  112. float4x4 modelMatrixInverse = _World2Object; //Inverse of current world matrix
  113.  
  114. output.normalWorld = normalize(mul(float4(input.normal,0),modelMatrixInverse).xyz);
  115. output.tangentWorld = normalize(mul(float4(input.tangent,0),modelMatrix).xyz);
  116. output.binormalWorld = normalize(cross(output.tangentWorld,output.normalWorld));
  117. output.pos = mul(projectionMatrix,mul(viewMatrix,mul(modelMatrix,input.vertex)));
  118. output.uv0 = input.uv0;
  119.  
  120. // Keywords used to create light mapping in unity - a negative of #ifdef LIGHTMAP_OFF
  121. #ifndef LIGHTMAP_OFF
  122. output.uvLM = input.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  123. //specifies the end of a conditional directive
  124. #endif
  125. // part of attenuation in the fragment program
  126. TRANSFER_VERTEX_TO_FRAGMENT(output)
  127. return output;
  128. }
  129. //fragment function
  130. fixed4 frag(vertexOutput input) : COLOR {
  131. float3x3 tangentTransform = float3x3( input.tangentWorld, input.binormalWorld, input.normalWorld);
  132. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - input.worldPos.xyz);
  133. float2 node_3 = input.uv0;
  134. float3 normalLocal = UnpackNormal(tex2D(_NormalMap,TRANSFORM_TEX(node_3.rg, _NormalMap))).rgb;
  135. float3 normalDirection = normalize( mul( normalLocal, tangentTransform ) );
  136.  
  137. // Keywords used to create light mapping in unity - a negative of #ifdef LIGHTMAP_OFF. #ifndef checks whether the given token has been #defined earlier in the file or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement. #ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.
  138. #ifndef LIGHTMAP_OFF
  139. float4 lmtex = tex2D(unity_Lightmap,input.uvLM);
  140. #ifndef DIRLIGHTMAP_OFF
  141. //Unity built-in function DecodeLightmap to decode information from data stored in lightmap textures and built-in UNITY_DIRBASIS macro defining basis vectors for Directional lightmaps
  142. float3 lightmap = DecodeLightmap(lmtex);
  143. //An index into the lightmap array
  144. float3 scalePerBasisVector = DecodeLightmap(tex2D(unity_LightmapInd,i.uvLM));
  145. UNITY_DIRBASIS
  146. // Saturate is a function that clamps it's input between 0 and 1.
  147. half3 normalInRnmBasis = saturate (mul (unity_DirBasis, normalLocal));
  148. lightmap *= dot (normalInRnmBasis, scalePerBasisVector);
  149. #else
  150. float3 lightmap = DecodeLightmap(tex2D(unity_Lightmap,i.uvLM));
  151. #endif
  152. #endif
  153. #ifndef LIGHTMAP_OFF
  154. #ifdef DIRLIGHTMAP_OFF
  155. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  156. #else
  157. //built-in UNITY_DIRBASIS macro defining basis vectors for Directional lightmaps
  158. float3 lightDirection = normalize (scalePerBasisVector.x * unity_DirBasis[0] + scalePerBasisVector.y * unity_DirBasis[1] + scalePerBasisVector.z * unity_DirBasis[2]);
  159. lightDirection = mul(lightDirection,tangentTransform); // Tangent to world
  160. #endif
  161. #else
  162. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  163. #endif
  164. float3 halfDirection = normalize(viewDirection+lightDirection);
  165. ////// Lighting:
  166. //The attenuation of light traditionally matches how light behaves in the physical world; it fades out quickly at first, but has a tiny effect for a very long range.
  167. float attenuation = LIGHT_ATTENUATION(input)*2;
  168. float3 attenColor = attenuation * _LightColor0.xyz;
  169. /////// Diffuse:
  170. //N*L to determine which faces are front-facing and just reduce shadows this way
  171. float NdotL = dot( normalDirection, lightDirection );
  172. #ifndef LIGHTMAP_OFF
  173. float3 diffuse = lightmap;
  174. #else
  175. //Current ambient color
  176. float3 diffuse = max( 0.0, NdotL) * attenColor + UNITY_LIGHTMODEL_AMBIENT.xyz*2;
  177. #endif
  178. ///////// Gloss:
  179. float gloss = exp2(_Glossiness*10.0+1.0);
  180. ////// Specular:
  181. NdotL = max(0.0, NdotL);
  182. //vertex program uses the TRANSFORM_TEX macro from UnityCG.cginc to make sure texture scale&offset is applied correctly, and fragment program just samples the texture and multiplies by the color property.
  183. float3 specularMask = tex2D(_SpecularColor,TRANSFORM_TEX(node_3.rg, _SpecularColor)).rgb;
  184. float3 specular = (floor(attenuation) * _LightColor0.xyz) * pow(max(0,dot(halfDirection,normalDirection)),gloss) * specularMask;
  185. float node_22 = 0.5;
  186. //tex-Cube performs a texture lookup in a given CUBE sampler and, in some cases, a shadow comparison. In this case it combines all the shaders,
  187. float3 finalColor = ( diffuse + ((((texCUBE(_AmbientCubemap,normalDirection).rgb-node_22)*_AmbientCubemapContrast)+node_22)*_AmbientCubemapIntensity) ) * (_Color.rgb*tex2D(_DiffuseColor,TRANSFORM_TEX(node_3.rg, _DiffuseColor)).rgb) + specular;
  188. /// Final Color:
  189. return fixed4(finalColor,1);
  190.  
  191. }
  192. ENDCG
  193. }
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement