Includes (Effects)

From OBGE - Oblivion Graphics Extender

Jump to: navigation, search

Contents

Color [OBGEv3]

Functions for color-transforms: YU'V', CIE XYZ and CIE Yxy for luminant E.

includes/Color.hlsl
/* ############################################################################# */
 
static const
float3x3 YUVs_RGB  = {{+ 0.29900, + 0.58700	 , + 0.11400	},
			{- 0.14740, - 0.28950	 , + 0.43690	},
			{+ 0.61500, - 0.51500	 , - 0.10000	}};
 
static const
float3x3 RGB_YUVs  = {{+ 1	     , + 0.000	   , + 1.13980	 },
			{+ 1	     , - 0.39380   , - 0.58050	 },
			{+ 1	     , + 2.02790   , + 0.000	 }};
 
float GetIntensity( const float3 rgb )
{
//  return dot( rgb, float3( 0.333f, 0.334f, 0.333f ) );
    return dot( YUVs_RGB[0], rgb );
}
 
float3 GetYUV( const float3 rgb )
{
    return mul( YUVs_RGB, rgb );
}
 
float3 GetRGB( const float3 yuv )
{
    return mul( RGB_YUVs, yuv );
}
 
/* ############################################################################# */
 
static const
float3 XYZweight  = {+ 0.212671 / 0.715160, +	0.715160 / 0.715160, + 0.072169 / 0.715160};
 
static const
float3x3 XYZ_RGB  = {{+ 0.412453, +	0.357580, + 0.180423},
			 {+ 0.212671, +	0.715160, + 0.072169},
			 {+ 0.019334, +	0.119193, + 0.950227}};
 
static const
float3x3 RGB_XYZ  = {{+ 3.240479  , - 1.537150  , - 0.498535	 },
			 {- 0.969256  , + 1.875992  , + 0.041556	 },
			 {+ 0.055648  , - 0.204043  , + 1.057311	 }};
 
float GetLuminance( const float3 rgb )
{
//  return dot( rgb, float3( 0.333f, 0.334f, 0.333f ) );
    return dot( XYZ_RGB[1], rgb );
}
 
float3 GetXYZfromRGB( const float3 rgb )
{
    return mul( XYZ_RGB, rgb );
}
 
float3 GetRGBfromXYZ( const float3 xyz )
{
    return mul( RGB_XYZ, xyz );
}

Depth [OBGEv3]

Functions for reading the depth-buffer.

includes/Depth.hlsl
/* ############################################################################# */
 
#define	nearZ	oblv_ProjectionDepthRange_MAINPASS.x
#define	farZ	oblv_ProjectionDepthRange_MAINPASS.y
#define	rangeZ	oblv_ProjectionDepthRange_MAINPASS.z
#define	sumZ	oblv_ProjectionDepthRange_MAINPASS.w
// .x - nearZ
// .y - farZ
// .z - farZ - nearZ
// .w - farZ + nearZ
shared float4 oblv_ProjectionDepthRange_MAINPASS;
 
// w = cot(fieldOfViewX / 2).
// h = w / aspectRatio.
//
// w       0       0                                                    0
// 0       h       0                                                    0
// 0       0                    zfarPlane / (znearPlane - zfarPlane)    1
// 0       0      -znearPlane * zfarPlane / (znearPlane - zfarPlane)    0
//
// nearZ = -[3][2]         / [2][2]
// farZ  =  [2][2] * nearZ / [2][2] - 1
shared row_major matrix oblv_ProjectionTransform_MAINPASS;
 
/* ############################################################################# */
 
texture2D oblv_CurrDepthStencilZ_MAINPASS;
 
sampler2D DpthSampler = sampler_state {
	texture = <oblv_CurrDepthStencilZ_MAINPASS>;
 
	AddressU = CLAMP;
	AddressV = CLAMP;
 
	MINFILTER = POINT;
	MAGFILTER = POINT;
	MIPFILTER = NONE;
};
 
sampler2D DpthSamplerL = sampler_state {
	texture = <oblv_CurrDepthStencilZ_MAINPASS>;
 
	AddressU = CLAMP;
	AddressV = CLAMP;
 
	MINFILTER = LINEAR;
	MAGFILTER = LINEAR;
	MIPFILTER = LINEAR;
};
 
