Advertisement
shadowx320

MultiE.shader

Sep 14th, 2018
1,197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.35 KB | None | 0 0
  1. Shader "LightweightPipeline/MultiE"
  2. {
  3. Properties
  4. {
  5. // Specular vs Metallic workflow
  6. [HideInInspector] _WorkflowMode("WorkflowMode", Float) = 1.0
  7.  
  8. _Color("Color", Color) = (0.5,0.5,0.5,1)
  9. _MainTex("Albedo", 2D) = "white" {}
  10.  
  11. _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
  12.  
  13. _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
  14. _GlossMapScale("Smoothness Scale", Range(0.0, 1.0)) = 1.0
  15. _SmoothnessTextureChannel("Smoothness texture channel", Float) = 0
  16.  
  17. [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
  18. _MetallicGlossMap("Metallic", 2D) = "white" {}
  19.  
  20. _SpecColor("Specular", Color) = (0.2, 0.2, 0.2)
  21. _SpecGlossMap("Specular", 2D) = "white" {}
  22.  
  23. [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
  24. [ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
  25.  
  26. _BumpScale("Scale", Float) = 1.0
  27. _BumpMap("Normal Map", 2D) = "bump" {}
  28.  
  29. _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
  30. _OcclusionMap("Occlusion", 2D) = "white" {}
  31.  
  32. _EmissionColor("Color", Color) = (0,0,0)
  33. _EmissionMap("Emission", 2D) = "white" {}
  34.  
  35. _SecondEmissionColor("Color", Color) = (0,0,0)
  36. _SecondEmissionMap("Second Emission", 2D) = "white" {}
  37.  
  38. // Blending state
  39. [HideInInspector] _Surface("__surface", Float) = 0.0
  40. [HideInInspector] _Blend("__blend", Float) = 0.0
  41. [HideInInspector] _AlphaClip("__clip", Float) = 0.0
  42. [HideInInspector] _SrcBlend("__src", Float) = 1.0
  43. [HideInInspector] _DstBlend("__dst", Float) = 0.0
  44. [HideInInspector] _ZWrite("__zw", Float) = 1.0
  45. [HideInInspector] _Cull("__cull", Float) = 2.0
  46. }
  47.  
  48. SubShader
  49. {
  50. // Lightweight Pipeline tag is required. If Lightweight pipeline is not set in the graphics settings
  51. // this Subshader will fail. One can add a subshader below or fallback to Standard built-in to make this
  52. // material work with both Lightweight Pipeline and Builtin Unity Pipeline
  53. Tags{ "RenderType" = "Opaque" "RenderPipeline" = "LightweightPipeline" "IgnoreProjector" = "True" }
  54. LOD 300
  55.  
  56. // ------------------------------------------------------------------
  57. // Forward pass. Shades all light in a single pass. GI + emission + Fog
  58. Pass
  59. {
  60. // Lightmode matches the ShaderPassName set in LightweightPipeline.cs. SRPDefaultUnlit and passes with
  61. // no LightMode tag are also rendered by Lightweight Pipeline
  62. Name "StandardLit"
  63. Tags{ "LightMode" = "LightweightForward" }
  64.  
  65. Blend[_SrcBlend][_DstBlend]
  66. ZWrite[_ZWrite]
  67. Cull[_Cull]
  68.  
  69. HLSLPROGRAM
  70. // Required to compile gles 2.0 with standard SRP library
  71. // All shaders must be compiled with HLSLcc and currently only gles is not using HLSLcc by default
  72. #pragma prefer_hlslcc gles
  73. #pragma exclude_renderers d3d11_9x
  74. #pragma target 2.0
  75.  
  76. // -------------------------------------
  77. // Material Keywords
  78. #pragma shader_feature _NORMALMAP
  79. #pragma shader_feature _ALPHATEST_ON
  80. #pragma shader_feature _ALPHAPREMULTIPLY_ON
  81. #pragma shader_feature _EMISSION
  82. #pragma shader_feature _METALLICSPECGLOSSMAP
  83. #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  84. #pragma shader_feature _OCCLUSIONMAP
  85.  
  86. #pragma shader_feature _SPECULARHIGHLIGHTS_OFF
  87. #pragma shader_feature _GLOSSYREFLECTIONS_OFF
  88. #pragma shader_feature _SPECULAR_SETUP
  89.  
  90. // -------------------------------------
  91. // Lightweight Pipeline keywords
  92. #pragma multi_compile _ _ADDITIONAL_LIGHTS
  93. #pragma multi_compile _ _VERTEX_LIGHTS
  94. #pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
  95. #pragma multi_compile _ _SHADOWS_ENABLED
  96. #pragma multi_compile _ _LOCAL_SHADOWS_ENABLED
  97. #pragma multi_compile _ _SHADOWS_SOFT
  98. #pragma multi_compile _ _SHADOWS_CASCADE
  99.  
  100. // -------------------------------------
  101. // Unity defined keywords
  102. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  103. #pragma multi_compile _ LIGHTMAP_ON
  104. #pragma multi_compile_fog
  105.  
  106. //--------------------------------------
  107. // GPU Instancing
  108. #pragma multi_compile_instancing
  109.  
  110. #pragma vertex LitPassVertex
  111. #pragma fragment LitPassFragment
  112.  
  113. // Including the following two function is enought for shading with Lightweight Pipeline. Everything is included in them.
  114. // Core.hlsl will include SRP shader library, all constant buffers not related to materials (perobject, percamera, perframe).
  115. // It also includes matrix/space conversion functions and fog.
  116. // Lighting.hlsl will include the light functions/data to abstract light constants. You should use GetMainLight and GetLight functions
  117. // that initialize Light struct. Lighting.hlsl also include GI, Light BDRF functions. It also includes Shadows.
  118. #include "LWRP/ShaderLibrary/Core.hlsl"
  119. #include "LWRP/ShaderLibrary/Lighting.hlsl"
  120.  
  121. // Not required but included here for simplicity. This defines all material related constants for the Standard surface shader like _Color, _MainTex, and so on.
  122. // These are specific to this shader. You should define your own constants.
  123. #include "LWRP/ShaderLibrary/InputSurfacePBR.hlsl"
  124.  
  125. struct VertexInput
  126. {
  127. float4 vertex : POSITION;
  128. float3 normal : NORMAL;
  129. float4 tangent : TANGENT;
  130. float2 texcoord : TEXCOORD0;
  131. float2 lightmapUV : TEXCOORD1;
  132. };
  133.  
  134. struct VertexOutput
  135. {
  136. float2 uv : TEXCOORD0;
  137. float2 lightmapUV : TEXCOORD1;
  138. float4 positionWSAndFogFactor : TEXCOORD2;
  139. half3 normal : TEXCOORD3;
  140.  
  141. #if _NORMALMAP
  142. half3 tangent : TEXCOORD4;
  143. half3 binormal : TEXCOORD5;
  144. #endif
  145.  
  146. #ifdef _SHADOWS_ENABLED
  147. float4 shadowCoord : TEXCOORD6;
  148. #endif
  149. float4 clipPos : SV_POSITION;
  150. };
  151.  
  152. VertexOutput LitPassVertex(VertexInput v)
  153. {
  154. VertexOutput o = (VertexOutput)0;
  155.  
  156. // Pretty much same as builtin Unity shader library.
  157. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  158. o.lightmapUV = v.lightmapUV.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  159.  
  160. // SRP shader library adds some functions to convert between spaces.
  161. // TransformObjectToHClip and some other functions are defined.
  162.  
  163. float3 positionWS = TransformObjectToWorld(v.vertex.xyz);
  164. o.clipPos = TransformWorldToHClip(positionWS);
  165.  
  166. float fogFactor = ComputeFogFactor(o.clipPos.z);
  167. o.positionWSAndFogFactor = float4(positionWS, fogFactor);
  168.  
  169. #ifdef _NORMALMAP
  170. OutputTangentToWorld(v.tangent, v.normal, o.tangent, o.binormal, o.normal);
  171. #else
  172. o.normal = TransformObjectToWorldNormal(v.normal);
  173. #endif
  174.  
  175. #ifdef _SHADOWS_ENABLED
  176. #if SHADOWS_SCREEN
  177. o.shadowCoord = ComputeShadowCoord(o.clipPos);
  178. #else
  179. o.shadowCoord = TransformWorldToShadowCoord(positionWS);
  180. #endif
  181. #endif
  182. return o;
  183. }
  184.  
  185. half4 LitPassFragment(VertexOutput IN) : SV_Target
  186. {
  187. // Surface data contains albedo, metallic, specular, smoothness, occlusion, emission and alpha
  188. // InitializeStandarLitSurfaceData initializes based on the rules for standard shader.
  189. // You can write your own function to initialize the surface data of your shader.
  190. SurfaceData surfaceData;
  191. InitializeStandardLitSurfaceData(IN.uv, surfaceData);
  192.  
  193. #if _NORMALMAP
  194. half3 normalWS = TangentToWorldNormal(surfaceData.normalTS, IN.tangent, IN.binormal, IN.normal);
  195. #else
  196. half3 normalWS = normalize(IN.normal);
  197. #endif
  198.  
  199. #ifdef LIGHTMAP_ON
  200. half3 bakedGI = SampleLightmap(IN.lightmapUV, normalWS);
  201. #else
  202. half3 bakedGI = SampleSH(normalWS);
  203. #endif
  204.  
  205. float3 positionWS = IN.positionWSAndFogFactor.xyz;
  206. half3 viewDirectionWS = SafeNormalize(GetCameraPositionWS() - positionWS);
  207.  
  208. // BRDFData holds energy conserving diffuse and specular material reflections and its roughness.
  209. // It's easy to plugin your own shading fuction. You just need replace LightingPhysicallyBased function
  210. // below with your own.
  211. BRDFData brdfData;
  212. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  213.  
  214. // Main light is the brighest directional light
  215. // Main light uses a separate set of constant buffers for light to avoid indexing and have tight memory access.
  216. Light mainLight = GetMainLight();
  217. #ifdef _SHADOWS_ENABLED
  218. mainLight.attenuation = MainLightRealtimeShadowAttenuation(IN.shadowCoord);
  219. #endif
  220.  
  221. half3 color = GlobalIllumination(brdfData, bakedGI, surfaceData.occlusion, normalWS, viewDirectionWS);
  222. color += LightingPhysicallyBased(brdfData, mainLight, normalWS, viewDirectionWS);
  223.  
  224. #ifdef _ADDITIONAL_LIGHTS
  225. int pixelLightCount = GetPixelLightCount();
  226. for (int i = 0; i < pixelLightCount; ++i)
  227. {
  228. Light light = GetLight(i, positionWS);
  229. light.attenuation *= LocalLightRealtimeShadowAttenuation(light.index, positionWS);
  230. color += LightingPhysicallyBased(brdfData, light, normalWS, viewDirectionWS);
  231. }
  232. #endif
  233.  
  234. color += surfaceData.emission;
  235.  
  236. float fogFactor = IN.positionWSAndFogFactor.w;
  237. ApplyFog(color, fogFactor);
  238. return half4(color, surfaceData.alpha);
  239. }
  240. ENDHLSL
  241. }
  242.  
  243. // Used for shadows
  244. UsePass "LightweightPipeline/Standard (Physically Based)/ShadowCaster"
  245.  
  246. // Used for depth prepass
  247. // If shadows cascade are enabled we need to perform a depth prepass.
  248. // We also need to use a depth prepass in some cases camera require depth texture
  249. // (e.g, MSAA is enabled and we can't resolve with Texture2DMS
  250. UsePass "LightweightPipeline/Standard (Physically Based)/DepthOnly"
  251.  
  252. // Used for Baking GI. This pass is stripped from build.
  253. UsePass "LightweightPipeline/Standard (Physically Based)/Meta"
  254. }
  255.  
  256. FallBack "Hidden/InternalErrorShader"
  257. CustomEditor "LightweightStandardGUI"
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement