Preview_IndirectDiffuseLight.shader 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. Shader "Hidden/IndirectDiffuseLight"
  2. {
  3. Properties
  4. {
  5. _Intensity ("Intensity", Float) = 1
  6. }
  7. SubShader
  8. {
  9. Pass
  10. {
  11. CGPROGRAM
  12. #pragma vertex vert_img
  13. #pragma fragment frag
  14. #include "UnityCG.cginc"
  15. #include "Preview.cginc"
  16. #include "Lighting.cginc"
  17. #include "UnityPBSLighting.cginc"
  18. float _Intensity;
  19. float4 frag(v2f_img i) : SV_Target
  20. {
  21. float3 vertexPos = PreviewFragmentPositionOS( i.uv );
  22. float3 worldPos = mul(unity_ObjectToWorld, float4(vertexPos,1)).xyz;
  23. float4 back = lerp(float4(0.4117,0.3843,0.3647,1),float4(0.4117,0.5059,0.6470,1),worldPos.y * 0.5 + 0.5);
  24. return float4(GammaToLinearSpace(back.rgb * _Intensity),1);
  25. }
  26. ENDCG
  27. }
  28. Pass // connected tangent
  29. {
  30. CGPROGRAM
  31. #pragma vertex vert_img
  32. #pragma fragment frag
  33. #include "UnityCG.cginc"
  34. #include "Preview.cginc"
  35. #include "Lighting.cginc"
  36. #include "UnityPBSLighting.cginc"
  37. float _Intensity;
  38. sampler2D _A;
  39. float4 frag(v2f_img i) : SV_Target
  40. {
  41. float3 vertexPos = PreviewFragmentPositionOS( i.uv );
  42. float3 normal = PreviewFragmentNormalOS( i.uv );
  43. float3 worldNormal = UnityObjectToWorldNormal(normal);
  44. float3 tangent = PreviewFragmentTangentOS( i.uv );
  45. float3 worldPos = mul(unity_ObjectToWorld, float4(vertexPos,1)).xyz;
  46. float3 worldTangent = UnityObjectToWorldDir(tangent);
  47. float tangentSign = -1;
  48. float3 worldBinormal = normalize( cross(worldNormal, worldTangent) * tangentSign);
  49. float4 tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
  50. float4 tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
  51. float4 tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
  52. float2 sphereUVs = i.uv;
  53. sphereUVs.x = (atan2(vertexPos.x, -vertexPos.z) / (UNITY_PI) + 0.5);
  54. // Needs further checking
  55. //float3 tangentNormal = tex2Dlod(_A, float4(sphereUVs,0,0)).xyz;
  56. float3 tangentNormal = tex2D(_A, sphereUVs).xyz;
  57. worldNormal = fixed3( dot( tSpace0.xyz, tangentNormal ), dot( tSpace1.xyz, tangentNormal ), dot( tSpace2.xyz, tangentNormal ) );
  58. float4 back = lerp(float4(0.4117,0.3843,0.3647,1),float4(0.4117,0.5059,0.6470,1),worldNormal.y * 0.5 + 0.5);
  59. return float4(GammaToLinearSpace(back.rgb * _Intensity),1);
  60. }
  61. ENDCG
  62. }
  63. Pass // connected world
  64. {
  65. CGPROGRAM
  66. #pragma vertex vert_img
  67. #pragma fragment frag
  68. #include "UnityCG.cginc"
  69. #include "Preview.cginc"
  70. #include "Lighting.cginc"
  71. #include "UnityPBSLighting.cginc"
  72. float _Intensity;
  73. sampler2D _A;
  74. float4 frag( v2f_img i ) : SV_Target
  75. {
  76. float3 vertexPos = PreviewFragmentPositionOS( i.uv );
  77. float3 normal = PreviewFragmentNormalOS( i.uv );
  78. float3 worldNormal = tex2D( _A, i.uv );
  79. float4 back = lerp( float4( 0.4117,0.3843,0.3647,1 ),float4( 0.4117,0.5059,0.6470,1 ),worldNormal.y * 0.5 + 0.5 );
  80. return float4( GammaToLinearSpace( back.rgb * _Intensity ),1 );
  81. }
  82. ENDCG
  83. }
  84. }
  85. }