/* ############################################################################# */
 
#define	ProjectionA	oblv_ProjectionTransform_MAINPASS[2][2]
#define	ProjectionB	oblv_ProjectionTransform_MAINPASS[3][2]
 
float LinearDepth(in float2 coord : TEXCOORD0) {
//	return (ProjectionB / (tex2D(DpthSampler, coord).x - ProjectionA)) / rangeZ;
	return ProjectionB / ((tex2D(DpthSampler, coord).x - ProjectionA) * rangeZ);
}
 
float LinearDepthL(in float2 coord : TEXCOORD0) {
//	return (ProjectionB / (tex2D(DpthSamplerL, coord).x - ProjectionA)) / rangeZ;
	return ProjectionB / ((tex2D(DpthSamplerL, coord).x - ProjectionA) * rangeZ);
}

Fog [OBGEv3]

Functions for fog calculation.

includes/Fog.hlsl
/* ############################################################################# */
 
shared float4 oblv_FogRange = float4(8000, 20500, 20500 - 8000, 20500 + 8000);
 
/* ############################################################################# */
 
float FogDecay(float depth) {
	const float fogNear = min(oblv_FogRange.x,  8000);
	const float fogFar  = min(oblv_FogRange.y, 20500);
 
	float decay = 1.0;
	if ((depth * rangeZ) >= fogNear)
		decay = lerp(decay, 0.0, ((depth * rangeZ) - fogNear) / (fogFar - fogNear));
 
	return saturate(decay);
}

Math [OBGEv3]

Functions for algebra-math.

includes/Math.hlsl
/* ############################################################################# */
 
// .x - top    left  frustum ray
// .y - bottom left  frustum ray
// .z - top    right frustum ray
// .w - bottom right frustum ray
shared row_major matrix oblv_CameraFrustum_MAINPASS;
shared           float3 oblv_CameraForward_MAINPASS;
 
shared row_major matrix oblv_WorldTransform_MAINPASS;
shared row_major matrix oblv_ProjectionInverse_MAINPASS;
 
shared row_major matrix oblv_ViewTransform_MAINPASS;
shared row_major matrix oblv_ViewInverse_MAINPASS;
 
shared row_major matrix oblv_ViewProjectionTransform_MAINPASS;
shared row_major matrix oblv_ViewProjectionInverse_MAINPASS;
 
shared row_major matrix oblv_WorldViewProjectionTransform_MAINPASS;
shared row_major matrix oblv_WorldViewProjectionInverse_MAINPASS;
 
static const float3 EyePosition = float3(
	-oblv_WorldTransform_MAINPASS[3][0],
	-oblv_WorldTransform_MAINPASS[3][1],
	-oblv_WorldTransform_MAINPASS[3][2]
);
 
/* ############################################################################# */
 
float3 matrixcross(float3 a, float3 b) {
	return mul(
		float3x3(
			float3(0, -a.z, a.y),
			float3(a.z, 0, -a.x),
			float3(-a.y, a.x, 0)
		),
		b
	);
}

Position [OBGEv3]

Functions for calculating eye- and world positions from depth.

includes/Position.hlsl
/* ############################################################################# */
 
static const float2 g_InvFocalLen = {
#define	PI	3.1415926535897932384626433832795
 
	1.0 / oblv_ProjectionTransform_MAINPASS[0][0],
	1.0 / oblv_ProjectionTransform_MAINPASS[1][1]
};
 
float3 EyePosition(in float2 uv, in float eye_z) {
	uv = (uv * float2(2.0, -2.0) - float2(1.0, -1.0));
	float3 pos = float3(uv * g_InvFocalLen * eye_z, eye_z);
	return pos;
}

Random [OBGEv3]

Functions for generating (pseudo-)random numbers.

includes/Random.hlsl
/* ############################################################################# */
 
float rand_1_05(in float2 uv)
{
	float2 noise = (frac(sin(dot(uv ,float2(12.9898,78.233)*2.0)) * 43758.5453));
	return abs(noise.x + noise.y) * 0.5;
}
 
