Skip to content

Commit

Permalink
Added post fx
Browse files Browse the repository at this point in the history
Removed the janky glow code from the post_rim shader and made it it's own shader. This was done to minimize weird artifacts from the previous version which utilized the main screen texture with a texture format that supports values higher than 1.0f
  • Loading branch information
Manashiku authored Mar 31, 2023
1 parent bfe9359 commit 6080d41
Show file tree
Hide file tree
Showing 10 changed files with 1,193 additions and 0 deletions.
152 changes: 152 additions & 0 deletions post_glow/glow.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#define GLOW_SAMPLE 15 // anything above 20 is going to potentially cause lag


float script : STANDARDSGLOBAL <
string ScriptOutput = "color";
string ScriptClass = "scene";
string ScriptOrder = "postprocess";
> = 0.8;

float2 screen_size : VIEWPORTPIXELSIZE;
static float2 screen_offset = ((float2)0.5f / screen_size);
float4 clear_color = {0.5f, 0.5f, 0.5f, 0.0};
float clear_depth = 1.0;

// ==========================================================
// TEXTURES
texture2D screen_texture : RENDERCOLORTARGET < float2 ViewPortRatio = {1.0,1.0};
int MipLevels = 0;
>;
texture2D depthstencil_texture : RENDERDEPTHSTENCILTARGET <
// float2 ViewPortRatio = {1.0, 1.0};
// string Format = "D3DFMT_D24S8";
>;
texture2D glow_texture : OFFSCREENRENDERTARGET
<
string Description = "glow texture ";
float2 ViewPortRatio = {1.0f, 1.0f};
float4 ClearColor = {0.0f, 0.0f, 0.0f, 1.0f};
float ClearDepth = 1.0f;
bool AntiAlias = false;
int Miplevels = 0;
string DefaultEffect =
"self=hide;"
"*=glow_off.fx;";
>;

// ==========================================================
// SAMPLERS

sampler screen_sampler = sampler_state
{
texture = <screen_texture>;
FILTER = ANISOTROPIC;
ADDRESSV = CLAMP;
ADDRESSU = CLAMP;
};

sampler glow_sampler = sampler_state
{
texture = <glow_texture>;
FILTER = ANISOTROPIC;
ADDRESSV = CLAMP;
ADDRESSU = CLAMP;
};


static float pi = 3.1415926;
static int samples = GLOW_SAMPLE;
static float sigma = (float)samples * 0.25;
static float s = 2 * sigma * sigma;

float gauss(float2 i)
{

return exp(-(i.x * i.x + i.y * i.y) / s) / (pi * s);
}

float3 gaussianBlur(sampler sp, float2 uv, float2 scale)
{
float3 pixel = (float3)0.0f;
float weightSum = 0.0f;
float weight;
float2 offset;

for(int i = -samples / 2; i < samples / 2; i++)
{
for(int j = -samples / 2; j < samples / 2; j++)
{
offset = float2(i, j);
weight = gauss(offset);
pixel += tex2Dlod(sp, float4(uv + scale * offset, 0.0f, 1.0f)).rgb * weight;
weightSum += weight;
}
}
return pixel / weightSum;
}



// ==========================================================
// STRUCTURE
struct vs_out
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};

// ==========================================================
// VERTEX AND PIXEL SHADER
vs_out vs_0(float4 pos : POSITION, float2 uv : TEXCOORD0)
{
vs_out o;
o.pos = pos;
o.uv = uv + screen_offset;
return o;
}

float4 ps_0(vs_out i) : COLOR
{
float4 color = (float4)1.0f;
float2 uv = i.uv;
color.xyz = color * tex2D(screen_sampler, uv);
float3 glow = gaussianBlur(glow_sampler, uv, (float2)1.0f / screen_size.xy);
color.xyz = color.xyz + glow;

return color;
}

technique post_test <
string Script =
"RenderColorTarget0=screen_texture;"
"RenderDepthStencilTarget=depthstencil_texture;"
"ClearSetColor=clear_color;"
"ClearSetDepth=clear_depth;"
"Clear=Color;"
"Clear=Depth;"
"ScriptExternal=Color;"


//final pass
"RenderColorTarget0=;"
"RenderDepthStencilTarget=;"
"ClearSetColor=clear_color;"
"ClearSetDepth=clear_depth;"
"Clear=Color;"
"Clear=Depth;"
"Pass=drawFinal;"
;
>
{
pass drawFinal <string Script = "RenderColorTarget0=;"
"RenderDepthStencilTarget=;""Draw=Buffer;";>
{

VertexShader = compile vs_3_0 vs_0();
ZEnable = false;
ZWriteEnable = false;
AlphaBlendEnable = true;
CullMode = None;
PixelShader = compile ps_3_0 ps_0();
}
}
119 changes: 119 additions & 0 deletions post_glow/glow.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
xof 0303txt 0032
template Vector {
<3d82ab5e-62da-11cf-ab39-0020af71e433>
FLOAT x;
FLOAT y;
FLOAT z;
}

template MeshFace {
<3d82ab5f-62da-11cf-ab39-0020af71e433>
DWORD nFaceVertexIndices;
array DWORD faceVertexIndices[nFaceVertexIndices];
}

template Mesh {
<3d82ab44-62da-11cf-ab39-0020af71e433>
DWORD nVertices;
array Vector vertices[nVertices];
DWORD nFaces;
array MeshFace faces[nFaces];
[...]
}

template MeshNormals {
<f6f23f43-7686-11cf-8f52-0040333594a3>
DWORD nNormals;
array Vector normals[nNormals];
DWORD nFaceNormals;
array MeshFace faceNormals[nFaceNormals];
}

template Coords2d {
<f6f23f44-7686-11cf-8f52-0040333594a3>
FLOAT u;
FLOAT v;
}

