e4s-sdk/gamedata/shaders/r2/combine_1.ps
2026-06-17 23:06:51 +03:00

164 lines
5.5 KiB
PostScript

#include "common.h"
//#define USE_SUPER_SPECULAR
//#define USE_ORIGINAL_SSAO
//#define HBAO_WORLD_JITTER
uniform sampler2D s_half_depth;
#include "lmodel.h"
#include "hmodel.h"
#include "ssao_blur.ps"
#include "ssao.ps"
#include "ssao_hbao.ps"
struct _input {
float4 hpos : POSITION ;
#ifdef USE_VTF
float4 tc0 : TEXCOORD0 ; // tc.xy, tc.w = tonemap scale
#else
float2 tc0 : TEXCOORD0 ; // tc.xy
#endif
float2 tcJ : TEXCOORD1; // jitter coords
};
struct _out {
half4 low : COLOR0 ;
half4 high : COLOR1 ;
};
uniform sampler1D fog_table ;
_out main ( _input I )
{
// Sample the buffers:
float4 P = tex2D (s_position, I.tc0); // position.(mtl or sun)
half4 N = tex2D (s_normal, I.tc0); // normal.hemi
half4 D = tex2D (s_diffuse, I.tc0); // rgb.gloss
half4 L = tex2D (s_accumulator, I.tc0); // diffuse.specular
// D.rgb *= (D.rgb+0.1); // rasie texures contrast
#ifdef USE_SUPER_SPECULAR
{
half ds = dot (D.rgb,1.h/3.h) ;
D.w = max (D.w, ds*ds/8.h) ;
}
#endif
#ifdef FORCE_GLOSS
D.w = FORCE_GLOSS ;
#endif
#ifdef USE_GAMMA_22
D.rgb = (D.rgb*D.rgb) ; // pow(2.2)
#endif
// static sun
half mtl = P.w;
#ifdef USE_R2_STATIC_SUN
half sun_occ = P.w*2 ;
mtl = xmaterial;
L += Ldynamic_color * sun_occ * plight_infinity (mtl, P.xyz, N.xyz, Ldynamic_dir);
#endif
// hemisphere
half3 hdiffuse,hspecular;
// Calculate SSAO
//#ifdef USE_SSAO_BLUR
// half occ = ssao_blur_ps(I.tc0);
#ifdef USE_HBAO
float occ = calc_hbao(P.z, N, I.tc0);
#else
half occ = calc_ssao(P, N, I.tc0, I.tcJ);
#endif
hmodel (hdiffuse, hspecular, mtl, N.w, D.w, P.xyz, N.xyz);
// hmodel (hdiffuse, hspecular, mtl, 1, D.w, P.xyz, N.xyz);
// hdiffuse*=hdiffuse; //. high contrast hemi
// hdiffuse*=(D.rgb*0.8 + 0.2h); // rise texture contrast for diffuse lighting
// hdiffuse = 0.8;
// hdiffuse *= (occ*(D.rgb + .1h));
hdiffuse *= occ;
hspecular *= occ;
half4 light = half4 (L.rgb + hdiffuse, L.w) ;
half4 C = D*light ; // rgb.gloss * light(diffuse.specular)
// half3 spec = (C.rgb*.5h + .5h)*C.w + hspecular + hspecular + hspecular; // replicated specular
half3 spec = C.www + hspecular; // replicated specular
#ifdef USE_SUPER_SPECULAR
spec = (C.rgb*.5h + .5h)*C.w + hspecular ;
#endif
// half3 color = C.rgb + D.rgb*spec ;
// half3 color = C.rgb + (D.rgb*spec+spec)/0.5h;
half3 color = C.rgb + spec ;
// half3 color = C.rgb + D.rgb*spec+hspecular+hspecular ; // More realistic and contrast specular - Ugrumiy edition
////////////////////////////////////////////////////////////////////////////////
/// For Test ///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#ifdef DBG_TEST_NMAP
//. hemi + sun + lighting + specular
color = hdiffuse + L.rgb + N;
#endif
#ifdef DBG_TEST_NMAP_SPEC
//. hemi + sun + lighting + specular
color = hdiffuse + L.rgb + N + spec;
#endif
#ifdef DBG_TEST_LIGHT
//. hemi + sun + lighting + specular
color = hdiffuse + L.rgb;
#endif
#ifdef DBG_TEST_LIGHT_SPEC
//. hemi + sun + lighting + specular
color = hdiffuse + L.rgb + spec;
#endif
#ifdef DBG_TEST_SPEC
//. only lighting and specular
color = spec;
#endif
////////////////////////////////////////////////////////////////////////////////
// here should be distance fog
float3 pos = P.xyz;
float distance = length (pos);
float fog = saturate (distance*fog_params.w + fog_params.x); //
color = lerp (color,fog_color,fog); //
half skyblend = saturate (fog*fog);
#ifdef DBG_TMAPPING
color = D.xyz;
#endif
// final tone-mapping
#ifdef USE_VTF
half tm_scale = I.tc0.w; // interpolated from VS
#else
half tm_scale = tex2D (s_tonemap,I.tc0).x;
#endif
#ifdef USE_SUPER_SPECULAR
color = spec - hspecular ;
#endif
// color = N; //show normals
// color = N.w; //show normals
// color = float4(occ.xxx,1.0f); //show occlusion
// color = occ;
_out o;
tonemap (o.low, o.high, color, tm_scale ) ;
o.low.a = skyblend ;
o.high.a = skyblend ;
// o.low = skyblend;
// o.hight = 0;
return o;
}