float2 rand_2_10(in float2 uv) {
	float noiseX = (frac(sin(dot(uv, float2(12.9898,78.233) * 2.0)) * 43758.5453));
	float noiseY = sqrt(1 - noiseX * noiseX);
	return float2(noiseX, noiseY);
}
 
float2 rand_2_0004(in float2 uv)
{
	float noiseX = (frac(sin(dot(uv, float2(12.9898,78.233)      )) * 43758.5453));
	float noiseY = (frac(sin(dot(uv, float2(12.9898,78.233) * 2.0)) * 43758.5453));
	return float2(noiseX, noiseY) * 0.004;
}

Reflection [OBGEv3]

Functions for reflections and reflection-maps.

includes/Reflection.hlsl
/* ############################################################################# */
 
texture2D oblv_Rendertarget0_REFLECTIONPASS;
sampler2D ReflSampler = sampler_state {
	texture = <oblv_Rendertarget0_REFLECTIONPASS>;
 
	AddressU = CLAMP;
	AddressV = CLAMP;
 
	MINFILTER = POINT;
	MAGFILTER = POINT;
	MIPFILTER = NONE;
};

Resolution [OBGEv3]

Functions for FoV and the resolution.

includes/Resolution.hlsl
/* ############################################################################# */
 
#define	rcpres		oblv_ReciprocalResolution_MAINPASS.xy
#define	aspect		(oblv_ReciprocalResolution_MAINPASS.z)
#define	raspect		(1.0 / aspect)
// .x - 1 / width
// .y - 1 / height
// .z - width * height
// .w - width / height
shared float4 oblv_ReciprocalResolution_MAINPASS;
 
/* ############################################################################# */
 
#define	FOV	oblv_ProjectionFoV_MAINPASS.x
// .x - fovX rad
// .y - fovY rad
// .z - fovX deg
// .w - fovY deg
shared float4 oblv_ProjectionFoV_MAINPASS;

Sun [OBGEv3]

Functions for the sun.

includes/Sun.hlsl
/* ############################################################################# */
 
/* Light properties */
shared float4 oblv_SunDirection;
shared float4 oblv_SunTiming;

Sun-PCS [OBGEv3]

Functions to calculate the sun-color from PCS.

includes/Sun-PCS.hlsl
/* ############################################################################# */
 
/* Light properties */
shared float4 oblv_SunDirection;
shared float4 oblv_SunTiming;
shared float4 oblv_PlayerPosition;
 
