This commit is contained in:
watrabi
2025-09-18 17:55:52 -04:00
commit 977f1ff4b8
15030 changed files with 17324420 additions and 0 deletions
@@ -0,0 +1,249 @@
#include "common.h"
struct Appdata
{
float4 Position : POSITION;
float2 Uv : TEXCOORD0;
float3 Normal : NORMAL0;
};
struct VertexOutput
{
float4 HPosition : POSITION;
float2 Uv : TEXCOORD0;
float4 Color : COLOR0;
float FogFactor : TEXCOORD1;
};
struct AALineVertexOutput
{
float4 HPosition : POSITION;
float4 Position : TEXCOORD1;
float4 Color : COLOR0;
float FogFactor : COLOR1;
float4 Start : TEXCOORD2;
float4 End : TEXCOORD3;
};
struct OutlineVertexOutput
{
float4 HPosition : POSITION;
float4 Color : COLOR0;
float4 Position : TEXCOORD0;
float4 CenterRadius : TEXCOORD1;
};
WORLD_MATRIX(WorldMatrix);
uniform float4 Color;
// pixel info is for AA line
// x -> Fov * 0.5f / screenSize.y;
// y -> ScreenWidth
// z -> ScreenWidth / ScreenHeight
// w -> Line thickness
uniform float4 PixelInfo;
VertexOutput AdornSelfLitVSGeneric(Appdata IN, float ambient)
{
VertexOutput OUT = (VertexOutput)0;
float4 position = mul(WorldMatrix, IN.Position);
float3 normal = normalize(mul((float3x3)WorldMatrix, IN.Normal));
float3 light = normalize(G(CameraPosition).xyz - position.xyz);
float ndotl = saturate(dot(normal, light));
float lighting = ambient + (1 - ambient) * ndotl;
float specular = pow(ndotl, 64.0);
OUT.HPosition = mul(G(ViewProjection), mul(WorldMatrix, IN.Position));
OUT.Uv = IN.Uv;
OUT.Color = float4(Color.rgb * lighting + specular, Color.a);
OUT.FogFactor = (G(FogParams).z - OUT.HPosition.w) * G(FogParams).w;
return OUT;
}
VertexOutput AdornSelfLitVS(Appdata IN)
{
return AdornSelfLitVSGeneric(IN, 0.5f);
}
VertexOutput AdornSelfLitHighlightVS(Appdata IN)
{
return AdornSelfLitVSGeneric(IN, 0.75f);
}
VertexOutput AdornVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
float4 position = mul(WorldMatrix, IN.Position);
#ifdef PIN_LIGHTING
float3 normal = normalize(mul((float3x3)WorldMatrix, IN.Normal));
float ndotl = dot(normal, -G(Lamp0Dir));
float3 lighting = G(AmbientColor) + saturate(ndotl) * G(Lamp0Color) + saturate(-ndotl) * G(Lamp1Color);
#else
float3 lighting = 1;
#endif
OUT.HPosition = mul(G(ViewProjection), position);
OUT.Uv = IN.Uv;
OUT.Color = float4(Color.rgb * lighting, Color.a);
OUT.FogFactor = (G(FogParams).z - OUT.HPosition.w) * G(FogParams).w;
return OUT;
}
TEX_DECLARE2D(DiffuseMap, 0);
float4 AdornPS(VertexOutput IN): COLOR0
{
float4 result = tex2D(DiffuseMap, IN.Uv) * IN.Color;
result.rgb = lerp(G(FogColor), result.rgb, saturate(IN.FogFactor));
return result;
}
AALineVertexOutput AdornAALineVS(Appdata IN)
{
AALineVertexOutput OUT = (AALineVertexOutput)0;
float4 position = mul(WorldMatrix, IN.Position);
float3 normal = normalize(mul((float3x3)WorldMatrix, IN.Normal));
// line start and end position in world space
float4 startPosW = mul(WorldMatrix, float4(1, 0, 0, 1));
float4 endPosW = mul(WorldMatrix, float4(-1, 0, 0, 1));
// Compute view-space w
float w = dot(G(ViewProjection)[3], float4(position.xyz, 1.0f));
// radius in pixels + constant because line has to be little bit bigget to perform anti aliasing
float radius = PixelInfo.w + 2;
// scale the way that line has same size on screen
if (length(position - startPosW) < length(position - endPosW))
{
float w = dot(G(ViewProjection)[3], float4(startPosW.xyz, 1.0f));
float pixel_radius = radius * w * PixelInfo.x;
position.xyz = startPosW.xyz + normal * pixel_radius;
}
else
{
float w = dot(G(ViewProjection)[3], float4(endPosW.xyz, 1.0f));
float pixel_radius = radius * w * PixelInfo.x;
position.xyz = endPosW.xyz + normal * pixel_radius;
}
// output for PS
OUT.HPosition = mul(G(ViewProjection), position);
OUT.Position = OUT.HPosition;
OUT.Start = mul(G(ViewProjection), startPosW);
OUT.End = mul(G(ViewProjection), endPosW);
OUT.FogFactor = (G(FogParams).z - OUT.HPosition.w) * G(FogParams).w;
// screen ratio
OUT.Position.y *= PixelInfo.z;
OUT.Start.y *= PixelInfo.z;
OUT.End.y *= PixelInfo.z;
return OUT;
}
float4 AdornAALinePS(AALineVertexOutput IN): COLOR0
{
IN.Position /= IN.Position.w ;
IN.Start /= IN.Start.w;
IN.End /= IN.End.w;
float4 result = 1;
float2 lineDir = normalize(IN.End.xy - IN.Start.xy);
float2 fragToPoint = IN.Position.xy - IN.Start.xy;
// tips of the line are not Anti-Aliesed, they are just cut
// discard as soon as we can
float startDist = dot(lineDir, fragToPoint);
float endDist = dot(lineDir, -IN.Position.xy + IN.End.xy);
if (startDist < 0)
discard;
if (endDist < 0)
discard;
float2 perpLineDir = float2(lineDir.y, -lineDir.x);
float dist = abs(dot(perpLineDir, fragToPoint));
// high point serves to compute the function which is described bellow.
float highPoint = 1 + (PixelInfo.w - 1) * 0.5;
// this is function that has this shape /¯¯¯\, it is symetric, centered around 0 on X axis
// slope parts are +- 45 degree and are 1px thick. Area of the shape sums to line thickness in pixels
// funtion for 1px would be /\, func for 2px is /¯\ and so on...
result.a = saturate(highPoint - (dist * 0.5 * PixelInfo.y));
result *= Color;
// convert to sRGB, its not perfect for non-black backgrounds, but its the best we can get
result.a = pow( saturate(1 - result.a), 1/2.2);
result.a = 1 - result.a;
result.rgb = lerp(G(FogColor), result.rgb, saturate(IN.FogFactor));
return result;
}
OutlineVertexOutput AdornOutlineVS(Appdata IN)
{
OutlineVertexOutput OUT = (OutlineVertexOutput)0;
float4 position = mul(WorldMatrix, IN.Position);
OUT.HPosition = mul(G(ViewProjection), position);
OUT.Color = Color;
OUT.Position = position;
OUT.CenterRadius = float4(mul(WorldMatrix, float4(0, 0, 0, 1)).xyz, length(mul(WorldMatrix, float4(1, 0, 0, 0))));
return OUT;
}
float4 AdornOutlinePS(OutlineVertexOutput IN): COLOR0
{
float3 rayO = IN.Position.xyz - IN.CenterRadius.xyz;
float3 rayD = normalize(IN.Position.xyz - G(CameraPosition));
// magnitude(rayO + t * rayD) = radius
// t^2 + bt + c = radius
float thickness = 1;
float r0 = IN.CenterRadius.w;
float r1 = max(0, IN.CenterRadius.w - thickness);
float b = 2 * dot(rayO, rayD);
float c0 = dot(rayO, rayO) - r0 * r0;
float c1 = dot(rayO, rayO) - r1 * r1;
if (b * b < 4 * c0)
discard;
if (b * b > 4 * c1)
discard;
return IN.Color;
}
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 1
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0.6
#define CFG_NORMAL_SHADOW_SCALE 0
#define CFG_SPECULAR_LOD 0.94
#define CFG_GLOSS_LOD 240
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0.25
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0.75
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_DIFFUSE_CONST
#include "material.hlsl"
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 1.3
#define CFG_GLOSS_SCALE 64
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.1
#define CFG_SPECULAR_LOD 0.13
#define CFG_GLOSS_LOD 44
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_BLEND_COLOR
#include "material.hlsl"
@@ -0,0 +1,22 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 3
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.3
#define CFG_SPECULAR_LOD 0.21
#define CFG_GLOSS_LOD 22
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_OPT_BLEND_COLOR
#define CFG_WANG_TILES
#define CFG_WANG_TILES_SCALE 1
#include "material.hlsl"
@@ -0,0 +1,252 @@
#include "globals.h"
// GLSLES has limited number of vertex shader registers so we have to use less bones
#if defined(GLSLES) && !defined(GL3)
#define MAX_BONE_COUNT 32
#else
#define MAX_BONE_COUNT 72
#endif
// PowerVR saturate() is compiled to min/max pair
// These are cross-platform specialized saturates that are free on PC and only cost 1 cycle on PowerVR
#ifdef GLSLES
float saturate0(float v) { return max(v, 0); }
float saturate1(float v) { return min(v, 1); }
#define WANG_SUBSET_SCALE 2
#else
float saturate0(float v) { return saturate(v); }
float saturate1(float v) { return saturate(v); }
#define WANG_SUBSET_SCALE 1
#endif
#define GBUFFER_MAX_DEPTH 500.0f
#ifndef DX11
#define TEX_DECLARE2D(name, reg) sampler2D name: register(s##reg)
#define TEX_DECLARE3D(name, reg) sampler3D name: register(s##reg)
#define TEX_DECLARECUBE(name, reg) samplerCUBE name: register(s##reg)
#define TEXTURE(name) name
#define TEXTURE_IN_2D(name) sampler2D name
#define TEXTURE_IN_3D(name) sampler3D name
#define TEXTURE_IN_CUBE(name) samplerCUBE name
#define WORLD_MATRIX(name) uniform float4x4 name;
#define WORLD_MATRIX_ARRAY(name, count) uniform float4 name [ count ];
#ifdef GLSL
#define ATTR_INT4 float4
#define ATTR_INT3 float3
#define ATTR_INT2 float2
#define ATTR_INT float
#else
#define ATTR_INT4 int4
#define ATTR_INT3 int3
#define ATTR_INT2 int2
#define ATTR_INT int
#endif
#else
#define TEX_DECLARE2D(name, reg) SamplerState name##Sampler: register(s##reg); Texture2D<float4> name##Texture: register(t##reg)
#define TEX_DECLARE3D(name, reg) SamplerState name##Sampler: register(s##reg); Texture3D<float4> name##Texture: register(t##reg)
#define TEX_DECLARECUBE(name, reg) SamplerState name##Sampler: register(s##reg); TextureCube<float4> name##Texture: register(t##reg)
#define tex2D(tex, uv) tex##Texture.Sample(tex##Sampler, uv)
#define tex3D(tex, uv) tex##Texture.Sample(tex##Sampler, uv)
#define texCUBE(tex, uv) tex##Texture.Sample(tex##Sampler, uv)
#define tex2Dgrad(tex, uv, DDX, DDY) tex##Texture.SampleGrad(tex##Sampler, uv, DDX, DDY)
#define tex2Dbias(tex, uv) tex##Texture.SampleBias(tex##Sampler, uv.xy, uv.w)
#define texCUBEbias(tex, uv) tex##Texture.SampleBias(tex##Sampler, uv.xyz, uv.w)
#define TEXTURE(name) name##Sampler, name##Texture
#define TEXTURE_IN_2D(name) SamplerState name##Sampler, Texture2D name##Texture
#define TEXTURE_IN_3D(name) SamplerState name##Sampler, Texture3D name##Texture
#define TEXTURE_IN_CUBE(name) SamplerState name##Sampler, TextureCube name##Texture
#define WORLD_MATRIX(name) cbuffer WorldMatrixCB : register( b1 ) { float4x4 name; }
#define WORLD_MATRIX_ARRAY(name, count) cbuffer WorldMatrixCB : register( b1 ) { float4 name[ count ]; }
#define ATTR_INT4 int4
#define ATTR_INT3 int3
#define ATTR_INT2 int2
#define ATTR_INT int
#endif
#if defined(GLSLES) || defined(PIN_WANG_FALLBACK)
#define TEXTURE_WANG(name) 0
void getWang(float unused, float2 uv, float tiling, out float2 wangUv, out float4 wangUVDerivatives)
{
wangUv = uv * WANG_SUBSET_SCALE;
wangUVDerivatives = float4(0,0,0,0); // not used in this mode
}
float4 sampleWang(TEXTURE_IN_2D(s), float2 uv, float4 wangUVDerivatives)
{
return tex2D(s,uv);
}
#else
#define TEXTURE_WANG(name) TEXTURE(name)
void getWang(TEXTURE_IN_2D(s), float2 uv, float tiling, out float2 wangUv, out float4 wangUVDerivatives)
{
#ifndef WIN_MOBILE
float idxTexSize = 128;
#else
float idxTexSize = 32;
#endif
float2 wangBase = uv * tiling * 4;
#if defined(DX11) && !defined(WIN_MOBILE)
// compensate the precision problem of Point Sampling on some cards. (We do it just at DX11 for performance reasons)
float2 wangUV = (floor(wangBase) + 0.5) / idxTexSize;
#else
float2 wangUV = wangBase / idxTexSize;
#endif
#if defined(DX11) || defined(GL3)
float2 wang = tex2D(s, wangUV).rg;
#else
float2 wang = tex2D(s, wangUV).ba;
#endif
wangUVDerivatives = float4(ddx(wangBase*0.25), ddy(wangBase*0.25));
wang *= 255.0/256.0;
wangUv = wang + frac(wangBase)*0.25;
}
float4 sampleWang(TEXTURE_IN_2D(s), float2 uv, float4 derivates)
{
return tex2Dgrad(s, uv, derivates.xy, derivates.zw);
}
#endif
float4 gbufferPack(float depth, float3 diffuse, float3 specular, float fog)
{
depth = saturate(depth / GBUFFER_MAX_DEPTH);
const float3 bitSh = float3(255*255, 255, 1);
const float3 lumVec = float3(0.299, 0.587, 0.114);
float2 comp;
comp = depth*float2(255,255*256);
comp = frac(comp);
comp = float2(depth,comp.x*256/255) - float2(comp.x, comp.y)/255;
float4 result;
result.r = lerp(1, dot(specular, lumVec), saturate(3 * fog));
result.g = lerp(0, dot(diffuse, lumVec), saturate(3 * fog));
result.ba = comp.yx;
return result;
}
float3 lgridOffset(float3 v, float3 n)
{
// cells are 4 studs in size
// offset in normal direction to prevent self-occlusion
// the offset has to be 1.5 cells in order to fully eliminate the influence of the source cell with trilinear filtering
// (i.e. 1 cell is enough for point filtering, but is not enough for trilinear filtering)
return v + n * (1.5f * 4.f);
}
float3 lgridPrepareSample(float3 c)
{
// yxz swizzle is necessary for GLSLES sampling to work efficiently
// (having .y as the first component allows to do the LUT lookup as a non-dependent texture fetch)
return c.yxz * G(LightConfig0).xyz + G(LightConfig1).xyz;
}
#if defined(GLSLES) && !defined(GL3)
#define LGRID_SAMPLER(name, register) TEX_DECLARE2D(name, register)
float4 lgridSample(TEXTURE_IN_2D(t), TEXTURE_IN_2D(lut), float3 data)
{
float4 offsets = tex2D(lut, data.xy);
// texture is 64 pixels high
// let's compute slice lerp coeff
float slicef = frac(data.x * 64);
// texture has 64 slices with 8x8 atlas setup
float2 base = saturate(data.yz) * 0.125;
float4 s0 = tex2D(t, base + offsets.xy);
float4 s1 = tex2D(t, base + offsets.zw);
return lerp(s0, s1, slicef);
}
#else
#define LGRID_SAMPLER(name, register) TEX_DECLARE3D(name, register)
float4 lgridSample(TEXTURE_IN_3D(t), TEXTURE_IN_2D(lut), float3 data)
{
float3 edge = step(G(LightConfig3).xyz, abs(data - G(LightConfig2).xyz));
float edgef = saturate1(dot(edge, 1));
// replace data with 0 on edges to minimize texture cache misses
float4 light = tex3D(t, data.yzx - data.yzx * edgef);
return lerp(light, G(LightBorder), edgef);
}
#endif
#ifdef GLSLES
float3 nmapUnpack(float4 value)
{
return value.rgb * 2 - 1;
}
#else
float3 nmapUnpack(float4 value)
{
float2 xy = value.ag * 2 - 1;
return float3(xy, sqrt(saturate(1 + dot(-xy, xy))));
}
#endif
float3 terrainNormal(float4 tnp0, float4 tnp1, float4 tnp2, float3 w, float3 normal, float3 tsel)
{
// Inspired by "Voxel-Based Terrain for Real-Time Virtual Simulations" [Lengyel2010] 5.5.2
float3 tangentTop = float3(normal.y, -normal.x, 0);
float3 tangentSide = float3(normal.z, 0, -normal.x);
float3 bitangentTop = float3(0, -normal.z, normal.y);
float3 bitangentSide = float3(0, -1, 0);
// Blend pre-unpack to save cycles
float3 tn = nmapUnpack(tnp0 * w.x + tnp1 * w.y + tnp2 * w.z);
// We blend all tangent frames together as a faster approximation to the correct world normal blend
float tselw = dot(tsel, w);
float3 tangent = lerp(tangentSide, tangentTop, tselw);
float3 bitangent = lerp(bitangentSide, bitangentTop, tselw);
return normalize(tangent * tn.x + bitangent * tn.y + normal * tn.z);
}
float3 shadowPrepareSample(float3 p)
{
float4 c = float4(p, 1);
return float3(dot(G(ShadowMatrix0), c), dot(G(ShadowMatrix1), c), dot(G(ShadowMatrix2), c));
}
float shadowDepth(float3 lpos)
{
return lpos.z;
}
float shadowStep(float d, float z)
{
// saturate returns 1 for z in [0.1..0.9]; it fades to 0 as z approaches 0 or 1
return step(d, z) * saturate(9 - 20 * abs(z - 0.5));
}
float shadowSample(TEXTURE_IN_2D(map), float3 lpos, float lightShadow)
{
float2 smDepth = tex2D(map, lpos.xy).rg;
float smShadow = shadowStep(smDepth.x, shadowDepth(lpos));
return (1 - smShadow * smDepth.y * G(OutlineBrightness_ShadowInfo).w) * lightShadow;
}
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 1.3
#define CFG_GLOSS_SCALE 128
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0
#define CFG_SPECULAR_LOD 0.07
#define CFG_GLOSS_LOD 22
#define CFG_NORMAL_DETAIL_TILING 10
#define CFG_NORMAL_DETAIL_SCALE 1
#define CFG_FAR_TILING 0.25
#define CFG_FAR_DIFFUSE_CUTOFF 0.75
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_NORMAL_CONST
#include "material.hlsl"
@@ -0,0 +1,329 @@
#include "common.h"
#define LQMAT_FADE_FACTOR (1.0f/300.0f)
struct Appdata
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 Uv : TEXCOORD0;
float2 UvStuds : TEXCOORD1;
float4 Color : COLOR0;
ATTR_INT4 Extra : COLOR1;
#ifdef PIN_SURFACE
float3 Tangent : TEXCOORD2;
#endif
float4 EdgeDistances : TEXCOORD3;
};
struct VertexOutput
{
float4 Uv_EdgeDistance1 : TEXCOORD0;
float4 UvStuds_EdgeDistance2 : TEXCOORD1;
float4 Color : COLOR0;
float4 LightPosition_Fog : TEXCOORD2;
float4 View_Depth : TEXCOORD3;
#if defined(PIN_HQ) || defined(PIN_REFLECTION)
float4 Normal_SpecPower : TEXCOORD4;
#endif
#ifdef PIN_SURFACE
float3 Tangent : TEXCOORD5;
#else
float4 Diffuse_Specular : COLOR1;
#endif
float4 PosLightSpace_Reflectance: TEXCOORD6;
};
#ifdef PIN_SKINNED
WORLD_MATRIX_ARRAY(WorldMatrixArray, MAX_BONE_COUNT * 3);
#endif
#ifdef PIN_DEBUG
uniform float4 DebugColor;
#endif
VertexOutput DefaultVS(Appdata IN, out float4 HPosition: POSITION)
{
VertexOutput OUT = (VertexOutput)0;
// Transform position and normal to world space
#ifdef PIN_SKINNED
int boneIndex = IN.Extra.r;
float4 worldRow0 = WorldMatrixArray[boneIndex * 3 + 0];
float4 worldRow1 = WorldMatrixArray[boneIndex * 3 + 1];
float4 worldRow2 = WorldMatrixArray[boneIndex * 3 + 2];
float3 posWorld = float3(dot(worldRow0, IN.Position), dot(worldRow1, IN.Position), dot(worldRow2, IN.Position));
float3 normalWorld = float3(dot(worldRow0.xyz, IN.Normal), dot(worldRow1.xyz, IN.Normal), dot(worldRow2.xyz, IN.Normal));
#else
float3 posWorld = IN.Position.xyz;
float3 normalWorld = IN.Normal;
#endif
// Decode diffuse/specular parameters; encoding depends on the skinned flag due to vertex declaration differences
#if defined(PIN_DEBUG)
float4 color = DebugColor;
#else
float4 color = IN.Color;
#endif
float specularIntensity = IN.Extra.g / 255.f;
float specularPower = IN.Extra.b;
float ndotl = dot(normalWorld, -G(Lamp0Dir));
#ifdef PIN_HQ
// We'll calculate specular in pixel shader
float2 lt = float2(saturate(ndotl), (ndotl > 0));
#else
// Using lit here improves performance on software vertex shader implementations
float2 lt = lit(ndotl, dot(normalize(-G(Lamp0Dir) + normalize(G(CameraPosition).xyz - posWorld.xyz)), normalWorld), specularPower).yz;
#endif
HPosition = mul(G(ViewProjection), float4(posWorld, 1));
OUT.Uv_EdgeDistance1.xy = IN.Uv;
OUT.UvStuds_EdgeDistance2.xy = IN.UvStuds;
OUT.Color = color;
OUT.LightPosition_Fog = float4(lgridPrepareSample(lgridOffset(posWorld, normalWorld)), (G(FogParams).z - HPosition.w) * G(FogParams).w);
OUT.View_Depth = float4(G(CameraPosition).xyz - posWorld, HPosition.w);
#if defined(PIN_HQ) || defined(PIN_REFLECTION)
float4 edgeDistances = IN.EdgeDistances*G(FadeDistance_GlowFactor).z + 0.5 * OUT.View_Depth.w * G(FadeDistance_GlowFactor).y;
OUT.Uv_EdgeDistance1.zw = edgeDistances.xy;
OUT.UvStuds_EdgeDistance2.zw = edgeDistances.zw;
OUT.Normal_SpecPower = float4(normalWorld, specularPower);
OUT.PosLightSpace_Reflectance.w = IN.Extra.a / 255.f;
#endif
#ifdef PIN_SURFACE
#ifdef PIN_SKINNED
float3 tangent = float3(dot(worldRow0.xyz, IN.Tangent), dot(worldRow1.xyz, IN.Tangent), dot(worldRow2.xyz, IN.Tangent));
#else
float3 tangent = IN.Tangent;
#endif
OUT.Tangent = tangent;
#else
float3 diffuse = lt.x * G(Lamp0Color) + max(-ndotl, 0) * G(Lamp1Color);
OUT.Diffuse_Specular = float4(diffuse, lt.y * specularIntensity);
#endif
OUT.PosLightSpace_Reflectance.xyz = shadowPrepareSample(posWorld);
return OUT;
}
#ifdef PIN_SURFACE
struct SurfaceInput
{
float4 Color;
float2 Uv;
float2 UvStuds;
#ifdef PIN_REFLECTION
float Reflectance;
#endif
};
struct Surface
{
float3 albedo;
float3 normal;
float specular;
float gloss;
float reflectance;
};
Surface surfaceShader(SurfaceInput IN, float2 fade);
Surface surfaceShaderExec(VertexOutput IN)
{
SurfaceInput SIN;
SIN.Color = IN.Color;
SIN.Uv = IN.Uv_EdgeDistance1.xy;
SIN.UvStuds = IN.UvStuds_EdgeDistance2.xy;
#ifdef PIN_REFLECTION
SIN.Reflectance = IN.PosLightSpace_Reflectance.w;
#endif
float2 fade;
fade.x = saturate0(1 - IN.View_Depth.w * LQMAT_FADE_FACTOR );
fade.y = saturate0(1 - IN.View_Depth.w * G(FadeDistance_GlowFactor).y );
return surfaceShader(SIN, fade);
}
#endif
TEX_DECLARE2D(StudsMap, 0);
LGRID_SAMPLER(LightMap, 1);
TEX_DECLARE2D(LightMapLookup, 2);
TEX_DECLARE2D(ShadowMap, 3);
TEX_DECLARECUBE(EnvironmentMap, 4);
TEX_DECLARE2D(DiffuseMap, 5);
TEX_DECLARE2D(NormalMap, 6);
TEX_DECLARE2D(SpecularMap, 7);
#ifndef GLSLES
TEX_DECLARE2D(NormalDetailMap, 8);
#endif
uniform float4 LqmatFarTilingFactor; // material tiling factor for low-quality shader, must be the same as CFG_FAR_TILING
float4 sampleFar1(TEXTURE_IN_2D(s), float2 uv, float fade, float cutoff)
{
#ifdef GLSLES
return tex2D(s, uv);
#else
if (cutoff == 0)
return tex2D(s, uv);
else
{
float cscale = 1 / (1 - cutoff);
return lerp(tex2D(s, uv * (LqmatFarTilingFactor.xy) ), tex2D(s, uv ), saturate0(fade * cscale - cutoff * cscale));
}
#endif
}
#if defined(PIN_WANG) || defined(PIN_WANG_FALLBACK)
float4 sampleWangSimple(TEXTURE_IN_2D(s), float2 uv)
{
float2 wangUv;
float4 wangUVDerivatives;
getWang(TEXTURE_WANG(NormalDetailMap), uv, 1, wangUv, wangUVDerivatives);
return sampleWang(TEXTURE(s), wangUv, wangUVDerivatives);
}
#endif
void DefaultPS(VertexOutput IN,
#ifdef PIN_GBUFFER
out float4 oColor1: COLOR1,
#endif
out float4 oColor0: COLOR0)
{
// Compute albedo term
#ifdef PIN_SURFACE
Surface surface = surfaceShaderExec(IN);
float4 albedo = float4(surface.albedo, IN.Color.a);
float3 bitangent = cross(IN.Normal_SpecPower.xyz, IN.Tangent.xyz);
float3 normal = normalize(surface.normal.x * IN.Tangent.xyz + surface.normal.y * bitangent + surface.normal.z * IN.Normal_SpecPower.xyz);
float ndotl = dot(normal, -G(Lamp0Dir));
float3 diffuseIntensity = saturate0(ndotl) * G(Lamp0Color) + max(-ndotl, 0) * G(Lamp1Color);
float specularIntensity = step(0, ndotl) * surface.specular;
float specularPower = surface.gloss;
float reflectance = surface.reflectance;
#elif PIN_LOWQMAT
#ifndef CFG_FAR_DIFFUSE_CUTOFF
#define CFG_FAR_DIFFUSE_CUTOFF (0.6f)
#endif
#if defined(PIN_WANG) || defined(PIN_WANG_FALLBACK)
float4 albedo = sampleWangSimple(TEXTURE(DiffuseMap), IN.Uv_EdgeDistance1.xy);
#else
float fade = saturate0(1 - IN.View_Depth.w * LQMAT_FADE_FACTOR);
float4 albedo = sampleFar1(TEXTURE(DiffuseMap), IN.Uv_EdgeDistance1.xy, fade, CFG_FAR_DIFFUSE_CUTOFF);
#endif
albedo.rgb = lerp(float3(1, 1, 1), IN.Color.rgb, albedo.a ) * albedo.rgb;
albedo.a = IN.Color.a;
float3 diffuseIntensity = IN.Diffuse_Specular.xyz;
float specularIntensity = IN.Diffuse_Specular.w;
float reflectance = 0;
#else
#ifdef PIN_PLASTIC
float4 studs = tex2D(StudsMap, IN.UvStuds_EdgeDistance2.xy);
float4 albedo = float4(IN.Color.rgb * (studs.r * 2), IN.Color.a);
#else
float4 albedo = tex2D(DiffuseMap, IN.Uv_EdgeDistance1.xy) * IN.Color;
#endif
#ifdef PIN_HQ
float3 normal = normalize(IN.Normal_SpecPower.xyz);
float specularPower = IN.Normal_SpecPower.w;
#elif defined(PIN_REFLECTION)
float3 normal = IN.Normal_SpecPower.xyz;
#endif
float3 diffuseIntensity = IN.Diffuse_Specular.xyz;
float specularIntensity = IN.Diffuse_Specular.w;
#ifdef PIN_REFLECTION
float reflectance = IN.PosLightSpace_Reflectance.w;
#endif
#endif
float4 light = lgridSample(TEXTURE(LightMap), TEXTURE(LightMapLookup), IN.LightPosition_Fog.xyz);
float shadow = shadowSample(TEXTURE(ShadowMap), IN.PosLightSpace_Reflectance.xyz, light.a);
// Compute reflection term
#if defined(PIN_SURFACE) || defined(PIN_REFLECTION)
float3 reflection = texCUBE(EnvironmentMap, reflect(-IN.View_Depth.xyz, normal)).rgb;
albedo.rgb = lerp(albedo.rgb, reflection.rgb, reflectance);
#endif
// Compute diffuse term
float3 diffuse = (G(AmbientColor) + diffuseIntensity * shadow + light.rgb) * albedo.rgb;
// Compute specular term
#ifdef PIN_HQ
float3 specular = G(Lamp0Color) * (specularIntensity * shadow * (float)(half)pow(saturate(dot(normal, normalize(-G(Lamp0Dir) + normalize(IN.View_Depth.xyz)))), specularPower));
#else
float3 specular = G(Lamp0Color) * (specularIntensity * shadow);
#endif
// Combine
oColor0.rgb = diffuse.rgb + specular.rgb;
oColor0.a = albedo.a;
#ifdef PIN_HQ
float ViewDepthMul = IN.View_Depth.w * G(FadeDistance_GlowFactor).y;
float outlineFade = saturate1( ViewDepthMul * G(OutlineBrightness_ShadowInfo).x + G(OutlineBrightness_ShadowInfo).y);
float2 minIntermediate = min(IN.Uv_EdgeDistance1.wz, IN.UvStuds_EdgeDistance2.wz);
float minEdgesPlus = min(minIntermediate.x, minIntermediate.y) / ViewDepthMul;
oColor0.rgb *= saturate1(outlineFade *(1.5 - minEdgesPlus) + minEdgesPlus);
#endif
float fogAlpha = saturate(IN.LightPosition_Fog.w);
#ifdef PIN_NEON
oColor0.rgb = IN.Color.rgb * G(FadeDistance_GlowFactor).w;
oColor0.a = 1 - fogAlpha * IN.Color.a;
diffuse.rgb = 0;
specular.rgb = 0;
#endif
oColor0.rgb = lerp(G(FogColor), oColor0.rgb, fogAlpha);
#ifdef PIN_GBUFFER
oColor1 = gbufferPack(IN.View_Depth.w, diffuse.rgb, specular.rgb, fogAlpha);
#endif
}
@@ -0,0 +1,21 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 2.7
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.5
#define CFG_SPECULAR_LOD 0.9
#define CFG_GLOSS_LOD 160
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#include "material.hlsl"
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 0.2
#define CFG_GLOSS_SCALE 128
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.2
#define CFG_SPECULAR_LOD 0.03
#define CFG_GLOSS_LOD 16
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_BLEND_COLOR
#include "material.hlsl"
@@ -0,0 +1,52 @@
#include "common.h"
// .xy = gbuffer width/height, .zw = inverse gbuffer width/height
uniform float4 TextureSize;
TEX_DECLARE2D(tex, 0);
struct v2f
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
#if defined(GLSL) || defined(DX11)
float4 convertPosition(float4 p)
{
return p;
}
#else
float4 convertPosition(float4 p)
{
// half-pixel offset
return p + float4(-TextureSize.z, TextureSize.w, 0, 0);
}
#endif
#if defined(GLSL)
float2 convertUv(float4 p)
{
return p.xy * 0.5 + 0.5;
}
#else
float2 convertUv(float4 p)
{
return p.xy * float2(0.5, -0.5) + 0.5;
}
#endif
v2f gbufferVS( in float4 pos : POSITION )
{
v2f o;
o.pos = convertPosition(pos);
o.uv = convertUv(pos);
return o;
}
float4 gbufferPS( v2f i ) : COLOR0
{
return tex2D( tex, i.uv );
}
@@ -0,0 +1,44 @@
#ifndef GLSL
struct Globals
{
#endif
float4x4 ViewProjection;
float4 ViewRight;
float4 ViewUp;
float4 ViewDir;
float3 CameraPosition;
float3 AmbientColor;
float3 Lamp0Color;
float3 Lamp0Dir;
float3 Lamp1Color;
float3 FogColor;
float4 FogParams;
float4 LightBorder;
float4 LightConfig0;
float4 LightConfig1;
float4 LightConfig2;
float4 LightConfig3;
float4 FadeDistance_GlowFactor;
float4 OutlineBrightness_ShadowInfo;
float4 ShadowMatrix0;
float4 ShadowMatrix1;
float4 ShadowMatrix2;
#ifndef GLSL
};
#ifdef DX11
cbuffer Globals: register( b0 ) { Globals _G; };
#else
uniform Globals _G: register(c0);
#endif
#define G(x) _G.x
#else
#define G(x) x
#endif
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 0.5
#define CFG_GLOSS_SCALE 128
#define CFG_REFLECTION_SCALE 0.2
#define CFG_NORMAL_SHADOW_SCALE 0.1
#define CFG_SPECULAR_LOD 0.19
#define CFG_GLOSS_LOD 24
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0.25
#define CFG_FAR_DIFFUSE_CUTOFF 0.6
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_NORMAL_CONST
#include "material.hlsl"
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 1
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.5
#define CFG_SPECULAR_LOD 0.17
#define CFG_GLOSS_LOD 18
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0.25
#define CFG_FAR_DIFFUSE_CUTOFF 0.6
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_BLEND_COLOR
#include "material.hlsl"
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1.0
#define CFG_SPECULAR_SCALE 1.2
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0.3
#define CFG_NORMAL_SHADOW_SCALE 0
#define CFG_SPECULAR_LOD 1
#define CFG_GLOSS_LOD 190
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0.25
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0.75
#define CFG_OPT_DIFFUSE_CONST
#include "material.hlsl"
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 1.0
#define CFG_GLOSS_SCALE 128
#define CFG_REFLECTION_SCALE 0.2
#define CFG_NORMAL_SHADOW_SCALE 0.1
#define CFG_SPECULAR_LOD 0.7
#define CFG_GLOSS_LOD 54
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_NORMAL_CONST
#include "material.hlsl"
@@ -0,0 +1,100 @@
#define PIN_SURFACE
#include "default.hlsl"
#ifndef CFG_WANG_TILES
float4 sampleFar(TEXTURE_IN_2D(s), float2 uv, float fade, float cutoff)
{
#ifdef GLSLES
return tex2D(s, uv);
#else
if (cutoff == 0)
return tex2D(s, uv);
else
{
float cscale = 1 / (1 - cutoff);
return lerp(tex2D(s, uv * (CFG_FAR_TILING) ), tex2D(s, uv ), saturate0(fade * cscale - cutoff * cscale));
}
#endif
}
#endif
Surface surfaceShader(SurfaceInput IN, float2 fade2)
{
#ifdef CFG_WANG_TILES
float2 wangUv;
float4 wangUVDerivatives;
getWang(TEXTURE_WANG(NormalDetailMap), IN.Uv, CFG_TEXTURE_TILING, wangUv, wangUVDerivatives);
#endif
float2 uv = IN.Uv * (CFG_TEXTURE_TILING);
float fadeDiffuse = fade2.x;
float fade = fade2.y;
#ifdef CFG_OPT_DIFFUSE_CONST
float4 diffuse = 1;
#else
#ifdef CFG_WANG_TILES
float4 diffuse = sampleWang(TEXTURE(DiffuseMap), wangUv, wangUVDerivatives);
#else
float4 diffuse = sampleFar(TEXTURE(DiffuseMap), uv, fadeDiffuse, CFG_FAR_DIFFUSE_CUTOFF);
#endif
diffuse.rgba = diffuse.rgba * (CFG_DIFFUSE_SCALE);
#endif
#ifdef CFG_OPT_NORMAL_CONST
float3 normal = float3(0, 0, 1);
#else
#ifdef CFG_WANG_TILES
float3 normal = nmapUnpack(sampleWang(TEXTURE(NormalMap), wangUv, wangUVDerivatives));
#else
float3 normal = nmapUnpack(sampleFar(TEXTURE(NormalMap), uv, fade, CFG_FAR_NORMAL_CUTOFF));
#endif
#endif
#ifndef GLSLES
#ifndef CFG_WANG_TILES // normal detail unavailable when running wang tiles
float3 normalDetail = nmapUnpack(tex2D(NormalDetailMap, uv * (CFG_NORMAL_DETAIL_TILING)));
normal.xy += normalDetail.xy * (CFG_NORMAL_DETAIL_SCALE);
#endif
#endif
normal.xy *= fade;
float shadowFactor = 1 + normal.x * (CFG_NORMAL_SHADOW_SCALE);
#ifdef CFG_OPT_BLEND_COLOR
float3 albedo = lerp(float3(1, 1, 1), IN.Color.rgb, diffuse.a) * diffuse.rgb * shadowFactor;
#else
float3 albedo = IN.Color.rgb * diffuse.rgb * shadowFactor;
#endif
#ifndef GLSLES
float4 studs = tex2D(StudsMap, IN.UvStuds);
albedo *= studs.r * 2;
#endif
#ifdef CFG_WANG_TILES
float2 specular = sampleWang(TEXTURE(SpecularMap), wangUv, wangUVDerivatives).rg;
#else
float2 specular = sampleFar(TEXTURE(SpecularMap), uv, fade, CFG_FAR_SPECULAR_CUTOFF).rg;
#endif
// make sure glossiness is never 0 to avoid fp specials
float2 specbase = specular * float2(CFG_SPECULAR_SCALE, CFG_GLOSS_SCALE) + float2(0, 0.01);
float2 specfade = lerp(float2(CFG_SPECULAR_LOD, CFG_GLOSS_LOD), specbase, fade);
Surface surface = (Surface)0;
surface.albedo = albedo;
surface.normal = normal;
surface.specular = specfade.r;
surface.gloss = specfade.g;
surface.reflectance = specular.g * fade * (CFG_REFLECTION_SCALE);
return surface;
}
@@ -0,0 +1,151 @@
#include "common.h"
struct Appdata
{
ATTR_INT4 Position : POSITION;
ATTR_INT3 Normal : NORMAL;
ATTR_INT4 Uv : TEXCOORD0;
#ifdef PIN_HQ
ATTR_INT4 EdgeDistances : TEXCOORD1;
ATTR_INT3 Tangent : TEXCOORD2;
#endif
};
struct VertexOutput
{
float4 HPosition : POSITION;
float4 UvHigh_EdgeDistance1 : TEXCOORD0;
float4 UvLow_EdgeDistance2 : TEXCOORD1;
float4 LightPosition_Fog : TEXCOORD2;
#ifdef PIN_HQ
float4 View_Depth : TEXCOORD3;
float4 Normal_Blend : TEXCOORD4;
float3 Tangent : TEXCOORD5;
#else
float4 Diffuse_Blend : COLOR0;
#endif
float3 PosLightSpace : TEXCOORD7;
};
WORLD_MATRIX(WorldMatrix);
VertexOutput MegaClusterVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
// Decode vertex data
float3 Normal = (IN.Normal - 127.0) / 127.0;
float4 UV = IN.Uv / 2048.0;
// Transform position and normal to world space
// Note: world matrix does not contain rotation/scale for static geometry so we can avoid transforming normal
float3 posWorld = mul(WorldMatrix, IN.Position).xyz;
float3 normalWorld = Normal;
OUT.HPosition = mul(G(ViewProjection), float4(posWorld, 1));
float blend = OUT.HPosition.w / 200;
OUT.LightPosition_Fog = float4(lgridPrepareSample(lgridOffset(posWorld, normalWorld)), (G(FogParams).z - OUT.HPosition.w) * G(FogParams).w);
OUT.UvHigh_EdgeDistance1.xy = UV.xy;
OUT.UvLow_EdgeDistance2.xy = UV.zw;
#ifdef PIN_HQ
OUT.View_Depth = float4(posWorld, OUT.HPosition.w * G(FadeDistance_GlowFactor).y);
float4 edgeDistances = IN.EdgeDistances*G(FadeDistance_GlowFactor).z + 0.5 * OUT.View_Depth.w;
OUT.UvHigh_EdgeDistance1.zw = edgeDistances.xy;
OUT.UvLow_EdgeDistance2.zw = edgeDistances.zw;
OUT.View_Depth.xyz = G(CameraPosition).xyz - posWorld;
OUT.Normal_Blend = float4(Normal, blend);
// decode tangent
OUT.Tangent = (IN.Tangent - 127.0) / 127.0;
#else
// IF LQ shading is performed in VS
float ndotl = dot(normalWorld, -G(Lamp0Dir));
float3 diffuse = saturate(ndotl) * G(Lamp0Color) + max(-ndotl, 0) * G(Lamp1Color);
OUT.Diffuse_Blend = float4(diffuse, blend);
#endif
OUT.PosLightSpace = shadowPrepareSample(posWorld);
return OUT;
}
TEX_DECLARE2D(DiffuseHighMap, 0);
TEX_DECLARE2D(DiffuseLowMap, 1);
TEX_DECLARE2D(NormalMap, 2);
TEX_DECLARE2D(SpecularMap, 3);
LGRID_SAMPLER(LightMap, 4);
TEX_DECLARE2D(LightMapLookup, 5);
TEX_DECLARE2D(ShadowMap, 6);
void MegaClusterPS(VertexOutput IN,
#ifdef PIN_GBUFFER
out float4 oColor1: COLOR1,
#endif
out float4 oColor0: COLOR0)
{
float4 high = tex2D(DiffuseHighMap, IN.UvHigh_EdgeDistance1.xy);
float4 low = tex2D(DiffuseLowMap, IN.UvLow_EdgeDistance2.xy);
float4 light = lgridSample(TEXTURE(LightMap), TEXTURE(LightMapLookup), IN.LightPosition_Fog.xyz);
float shadow = shadowSample(TEXTURE(ShadowMap), IN.PosLightSpace, light.a);
#ifdef PIN_HQ
float3 albedo = lerp(high.rgb, low.rgb, saturate1(IN.Normal_Blend.a));
// sample normal map and specular map
float4 normalMapSample = tex2D(NormalMap, IN.UvHigh_EdgeDistance1.xy);
float4 specularMapSample = tex2D(SpecularMap, IN.UvHigh_EdgeDistance1.xy);
// compute bitangent and world space normal
float3 bitangent = cross(IN.Normal_Blend.xyz, IN.Tangent.xyz);
float3 nmap = nmapUnpack(normalMapSample);
float3 normal = normalize(nmap.x * IN.Tangent.xyz + nmap.y * bitangent + nmap.z * IN.Normal_Blend.xyz);
float ndotl = dot(normal, -G(Lamp0Dir));
float3 diffuseIntensity = saturate0(ndotl) * G(Lamp0Color) + max(-ndotl, 0) * G(Lamp1Color);
float specularIntensity = step(0, ndotl) * specularMapSample.r;
float specularPower = specularMapSample.g * 255 + 0.01;
// Compute diffuse and specular and combine them
float3 diffuse = (G(AmbientColor) + diffuseIntensity * shadow + light.rgb) * albedo.rgb;
float3 specular = G(Lamp0Color) * (specularIntensity * shadow * (float)(half)pow(saturate(dot(normal, normalize(-G(Lamp0Dir) + normalize(IN.View_Depth.xyz)))), specularPower));
oColor0.rgb = diffuse + specular;
// apply outlines
float outlineFade = saturate1(IN.View_Depth.w * G(OutlineBrightness_ShadowInfo).x + G(OutlineBrightness_ShadowInfo).y);
float2 minIntermediate = min(IN.UvHigh_EdgeDistance1.wz, IN.UvLow_EdgeDistance2.wz);
float minEdgesPlus = min(minIntermediate.x, minIntermediate.y) / IN.View_Depth.w;
oColor0.rgb *= saturate1(outlineFade * (1.5 - minEdgesPlus) + minEdgesPlus);
oColor0.a = 1;
#else
float3 albedo = lerp(high.rgb, low.rgb, saturate1(IN.Diffuse_Blend.a));
// Compute diffuse term
float3 diffuse = (G(AmbientColor) + IN.Diffuse_Blend.rgb * shadow + light.rgb) * albedo.rgb;
// Combine
oColor0.rgb = diffuse;
oColor0.a = 1;
#endif
float fogAlpha = saturate(IN.LightPosition_Fog.w);
oColor0.rgb = lerp(G(FogColor), oColor0.rgb, fogAlpha);
#ifdef PIN_GBUFFER
oColor1 = gbufferPack(IN.View_Depth.w*G(FadeDistance_GlowFactor).x, diffuse.rgb, 0, fogAlpha);
#endif
}
@@ -0,0 +1,21 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 2
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.3
#define CFG_SPECULAR_LOD 0.8
#define CFG_GLOSS_LOD 120
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0.25
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0.75
#include "material.hlsl"
@@ -0,0 +1,3 @@
#define PIN_PLASTIC
#define PIN_NEON
#include "default.hlsl"
@@ -0,0 +1,240 @@
#include "common.h"
TEX_DECLARE2D(tex, 0);
TEX_DECLARE2D(cstrip, 1);
TEX_DECLARE2D(astrip, 2);
uniform float4 throttleFactor; // .x = alpha cutoff, .y = alpha boost (clamp), .w - additive/alpha ratio for Crazy shaders
uniform float4 modulateColor;
uniform float4 zOffset;
struct VS_INPUT
{
float4 pos : POSITION;
ATTR_INT4 scaleRotLife : TEXCOORD0; // transform matrix
ATTR_INT2 disp : TEXCOORD1; // .xy = corner, either (0,0), (1,0), (0,1), or (1,1)
ATTR_INT2 cline: TEXCOORD2; // .x = color line [0...32767]
};
struct VS_OUTPUT
{
float4 pos : POSITION;
float3 uvFog : TEXCOORD0;
float2 colorLookup : TEXCOORD1;
};
float4 rotScale( float4 scaleRotLife )
{
float cr = cos( scaleRotLife.z );
float sr = sin( scaleRotLife.z );
float4 r;
r.x = cr * scaleRotLife.x;
r.y = -sr * scaleRotLife.x;
r.z = sr * scaleRotLife.y;
r.w = cr * scaleRotLife.y;
return r;
}
float4 mulq( float4 a, float4 b )
{
float3 i = cross( a.xyz, b.xyz ) + a.w * b.xyz + b.w * a.xyz;
float r = a.w * b.w - dot( a.xyz, b.xyz );
return float4( i, r );
}
float4 conj( float4 a ) { return float4( -a.xyz, a.w ); }
float4 rotate( float4 v, float4 q )
{
return mulq( mulq( q, v ), conj( q ) );
}
float4 axis_angle( float3 axis, float angle )
{
return float4( sin(angle/2) * axis, cos(angle/2) );
}
VS_OUTPUT vs( VS_INPUT input )
{
VS_OUTPUT o;
float4 pos = float4( input.pos.xyz, 1 );
float2 disp = input.disp.xy * 2 - 1; // -1..1
float4 scaleRotLifeFlt = (float4)input.scaleRotLife * float4( 1.0f/256.0f, 1.0f/256.0f, 2.0 * 3.1415926f / 32767.0f, 1.0f / 32767.0f );
scaleRotLifeFlt.xy += 127.0f;
float4 rs = rotScale( scaleRotLifeFlt );
pos += G(ViewRight) * dot( disp, rs.xy );
pos += G(ViewUp) * dot( disp, rs.zw );
float4 pos2 = pos + G(ViewDir)*zOffset.x; // Z-offset position in world space
o.pos = mul( G(ViewProjection), pos );
o.uvFog.xy = input.disp.xy;
o.uvFog.y = 1 - o.uvFog.y;
o.uvFog.z = (G(FogParams).z - o.pos.w) * G(FogParams).w;
o.colorLookup.x = 1 - max( 0, min(1, scaleRotLifeFlt.w ) );
o.colorLookup.y = (float)input.cline.x * (1.0 / 32767.0f);
pos2 = mul( G(ViewProjection), pos2 ); // Z-offset position in clip space
o.pos.z = pos2.z * o.pos.w/pos2.w; // Only need z
return o;
}
float4 psAdd( VS_OUTPUT input ) : COLOR0 // #0
{
float4 texcolor = tex2D( tex, input.uvFog.xy );
float4 vcolor = tex2D( cstrip, input.colorLookup.xy );
vcolor.a = tex2D( astrip, input.colorLookup.xy ).r;
float4 result;
result.rgb = (texcolor.rgb + vcolor.rgb) * modulateColor.rgb;
result.a = texcolor.a * vcolor.a;
result.rgb *= result.a;
result.rgb = lerp( 0.0f.xxx, result.rgb, saturate( input.uvFog.zzz ) );
return result;
}
float4 psModulate( VS_OUTPUT input ) : COLOR0 // #1
{
float4 texcolor = tex2D( tex, input.uvFog.xy );
float4 vcolor = tex2D( cstrip, input.colorLookup.xy ) * modulateColor;
vcolor.a = tex2D( astrip, input.colorLookup.xy ).r * modulateColor.a;
float4 result;
result.rgb = texcolor.rgb * vcolor.rgb;
result.a = texcolor.a * vcolor.a;
result.rgb = lerp( G(FogColor).rgb, result.rgb, saturate( input.uvFog.zzz ) );
return result;
}
// - this shader is crazy
// - used instead of additive particles to help see bright particles (e.g. fire) on top of extremely bright backgrounds
// - requires ONE | INVSRCALPHA blend mode, useless otherwise
// - does not use color strip texture
// - outputs a blend between additive blend and alpha blend in fragment alpha
// - ratio multiplier is in throttleFactor.w
float4 psCrazy( VS_OUTPUT input ) : COLOR0
{
float4 texcolor = tex2D( tex, input.uvFog.xy );
float4 vcolor = float4(1,0,0,0); //tex2D( cstrip, input.colorLookup.xy ); // not actually used
vcolor.a = tex2D( astrip, input.colorLookup.xy ).r;
float blendRatio = throttleFactor.w; // yeah yeah
float4 result;
result.rgb = (texcolor.rgb ) * modulateColor.rgb * vcolor.a * texcolor.a;
result.a = blendRatio * texcolor.a * vcolor.a;
result = lerp( 0.0f.xxxx, result, saturate( input.uvFog.zzzz ) );
return result;
}
float4 psCrazySparkles( VS_OUTPUT input ) : COLOR0
{
float4 texcolor = tex2D( tex, input.uvFog.xy );
float4 vcolor = tex2D( cstrip, input.colorLookup.xy );
vcolor.a = tex2D( astrip, input.colorLookup.xy ).r;
float blendRatio = throttleFactor.w;
float4 result;
if( texcolor.a < 0.5f )
{
result.rgb = vcolor.rgb * modulateColor.rgb * (2 * texcolor.a);
}
else
{
result.rgb = lerp( vcolor.rgb * modulateColor.rgb, texcolor.rgb, 2*texcolor.a-1 );
}
//vcolor.a *= modulateColor.a;
result.rgb *= vcolor.a;
result.a = blendRatio * texcolor.a * vcolor.a;
result = lerp( 0.0f.xxxx, result, saturate( input.uvFog.zzzz ) );
return result;
}
///////////////////////////////////////////////////////////////////////////////////
struct VS_INPUT2
{
float4 pos : POSITION;
ATTR_INT4 scaleRotLife : TEXCOORD0; // transform matrix
ATTR_INT2 disp : TEXCOORD1; // .xy = corner, either (0,0), (1,0), (0,1), or (1,1)
ATTR_INT2 cline: TEXCOORD2; // .x = color line [0...32767]
ATTR_INT4 color: TEXCOORD3; // .xyzw
};
struct VS_OUTPUT2
{
float4 pos : POSITION;
float3 uvFog : TEXCOORD0;
float4 color : TEXCOORD1;
};
VS_OUTPUT2 vsCustom( VS_INPUT2 input )
{
VS_OUTPUT2 o;
float4 pos = input.pos;
float2 disp = input.disp.xy * 2 - 1; // -1..1
float4 scaleRotLifeFlt = (float4)input.scaleRotLife * float4( 1.0/256.0f, 1.0/256.0f, 2.0 * 3.1415926f / 32767.0, 1.0 / 32767.0f );
scaleRotLifeFlt.xy += 127.0f;
float4 rs = rotScale( scaleRotLifeFlt );
pos += G(ViewRight) * dot( disp, rs.xy );
pos += G(ViewUp) * dot( disp, rs.zw );
float4 pos2 = pos + G(ViewDir)*zOffset.x; // Z-offset position in world space
o.pos = mul( G(ViewProjection), pos );
o.uvFog.xy = input.disp.xy;
o.uvFog.y = 1 - o.uvFog.y;
o.uvFog.z = (G(FogParams).z - o.pos.w) * G(FogParams).w;
o.color = input.color * (1/255.0f);
pos2 = mul( G(ViewProjection), pos2 ); // Z-offset position in clip space
o.pos.z = pos2.z * o.pos.w/pos2.w; // Only need z
return o;
}
float4 psCustom( VS_OUTPUT2 input ) : COLOR0 // #1
{
float4 texcolor = tex2D( tex, input.uvFog.xy );
float4 vcolor = input.color;
float blendRatio = throttleFactor.w; // yeah yeah
float4 result;
result.rgb = texcolor.rgb * vcolor.rgb * vcolor.a * texcolor.a;
result.a = blendRatio * texcolor.a * vcolor.a;
result = lerp( 0.0f.xxxx, result, saturate( input.uvFog.zzzz ) );
return result;
}
@@ -0,0 +1,21 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 2.5
#define CFG_GLOSS_SCALE 128
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0
#define CFG_SPECULAR_LOD 0.15
#define CFG_GLOSS_LOD 22
#define CFG_NORMAL_DETAIL_TILING 6
#define CFG_NORMAL_DETAIL_SCALE 1.5
#define CFG_FAR_TILING 0
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#include "material.hlsl"
@@ -0,0 +1,52 @@
#if defined(PIN_HQ)
#define PIN_SURFACE
#include "default.hlsl"
#define CFG_TEXTURE_TILING 1
#define CFG_BUMP_INTENSITY 0.5
#define CFG_SPECULAR 0.4
#define CFG_GLOSS 9
#define CFG_NORMAL_SHADOW_SCALE 0.1
Surface surfaceShader(SurfaceInput IN, float2 fade2)
{
float fade = fade2.y;
float4 studs = tex2D(DiffuseMap, IN.UvStuds);
float3 normal = nmapUnpack(tex2D(NormalMap, IN.UvStuds));
#ifdef GLSLES
float3 noise = float3(0, 0, 1);
#else
float3 noise = nmapUnpack(tex2D(NormalDetailMap, IN.Uv * (CFG_TEXTURE_TILING)));
#endif
float noiseScale = saturate0(IN.Color.a * 2 * (CFG_BUMP_INTENSITY) - 1 * (CFG_BUMP_INTENSITY));
#ifdef PIN_REFLECTION
noiseScale *= saturate(1 - 2 * IN.Reflectance);
#endif
normal.xy += noise.xy * noiseScale;
normal.xy *= fade;
Surface surface = (Surface)0;
surface.albedo = IN.Color.rgb * (studs.r * 2);
surface.normal = normal;
surface.specular = (CFG_SPECULAR);
surface.gloss = (CFG_GLOSS);
#ifdef PIN_REFLECTION
surface.reflectance = IN.Reflectance;
#endif
return surface;
}
#else
#define PIN_PLASTIC
#include "default.hlsl"
#endif
@@ -0,0 +1,40 @@
#include "common.h"
struct Appdata
{
float4 Position : POSITION;
float2 Uv : TEXCOORD0;
float4 Color : COLOR0;
};
struct VertexOutput
{
float4 HPosition : POSITION;
float2 Uv : TEXCOORD0;
float4 Color : COLOR0;
};
VertexOutput ProfilerVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
OUT.HPosition = mul(G(ViewProjection), IN.Position);
OUT.HPosition.y = -OUT.HPosition.y;
OUT.Uv = IN.Uv;
OUT.Color = IN.Color;
return OUT;
}
TEX_DECLARE2D(DiffuseMap, 0);
float4 ProfilerPS(VertexOutput IN): COLOR0
{
float4 c0 = tex2D(DiffuseMap, IN.Uv);
float4 c1 = tex2D(DiffuseMap, IN.Uv + float2(0, 1.f / 9.f));
return c0.a < 0.5 ? float4(0, 0, 0, c1.a) : c0 * IN.Color;
}
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 1
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.5
#define CFG_SPECULAR_LOD 0.35
#define CFG_GLOSS_LOD 103
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0.5
#define CFG_FAR_DIFFUSE_CUTOFF 0.6
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_BLEND_COLOR
#include "material.hlsl"
@@ -0,0 +1,21 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 0.4
#define CFG_GLOSS_SCALE 32
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0
#define CFG_SPECULAR_LOD 0.07
#define CFG_GLOSS_LOD 6
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#include "material.hlsl"
@@ -0,0 +1,211 @@
#include "common.h"
TEX_DECLARE2D(Texture, 0);
TEX_DECLARE2D(Mask, 1);
// .xy = gbuffer width/height, .zw = inverse gbuffer width/height
uniform float4 TextureSize;
uniform float4 Params1;
uniform float4 Params2;
#if defined(GLSL) || defined(DX11)
float4 convertPosition(float4 p, float scale)
{
return p;
}
#else
float4 convertPosition(float4 p, float scale)
{
// half-pixel offset
return p + float4(-TextureSize.z, TextureSize.w, 0, 0) * scale;
}
#endif
#ifndef GLSL
float2 convertUv(float4 p)
{
return p.xy * float2(0.5, -0.5) + 0.5;
}
#else
float2 convertUv(float4 p)
{
return p.xy * 0.5 + 0.5;
}
#endif
// simple pass through structure
struct VertexOutput
{
float4 p : POSITION;
float2 uv : TEXCOORD0;
};
// position and tex coord + 4 additional tex coords
struct VertexOutput_4uv
{
float4 p : POSITION;
float2 uv : TEXCOORD0;
float4 uv12 : TEXCOORD1;
float4 uv34 : TEXCOORD2;
};
// position and tex coord + 8 additional tex coords
struct VertexOutput_8uv
{
float4 p : POSITION;
float2 uv : TEXCOORD0;
float4 uv12 : TEXCOORD1;
float4 uv34 : TEXCOORD2;
float4 uv56 : TEXCOORD3;
float4 uv78 : TEXCOORD4;
};
VertexOutput passThrough_vs(float4 p: POSITION)
{
VertexOutput OUT;
OUT.p = convertPosition(p, 1);
OUT.uv = convertUv(p);
return OUT;
}
float4 passThrough_ps( VertexOutput IN ) : COLOR0
{
return tex2D(Texture, IN.uv);
}
VertexOutput_4uv downsample4x4_vs(float4 p: POSITION)
{
float2 uv = convertUv(p);
VertexOutput_4uv OUT;
OUT.p = convertPosition(p, 1);
OUT.uv = uv;
float2 uvOffset = TextureSize.zw * 0.25f;
OUT.uv12.xy = uv + uvOffset * float2(-1, -1);
OUT.uv12.zw = uv + uvOffset * float2(+1, -1);
OUT.uv34.xy = uv + uvOffset * float2(-1, +1);
OUT.uv34.zw = uv + uvOffset * float2(+1, +1);
return OUT;
}
float4 imageProcess_ps( VertexOutput IN ) : COLOR0
{
float3 color = tex2D(Texture, IN.uv).rgb;
float4 tintColor = float4(Params2.xyz,1);
//float4 tintColor = float4(18.0 / 255.0, 58.0 / 255.0, 80.0 / 255.0, 1);
float contrast = Params1.y;
float brightness = Params1.x;
float grayscaleLvl = Params1.z;
color = contrast*(color - 0.5) + 0.5 + brightness;
float grayscale = (color.r + color.g + color.g) / 3.0;
return lerp(float4(color.rgb,1), float4(grayscale.xxx,1), grayscaleLvl) * tintColor;
}
float4 gauss(float samples, float2 uv)
{
float2 step = Params1.xy;
float sigma = Params1.z;
float sigmaN1 = 1 / sqrt(2 * 3.1415926 * sigma * sigma);
float sigmaN2 = 1 / (2 * sigma * sigma);
// First sample is in the center and accounts for our pixel
float4 result = tex2D(Texture, uv) * sigmaN1;
float weight = sigmaN1;
// Every loop iteration computes impact of 4 pixels
// Each sample computes impact of 2 neighbor pixels, starting with the next one to the right
// Note that we sample exactly in between pixels to leverage bilinear filtering
for (int i = 0; i < samples; ++i)
{
float ix = 2 * i + 1.5;
float iw = 2 * exp(-ix * ix * sigmaN2) * sigmaN1;
result += (tex2D(Texture, uv + step * ix) + tex2D(Texture, uv - step * ix)) * iw;
weight += 2 * iw;
}
// Since the above is an approximation of the integral with step functions, normalization compensates for the error
return (result / weight);
}
float4 blur3_ps(VertexOutput IN): COLOR0
{
return gauss(3, IN.uv);
}
float4 blur5_ps(VertexOutput IN): COLOR0
{
return gauss(5, IN.uv);
}
float4 blur7_ps(VertexOutput IN): COLOR0
{
return gauss(7, IN.uv);
}
float4 glowApply_ps( VertexOutput IN ) : COLOR0
{
float4 color = tex2D(Texture, IN.uv);
return float4(color.rgb * Params1.x, color.a);
}
// this is specific glow downsample
float4 downSample4x4Glow_ps( VertexOutput_4uv IN ) : COLOR0
{
float4 avgColor = tex2D( Texture, IN.uv12.xy );
avgColor += tex2D( Texture, IN.uv12.zw );
avgColor += tex2D( Texture, IN.uv34.xy );
avgColor += tex2D( Texture, IN.uv34.zw );
avgColor *= 0.25;
return float4(avgColor.rgb, 1) * (1-avgColor.a);
}
float4 ShadowBlurPS(VertexOutput IN): COLOR0
{
#ifdef GLSLES
int N = 1;
float sigma = 0.5;
#else
int N = 3;
float sigma = 1.5;
#endif
float2 step = Params1.xy;
float sigmaN1 = 1 / sqrt(2 * 3.1415926 * sigma * sigma);
float sigmaN2 = 1 / (2 * sigma * sigma);
float depth = 1;
float color = 0;
float weight = 0;
for (int i = -N; i <= N; ++i)
{
float ix = i;
float iw = exp(-ix * ix * sigmaN2) * sigmaN1;
float4 data = tex2D(Texture, IN.uv + step * ix);
depth = min(depth, data.x);
color += data.y * iw;
weight += iw;
}
float mask = tex2D(Mask, IN.uv).r;
// Since the above is an approximation of the integral with step functions, normalization compensates for the error
return float4(depth, color * mask * (1 / weight), 0, 0);
}
@@ -0,0 +1,50 @@
#include "common.h"
struct Appdata
{
float4 Position : POSITION;
ATTR_INT4 Extra : COLOR1;
};
struct VertexOutput
{
float4 HPosition: POSITION;
float3 PosLightSpace: TEXCOORD0;
};
#ifdef PIN_SKINNED
WORLD_MATRIX_ARRAY(WorldMatrixArray, MAX_BONE_COUNT * 3);
#endif
VertexOutput ShadowVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
// Transform position to world space
#ifdef PIN_SKINNED
int boneIndex = IN.Extra.r;
float4 worldRow0 = WorldMatrixArray[boneIndex * 3 + 0];
float4 worldRow1 = WorldMatrixArray[boneIndex * 3 + 1];
float4 worldRow2 = WorldMatrixArray[boneIndex * 3 + 2];
float3 posWorld = float3(dot(worldRow0, IN.Position), dot(worldRow1, IN.Position), dot(worldRow2, IN.Position));
#else
float3 posWorld = IN.Position.xyz;
#endif
OUT.HPosition = mul(G(ViewProjection), float4(posWorld, 1));
OUT.PosLightSpace = shadowPrepareSample(posWorld);
return OUT;
}
float4 ShadowPS(VertexOutput IN): COLOR0
{
float depth = shadowDepth(IN.PosLightSpace);
return float4(depth, 1, 0, 0);
}
@@ -0,0 +1,53 @@
#include "common.h"
struct Appdata
{
float4 Position : POSITION;
float2 Uv : TEXCOORD0;
float4 Color : COLOR0;
};
struct VertexOutput
{
float4 HPosition : POSITION;
float PSize : PSIZE;
float2 Uv : TEXCOORD0;
float4 Color : COLOR0;
};
WORLD_MATRIX(WorldMatrix);
uniform float4 Color;
uniform float4 Color2;
VertexOutput SkyVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
float4 wpos = mul(WorldMatrix, IN.Position);
OUT.HPosition = mul(G(ViewProjection), wpos);
#ifndef GLSLES
// snap to far plane to prevent scene-sky intersections
// small offset is needed to prevent 0/0 division in case w=0, which causes rasterization issues
// some mobile chips (hello, Vivante!) don't like it
OUT.HPosition.z = OUT.HPosition.w - 1.f / 16;
#endif
OUT.PSize = 2.0; // star size
OUT.Uv = IN.Uv;
OUT.Color = IN.Color * lerp(Color2,Color,wpos.y/1700);
//OUT.Color = IN.Color * Color;
return OUT;
}
TEX_DECLARE2D(DiffuseMap, 0);
float4 SkyPS(VertexOutput IN): COLOR0
{
return tex2D(DiffuseMap, IN.Uv) * IN.Color;
}
@@ -0,0 +1,21 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 0.9
#define CFG_GLOSS_SCALE 128
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.5
#define CFG_SPECULAR_LOD 0.14
#define CFG_GLOSS_LOD 20
#define CFG_NORMAL_DETAIL_TILING 5
#define CFG_NORMAL_DETAIL_SCALE 1
#define CFG_FAR_TILING 0.25
#define CFG_FAR_DIFFUSE_CUTOFF 0.75
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#include "material.hlsl"
@@ -0,0 +1,174 @@
#include "common.h"
struct Appdata
{
ATTR_INT4 Position : POSITION;
ATTR_INT4 Normal : NORMAL;
ATTR_INT4 Material0 : TEXCOORD0;
ATTR_INT4 Material1 : TEXCOORD1;
};
struct VertexOutput
{
float4 HPosition : POSITION;
float3 Weights: COLOR0;
float4 Uv0: TEXCOORD0;
float4 Uv1: TEXCOORD1;
float4 Uv2: TEXCOORD2;
float4 LightPosition_Fog : TEXCOORD3;
float3 PosLightSpace : TEXCOORD4;
#ifdef PIN_HQ
float3 Normal: TEXCOORD5;
float4 View_Depth: TEXCOORD6;
float3 Tangents: COLOR1;
#else
float3 Diffuse: COLOR1;
#endif
};
WORLD_MATRIX_ARRAY(WorldMatrixArray, 72);
uniform float4 LayerScale;
float4 getUV(float3 position, ATTR_INT material, ATTR_INT projection, float seed)
{
float3 u = WorldMatrixArray[1 + int(projection)].xyz;
float3 v = WorldMatrixArray[19 + int(projection)].xyz;
float4 m = WorldMatrixArray[37 + int(material)];
float2 uv = float2(dot(position, u), dot(position, v)) * m.x + m.y * float2(seed, floor(seed * 2.6651441f));
return float4(uv, m.zw);
}
VertexOutput TerrainVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
float3 posWorld = IN.Position.xyz * WorldMatrixArray[0].w + WorldMatrixArray[0].xyz;
float3 normalWorld = IN.Normal.xyz * (1.0 / 127.0) - 1.0;
OUT.HPosition = mul(G(ViewProjection), float4(posWorld, 1));
OUT.LightPosition_Fog = float4(lgridPrepareSample(lgridOffset(posWorld, normalWorld)), (G(FogParams).z - OUT.HPosition.w) * G(FogParams).w);
OUT.PosLightSpace = shadowPrepareSample(posWorld);
OUT.Uv0 = getUV(posWorld, IN.Material0.x, IN.Material1.x, IN.Normal.w);
OUT.Uv1 = getUV(posWorld, IN.Material0.y, IN.Material1.y, IN.Material0.w);
OUT.Uv2 = getUV(posWorld, IN.Material0.z, IN.Material1.z, IN.Material1.w);
#if defined(GLSLES) && !defined(GL3) // iPad2 workaround
OUT.Weights = abs(IN.Position.www - float3(0, 1, 2)) < 0.1;
#else
OUT.Weights = IN.Position.www == float3(0, 1, 2);
#endif
#ifdef PIN_HQ
OUT.Normal = normalWorld;
OUT.View_Depth = float4(G(CameraPosition) - posWorld, OUT.HPosition.w);
OUT.Tangents = float3(IN.Material1.xyz) > 7.5; // side vs top
#else
float ndotl = dot(normalWorld, -G(Lamp0Dir));
float3 diffuse = max(ndotl, 0) * G(Lamp0Color) + max(-ndotl, 0) * G(Lamp1Color);
OUT.Diffuse = diffuse;
#endif
return OUT;
}
TEX_DECLARE2D(AlbedoMap, 0);
TEX_DECLARE2D(NormalMap, 1);
TEX_DECLARE2D(SpecularMap, 2);
TEX_DECLARECUBE(EnvMap, 3);
LGRID_SAMPLER(LightMap, 4);
TEX_DECLARE2D(LightMapLookup, 5);
TEX_DECLARE2D(ShadowMap, 6);
float4 sampleMap(TEXTURE_IN_2D(s), float4 uv)
{
#ifdef PIN_HQ
float2 uvs = uv.xy * LayerScale.xy;
return tex2Dgrad(s, frac(uv.xy) * LayerScale.xy + uv.zw, ddx(uvs), ddy(uvs));
#else
return tex2D(s, frac(uv.xy) * LayerScale.xy + uv.zw);
#endif
}
float4 sampleBlend(TEXTURE_IN_2D(s), float4 uv0, float4 uv1, float4 uv2, float3 w)
{
return
sampleMap(TEXTURE(s), uv0) * w.x +
sampleMap(TEXTURE(s), uv1) * w.y +
sampleMap(TEXTURE(s), uv2) * w.z;
}
float3 sampleNormal(TEXTURE_IN_2D(s), float4 uv0, float4 uv1, float4 uv2, float3 w, float3 normal, float3 tsel)
{
return terrainNormal(sampleMap(TEXTURE(s), uv0), sampleMap(TEXTURE(s), uv1), sampleMap(TEXTURE(s), uv2), w, normal, tsel);
}
void TerrainPS(VertexOutput IN,
#ifdef PIN_GBUFFER
out float4 oColor1: COLOR1,
#endif
out float4 oColor0: COLOR0)
{
float4 light = lgridSample(TEXTURE(LightMap), TEXTURE(LightMapLookup), IN.LightPosition_Fog.xyz);
float shadow = shadowSample(TEXTURE(ShadowMap), IN.PosLightSpace, light.a);
float3 w = IN.Weights.xyz;
float4 albedo = sampleBlend(TEXTURE(AlbedoMap), IN.Uv0, IN.Uv1, IN.Uv2, w);
#ifdef PIN_HQ
float fade = saturate0(1 - IN.View_Depth.w * G(FadeDistance_GlowFactor).y);
#ifndef PIN_GBUFFER
float3 normal = IN.Normal;
#else
float3 normal = sampleNormal(TEXTURE(NormalMap), IN.Uv0, IN.Uv1, IN.Uv2, w, IN.Normal, IN.Tangents);
#endif
float4 params = sampleBlend(TEXTURE(SpecularMap), IN.Uv0, IN.Uv1, IN.Uv2, w);
float ndotl = dot(normal, -G(Lamp0Dir));
// Compute diffuse term
float3 diffuse = (G(AmbientColor) + (saturate(ndotl) * G(Lamp0Color) + max(-ndotl, 0) * G(Lamp1Color)) * shadow + light.rgb + params.b * 2) * albedo.rgb;
// Compute specular term
float specularIntensity = step(0, ndotl) * params.r * fade;
float specularPower = params.g * 128 + 0.01;
float3 specular = G(Lamp0Color) * (specularIntensity * shadow * (float)(half)pow(saturate(dot(normal, normalize(-G(Lamp0Dir) + normalize(IN.View_Depth.xyz)))), specularPower));
#else
// Compute diffuse term
float3 diffuse = (G(AmbientColor) + IN.Diffuse * shadow + light.rgb) * albedo.rgb;
// Compute specular term
float3 specular = 0;
#endif
// Combine
oColor0.rgb = diffuse + specular;
oColor0.a = 1;
float fogAlpha = saturate(IN.LightPosition_Fog.w);
oColor0.rgb = lerp(G(FogColor), oColor0.rgb, fogAlpha);
#ifdef PIN_GBUFFER
oColor1 = gbufferPack(IN.View_Depth.w, diffuse.rgb, specular.rgb, fogAlpha);
#endif
}
@@ -0,0 +1,2 @@
#define PIN_PLASTIC
#include "default.hlsl"
@@ -0,0 +1,310 @@
#include "common.h"
// Tunables
#define CFG_TEXTURE_TILING 0.2
#define CFG_TEXTURE_DETILING 0.1
#define CFG_SPECULAR 2
#define CFG_GLOSS 900
#define CFG_NORMAL_STRENGTH 0.25
#define CFG_REFRACTION_STRENGTH 0.05
#define CFG_FRESNEL_OFFSET 0.3
#define CFG_SSR_STEPS 8
#define CFG_SSR_START_DISTANCE 1
#define CFG_SSR_STEP_CLAMP 0.2
#define CFG_SSR_DEPTH_CUTOFF 10
// Shader code
struct Appdata
{
ATTR_INT4 Position : POSITION;
ATTR_INT4 Normal : NORMAL;
ATTR_INT4 Material0 : TEXCOORD0;
ATTR_INT4 Material1 : TEXCOORD1;
};
struct VertexOutput
{
float4 HPosition : POSITION;
float4 Weights_Wave: COLOR0;
float3 Tangents: COLOR1;
float2 Uv0: TEXCOORD0;
float2 Uv1: TEXCOORD1;
float2 Uv2: TEXCOORD2;
float4 LightPosition_Fog : TEXCOORD3;
float3 Normal: TEXCOORD4;
float4 View_Depth: TEXCOORD5;
#ifdef PIN_HQ
float4 PositionScreen: TEXCOORD6;
#endif
};
WORLD_MATRIX_ARRAY(WorldMatrixArray, 72);
uniform float4 WaveParams; // .x = frequency .y = phase .z = height .w = lerp
uniform float4 WaterColor; // deep water color
uniform float4 WaterParams; // .x = refraction depth scale, .y = refraction depth offset
float3 displacePosition(float3 position, float waveFactor)
{
float x = sin((position.z - position.x) * WaveParams.x - WaveParams.y);
float z = sin((position.z + position.x) * WaveParams.x + WaveParams.y);
float p = (x + z) * WaveParams.z;
float3 result = position;
result.y += p * waveFactor;
return result;
}
float4 clipToScreen(float4 pos)
{
#ifdef GLSL
pos.xy = pos.xy * 0.5 + 0.5 * pos.w;
#else
pos.xy = pos.xy * float2(0.5, -0.5) + 0.5 * pos.w;
#endif
return pos;
}
float2 getUV(float3 position, ATTR_INT projection, float seed)
{
float3 u = WorldMatrixArray[1 + int(projection)].xyz;
float3 v = WorldMatrixArray[19 + int(projection)].xyz;
float2 uv = float2(dot(position, u), dot(position, v)) * (0.25 * CFG_TEXTURE_TILING) + CFG_TEXTURE_DETILING * float2(seed, floor(seed * 2.6651441f));
return uv;
}
VertexOutput WaterVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
float3 posWorld = IN.Position.xyz * WorldMatrixArray[0].w + WorldMatrixArray[0].xyz;
float3 normalWorld = IN.Normal.xyz * (1.0 / 127.0) - 1.0;
#if defined(GLSLES) && !defined(GL3) // iPad2 workaround
float3 weights = abs(IN.Position.www - float3(0, 1, 2)) < 0.1;
#else
float3 weights = IN.Position.www == float3(0, 1, 2);
#endif
float waveFactor = dot(weights, IN.Material0.xyz) * (1.0 / 255.0);
#ifdef PIN_HQ
float fade = saturate0(1 - dot(posWorld - G(CameraPosition), -G(ViewDir).xyz) * G(FadeDistance_GlowFactor).y);
posWorld = displacePosition(posWorld, waveFactor * fade);
#endif
OUT.HPosition = mul(G(ViewProjection), float4(posWorld, 1));
OUT.LightPosition_Fog = float4(lgridPrepareSample(lgridOffset(posWorld, normalWorld)), (G(FogParams).z - OUT.HPosition.w) * G(FogParams).w);
OUT.Uv0 = getUV(posWorld, IN.Material1.x, IN.Normal.w);
OUT.Uv1 = getUV(posWorld, IN.Material1.y, IN.Material0.w);
OUT.Uv2 = getUV(posWorld, IN.Material1.z, IN.Material1.w);
OUT.Weights_Wave.xyz = weights;
OUT.Weights_Wave.w = waveFactor;
OUT.Normal = normalWorld;
OUT.View_Depth = float4(G(CameraPosition) - posWorld, OUT.HPosition.w);
OUT.Tangents = float3(IN.Material1.xyz) > 7.5; // side vs top
#ifdef PIN_HQ
OUT.PositionScreen = clipToScreen(OUT.HPosition);
#endif
return OUT;
}
TEX_DECLARE2D(NormalMap1, 0);
TEX_DECLARE2D(NormalMap2, 1);
TEX_DECLARECUBE(EnvMap, 2);
LGRID_SAMPLER(LightMap, 3);
TEX_DECLARE2D(LightMapLookup, 4);
TEX_DECLARE2D(GBufferColor, 5);
TEX_DECLARE2D(GBufferDepth, 6);
float fresnel(float ndotv)
{
return saturate(0.78 - 2.5 * abs(ndotv)) + CFG_FRESNEL_OFFSET;
}
float4 sampleMix(float2 uv)
{
#ifdef PIN_HQ
return lerp(tex2D(NormalMap1, uv), tex2D(NormalMap2, uv), WaveParams.w);
#else
return tex2D(NormalMap1, uv);
#endif
}
float3 sampleNormal(float2 uv0, float2 uv1, float2 uv2, float3 w, float3 normal, float3 tsel)
{
return terrainNormal(sampleMix(uv0), sampleMix(uv1), sampleMix(uv2), w, normal, tsel);
}
float3 sampleNormalSimple(float2 uv0, float2 uv1, float2 uv2, float3 w)
{
float4 data = sampleMix(uv0) * w.x + sampleMix(uv1) * w.y + sampleMix(uv2) * w.z;
return nmapUnpack(data).xzy;
}
float unpackDepth(float2 uv)
{
float4 geomTex = tex2D(GBufferDepth, uv);
float d = geomTex.z * (1.0f/256.0f) + geomTex.w;
return d * GBUFFER_MAX_DEPTH;
}
float3 getRefractedColor(float4 cpos, float3 N, float3 waterColor)
{
float2 refruv0 = cpos.xy / cpos.w;
float2 refruv1 = refruv0 + N.xz * CFG_REFRACTION_STRENGTH;
float4 refr0 = tex2D(GBufferColor, refruv0);
refr0.w = unpackDepth(refruv0);
float4 refr1 = tex2D(GBufferColor, refruv1);
refr1.w = unpackDepth(refruv1);
float4 result = lerp(refr0, refr1, saturate(refr1.w - cpos.w));
// Estimate water absorption by a scaled depth difference
float depthfade = saturate((result.w - cpos.w) * WaterParams.x + WaterParams.y);
// Since GBuffer depth is clamped we tone the refraction down after half of the range for a smooth fadeout
float gbuffade = saturate(cpos.w * (2.f / GBUFFER_MAX_DEPTH) - 1);
float fade = saturate(depthfade + gbuffade);
return lerp(result.rgb, waterColor, fade);
}
float3 getReflectedColor(float4 cpos, float3 wpos, float3 R)
{
float3 result = 0;
float inside = 0;
float distance = CFG_SSR_START_DISTANCE;
float diff = 0;
float diffclamp = cpos.w * CFG_SSR_STEP_CLAMP;
float4 Pproj = cpos;
float4 Rproj = clipToScreen(mul(G(ViewProjection), float4(R, 0)));
#ifndef GLSL
[unroll]
#endif
for (int i = 0; i < CFG_SSR_STEPS; ++i)
{
distance += clamp(diff, -diffclamp, diffclamp);
float4 cposi = Pproj + Rproj * distance;
float2 uv = cposi.xy / cposi.w;
float depth = unpackDepth(uv);
diff = depth - cposi.w;
}
float4 cposi = Pproj + Rproj * distance;
float2 uv = cposi.xy / cposi.w;
// Ray hit has to be inside the screen bounds
float ufade = abs(uv.x - 0.5) < 0.5;
float vfade = abs(uv.y - 0.5) < 0.5;
// Fade reflections out with distance; use max(ray hit, original depth) to discard hits that are too far
// 4 - depth * 4 would give us fade out from 0.75 to 1; 3.9 makes sure reflections go to 0 slightly before GBUFFER_MAX_DEPTH
float wfade = saturate((4 - 0.1) - max(cpos.w, cposi.w) * (4.f / GBUFFER_MAX_DEPTH));
// Ray hit has to be reasonably close to where we started
float dfade = abs(diff) < CFG_SSR_DEPTH_CUTOFF;
// Avoid back-projection
float Vfade = Rproj.w > 0;
float fade = ufade * vfade * wfade * dfade * Vfade;
return lerp(texCUBE(EnvMap, R).rgb, tex2D(GBufferColor, uv).rgb, fade);
}
float4 WaterPS(VertexOutput IN): COLOR0
{
float4 light = lgridSample(TEXTURE(LightMap), TEXTURE(LightMapLookup), IN.LightPosition_Fog.xyz);
float shadow = light.a;
float3 w = IN.Weights_Wave.xyz;
// Use simplified normal reconstruction for LQ mobile (assumes flat water surface)
#if defined(GLSLES) && !defined(PIN_HQ)
float3 normal = sampleNormalSimple(IN.Uv0, IN.Uv1, IN.Uv2, w);
#else
float3 normal = sampleNormal(IN.Uv0, IN.Uv1, IN.Uv2, w, IN.Normal, IN.Tangents);
#endif
// Flatten the normal for Fresnel and for reflections to make them less chaotic
float3 flatNormal = lerp(IN.Normal, normal, CFG_NORMAL_STRENGTH);
float3 waterColor = WaterColor.rgb;
#ifdef PIN_HQ
float fade = saturate0(1 - IN.View_Depth.w * G(FadeDistance_GlowFactor).y);
float3 view = normalize(IN.View_Depth.xyz);
float fre = fresnel(dot(flatNormal, view)) * IN.Weights_Wave.w;
float3 position = G(CameraPosition) - IN.View_Depth.xyz;
#ifdef PIN_GBUFFER
float3 refr = getRefractedColor(IN.PositionScreen, normal, waterColor);
float3 refl = getReflectedColor(IN.PositionScreen, position, reflect(-view, flatNormal));
#else
float3 refr = waterColor;
float3 refl = texCUBE(EnvMap, reflect(-view, flatNormal)).rgb;
#endif
float specularIntensity = CFG_SPECULAR * fade;
float specularPower = CFG_GLOSS;
float3 specular = G(Lamp0Color) * (specularIntensity * shadow * (float)(half)pow(saturate(dot(normal, normalize(-G(Lamp0Dir) + view))), specularPower));
#else
float3 view = normalize(IN.View_Depth.xyz);
float fre = fresnel(dot(flatNormal, view));
float3 refr = waterColor;
float3 refl = texCUBE(EnvMap, reflect(-IN.View_Depth.xyz, flatNormal)).rgb;
float3 specular = 0;
#endif
// Combine
float4 result;
result.rgb = lerp(refr, refl, fre) * (G(AmbientColor).rgb + G(Lamp0Color).rgb * shadow + light.rgb) + specular;
result.a = 1;
float fogAlpha = saturate(IN.LightPosition_Fog.w);
result.rgb = lerp(G(FogColor), result.rgb, fogAlpha);
return result;
}
@@ -0,0 +1,349 @@
#include "screenspace.hlsl"
// tweakables
#define SSAO_NUM_PAIRS 8
#define SSAO_SPHERE_RAD 2.0f // world-space
#define SSAO_MIN_PIXEL_RANGE 10.0f
#define SSAO_MAX_PIXEL_RANGE 100.0f
#define BLUR_DEPTH_DELTA 0.4f
#define COMPOSITE_DEPTH_DELTA 0.02f
#define COMPOSITE_DEPTH_DELTA2 0.4f
TEX_DECLARE2D(depthBuffer, 0);
TEX_DECLARE2D(randMap, 1);
TEX_DECLARE2D(map, 2);
TEX_DECLARE2D(geomMap, 3);
TEX_DECLARE2D(colorMap, 4);
VertexOutput_4uv ssaoDepthDown_vs(float4 p: POSITION)
{
float2 uv = convertUv(p);
VertexOutput_4uv OUT;
OUT.p = convertPosition(p, 2);
OUT.uv = uv;
float2 uvOffset = TextureSize.zw * 0.25f;
OUT.uv12.xy = uv + uvOffset * float2(-1, -1);
OUT.uv12.zw = uv + uvOffset * float2(+1, -1);
OUT.uv34.xy = uv + uvOffset * float2(-1, +1);
OUT.uv34.zw = uv + uvOffset * float2(+1, +1);
return OUT;
}
// used for ssao blurring passes
VertexOutput_8uv ssaoBlur_vs(float4 position, float2 uvOffset)
{
float2 uv = convertUv(position);
VertexOutput_8uv OUT;
OUT.p = convertPosition(position, 2);
OUT.uv = uv;
OUT.uv12.xy = uv + 1 * uvOffset;
OUT.uv12.zw = uv + 2 * uvOffset;
OUT.uv34.xy = uv + 3 * uvOffset;
OUT.uv34.zw = uv + 4 * uvOffset;
OUT.uv56.xy = uv - 1 * uvOffset;
OUT.uv56.zw = uv - 2 * uvOffset;
OUT.uv78.xy = uv - 3 * uvOffset;
OUT.uv78.zw = uv - 4 * uvOffset;
return OUT;
}
VertexOutput_8uv ssaoBlurX_vs(float4 p: POSITION)
{
return ssaoBlur_vs(p, float2(TextureSize.z * 1, 0));
}
VertexOutput_8uv ssaoBlurY_vs(float4 p: POSITION)
{
return ssaoBlur_vs(p, float2(0, TextureSize.w * 1));
}
float unpackDepth( TEXTURE_IN_2D(s), float2 uv )
{
float4 geomTex = tex2D(s, uv);
float d = geomTex.z * (1.0f/256.0f) + geomTex.w;
return d;
}
float getDepth( TEXTURE_IN_2D(s), float2 uv )
{
return (float)tex2D(s,uv).r;
}
#define NUM_PAIRS SSAO_NUM_PAIRS
#define RANGE 60.0/1024.0
#define pi 3.14159265359
#define RAD(X) ( (X) * (pi/180) )
float2 GetRotatedSample(float i)
{
return (i+1) / (NUM_PAIRS+2) * float2(cos( RAD(45) + i / NUM_PAIRS * 2 * pi ), sin( RAD(45) + i / NUM_PAIRS * 2 * pi ) );
}
#define NUM_SAMPLES NUM_PAIRS*2+1
float4 ssao_ps(
VertexOutput IN): COLOR0
{
float2 mapSize = TextureSize.xy;
float baseDepth = getDepth( TEXTURE(depthBuffer), IN.uv );
float4 noiseTex = tex2D(randMap, IN.uv*mapSize/4) * 2 - 1;
float2x2 rotation =
{
{ noiseTex.y, noiseTex.x },
{ -noiseTex.x, noiseTex.y }
};
float2 OFFSETS1[NUM_PAIRS] =
{
GetRotatedSample(0),
GetRotatedSample(1),
GetRotatedSample(2),
GetRotatedSample(3),
GetRotatedSample(4),
GetRotatedSample(5),
#if NUM_PAIRS > 6
GetRotatedSample(6),
GetRotatedSample(7),
#if NUM_PAIRS > 8
GetRotatedSample(8),
GetRotatedSample(9),
GetRotatedSample(10),
GetRotatedSample(11),
#endif
#endif
};
float occ = 1;
float sphereRadiusZB = (float) ( 2.0f / GBUFFER_MAX_DEPTH );
#define MINPIXEL SSAO_MIN_PIXEL_RANGE
#define MAXPIXEL SSAO_MAX_PIXEL_RANGE
float radiusTex = (float)clamp( 0.5*sphereRadiusZB / baseDepth, MINPIXEL / mapSize.x, MAXPIXEL / mapSize.y);
float numSamples = 2;
for(int i = 0; i < NUM_PAIRS; i++)
{
float2 offset1 = mul(rotation, OFFSETS1[i]);
float2 offseted1 = IN.uv + offset1 * radiusTex;
float2 offseted2 = IN.uv - offset1 * radiusTex;
float2 offsetDepth;
offsetDepth.x = getDepth( TEXTURE(depthBuffer), offseted1 );
offsetDepth.y = getDepth( TEXTURE(depthBuffer), offseted2 );
float2 diff = offsetDepth - baseDepth.xx;
float normalizedOffsetLen = (float)(i+1)/(NUM_PAIRS+2);
float segmentDiff = (float) ( 1.5f*sphereRadiusZB*sqrt(1-normalizedOffsetLen*normalizedOffsetLen) );
float2 normalizedDiff = (diff / segmentDiff) + 0.5;
float minDiff = min(normalizedDiff.x, normalizedDiff.y);
// At 0, full sample
// At -1, zero sample, zero weight
float sampleadd = (float) saturate(1+minDiff);
float a = (float)(saturate(normalizedDiff.x) + saturate(normalizedDiff.y))*sampleadd;
occ += a;
numSamples += 2 * sampleadd;
}
occ = occ / numSamples;
float finalocc = (float)saturate(occ*2);
if(baseDepth - (1.0f-1/256.0f) > 0)
finalocc += 1;
return float4(finalocc, finalocc, finalocc, 1);
}
// this function estimates depth discrepancy tolerance for the blur filter
float depthTolerance( float baseDepth, float sphereRadiusZB )
{
float ramp = 80; // tweak
return ( clamp( sphereRadiusZB * (baseDepth * ramp) , 0.1f * sphereRadiusZB, 40*sphereRadiusZB ) );
}
float ssaoBlur(
float2 uv,
float4 uv12,
float4 uv34,
float4 uv56,
float4 uv78,
TEXTURE_IN_2D(map),
TEXTURE_IN_2D(depthBuffer)
)
{
float sphereRadiusZB = BLUR_DEPTH_DELTA / GBUFFER_MAX_DEPTH;
float4 i = { 1, 2, 3, 4 };
float4 iw = 4-i;
float4 denom = 1;
float4 sum = tex2D(map, uv).rrrr * denom;
float baseDepth = getDepth( TEXTURE(depthBuffer), uv );
float4 newDepth, delta, ssample, coef;
newDepth.x = getDepth( TEXTURE(depthBuffer), uv12.xy );
newDepth.y = getDepth( TEXTURE(depthBuffer), uv12.zw );
newDepth.z = getDepth( TEXTURE(depthBuffer), uv34.xy );
newDepth.w = getDepth( TEXTURE(depthBuffer), uv34.zw );
delta = (newDepth - baseDepth.xxxx);
coef = iw * ( abs(delta) < depthTolerance( baseDepth, sphereRadiusZB ).xxxx );
ssample.x = tex2D( map, uv12.xy ).r;
ssample.y = tex2D( map, uv12.zw ).r;
ssample.z = tex2D( map, uv34.xy ).r;
ssample.w = tex2D( map, uv34.zw ).r;
sum += ssample * coef;
denom += coef;
////////////////////////////////////////
newDepth.x = getDepth( TEXTURE(depthBuffer), uv56.xy );
newDepth.y = getDepth( TEXTURE(depthBuffer), uv56.zw );
newDepth.z = getDepth( TEXTURE(depthBuffer), uv78.xy );
newDepth.w = getDepth( TEXTURE(depthBuffer), uv78.zw );
delta = newDepth - baseDepth.xxxx;
coef = iw * ( abs(delta) < depthTolerance( baseDepth, sphereRadiusZB ).xxxx );
ssample.x = tex2D( map, uv56.xy ).r;
ssample.y = tex2D( map, uv56.zw ).r;
ssample.z = tex2D( map, uv78.xy ).r;
ssample.w = tex2D( map, uv78.zw ).r;
sum += ssample * coef;
denom += coef;
return dot( sum, float4(1,1,1,1) ) / dot( denom, float4(1,1,1,1) );
}
float4 ssaoBlurX_ps(VertexOutput_8uv IN): COLOR0
{
float ssaoTerm = ssaoBlur( IN.uv, IN.uv12, IN.uv34, IN.uv56, IN.uv78, TEXTURE(map), TEXTURE(depthBuffer));
return float4(ssaoTerm.xxx, 1);
}
#define SPECULAR_WEIGHT 3
float4 ssaoBlurY_ps(VertexOutput_8uv IN): COLOR0
{
float ssaoTerm = ssaoBlur( IN.uv, IN.uv12, IN.uv34, IN.uv56, IN.uv78, TEXTURE(map), TEXTURE(depthBuffer));
float4 geom = tex2D(geomMap, IN.uv);
float specular = geom.x;
float diffuse = geom.y + 0.001;
// Making specular kill SSAO faster, so it doesn't get capped by 1
return (SPECULAR_WEIGHT*specular + diffuse * ssaoTerm) / (SPECULAR_WEIGHT*specular + diffuse);
}
float4 ssaoDepthDown_ps( VertexOutput_4uv IN ) : COLOR0
{
float4 d;
d.x = unpackDepth( TEXTURE(depthBuffer), IN.uv12.xy );
d.y = unpackDepth( TEXTURE(depthBuffer), IN.uv12.zw );
d.z = unpackDepth( TEXTURE(depthBuffer), IN.uv34.xy );
d.w = unpackDepth( TEXTURE(depthBuffer), IN.uv34.zw );
float2 tmp = min( d.xy, d.zw );
return min( tmp.x, tmp.y ).x;
}
VertexOutput_4uv ssaoComposit_vs(float4 p: POSITION)
{
float2 uv = convertUv(p);
VertexOutput_4uv OUT;
OUT.p = convertPosition(p, 1);
OUT.uv = uv;
float2 uvOffset = TextureSize.zw * 2;
OUT.uv12.xy = uv + float2(uvOffset.x, 0);
OUT.uv12.zw = uv - float2(uvOffset.x, 0);
OUT.uv34.xy = uv + float2(0, uvOffset.y);
OUT.uv34.zw = uv - float2(0, uvOffset.y);
return OUT;
}
#define CMP_LESS(X,Y) ( (X) < (Y) )
float4 ssaoComposit_ps(VertexOutput_4uv IN): COLOR0
{
//return float4(1,0,0,0.5);
float depth_range = COMPOSITE_DEPTH_DELTA / GBUFFER_MAX_DEPTH;
float depth_range2 = COMPOSITE_DEPTH_DELTA2 / GBUFFER_MAX_DEPTH;
// we're here
float baseDepth = unpackDepth( TEXTURE(geomMap), IN.uv );
float ssaoTerm = 1.0f;
float depth = getDepth( TEXTURE(depthBuffer), IN.uv );
float diff = abs( depth - baseDepth );
ssaoTerm = tex2D( map, IN.uv ).x;
float chk1 = CMP_LESS( depth_range, diff ); // can we trust the base depth? 0 - yes, 1 - no
float4 ssaoTermNew = 0, chk2, depth2, diff2;
depth2.x = getDepth( TEXTURE(depthBuffer), IN.uv12.xy );
depth2.y = getDepth( TEXTURE(depthBuffer), IN.uv12.zw );
depth2.z = getDepth( TEXTURE(depthBuffer), IN.uv34.xy );
depth2.w = getDepth( TEXTURE(depthBuffer), IN.uv34.zw );
ssaoTermNew.x = tex2D( map, IN.uv12.xy ).x;
ssaoTermNew.y = tex2D( map, IN.uv12.zw ).x;
ssaoTermNew.z = tex2D( map, IN.uv34.xy ).x;
ssaoTermNew.w = tex2D( map, IN.uv34.zw ).x;
diff2 = abs( depth2 - baseDepth.xxxx );
chk2 = CMP_LESS( diff2, depth_range2.xxxx );
ssaoTermNew *= chk2;
float den = dot( chk2, 1 ); // + 1e-5f; - TODO: add this if we encounter glitches; //
ssaoTermNew.x = dot( ssaoTermNew, 1 ) / den;
// the final decision: pick the base sample or its estimate, if base depth in unauthorative
ssaoTerm = saturate(den*chk1) ? ssaoTermNew.x : ssaoTerm;
//return float4(ssaoTermNew.rgb,1);
float4 colorMapSample = tex2D(colorMap, IN.uv);
return float4(colorMapSample.rgb * ssaoTerm, colorMapSample.a);
}
@@ -0,0 +1,39 @@
#include "common.h"
struct Appdata
{
float4 Position : POSITION;
float2 Uv : TEXCOORD0;
};
struct VertexOutput
{
float4 HPosition : POSITION;
float2 Uv : TEXCOORD0;
};
VertexOutput TexCompVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
OUT.HPosition = mul(G(ViewProjection), IN.Position);
OUT.Uv = IN.Uv;
return OUT;
}
TEX_DECLARE2D(DiffuseMap, 0);
uniform float4 Color;
float4 TexCompPS(VertexOutput IN): COLOR0
{
return tex2Dbias(DiffuseMap, float4(IN.Uv, 0, -10)) * Color;
}
float4 TexCompPMAPS(VertexOutput IN): COLOR0
{
float4 tex = tex2Dbias(DiffuseMap, float4(IN.Uv, 0, -10));
return float4(tex.rgb * tex.a * Color.rgb, tex.a * Color.a);
}
@@ -0,0 +1,58 @@
#include "common.h"
struct Appdata
{
float4 Position : POSITION;
float2 Uv : TEXCOORD0;
float4 Color : COLOR0;
};
struct VertexOutput
{
float4 HPosition : POSITION;
float2 Uv : TEXCOORD0;
float4 Color : COLOR0;
#if defined(PIN_FOG)
float FogFactor : TEXCOORD1;
#endif
};
uniform float4 UIParams; // x = luminance sampling on/off, w = z offset
TEX_DECLARE2D(DiffuseMap, 0);
VertexOutput UIVS(Appdata IN)
{
VertexOutput OUT = (VertexOutput)0;
OUT.HPosition = mul(G(ViewProjection), IN.Position);
OUT.HPosition.z -= UIParams.w; // against z-fighting
OUT.Uv = IN.Uv;
OUT.Color = IN.Color;
#if defined(PIN_FOG)
OUT.FogFactor = (G(FogParams).z - OUT.HPosition.w) * G(FogParams).w;
#endif
return OUT;
}
float4 UIPS(VertexOutput IN): COLOR0
{
float4 base;
if (UIParams.x > 0.5)
base = float4(1, 1, 1,tex2D(DiffuseMap, IN.Uv).r);
else
base = tex2D(DiffuseMap, IN.Uv);
float4 result = IN.Color * base;
#if defined(PIN_FOG)
result.rgb = lerp(G(FogColor), result.rgb, saturate(IN.FogFactor));
#endif
return result;
}
@@ -0,0 +1,215 @@
//
// Water shader.
// Big, fat and ugly.
//
// All (most) things considered, I have converged to this particular way of rendering water:
//
// Vertex waves
// No transparency. Solid color for deep water.
// Fresnel law, reflects environment.
// Phong speculars.
// Ripples via animated normal map. Adjustable intensity, speed and scale. Affect reflection and speculars.
#include "common.h"
WORLD_MATRIX(WorldMatrix);
uniform float4 nmAnimLerp; // ratio between normal map frames
uniform float4 waveParams; // .x = frequency .y = phase .z = height
uniform float4 WaterColor; // deep water color
#ifdef PIN_HQ
# define WATER_LOD 1
#else
# define WATER_LOD 2
#endif
#define LODBIAS (-1)
float fadeFactor( float3 wspos )
{
return saturate( -0.4f + 1.4f*length( G(CameraPosition) - wspos.xyz ) * G(FadeDistance_GlowFactor).y );
}
float wave( float4 wspos )
{
float x = sin( ( wspos.z - wspos.x - waveParams.y ) * waveParams.x );
float z = sin( ( wspos.z + wspos.x + waveParams.y ) * waveParams.x );
float p = (x + z) * waveParams.z;
return p - p * fadeFactor( wspos.xyz );
}
// perturbs the water mesh and vertex normals
void makeWaves( inout float4 wspos, inout float3 wsnrm )
{
#if WATER_LOD == 0
float gridSize = 4.0f;
float4 wspos1 = wspos;
float4 wspos2 = wspos;
wspos1.x += gridSize;
wspos2.z += gridSize;
wspos.y += wave(wspos) ;
wspos1.y += wave(wspos1);
wspos2.y += wave(wspos2);
wsnrm = normalize( cross( wspos2.xyz - wspos.xyz, wspos1.xyz - wspos.xyz ) );
#elif WATER_LOD == 1
wspos.y += wave( wspos );
#else /* do n0thing */
#endif
}
struct V2P
{
float4 pos : POSITION;
float4 tc0Fog : TEXCOORD0;
float4 wspos : TEXCOORD1;
float3 wsnrm : TEXCOORD2;
float3 light : TEXCOORD3;
float3 fade : TEXCOORD4;
};
V2P water_vs(
ATTR_INT4 pos : POSITION,
ATTR_INT3 nrm : NORMAL
)
{
V2P o;
// Decode vertex data
float3 normal = (nrm - 127.0) / 127.0;
normal = normalize(normal);
float4 wspos = mul( WorldMatrix, pos );
float3 wsnrm = normal;
wspos.y -= 2*waveParams.z;
makeWaves( /*INOUT*/ wspos, /*INOUT*/ wsnrm );
o.wspos = wspos;
o.wsnrm = wsnrm;
if( normal.y < 0.01f ) o.wsnrm = normal;
// box mapping
//float3x2 m = { wspos.xz, wspos.xy, wspos.yz };
//float2 tcselect = mul( abs( nrm.yzx ), m );
float2 tcselect;
float3 wspostc = float3( wspos.x, -wspos.y, wspos.z );
tcselect.x = dot( abs( normal.yxz ), wspostc.xzx );
tcselect.y = dot( abs( normal.yxz ), wspostc.zyy );
o.pos = mul( G(ViewProjection), wspos );
o.tc0Fog.xy = tcselect * .05f;
o.tc0Fog.z = saturate( (G(FogParams).z - o.pos.w) * G(FogParams).w );
o.tc0Fog.w = LODBIAS;
o.light = lgridPrepareSample(lgridOffset(wspos.xyz, wsnrm.xyz));
o.fade.x = fadeFactor( wspos.xyz );
o.fade.y = (1-o.fade.x) * saturate( dot( wsnrm, -G(Lamp0Dir) ) ) * 100;
o.fade.z = 1 - 0.9*saturate1( exp( -0.005 * length( G(CameraPosition) - wspos.xyz ) ) );
return o;
}
//////////////////////////////////////////////////////////////////////////////
TEX_DECLARE2D(NormalMap1, 0);
TEX_DECLARE2D(NormalMap2, 1);
TEX_DECLARECUBE(EnvMap, 2);
LGRID_SAMPLER(LightMap, 3);
TEX_DECLARE2D(LightMapLookup, 4);
float3 pixelNormal( float4 tc0 )
{
float4 nm1 = tex2Dbias( NormalMap1, tc0 );
#if WATER_LOD <= 1
float4 nm2 = tex2Dbias( NormalMap2, tc0 );
float4 nm3 = lerp( nm1, nm2, nmAnimLerp.xxxx );
#else
float4 nm3 = nm1;
#endif
return nmapUnpack( nm3 );
}
// Fresnel approximation. N1 and N2 are refractive indices.
// for above water, use n1 = 1, n2 = 1.3, for underwater use n1 = 1.3, n2 = 1
float fresnel( float3 N, float3 V, float n1, float n2, float p, float fade )
{
#if WATER_LOD == 0
float r0 = (n1-n2)/(n1+n2);
r0 *= r0;
return r0 + (1-r0) * pow( 1 - abs( dot( normalize(N), V ) ), p );
#else
return 0.1 + saturate( - 1.9 * abs( dot( N, V ) ) + 0.8); // HAXX!
//return 1 - 2 * abs( dot( N, V ) );
#endif
}
float4 envColor( float3 N, float3 V, float fade )
{
float3 dir = reflect( V, N );
return texCUBE(EnvMap, dir) * 0.91f;
}
float4 deepWaterColor(float4 light)
{
//float4 tint = 5*float4( 0.1f, 0.1f, 0.13f, 1);
float4 tint = 0.8f*float4( 118, 143, 153, 255 ) / 255;
return (light + texCUBEbias( EnvMap, float4( 0,1,0, 10.0f) )) * tint;
}
//////////////////////////////////////////
//////////////////////////////////////////
float4 water_ps( V2P v ) : COLOR0
{
float4 WaterColorTest = 0.5 * float4( 26, 169, 185, 0 ) / 255;
float4 FogColorTest = 0.8 * float4( 35, 107, 130, 0 ) / 255;
float3 N2 = v.wsnrm;
float3 N1 = pixelNormal( v.tc0Fog ).xzy;
float3 N3 = 0.5*(N2 + N1);
N3 = lerp( N3, N2, v.fade.z );
float3 L = /*normalize*/(-G(Lamp0Dir).xyz);
float3 E = normalize( G(CameraPosition) - v.wspos.xyz );
float4 light = lgridSample(TEXTURE(LightMap), TEXTURE(LightMapLookup), v.light.xyz);
float fre = fresnel( N3, E, 1.0f, 1.3f, 5, v.fade.x );
float3 diffuse = deepWaterColor(light).rgb;
float3 env = envColor( N3, -E, v.fade.x ).rgb;
float3 R = reflect( -L, N1 );
#if WATER_LOD <= 1
float specular = pow( saturate0( dot( R, E ) ), 1600 ) * L.y * 100; // baseline
# ifndef GLSLES
specular = 0.65 * saturate1( specular * saturate0( light.a - 0.4f ) );
# endif
#else
float specular = 0;
#endif
float3 result = lerp( diffuse, env, fre ) + specular.xxx;
result = lerp( G(FogColor).rgb, result, v.tc0Fog.z );
return float4( result, 1 );
}
@@ -0,0 +1,21 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 2
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.3
#define CFG_SPECULAR_LOD 0.25
#define CFG_GLOSS_LOD 32
#define CFG_NORMAL_DETAIL_TILING 7
#define CFG_NORMAL_DETAIL_SCALE 0.6
#define CFG_FAR_TILING 0
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#include "material.hlsl"
@@ -0,0 +1,23 @@
#define CFG_TEXTURE_TILING 1
#define CFG_DIFFUSE_SCALE 1
#define CFG_SPECULAR_SCALE 2
#define CFG_GLOSS_SCALE 256
#define CFG_REFLECTION_SCALE 0
#define CFG_NORMAL_SHADOW_SCALE 0.3
#define CFG_SPECULAR_LOD 0.28
#define CFG_GLOSS_LOD 53
#define CFG_NORMAL_DETAIL_TILING 0
#define CFG_NORMAL_DETAIL_SCALE 0
#define CFG_FAR_TILING 0
#define CFG_FAR_DIFFUSE_CUTOFF 0
#define CFG_FAR_NORMAL_CUTOFF 0
#define CFG_FAR_SPECULAR_CUTOFF 0
#define CFG_OPT_BLEND_COLOR
#include "material.hlsl"