Guest User

Untitled

a guest
Jul 17th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.90 KB | None | 0 0
  1. // Copyright (c) Valve Corporation, All rights reserved. ======================================================================================================
  2.  
  3. Shader "Valve/vr_standard"
  4. {
  5. Properties
  6. {
  7. [Toggle( S_UNLIT )] g_bUnlit( "g_bUnlit", Int ) = 0
  8.  
  9. _Color( "Color", Color ) = ( 1, 1, 1, 1 )
  10. _MainTex( "Albedo", 2D ) = "white" {}
  11.  
  12. _Cutoff( "Alpha Cutoff", Range( 0.0, 1.0 ) ) = 0.5
  13.  
  14. _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
  15. _SpecColor("Specular", Color) = (0.2,0.2,0.2)
  16. _SpecGlossMap("Specular", 2D) = "white" {}
  17.  
  18. g_flReflectanceMin( "g_flReflectanceMin", Range( 0.0, 1.0 ) ) = 0.0
  19. g_flReflectanceMax( "g_flReflectanceMax", Range( 0.0, 1.0 ) ) = 1.0
  20. [HideInInspector] g_flReflectanceScale( "g_flReflectanceScale", Range( 0.0, 1.0 ) ) = 1.0
  21. [HideInInspector] g_flReflectanceBias( "g_flReflectanceBias", Range( 0.0, 1.0 ) ) = 0.0
  22.  
  23. [Gamma] _Metallic( "Metallic", Range( 0.0, 1.0 ) ) = 0.0
  24. _MetallicGlossMap( "Metallic", 2D ) = "white" {}
  25.  
  26. _BumpScale( "Scale", Float ) = 1.0
  27. [Normal] _BumpMap( "Normal Map", 2D ) = "bump" {}
  28.  
  29. _Parallax ( "Height Scale", Range ( 0.005, 0.08 ) ) = 0.02
  30. _ParallaxMap ( "Height Map", 2D ) = "black" {}
  31.  
  32. _OcclusionStrength( "Strength", Range( 0.0, 1.0 ) ) = 1.0
  33. _OcclusionMap( "Occlusion", 2D ) = "white" {}
  34. _OcclusionStrengthDirectDiffuse( "StrengthDirectDiffuse", Range( 0.0, 1.0 ) ) = 1.0
  35. _OcclusionStrengthDirectSpecular( "StrengthDirectSpecular", Range( 0.0, 1.0 ) ) = 1.0
  36. _OcclusionStrengthIndirectDiffuse( "StrengthIndirectDiffuse", Range( 0.0, 1.0 ) ) = 1.0
  37. _OcclusionStrengthIndirectSpecular( "StrengthIndirectSpecular", Range( 0.0, 1.0 ) ) = 1.0
  38.  
  39. g_flCubeMapScalar( "Cube Map Scalar", Range( 0.0, 2.0 ) ) = 1.0
  40.  
  41. _EmissionColor( "Color", Color ) = ( 0, 0, 0 )
  42. _EmissionMap( "Emission", 2D ) = "white" {}
  43.  
  44. _DetailMask( "Detail Mask", 2D ) = "white" {}
  45.  
  46. _DetailAlbedoMap( "Detail Albedo x2", 2D ) = "grey" {}
  47. _DetailNormalMapScale( "Scale", Float ) = 1.0
  48. _DetailNormalMap( "Normal Map", 2D ) = "bump" {}
  49.  
  50. g_tOverrideLightmap( "Override Lightmap", 2D ) = "white" {}
  51.  
  52. [Enum(UV0,0,UV1,1)] _UVSec ( "UV Set for secondary textures", Float ) = 0
  53.  
  54. [Toggle( S_RECEIVE_SHADOWS )] g_bReceiveShadows( "g_bReceiveShadows", Int ) = 1
  55.  
  56. [Toggle( S_RENDER_BACKFACES )] g_bRenderBackfaces( "g_bRenderBackfaces", Int ) = 0
  57.  
  58. [Toggle( S_WORLD_ALIGNED_TEXTURE )] g_bWorldAlignedTexture( "g_bWorldAlignedTexture", Int ) = 0
  59. g_vWorldAlignedTextureSize( "g_vWorldAlignedTextureSize", Vector ) = ( 1.0, 1.0, 1.0, 0.0 )
  60. g_vWorldAlignedTextureNormal( "g_vWorldAlignedTextureNormal", Vector ) = ( 0.0, 1.0, 0.0, 0.0 )
  61. g_vWorldAlignedTexturePosition( "g_vWorldAlignedTexturePosition", Vector ) = ( 0.0, 0.0, 0.0, 0.0 )
  62. [HideInInspector] g_vWorldAlignedNormalTangentU( "g_vWorldAlignedNormalTangentU", Vector ) = ( -1.0, 0.0, 0.0, 0.0)
  63. [HideInInspector] g_vWorldAlignedNormalTangentV( "g_vWorldAlignedNormalTangentV", Vector ) = ( 0.0, 0.0, 1.0, 0.0)
  64.  
  65. [HideInInspector] _SpecularMode( "__specularmode", Int ) = 1
  66. [HideInInspector] _Cull ( "__cull", Int ) = 2
  67.  
  68. // Blending state
  69. [HideInInspector] _Mode ( "__mode", Float ) = 0.0
  70. [HideInInspector] _SrcBlend ( "__src", Float ) = 1.0
  71. [HideInInspector] _DstBlend ( "__dst", Float ) = 0.0
  72. [HideInInspector] _ZWrite ( "__zw", Float ) = 1.0
  73. [HideInInspector] _FogMultiplier ( "__fogmult", Float ) = 1.0
  74. }
  75.  
  76. SubShader
  77. {
  78. Tags { "RenderType" = "Opaque" "PerformanceChecks" = "False" }
  79. LOD 300
  80.  
  81. //-------------------------------------------------------------------------------------------------------------------------------------------------------------
  82. // Base forward pass (directional light, emission, lightmaps, ...)
  83. //-------------------------------------------------------------------------------------------------------------------------------------------------------------
  84. Pass
  85. {
  86. Name "FORWARD"
  87. Tags { "LightMode" = "ForwardBase" "PassFlags" = "OnlyDirectional" } // NOTE: "OnlyDirectional" prevents Unity from baking dynamic lights into SH terms at runtime
  88.  
  89. Blend [_SrcBlend] [_DstBlend]
  90. ZWrite [_ZWrite]
  91. Cull [_Cull]
  92.  
  93. CGPROGRAM
  94. #pragma target 5.0
  95. #pragma only_renderers d3d11
  96. #pragma exclude_renderers gles
  97.  
  98. //-------------------------------------------------------------------------------------------------------------------------------------------------------------
  99. #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
  100. #pragma shader_feature _NORMALMAP
  101. #pragma shader_feature _METALLICGLOSSMAP
  102. #pragma shader_feature _SPECGLOSSMAP
  103. #pragma shader_feature _EMISSION
  104. #pragma shader_feature _DETAIL_MULX2
  105. //#pragma shader_feature _PARALLAXMAP
  106.  
  107. #pragma shader_feature S_SPECULAR_NONE S_SPECULAR_BLINNPHONG S_SPECULAR_METALLIC
  108. #pragma shader_feature S_UNLIT
  109. #pragma shader_feature S_OVERRIDE_LIGHTMAP
  110. #pragma shader_feature S_WORLD_ALIGNED_TEXTURE
  111. #pragma shader_feature S_RECEIVE_SHADOWS
  112. #pragma shader_feature S_OCCLUSION
  113. #pragma shader_feature S_RENDER_BACKFACES
  114.  
  115. #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  116. #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
  117. #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
  118.  
  119. #pragma multi_compile _ MATRIX_PALETTE_SKINNING_1BONE
  120. #pragma multi_compile _ D_VALVE_FOG
  121. #pragma multi_compile _ D_VALVE_SHADOWING_POINT_LIGHTS
  122.  
  123. #pragma skip_variants SHADOWS_SOFT
  124.  
  125. #pragma vertex MainVs
  126. #pragma fragment MainPs
  127.  
  128. // Dynamic combo skips (Static combo skips happen in ValveShaderGUI.cs in SetMaterialKeywords())
  129. #if ( S_UNLIT )
  130. #undef LIGHTMAP_OFF
  131. #define LIGHTMAP_OFF 1
  132. #undef LIGHTMAP_ON
  133.  
  134. #undef DIRLIGHTMAP_OFF
  135. #define DIRLIGHTMAP_OFF 1
  136. #undef DIRLIGHTMAP_COMBINED
  137. #undef DIRLIGHTMAP_SEPARATE
  138.  
  139. #undef DYNAMICLIGHTMAP_OFF
  140. #define DYNAMICLIGHTMAP_OFF 1
  141. #undef DYNAMICLIGHTMAP_ON
  142. #endif
  143.  
  144. // Includes -------------------------------------------------------------------------------------------------------------------------------------------------
  145. #include "UnityCG.cginc"
  146. #include "UnityLightingCommon.cginc"
  147. #include "UnityStandardUtils.cginc"
  148. #include "UnityStandardInput.cginc"
  149. #include "vr_utils.cginc"
  150. #include "vr_lighting.cginc"
  151. #include "vr_matrix_palette_skinning.cginc"
  152. #include "vr_fog.cginc"
  153.  
  154. // Structs --------------------------------------------------------------------------------------------------------------------------------------------------
  155. struct VS_INPUT
  156. {
  157. float4 vPositionOs : POSITION;
  158. float3 vNormalOs : NORMAL;
  159. float2 vTexCoord0 : TEXCOORD0;
  160. #if ( _DETAIL || S_OVERRIDE_LIGHTMAP || LIGHTMAP_ON )
  161. float2 vTexCoord1 : TEXCOORD1;
  162. #endif
  163. #if ( DYNAMICLIGHTMAP_ON || UNITY_PASS_META )
  164. float2 vTexCoord2 : TEXCOORD2;
  165. #endif
  166.  
  167. #if ( _NORMALMAP )
  168. float4 vTangentUOs_flTangentVSign : TANGENT;
  169. #endif
  170.  
  171. #if ( MATRIX_PALETTE_SKINNING )
  172. float4 vBoneIndices : COLOR;
  173. #endif
  174. };
  175.  
  176. struct PS_INPUT
  177. {
  178. float4 vPositionPs : SV_Position;
  179.  
  180. #if ( !S_UNLIT )
  181. float3 vPositionWs : TEXCOORD0;
  182. float3 vNormalWs : TEXCOORD1;
  183. #endif
  184.  
  185. #if ( _DETAIL )
  186. float4 vTextureCoords : TEXCOORD2;
  187. #else
  188. float2 vTextureCoords : TEXCOORD2;
  189. #endif
  190.  
  191. #if ( S_OVERRIDE_LIGHTMAP || LIGHTMAP_ON || DYNAMICLIGHTMAP_ON )
  192. #if ( DYNAMICLIGHTMAP_ON )
  193. centroid float4 vLightmapUV : TEXCOORD3;
  194. #else
  195. centroid float2 vLightmapUV : TEXCOORD3;
  196. #endif
  197. #endif
  198.  
  199. #if ( _NORMALMAP )
  200. float3 vTangentUWs : TEXCOORD4;
  201. float3 vTangentVWs : TEXCOORD5;
  202. #endif
  203.  
  204. #if ( D_VALVE_FOG )
  205. float2 vFogCoords : TEXCOORD6;
  206. #endif
  207. };
  208.  
  209. float g_flValveGlobalVertexScale = 1.0; // Used to "hide" all valve materials for debugging
  210.  
  211. // World-aligned texture
  212. float3 g_vWorldAlignedTextureSize = float3( 1.0, 1.0, 1.0 );
  213. float3 g_vWorldAlignedNormalTangentU = float3( -1.0, 0.0, 0.0 );
  214. float3 g_vWorldAlignedNormalTangentV = float3( 0.0, 0.0, 1.0 );
  215. float3 g_vWorldAlignedTexturePosition = float3( 0.0, 0.0, 0.0 );
  216.  
  217. // MainVs ---------------------------------------------------------------------------------------------------------------------------------------------------
  218. PS_INPUT MainVs( VS_INPUT i )
  219. {
  220. PS_INPUT o = ( PS_INPUT )0;
  221.  
  222. #if ( MATRIX_PALETTE_SKINNING )
  223. {
  224. #if ( _NORMALMAP )
  225. {
  226. MatrixPaletteSkinning( i.vPositionOs.xyzw, i.vNormalOs.xyz, i.vTangentUOs_flTangentVSign.xyz, i.vBoneIndices.xyzw );
  227. }
  228. #else
  229. {
  230. MatrixPaletteSkinning( i.vPositionOs.xyzw, i.vNormalOs.xyz, i.vBoneIndices.xyzw );
  231. }
  232. #endif
  233. }
  234. #endif
  235.  
  236. // Position
  237. i.vPositionOs.xyzw *= g_flValveGlobalVertexScale; // Used to "hide" all valve materials for debugging
  238. float3 vPositionWs = mul( unity_ObjectToWorld, i.vPositionOs.xyzw ).xyz;
  239. #if ( !S_UNLIT )
  240. {
  241. o.vPositionWs.xyz = vPositionWs.xyz;
  242. }
  243. #endif
  244. o.vPositionPs.xyzw = mul( UNITY_MATRIX_MVP, i.vPositionOs.xyzw );
  245.  
  246. // Normal
  247. float3 vNormalWs = UnityObjectToWorldNormal( i.vNormalOs.xyz );
  248. #if ( !S_UNLIT )
  249. {
  250. o.vNormalWs.xyz = vNormalWs.xyz;
  251. }
  252. #endif
  253.  
  254. #if ( _NORMALMAP )
  255. {
  256. // TangentU and TangentV
  257. float3 vTangentUWs = UnityObjectToWorldDir( i.vTangentUOs_flTangentVSign.xyz ); // Transform tangentU into world space
  258. //vTangentUWs.xyz = normalize( vTangentUWs.xyz - ( vNormalWs.xyz * dot( vTangentUWs.xyz, vNormalWs.xyz ) ) ); // Force tangentU perpendicular to normal and normalize
  259.  
  260. o.vTangentUWs.xyz = vTangentUWs.xyz;
  261. o.vTangentVWs.xyz = cross( vNormalWs.xyz, vTangentUWs.xyz ) * i.vTangentUOs_flTangentVSign.w;
  262. }
  263. #endif
  264.  
  265. #if ( S_WORLD_ALIGNED_TEXTURE )
  266. {
  267. float3 vTexturePositionScaledWs = ( vPositionWs.xyz - g_vWorldAlignedTexturePosition.xyz ) / g_vWorldAlignedTextureSize.xyz;
  268. o.vTextureCoords.x = dot( vTexturePositionScaledWs.xyz, g_vWorldAlignedNormalTangentU.xyz );
  269. o.vTextureCoords.y = dot( vTexturePositionScaledWs.xyz, g_vWorldAlignedNormalTangentV.xyz );
  270. #if ( _DETAIL )
  271. {
  272. o.vTextureCoords.zw = TRANSFORM_TEX( o.vTextureCoords.xy, _DetailAlbedoMap );
  273. }
  274. #endif
  275. }
  276. #else
  277. {
  278. // Texture coords (Copied from Unity's TexCoords() helper function)
  279. o.vTextureCoords.xy = TRANSFORM_TEX( i.vTexCoord0, _MainTex );
  280. #if ( _DETAIL )
  281. {
  282. o.vTextureCoords.zw = TRANSFORM_TEX( ( ( _UVSec == 0 ) ? i.vTexCoord0 : i.vTexCoord1 ), _DetailAlbedoMap );
  283. }
  284. #endif
  285. }
  286. #endif
  287.  
  288. // Indirect lighting uv's or light probe
  289. #if ( S_OVERRIDE_LIGHTMAP )
  290. {
  291. o.vLightmapUV.xy = i.vTexCoord1.xy;
  292. }
  293. #elif ( LIGHTMAP_ON )
  294. {
  295. // Static lightmaps
  296. o.vLightmapUV.xy = i.vTexCoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  297. }
  298. #endif
  299.  
  300. #if ( DYNAMICLIGHTMAP_ON )
  301. {
  302. o.vLightmapUV.zw = i.vTexCoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  303. }
  304. #endif
  305.  
  306. #if ( D_VALVE_FOG )
  307. {
  308. o.vFogCoords.xy = CalculateFogCoords( vPositionWs.xyz );
  309. }
  310. #endif
  311.  
  312. return o;
  313. }
  314.  
  315. // MainPs ---------------------------------------------------------------------------------------------------------------------------------------------------
  316. #define g_vColorTint _Color
  317. #define g_tColor _MainTex
  318. #define g_tNormalMap _BumpMap
  319. #define g_flBumpScale _BumpScale
  320. #define g_vReflectance _SpecColor
  321. #define g_tReflectanceGloss _SpecGlossMap
  322. #define g_flGlossScale _Glossiness
  323. #define g_tDetailAlbedo _DetailAlbedoMap
  324. #define g_tDetailNormal _DetailNormalMap
  325. #define g_flDetailNormalScale _DetailNormalMapScale
  326.  
  327. float g_flReflectanceScale = 1.0;
  328. float g_flReflectanceBias = 0.0;
  329.  
  330. float _OcclusionStrengthDirectDiffuse = 1.0;
  331. float _OcclusionStrengthDirectSpecular = 1.0;
  332. float _OcclusionStrengthIndirectDiffuse = 1.0;
  333. float _OcclusionStrengthIndirectSpecular = 1.0;
  334.  
  335. float _FogMultiplier = 1.0;
  336.  
  337. struct PS_OUTPUT
  338. {
  339. float4 vColor : SV_Target0;
  340. };
  341.  
  342. PS_OUTPUT MainPs( PS_INPUT i
  343. #if ( S_RENDER_BACKFACES )
  344. , bool bIsFrontFace : SV_IsFrontFace
  345. #endif
  346. )
  347. {
  348. PS_OUTPUT o = ( PS_OUTPUT )0;
  349.  
  350. //-----------------------------------------------------------//
  351. // Negate the world normal if we are rendering the back face //
  352. //-----------------------------------------------------------//
  353. #if ( S_RENDER_BACKFACES )
  354. {
  355. i.vNormalWs.xyz *= ( bIsFrontFace ? 1.0 : -1.0 );
  356. }
  357. #endif
  358.  
  359. //--------//
  360. // Albedo //
  361. //--------//
  362. float4 vAlbedoTexel = tex2D( g_tColor, i.vTextureCoords.xy ) * g_vColorTint.rgba;
  363. float3 vAlbedo = vAlbedoTexel.rgb;
  364.  
  365. // Apply detail to albedo
  366. #if ( _DETAIL )
  367. {
  368. float flDetailMask = DetailMask( i.vTextureCoords.xy );
  369. float3 vDetailAlbedo = tex2D( g_tDetailAlbedo, i.vTextureCoords.zw ).rgb;
  370. #if ( _DETAIL_MULX2 )
  371. vAlbedo.rgb *= LerpWhiteTo( vDetailAlbedo.rgb * unity_ColorSpaceDouble.rgb, flDetailMask );
  372. #elif ( _DETAIL_MUL )
  373. vAlbedo.rgb *= LerpWhiteTo( vDetailAlbedo.rgb, flDetailMask );
  374. #elif ( _DETAIL_ADD )
  375. vAlbedo.rgb += vDetailAlbedo.rgb * flDetailMask;
  376. #elif ( _DETAIL_LERP )
  377. vAlbedo.rgb = lerp( vAlbedo.rgb, vDetailAlbedo.rgb, flDetailMask );
  378. #endif
  379. }
  380. #endif
  381.  
  382. //--------------//
  383. // Translucency //
  384. //--------------//
  385. #if ( _ALPHATEST_ON )
  386. {
  387. clip( vAlbedoTexel.a - _Cutoff );
  388. }
  389. #endif
  390.  
  391. #if ( _ALPHAPREMULTIPLY_ON )
  392. {
  393. vAlbedo.rgb *= vAlbedoTexel.a;
  394. }
  395. #endif
  396.  
  397. #if ( _ALPHABLEND_ON || _ALPHAPREMULTIPLY_ON )
  398. {
  399. o.vColor.a = vAlbedoTexel.a;
  400. }
  401. #else
  402. {
  403. o.vColor.a = 1.0;
  404. }
  405. #endif
  406.  
  407. //---------------//
  408. // Tangent Space //
  409. //---------------//
  410. float3 vTangentUWs = float3( 1.0, 0.0, 0.0 );
  411. float3 vTangentVWs = float3( 0.0, 1.0, 0.0 );
  412. #if ( _NORMALMAP )
  413. {
  414. vTangentUWs.xyz = i.vTangentUWs.xyz;
  415. vTangentVWs.xyz = i.vTangentVWs.xyz;
  416. }
  417. #endif
  418.  
  419. //--------//
  420. // Normal //
  421. //--------//
  422. float3 vGeometricNormalWs = float3( 0.0, 0.0, 1.0 );
  423. #if ( !S_UNLIT )
  424. {
  425. i.vNormalWs.xyz = normalize( i.vNormalWs.xyz );
  426. vGeometricNormalWs.xyz = i.vNormalWs.xyz;
  427. }
  428. #endif
  429.  
  430. float3 vNormalWs = vGeometricNormalWs.xyz;
  431. float3 vNormalTs = float3( 0.0, 0.0, 1.0 );
  432. #if ( _NORMALMAP )
  433. {
  434. vNormalTs.xyz = UnpackScaleNormal( tex2D( g_tNormalMap, i.vTextureCoords.xy ), g_flBumpScale );
  435. //vNormalTs.y = -vNormalTs.y;
  436.  
  437. // Apply detail to tangent normal
  438. #if ( _DETAIL )
  439. {
  440. float flDetailMask = DetailMask( i.vTextureCoords.xy );
  441. float3 vDetailNormalTs = UnpackScaleNormal( tex2D( g_tDetailNormal, i.vTextureCoords.zw ), g_flDetailNormalScale );
  442. #if ( _DETAIL_LERP )
  443. {
  444. vNormalTs.xyz = lerp( vNormalTs.xyz, vDetailNormalTs.xyz, flDetailMask );
  445. }
  446. #else
  447. {
  448. vNormalTs.xyz = lerp( vNormalTs.xyz, BlendNormals( vNormalTs.xyz, vDetailNormalTs.xyz ), flDetailMask );
  449. }
  450. #endif
  451. }
  452. #endif
  453.  
  454. // Convert to world space
  455. vNormalWs.xyz = Vec3TsToWsNormalized( vNormalTs.xyz, vGeometricNormalWs.xyz, vTangentUWs.xyz, vTangentVWs.xyz );
  456. }
  457. #endif
  458.  
  459. //-----------//
  460. // Roughness //
  461. //-----------//
  462. float2 vRoughness = float2( 0.6, 0.6 );// vNormalTexel.rb;
  463. //#if ( S_HIGH_QUALITY_GLOSS )
  464. //{
  465. // float4 vGlossTexel = Tex2D( g_tGloss, i.vTextureCoords.xy );
  466. // vRoughness.xy += vGlossTexel.ag;
  467. //}
  468. //#endif
  469.  
  470. // Reflectance and gloss
  471. float3 vReflectance = float3( 0.0, 0.0, 0.0 );
  472. float flGloss = 0.0;
  473. #if ( S_SPECULAR_METALLIC )
  474. {
  475. float2 vMetallicGloss;// = MetallicGloss( i.vTextureCoords.xy );
  476. #ifdef _METALLICGLOSSMAP
  477. vMetallicGloss.xy = tex2D(_MetallicGlossMap, i.vTextureCoords.xy).ra;
  478. #else
  479. vMetallicGloss.xy = half2(_Metallic, _Glossiness);
  480. #endif
  481.  
  482. float flOneMinusReflectivity;
  483. float3 vSpecColor;
  484. float3 diffColor = DiffuseAndSpecularFromMetallic( vAlbedo.rgb, vMetallicGloss.x, /*out*/ vSpecColor, /*out*/ flOneMinusReflectivity);
  485. vAlbedo.rgb = diffColor.rgb;
  486.  
  487. vReflectance.rgb = vSpecColor.rgb;
  488. flGloss = vMetallicGloss.y;
  489. }
  490. #elif ( S_SPECULAR_BLINNPHONG )
  491. {
  492. float4 vReflectanceGloss; // = SpecularGloss( i.vTextureCoords.xy );
  493. #ifdef _SPECGLOSSMAP
  494. vReflectanceGloss.rgba = tex2D(_SpecGlossMap, i.vTextureCoords.xy);
  495. #else
  496. vReflectanceGloss.rgba = float4(_SpecColor.rgb, _Glossiness);
  497. #endif
  498.  
  499. vReflectanceGloss.rgb = ( vReflectanceGloss.rgb * g_flReflectanceScale.xxx ) + g_flReflectanceBias.xxx;
  500. vReflectance.rgb = vReflectanceGloss.rgb;
  501. flGloss = vReflectanceGloss.a;
  502. }
  503. #endif
  504.  
  505. vRoughness.xy = ( 1.0 - flGloss ).xx;
  506. #if ( !S_SPECULAR_NONE )
  507. {
  508. vRoughness.xy = AdjustRoughnessByGeometricNormal( vRoughness.xy, vGeometricNormalWs.xyz );
  509. }
  510. #endif
  511.  
  512. //----------//
  513. // Lighting //
  514. //----------//
  515. LightingTerms_t lightingTerms;
  516. lightingTerms.vDiffuse.rgb = float3( 1.0, 1.0, 1.0 );
  517. lightingTerms.vSpecular.rgb = float3( 0.0, 0.0, 0.0 );
  518. lightingTerms.vIndirectDiffuse.rgb = float3( 0.0, 0.0, 0.0 );
  519. lightingTerms.vIndirectSpecular.rgb = float3( 0.0, 0.0, 0.0 );
  520. lightingTerms.vTransmissiveSunlight.rgb = float3( 0.0, 0.0, 0.0 );
  521.  
  522. float flFresnelExponent = 5.0;
  523. float flMetalness = 0.0f;
  524.  
  525. #if ( !S_UNLIT )
  526. {
  527. float4 vLightmapUV = float4( 0.0, 0.0, 0.0, 0.0 );
  528. #if ( S_OVERRIDE_LIGHTMAP || LIGHTMAP_ON || DYNAMICLIGHTMAP_ON )
  529. {
  530. vLightmapUV.xy = i.vLightmapUV.xy;
  531. #if ( DYNAMICLIGHTMAP_ON )
  532. {
  533. vLightmapUV.zw = i.vLightmapUV.zw;
  534. }
  535. #endif
  536. }
  537. #endif
  538.  
  539. // Compute lighting
  540. lightingTerms = ComputeLighting( i.vPositionWs.xyz, vNormalWs.xyz, vTangentUWs.xyz, vTangentVWs.xyz, vRoughness.xy, vReflectance.rgb, flFresnelExponent, vLightmapUV.xyzw );
  541.  
  542. #if ( S_OCCLUSION )
  543. {
  544. float flOcclusion = tex2D( _OcclusionMap, i.vTextureCoords.xy ).g;
  545. lightingTerms.vDiffuse.rgb *= LerpOneTo( flOcclusion, _OcclusionStrength * _OcclusionStrengthDirectDiffuse );
  546. lightingTerms.vSpecular.rgb *= LerpOneTo( flOcclusion, _OcclusionStrength * _OcclusionStrengthDirectSpecular );
  547. lightingTerms.vIndirectDiffuse.rgb *= LerpOneTo( flOcclusion, _OcclusionStrength * _OcclusionStrengthIndirectDiffuse );
  548. lightingTerms.vIndirectSpecular.rgb *= LerpOneTo( flOcclusion, _OcclusionStrength * _OcclusionStrengthIndirectSpecular );
  549. }
  550. #endif
  551. }
  552. #endif
  553.  
  554. // Diffuse
  555. o.vColor.rgb = ( lightingTerms.vDiffuse.rgb + lightingTerms.vIndirectDiffuse.rgb ) * vAlbedo.rgb;
  556.  
  557. // Specular
  558. #if ( !S_SPECULAR_NONE )
  559. {
  560. o.vColor.rgb += lightingTerms.vSpecular.rgb;
  561. }
  562. #endif
  563. o.vColor.rgb += lightingTerms.vIndirectSpecular.rgb; // Indirect specular applies its own fresnel in the forward lighting header file
  564.  
  565. // Emission - Unity just adds the emissive term at the end instead of adding it to the diffuse lighting term. Artists may want both options.
  566. float3 vEmission = Emission( i.vTextureCoords.xy );
  567. o.vColor.rgb += vEmission.rgb;
  568.  
  569. // Fog
  570. #if ( D_VALVE_FOG )
  571. {
  572. o.vColor.rgb = ApplyFog( o.vColor.rgb, i.vFogCoords.xy, _FogMultiplier );
  573. }
  574. #endif
  575.  
  576. // Dither to fix banding artifacts
  577. o.vColor.rgb += ScreenSpaceDither( i.vPositionPs.xy );
  578.  
  579. return o;
  580. }
  581. ENDCG
  582. }
  583.  
  584. //-------------------------------------------------------------------------------------------------------------------------------------------------------------
  585. // Shadow rendering pass
  586. //-------------------------------------------------------------------------------------------------------------------------------------------------------------
  587. //Pass
  588. //{
  589. // Name "ShadowCaster"
  590. // Tags { "LightMode" = "ShadowCaster" }
  591. //
  592. // ZWrite On ZTest LEqual
  593. //
  594. // CGPROGRAM
  595. // #pragma target 5.0
  596. // // TEMPORARY: GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
  597. // #pragma exclude_renderers gles
  598. //
  599. // // -------------------------------------
  600. // #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
  601. // #pragma multi_compile_shadowcaster
  602. //
  603. // #pragma vertex vertShadowCaster
  604. // #pragma fragment fragShadowCaster
  605. //
  606. // #include "UnityStandardShadow.cginc"
  607. // ENDCG
  608. //}
  609.  
  610. //-------------------------------------------------------------------------------------------------------------------------------------------------------------
  611. // Extracts information for lightmapping, GI (emission, albedo, ...)
  612. // This pass it not used during regular rendering.
  613. //-------------------------------------------------------------------------------------------------------------------------------------------------------------
  614. Pass
  615. {
  616. Name "META"
  617. Tags { "LightMode"="Meta" }
  618.  
  619. Cull Off
  620.  
  621. CGPROGRAM
  622. #pragma only_renderers d3d11
  623.  
  624. #pragma vertex vert_meta
  625. #pragma fragment frag_meta
  626.  
  627. #pragma shader_feature _EMISSION
  628. #pragma shader_feature _METALLICGLOSSMAP
  629. #pragma shader_feature ___ _DETAIL_MULX2
  630.  
  631. #include "UnityStandardMeta.cginc"
  632. ENDCG
  633. }
  634. }
  635.  
  636. CustomEditor "ValveShaderGUI"
  637. }
Advertisement
Add Comment
Please, Sign In to add comment