texture2D cust_texSun < string filename = "sky/pcs/Sun_025k.dds"; >;
sampler2D cust_samplerSun = sampler_state
{
    Texture = <cust_texSun>;
 
    AddressU  = CLAMP;
    AddressV  = CLAMP;
    AddressW  = CLAMP;
 
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
 
texture2D cust_texOpticalDepth < string filename = "sky/pcs/OptDpta_2D.dds"; >;
sampler2D cust_samplerOpticalDepth = sampler_state
{
    Texture = <cust_texOpticalDepth>;
 
    AddressU  = CLAMP;
    AddressV  = CLAMP;
 
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
 
    Bordercolor = 0.0f;
};
 
/* ############################################################################# */
 
static const float3 cust_SunIntensity = float3(1.0, 0.960784, 0.949019);
 
#define	VARYING_EFFICIENTS
#define	SUNRSE_IndRay	1.5000
#define	MIDDAY_IndRay	1.0000
#define	SUNSET_IndRay	2.5000
#define	SUNRSE_IndMie	4.0000
#define	MIDDAY_IndMie	1.0000
#define	SUNSET_IndMie	0.5000
#define	SUNRSE_MieCSB	0.0085
#define	MIDDAY_MieCSB	0.0025
#define	SUNSET_MieCSB	0.0015
 
/* ############################################################################# */
 
inline float get_height_coefficient(float3 Eye, float fPlanetRadius, float fAtmosphereRadius)
{
	float r = length(Eye);
	return
		sqrt(                r * r                 - fPlanetRadius * fPlanetRadius) /
		sqrt(fAtmosphereRadius * fAtmosphereRadius - fPlanetRadius * fPlanetRadius);
}
 
float4 SunColor() {
 
	float3 L = -oblv_SunDirection.xyz;
 
    /* scattering shader ------------------------------------- */
 
	float cust_fIndRay    = MIDDAY_IndRay;
	float cust_fIndMie    = MIDDAY_IndMie;
	float cust_fMieCoefSB = MIDDAY_MieCSB;
 
#ifdef	VARYING_EFFICIENTS
	/*  morning:   1.5, 4.0, 0.0085
	 *  noon:      1.0, 1.0, 0.0025
	 *  afternoon: 2.0, 0.5, 0.0015
	 */
	float3 T = normalize(float3(-L.x, 0, -L.z));
	float noon = T.z;//saturate(dot(float3(0, 0, 1), T));
 
	if (T.x < 0) {
		cust_fIndRay    = lerp(SUNSET_IndRay, cust_fIndRay, noon);
		cust_fIndMie    = lerp(SUNSET_IndMie, cust_fIndMie, noon);
		cust_fMieCoefSB = lerp(SUNSET_MieCSB, cust_fMieCoefSB, noon);
	}
	else {
		cust_fIndRay    = lerp(SUNRSE_IndRay, cust_fIndRay, noon);
		cust_fIndMie    = lerp(SUNRSE_IndMie, cust_fIndMie, noon);
		cust_fMieCoefSB = lerp(SUNRSE_MieCSB, cust_fMieCoefSB, noon);
	}
#endif
 
    /* sun color ----------------------------------------------------------- */
 
	/* A&P dimensions */
	const float fAtmosphereRadius = 6432797.0;
	const float fPlanetRadius = 6372797.0;
	const float fThicknessInv = 1.0 / (60000);
 
//efine	SUN_SIZE	(1024 - (256 * pow(1.0 - L.z, 0.125)))
#define	SUN_SIZE	(1024)
#define	SUN_BRIGHTNESS	(400)
#define	PLAYER_HEIGHT	oblv_PlayerPosition.z
 
	/* we normally should put the real player-position here (from where?)
	 *
	 * additionally, the reflections' pass camera is below surface, we could
	 * intersect the view-vector with the planet's surface to get an individual
	 * view-vector per pixel, though I don't know if the few hundret meters we
	 * can go around really affect the sky's appearance
	 *
	 * for the moment we neglect any variable position of the camera
	 */
	float3 Eye = float3(0.0, 0.0, fPlanetRadius + max(PLAYER_HEIGHT, 0));	// Eye is 0km up in global
//	float3 L = -oblv_SunDirection.xyz;
 
	// new parameterization
	// if we are directly on the planet-surface this is 0 and 1 for above atmosphere
	// range [0.0, 1.0] or more
	float height = get_height_coefficient(Eye, fPlanetRadius, fAtmosphereRadius);
 
	/* eye-normal */
	float3 nEye = normalize(Eye);
 
	/* zenith:   dot([0,0,1], [0,0,-1]) = -1 = 0.0
	 * horizont: dot([0,0,1], [0,-1,0]) =  0 = 0.5
	 */
	float4 tl = tex2Dlod(cust_samplerOpticalDepth, float4(height, 0.5f * (dot(nEye, L)) + 0.5f, 0, 0));
 
	float4 cust_SunColor = tex2Dlod(cust_samplerSun, float4(0.5, (dot(L, L) - 1.0) * SUN_SIZE + 0.5, 0, 0));
 
	float3 sunIntensityAtt = float3(1,1,1) * exp(
		-cust_fIndRay * tl.xyz
		-cust_fIndMie * tl.aaa
	);
 
        float4 sunColor = float4(SUN_BRIGHTNESS * cust_SunColor * sunIntensityAtt, cust_SunColor.g);
 
        return sunColor;
}

Time [OBGEv3]

Functions for timings.

includes/Time.hlsl
/* ############################################################################# */
 
// .x - random() * 1000 * 1000 / frequency
// .y - random() * 1000        / frequency
// .z - random()               / frequency
// .w -
shared int4 obge_Tick;
 
/* ############################################################################# */
 
// .x - total seconds
// .y - seconds of day
// .z - minutes of day
// .w - hours of day
shared int4 oblv_GameTime;