MGKJ_Cubemap_1.00.shader 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //2012.03.10
  2. //2013.06.26
  3. Shader "@MGKJ_Cubemap"
  4. {
  5. Properties
  6. {
  7. _SpecColor2("Specular Color", Color) = (0,0,0,1)
  8. _Shininess("Shininess", Range(0,1) ) = 0.5
  9. _Color ("Main Color", Color) = (0,0,0,1)
  10. _MainTex ("DiffuseMap", 2D) = "white" {}
  11. _globalInfluence("Global Influence", Range(0,1) ) = 1
  12. _brightness("Brightness", Range(1,5) ) = 1
  13. _contrast("Contrast", Range(1,2.5) ) = 1
  14. _lightmap_color("Lighting Color", Color) = (1,1,1,1)
  15. _LightMap ("SecondMap (CompleteMap or LightMap)", 2D) = "white" {}
  16. _changeType ("<<<HDR Type *** NoHDR>>>", Range(0,1)) =0
  17. // _NormalDeepth ("Normal Deepth", Range(-1,2) ) = 0.5
  18. _CubeMap ("Reflection Cubemap", Cube) = "black" {}
  19. _Reflect("Reflect Blender", range (0,1)) = 0.5
  20. _FresnelPower("Fresnel", range (-5,0)) = -1
  21. _FresnelBias ("Fresnel Bias", Range(0.015, 1)) = 0.015
  22. // _Parallax ("Height", Range (-0.05, 0.05)) = 0.02
  23. // _relaxedcone_relief_map ("relaxedcone", 2D ) = "white" {}
  24. _SelectColor ("Outline Color", Color) = (0.2,0.6,0.8,0)
  25. _SelectColorAlpha("Select Color Alpha",Range(0,1) ) = 0
  26. }
  27. SubShader {
  28. Tags { "RenderType"="Opaque" }
  29. LOD 200
  30. CGPROGRAM
  31. #pragma surface surf BlinnPhong_wjm finalcolor:mycolor
  32. #pragma target 3.0
  33. #pragma profileoption MaxTexIndirections=64
  34. #define DEPTH_BIAS
  35. #define BORDER_CLAMP
  36. float4x4 _RotateUVM;
  37. float _GLOBALBRIGHTNESS;
  38. float _GLOBALCONTRASR;
  39. float depth_bias;
  40. float border_clamp;
  41. float4 _SpecColor2;
  42. float _Parallax;
  43. float _Shininess;
  44. float _brightness;
  45. float _contrast;
  46. float _NormalDeepth;
  47. float4 _Color;
  48. float4 _lightmap_color;
  49. float _globalInfluence;
  50. float _changeType;
  51. float _Reflect;
  52. float _FresnelPower;
  53. float _FresnelBias;
  54. sampler2D _MainTex;
  55. sampler2D _BumpMap;
  56. sampler2D _LightMap;
  57. sampler2D _relaxedcone_relief_map;
  58. samplerCUBE _CubeMap;
  59. float4 _SelectColor ;
  60. float _SelectColorAlpha;
  61. struct Input {
  62. float2 uv_MainTex;
  63. // float2 uv_BumpMap;
  64. float2 uv2_LightMap;
  65. float3 worldRefl;
  66. float3 viewDir;
  67. INTERNAL_DATA
  68. };
  69. struct SurfaceOutput_wjm
  70. {
  71. half3 Albedo;
  72. half3 Normal;
  73. half3 Emission;
  74. half3 Gloss;
  75. half Specular;
  76. half Alpha;
  77. };
  78. inline half4 LightingBlinnPhong_wjm_PrePass (SurfaceOutput_wjm s, half4 light)
  79. {
  80. half3 spec = light.a * s.Gloss;
  81. half4 c;
  82. c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
  83. c.a = s.Alpha;
  84. return c;
  85. }
  86. inline half4 LightingBlinnPhong_wjm (SurfaceOutput_wjm s, half3 lightDir, half3 viewDir, half atten)
  87. {
  88. half3 h = normalize (lightDir + viewDir);
  89. half diff = max (0, dot ( lightDir, s.Normal ));
  90. float nh = max (0, dot (s.Normal, h));
  91. float spec = pow (nh, s.Specular*128.0);
  92. half4 lightPower;
  93. lightPower.rgb = _LightColor0.rgb * diff;
  94. lightPower.w = spec * Luminance (_LightColor0.rgb);
  95. lightPower *= atten * 2.0;
  96. half4 c;
  97. c.rgb = (s.Albedo * lightPower.rgb + lightPower.rgb * spec*s.Gloss);
  98. c.a = s.Alpha;
  99. return c;
  100. }
  101. void mycolor (Input IN, SurfaceOutput_wjm o, inout fixed4 color)
  102. {
  103. // color *= _lightmap_color;
  104. color += _SelectColor*_SelectColorAlpha;
  105. }
  106. // setup ray pos and dir based on view vector
  107. // and apply _Parallax bias and _Parallax factor
  108. void setup_ray(Input IN,out float3 p,out float3 v)
  109. {
  110. p = float3(IN.uv_MainTex,0);
  111. v = normalize(IN.viewDir);
  112. v.z = abs(v.z);
  113. #ifdef DEPTH_BIAS
  114. float db = 1.0-v.z; db*=db; db*=db; db=1.0-db*db;
  115. v.xy *= db;
  116. #endif
  117. v.xy *= _Parallax;
  118. }
  119. // ray intersect _Parallax map using binary cone space leaping
  120. // _Parallax value stored in alpha channel (black is at object surface)
  121. // and cone ratio stored in blue channel
  122. void ray_intersect_relaxedcone(
  123. sampler2D relaxedcone_relief_map,
  124. inout float3 p,
  125. inout float3 v)
  126. {
  127. const int cone_steps=10;
  128. const int binary_steps=5;
  129. float3 p0 = p;
  130. v /= v.z;
  131. float dist = length(v.xy);
  132. for( int i=0;i<cone_steps;i++ )
  133. {
  134. float4 tex = tex2D(relaxedcone_relief_map, p.xy);
  135. float height = saturate(tex.w - p.z);
  136. float cone_ratio = tex.z;
  137. p += v * (cone_ratio * height / (dist + cone_ratio));
  138. }
  139. v *= p.z*0.5;
  140. p = p0 + v;
  141. for( int i=0;i<binary_steps;i++ )
  142. {
  143. float4 tex = tex2D(relaxedcone_relief_map, p.xy);
  144. v *= 0.5;
  145. if (p.z<tex.w)
  146. p+=v;
  147. else
  148. p-=v;
  149. }
  150. }
  151. inline float3 DecodeLightmap2( float4 color )
  152. {
  153. #if defined(SHADER_API_GLES) && defined(SHADER_API_MOBILE)
  154. return 2.0 * color.rgb;
  155. #else
  156. // potentially faster to do the scalar multiplication
  157. // in parenthesis for scalar GPUs
  158. // return pow((8.0 * color.a),2.2) * color.rgb;
  159. return pow((_GLOBALBRIGHTNESS+_brightness)* lerp(8.0 * color.a,1,_changeType)* color.rgb,_contrast+_GLOBALCONTRASR);
  160. #endif
  161. }
  162. void surf (Input IN, inout SurfaceOutput_wjm o)
  163. {
  164. // float3 p,v;
  165. // setup_ray(IN,p,v);
  166. // ray_intersect_relaxedcone(_relaxedcone_relief_map,p,v);
  167. IN.uv_MainTex=mul(IN.uv_MainTex.xyxy, _RotateUVM).xy;
  168. // o.Normal = tex2D(_relaxedcone_relief_map,p.xy).xyz;
  169. float4 Tex2D0=tex2D(_MainTex,IN.uv_MainTex);
  170. // float4 Tex2D1=pow(_brightness.xxxx +tex2D(_LightMap,(IN.uv2_LightMap).xy),_contrast.xxxx)- _brightness.xxxx;
  171. float4 Tex2D1=0;
  172. Tex2D1+=_lightmap_color*float4(DecodeLightmap2(tex2D(_LightMap,(IN.uv2_LightMap).xy)),1);
  173. float3 worldRefl = WorldReflectionVector (IN, o.Normal);
  174. // worldRefl=reflect(IN.viewDir,IN.worldRefl)
  175. fixed4 cubeColor = texCUBE (_CubeMap, worldRefl)*min(2*dot(Tex2D1.rgb, fixed3(0.22, 0.707, 0.071) ),1);
  176. float fresnel_intensity=1-clamp(pow((max(0,dot (normalize(IN.viewDir), o.Normal))),-_FresnelPower).xxxx,0,1);
  177. fresnel_intensity= min(fresnel_intensity + _FresnelBias, 1.0f);
  178. float3 diffuseColor=Tex2D0*Tex2D1;
  179. float3 diffuseFinalcolor=lerp(diffuseColor,cubeColor,_Reflect*fresnel_intensity);
  180. o.Albedo=_Color.rgb*diffuseFinalcolor;
  181. // o.Albedo=_Color.rgb*Tex2D0;
  182. o.Emission=_lightmap_color*diffuseFinalcolor;
  183. o.Specular=_Shininess ;
  184. o.Gloss =Tex2D0.a*_SpecColor2;
  185. o.Alpha = Tex2D0.a * _Color.a;
  186. }
  187. ENDCG
  188. }
  189. FallBack "Diffuse"
  190. }