template MeshTextureCoords {
<f6f23f40-7686-11cf-8f52-0040333594a3>
DWORD nTextureCoords;
array Coords2d textureCoords[nTextureCoords];
}

template ColorRGBA {
<35ff44e0-6c7c-11cf-8f52-0040333594a3>
FLOAT red;
FLOAT green;
FLOAT blue;
FLOAT alpha;
}

template ColorRGB {
<d3e16e81-7835-11cf-8f52-0040333594a3>
FLOAT red;
FLOAT green;
FLOAT blue;
}

template Material {
<3d82ab4d-62da-11cf-ab39-0020af71e433>
ColorRGBA faceColor;
FLOAT power;
ColorRGB specularColor;
ColorRGB emissiveColor;
[...]
}

template MeshMaterialList {
<f6f23f42-7686-11cf-8f52-0040333594a3>
DWORD nMaterials;
DWORD nFaceIndexes;
array DWORD faceIndexes[nFaceIndexes];
[Material <3d82ab4d-62da-11cf-ab39-0020af71e433>]
}


Mesh {
4;
0.100000;0.000000;0.100000;,
-0.100000;0.000000;-0.100000;,
-0.100000;0.000000;0.100000;,
0.100000;0.000000;-0.100000;;
2;
3;0,1,2;,
3;1,0,3;;

MeshNormals {
4;
0.000000;1.000000;0.000000;,
0.000000;1.000000;0.000000;,
0.000000;1.000000;0.000000;,
0.000000;1.000000;0.000000;;
2;
3;0,1,2;,
3;1,0,3;;
}

MeshTextureCoords {
4;
0.000000;0.000000;,
0.000000;0.000000;,
0.000000;0.000000;,
0.000000;0.000000;;
}

MeshMaterialList {
1;
2;
0,
0;

Material {
0.000000;0.000000;0.000000;1.000000;;
0.000000;
0.000000;0.000000;0.000000;;
0.000000;0.000000;0.000000;;
}
}
}
111 changes: 111 additions & 0 deletions post_glow/glow_off.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#define EMISSION_TOGGLE 0 // 0: off, 1: on
#define ALPHA_TYPE 0 // 0 : NONE, 1 : ALPHA ON, 2: ALPHA CLIP
// an alpha type of 0 will disable the glow
#define ALPHA_CLIP_RATE 0.5f

float4x4 mm_wvp : WORLDVIEWPROJECTION;
float4x4 mm_view : VIEW;
#ifdef MIKUMIKUMOVING
float3 light_direction[MMM_LightCount] : LIGHTDIRECTIONS;
bool light_enable[MMM_LightCount] : LIGHTENABLES;
float3 light_ambients[MMM_LightCount] : LIGHTAMBIENTCOLORS;
#else
float3 light_direction : DIRECTION < string Object = "Light"; >;
#endif
bool use_texture;
texture2D diffuse_texture : MATERIALTEXTURE;
sampler2D diffuse_sampler = sampler_state
{
texture = < diffuse_texture >;
FILTER = ANISOTROPIC;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
};

struct vs_in
{
float4 pos : POSITION;
float3 normal : TEXCOORD4;
float2 uv_a : TEXCOORD0;
float2 uv_b : TEXCOORD1;
float4 vertex : TEXCOORD2;
// float3 tangent : TEXCOORD4;
};

struct vs_out
{
float4 pos : POSITION;
float3 normal : TEXCOORD0;
float2 uv : TEXCOORD1;
};

#ifdef MIKUMIKUMOVING
vs_out vs_model(MMM_SKINNING_INPUT IN,vs_in i)
{
MMM_SKINNING_OUTPUT mmm = MMM_SkinnedPositionNormal(IN.Pos, IN.Normal, IN.BlendWeight, IN.BlendIndices, IN.SdefC, IN.SdefR0, IN.SdefR1);
i.pos = mmm.Position;
i.normal = mmm.Normal;
#else
vs_out vs_model(vs_in i)
{
#endif
vs_out o;
// i.pos.xyz = i.pos.xyz + i.normal * 0.05f * i.vertex.a;
o.pos = mul(i.pos, mm_wvp);
o.normal = i.normal;
o.uv = i.uv_a;
return o;
}

float4 ps_model(vs_out i) : COLOR0
{
// #ifdef MIKUMIKUMOVING
// float ndotl = dot(i.normal, -light_direction[0].xyz);
// #else
// float ndotl = dot(i.normal, -light_direction.xyz);
// #endif
float3 emission = (float3)1.0f;
float alpha = 1.0f;

if(use_texture)
{
// sample alpha if it has a texture
float4 diffuse = tex2D(diffuse_sampler, i.uv);
emission.xyz = diffuse.xyz * diffuse.w;
#if ALPHA_TYPE == 1
alpha = alpha * diffuse.w;
#if ALPHA_TYPE == 2
clip(alpha - ALPHA_CLIP_RATE);
#endif
#endif
}
#if EMISSION_TOGGLE == 0
emission = (float3)0.0f;
#endif
return float4(emission, alpha);
}


technique model_ss_tech < string MMDPass = "object_ss"; >
{
pass model_front
{
cullmode = none;
VertexShader = compile vs_3_0 vs_model();
PixelShader = compile ps_3_0 ps_model();
}
};


technique model_tech < string MMDPass = "object"; >
{
pass model_front
{
cullmode = none;
VertexShader = compile vs_3_0 vs_model();
PixelShader = compile ps_3_0 ps_model();
}
}

technique mmd_shadow < string MMDPass = "shadow"; > {}
technique mmd_edge < string MMDPass = "edge"; > {}
Loading

0 comments on commit 6080d41

Please sign in to comment.