Skip to content

Commit 90c55b7

Browse files
committed
Refactored shaders to use a base preprocessor include
1 parent 8dff570 commit 90c55b7

File tree

7 files changed

+128
-240
lines changed

7 files changed

+128
-240
lines changed

project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ run/max_fps=24
2222

2323
window/size/viewport_width=960
2424
window/size/viewport_height=720
25-
window/vsync/vsync_mode=0
2625
window/stretch/mode="viewport"
26+
window/vsync/vsync_mode=0
2727

2828
[importer_defaults]
2929

shaders/n64_base.gdshaderinc

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
render_mode LIT, cull_disabled, shadows_disabled, specular_disabled, DEPTH, BLEND;
2+
3+
uniform vec4 modulate_color : source_color = vec4(1.0);
4+
5+
#ifndef NO_TEXTURE
6+
uniform sampler2D albedoTex : source_color, filter_nearest, repeat_enable;
7+
#endif
8+
9+
#if !defined(NO_TEXTURE) && !defined(METAL) // METAL doesn't use UV, so no need for panning properties
10+
uniform vec2 uv_scale = vec2(1.0, 1.0);
11+
uniform vec2 uv_offset = vec2(.0, .0);
12+
uniform vec2 uv_pan_velocity = vec2(0.0);
13+
#endif
14+
15+
#ifdef ALPHA_SCISSOR
16+
uniform bool billboard = false;
17+
uniform bool y_billboard = false;
18+
uniform float alpha_scissor : hint_range(0, 1) = 0.1;
19+
#endif
20+
21+
// https://www.emutalk.net/threads/emulating-nintendo-64-3-sample-bilinear-filtering-using-shaders.54215/
22+
vec4 n64BilinearFilter(vec4 vtx_color, vec2 texcoord) {
23+
ivec2 tex_size = textureSize(albedoTex, 0);
24+
float Texture_X = float(tex_size.x);
25+
float Texture_Y = float(tex_size.y);
26+
27+
vec2 tex_pix_a = vec2(1.0/Texture_X,0.0);
28+
vec2 tex_pix_b = vec2(0.0,1.0/Texture_Y);
29+
vec2 tex_pix_c = vec2(tex_pix_a.x,tex_pix_b.y);
30+
vec2 half_tex = vec2(tex_pix_a.x*0.5,tex_pix_b.y*0.5);
31+
vec2 UVCentered = texcoord - half_tex;
32+
33+
vec4 diffuseColor = texture(albedoTex,UVCentered);
34+
vec4 sample_a = texture(albedoTex,UVCentered+tex_pix_a);
35+
vec4 sample_b = texture(albedoTex,UVCentered+tex_pix_b);
36+
vec4 sample_c = texture(albedoTex,UVCentered+tex_pix_c);
37+
38+
float interp_x = modf(UVCentered.x * Texture_X, Texture_X);
39+
float interp_y = modf(UVCentered.y * Texture_Y, Texture_Y);
40+
41+
if (UVCentered.x < 0.0)
42+
{
43+
interp_x = 1.0-interp_x*(-1.0);
44+
}
45+
if (UVCentered.y < 0.0)
46+
{
47+
interp_y = 1.0-interp_y*(-1.0);
48+
}
49+
50+
diffuseColor = (diffuseColor + interp_x * (sample_a - diffuseColor) + interp_y * (sample_b - diffuseColor))*(1.0-step(1.0, interp_x + interp_y));
51+
diffuseColor += (sample_c + (1.0-interp_x) * (sample_b - sample_c) + (1.0-interp_y) * (sample_a - sample_c))*step(1.0, interp_x + interp_y);
52+
53+
return diffuseColor * vtx_color;
54+
}
55+
56+
void vertex()
57+
{
58+
#if !defined(NO_TEXTURE) && !defined(METAL) // METAL doesn't use UV, so no need to pan UVs
59+
UV = UV * uv_scale + uv_offset;
60+
UV += uv_pan_velocity * TIME;
61+
#endif
62+
63+
#ifdef ALPHA_SCISSOR
64+
if (y_billboard)
65+
{
66+
MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0],MODEL_MATRIX[1],vec4(normalize(cross(INV_VIEW_MATRIX[0].xyz,MODEL_MATRIX[1].xyz)), 0.0),MODEL_MATRIX[3]);
67+
MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(1.0, 0.0, 0.0, 0.0),vec4(0.0, 1.0/length(MODEL_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0),vec4(0.0, 0.0, 0.0 ,1.0));
68+
MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0],INV_VIEW_MATRIX[1],INV_VIEW_MATRIX[2],MODEL_MATRIX[3]);
69+
MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0),vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0),vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0),vec4(0.0, 0.0, 0.0, 1.0));
70+
}
71+
else if (billboard)
72+
{
73+
mat4 mat_world = mat4(normalize(INV_VIEW_MATRIX[0])*length(MODEL_MATRIX[0]),normalize(INV_VIEW_MATRIX[1])*length(MODEL_MATRIX[0]),normalize(INV_VIEW_MATRIX[2])*length(MODEL_MATRIX[2]),MODEL_MATRIX[3]);
74+
// MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0],INV_VIEW_MATRIX[1],INV_VIEW_MATRIX[2],MODEL_MATRIX[3]);
75+
mat_world *= mat4( vec4(cos(INSTANCE_CUSTOM.x),-sin(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(sin(INSTANCE_CUSTOM.x), cos(INSTANCE_CUSTOM.x), 0.0, 0.0),vec4(0.0, 0.0, 1.0, 0.0),vec4(0.0, 0.0, 0.0, 1.0));
76+
MODELVIEW_MATRIX = VIEW_MATRIX * mat_world;
77+
}
78+
#endif
79+
}
80+
81+
void fragment()
82+
{
83+
#ifdef METAL
84+
vec2 texture_uv = vec2(NORMAL.x / 2.0 + 0.5, (-NORMAL.y) / 2.0 + 0.5); // Special thanks to Adam McLaughlan
85+
#elif !defined(NO_TEXTURE)
86+
vec2 texture_uv = UV;
87+
#endif
88+
89+
vec4 color_base = COLOR * modulate_color;
90+
91+
#ifdef NO_TEXTURE
92+
ALBEDO = color_base.rgb;
93+
#else
94+
vec4 texture_color = n64BilinearFilter(COLOR, texture_uv);
95+
ALBEDO = (color_base * texture_color).rgb;
96+
#endif
97+
98+
#ifdef LIGHT_VOLUME
99+
ALPHA = 1.0 - UV.y;
100+
#elif defined(ALPHA_BLEND) || defined(ALPHA_SCISSOR)
101+
ALPHA = texture_color.a * color_base.a;
102+
#endif
103+
104+
#ifdef ALPHA_SCISSOR
105+
ALPHA_SCISSOR_THRESHOLD = alpha_scissor;
106+
#endif
107+
}

shaders/n64_lit.gdshader

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,7 @@
11
shader_type spatial;
2-
render_mode diffuse_lambert, vertex_lighting, cull_disabled, shadows_disabled, specular_disabled;
32

4-
uniform vec4 modulate_color : source_color = vec4(1.0);
5-
uniform sampler2D albedoTex : source_color, filter_nearest_mipmap, repeat_enable;
6-
uniform vec2 uv_scale = vec2(1.0, 1.0);
7-
uniform vec2 uv_offset = vec2(.0, .0);
8-
uniform vec2 uv_pan_velocity = vec2(0.0);
3+
#define LIT diffuse_lambert, vertex_lighting
4+
#define DEPTH depth_draw_opaque
5+
#define BLEND blend_mix
96

10-
// https://www.emutalk.net/threads/emulating-nintendo-64-3-sample-bilinear-filtering-using-shaders.54215/
11-
vec4 n64BilinearFilter(vec4 vtx_color, vec2 texcoord) {
12-
ivec2 tex_size = textureSize(albedoTex, 0);
13-
float Texture_X = float(tex_size.x);
14-
float Texture_Y = float(tex_size.y);
15-
16-
vec2 tex_pix_a = vec2(1.0/Texture_X,0.0);
17-
vec2 tex_pix_b = vec2(0.0,1.0/Texture_Y);
18-
vec2 tex_pix_c = vec2(tex_pix_a.x,tex_pix_b.y);
19-
vec2 half_tex = vec2(tex_pix_a.x*0.5,tex_pix_b.y*0.5);
20-
vec2 UVCentered = texcoord - half_tex;
21-
22-
vec4 diffuseColor = texture(albedoTex,UVCentered);
23-
vec4 sample_a = texture(albedoTex,UVCentered+tex_pix_a);
24-
vec4 sample_b = texture(albedoTex,UVCentered+tex_pix_b);
25-
vec4 sample_c = texture(albedoTex,UVCentered+tex_pix_c);
26-
27-
float interp_x = modf(UVCentered.x * Texture_X, Texture_X);
28-
float interp_y = modf(UVCentered.y * Texture_Y, Texture_Y);
29-
30-
if (UVCentered.x < 0.0)
31-
{
32-
interp_x = 1.0-interp_x*(-1.0);
33-
}
34-
if (UVCentered.y < 0.0)
35-
{
36-
interp_y = 1.0-interp_y*(-1.0);
37-
}
38-
39-
diffuseColor = (diffuseColor + interp_x * (sample_a - diffuseColor) + interp_y * (sample_b - diffuseColor))*(1.0-step(1.0, interp_x + interp_y));
40-
diffuseColor += (sample_c + (1.0-interp_x) * (sample_b - sample_c) + (1.0-interp_y) * (sample_a - sample_c))*step(1.0, interp_x + interp_y);
41-
42-
return diffuseColor * vtx_color;
43-
}
44-
45-
void vertex()
46-
{
47-
UV *= uv_scale + uv_offset;
48-
UV += uv_pan_velocity * TIME;
49-
}
50-
51-
void fragment()
52-
{
53-
ALBEDO = n64BilinearFilter(COLOR, UV).rgb * modulate_color.rgb;
54-
}
7+
#include "n64_base.gdshaderinc"

shaders/n64_lit_metal.gdshader

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,8 @@
11
shader_type spatial;
2-
render_mode diffuse_lambert, vertex_lighting, cull_disabled, shadows_disabled, specular_disabled;
32

4-
uniform sampler2D albedoTex : source_color, filter_nearest_mipmap, repeat_enable;
5-
uniform vec4 modulate_color : source_color = vec4(1.0);
6-
uniform vec4 metal_modulate_color : source_color = vec4(1.0);
3+
#define LIT diffuse_lambert, vertex_lighting
4+
#define DEPTH depth_draw_opaque
5+
#define BLEND blend_mix
6+
#define METAL
77

8-
// https://www.emutalk.net/threads/emulating-nintendo-64-3-sample-bilinear-filtering-using-shaders.54215/
9-
vec4 n64BilinearFilter(vec4 vtx_color, vec2 texcoord) {
10-
ivec2 tex_size = textureSize(albedoTex, 0);
11-
float Texture_X = float(tex_size.x);
12-
float Texture_Y = float(tex_size.y);
13-
14-
vec2 tex_pix_a = vec2(1.0/Texture_X,0.0);
15-
vec2 tex_pix_b = vec2(0.0,1.0/Texture_Y);
16-
vec2 tex_pix_c = vec2(tex_pix_a.x,tex_pix_b.y);
17-
vec2 half_tex = vec2(tex_pix_a.x*0.5,tex_pix_b.y*0.5);
18-
vec2 UVCentered = texcoord - half_tex;
19-
20-
vec4 diffuseColor = texture(albedoTex,UVCentered);
21-
vec4 sample_a = texture(albedoTex,UVCentered+tex_pix_a);
22-
vec4 sample_b = texture(albedoTex,UVCentered+tex_pix_b);
23-
vec4 sample_c = texture(albedoTex,UVCentered+tex_pix_c);
24-
25-
float interp_x = modf(UVCentered.x * Texture_X, Texture_X);
26-
float interp_y = modf(UVCentered.y * Texture_Y, Texture_Y);
27-
28-
if (UVCentered.x < 0.0)
29-
{
30-
interp_x = 1.0-interp_x*(-1.0);
31-
}
32-
if (UVCentered.y < 0.0)
33-
{
34-
interp_y = 1.0-interp_y*(-1.0);
35-
}
36-
37-
diffuseColor = (diffuseColor + interp_x * (sample_a - diffuseColor) + interp_y * (sample_b - diffuseColor))*(1.0-step(1.0, interp_x + interp_y));
38-
diffuseColor += (sample_c + (1.0-interp_x) * (sample_b - sample_c) + (1.0-interp_y) * (sample_a - sample_c))*step(1.0, interp_x + interp_y);
39-
40-
return diffuseColor * vtx_color;
41-
}
42-
43-
void vertex()
44-
{
45-
// Special thanks Adam McLaughlan
46-
NORMAL = (MODELVIEW_MATRIX * vec4(NORMAL, 0.0)).xyz;
47-
NORMAL = normalize(NORMAL);
48-
}
49-
50-
void fragment()
51-
{
52-
// Special thanks Adam McLaughlan
53-
vec2 norm = vec2(NORMAL.x / 2.0 + 0.5, (-NORMAL.y) / 2.0 + 0.5);
54-
ALBEDO = n64BilinearFilter(COLOR, norm).rgb;
55-
}
8+
#include "n64_base.gdshaderinc"

shaders/n64_lit_transparent.gdshader

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,8 @@
11
shader_type spatial;
2-
render_mode diffuse_lambert, vertex_lighting, cull_disabled, shadows_disabled, specular_disabled;
32

4-
uniform vec4 modulate_color : source_color = vec4(1.0);
5-
uniform sampler2D albedoTex : source_color, filter_nearest_mipmap, repeat_enable;
6-
uniform vec2 uv_scale = vec2(1.0, 1.0);
7-
uniform vec2 uv_offset = vec2(.0, .0);
8-
uniform vec2 uv_pan_velocity = vec2(0.0);
3+
#define LIT diffuse_lambert, vertex_lighting
4+
#define DEPTH depth_draw_opaque
5+
#define BLEND blend_mix
6+
#define ALPHA_BLEND
97

10-
// https://www.emutalk.net/threads/emulating-nintendo-64-3-sample-bilinear-filtering-using-shaders.54215/
11-
vec4 n64BilinearFilter(vec4 vtx_color, vec2 texcoord) {
12-
ivec2 tex_size = textureSize(albedoTex, 0);
13-
float Texture_X = float(tex_size.x);
14-
float Texture_Y = float(tex_size.y);
15-
16-
vec2 tex_pix_a = vec2(1.0/Texture_X,0.0);
17-
vec2 tex_pix_b = vec2(0.0,1.0/Texture_Y);
18-
vec2 tex_pix_c = vec2(tex_pix_a.x,tex_pix_b.y);
19-
vec2 half_tex = vec2(tex_pix_a.x*0.5,tex_pix_b.y*0.5);
20-
vec2 UVCentered = texcoord - half_tex;
21-
22-
vec4 diffuseColor = texture(albedoTex,UVCentered);
23-
vec4 sample_a = texture(albedoTex,UVCentered+tex_pix_a);
24-
vec4 sample_b = texture(albedoTex,UVCentered+tex_pix_b);
25-
vec4 sample_c = texture(albedoTex,UVCentered+tex_pix_c);
26-
27-
float interp_x = modf(UVCentered.x * Texture_X, Texture_X);
28-
float interp_y = modf(UVCentered.y * Texture_Y, Texture_Y);
29-
30-
if (UVCentered.x < 0.0)
31-
{
32-
interp_x = 1.0-interp_x*(-1.0);
33-
}
34-
if (UVCentered.y < 0.0)
35-
{
36-
interp_y = 1.0-interp_y*(-1.0);
37-
}
38-
39-
diffuseColor = (diffuseColor + interp_x * (sample_a - diffuseColor) + interp_y * (sample_b - diffuseColor))*(1.0-step(1.0, interp_x + interp_y));
40-
diffuseColor += (sample_c + (1.0-interp_x) * (sample_b - sample_c) + (1.0-interp_y) * (sample_a - sample_c))*step(1.0, interp_x + interp_y);
41-
42-
return diffuseColor * vtx_color;
43-
}
44-
45-
void vertex()
46-
{
47-
UV *= uv_scale + uv_offset;
48-
UV += uv_pan_velocity * TIME;
49-
}
50-
51-
void fragment()
52-
{
53-
vec4 filtered_surface = n64BilinearFilter(COLOR, UV) * modulate_color;
54-
ALBEDO = filtered_surface.rgb;
55-
ALPHA = filtered_surface.a;
56-
}
8+
#include "n64_base.gdshaderinc"

shaders/n64_unlit_particle.gdshader

Lines changed: 5 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,8 @@
11
shader_type spatial;
2-
render_mode depth_draw_opaque, unshaded;
32

4-
uniform vec4 modulate_color : source_color = vec4(1.);
5-
uniform sampler2D albedoTex : source_color, filter_nearest_mipmap, repeat_enable;
6-
uniform vec2 uv_scale = vec2(1.0, 1.0);
7-
uniform vec2 uv_offset = vec2(.0, .0);
8-
uniform bool billboard = false;
9-
uniform bool y_billboard = false;
10-
uniform float alpha_scissor : hint_range(0, 1) = 0.1;
3+
#define LIT unshaded
4+
#define DEPTH depth_draw_always
5+
#define BLEND blend_mix
6+
#define ALPHA_SCISSOR
117

12-
// https://www.emutalk.net/threads/emulating-nintendo-64-3-sample-bilinear-filtering-using-shaders.54215/
13-
vec4 n64BilinearFilter(vec4 vtx_color, vec2 texcoord) {
14-
ivec2 tex_size = textureSize(albedoTex, 0);
15-
float Texture_X = float(tex_size.x);
16-
float Texture_Y = float(tex_size.y);
17-
18-
vec2 tex_pix_a = vec2(1.0/Texture_X,0.0);
19-
vec2 tex_pix_b = vec2(0.0,1.0/Texture_Y);
20-
vec2 tex_pix_c = vec2(tex_pix_a.x,tex_pix_b.y);
21-
vec2 half_tex = vec2(tex_pix_a.x*0.5,tex_pix_b.y*0.5);
22-
vec2 UVCentered = texcoord - half_tex;
23-
24-
vec4 diffuseColor = texture(albedoTex,UVCentered);
25-
vec4 sample_a = texture(albedoTex,UVCentered+tex_pix_a);
26-
vec4 sample_b = texture(albedoTex,UVCentered+tex_pix_b);
27-
vec4 sample_c = texture(albedoTex,UVCentered+tex_pix_c);
28-
29-
float interp_x = modf(UVCentered.x * Texture_X, Texture_X);
30-
float interp_y = modf(UVCentered.y * Texture_Y, Texture_Y);
31-
32-
if (UVCentered.x < 0.0)
33-
{
34-
interp_x = 1.0-interp_x*(-1.0);
35-
}
36-
if (UVCentered.y < 0.0)
37-
{
38-
interp_y = 1.0-interp_y*(-1.0);
39-
}
40-
41-
diffuseColor = (diffuseColor + interp_x * (sample_a - diffuseColor) + interp_y * (sample_b - diffuseColor))*(1.0-step(1.0, interp_x + interp_y));
42-
diffuseColor += (sample_c + (1.0-interp_x) * (sample_b - sample_c) + (1.0-interp_y) * (sample_a - sample_c))*step(1.0, interp_x + interp_y);
43-
44-
return diffuseColor * vtx_color;
45-
}
46-
47-
48-
void vertex()
49-
{
50-
UV = UV * uv_scale + uv_offset;
51-
52-
if (y_billboard)
53-
{
54-
MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0],MODEL_MATRIX[1],vec4(normalize(cross(INV_VIEW_MATRIX[0].xyz,MODEL_MATRIX[1].xyz)), 0.0),MODEL_MATRIX[3]);
55-
MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(1.0, 0.0, 0.0, 0.0),vec4(0.0, 1.0/length(MODEL_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0),vec4(0.0, 0.0, 0.0 ,1.0));
56-
}
57-
else if (billboard)
58-
{
59-
mat4 mat_world = mat4(normalize(INV_VIEW_MATRIX[0])*length(MODEL_MATRIX[0]),normalize(INV_VIEW_MATRIX[1])*length(MODEL_MATRIX[0]),normalize(INV_VIEW_MATRIX[2])*length(MODEL_MATRIX[2]),MODEL_MATRIX[3]);
60-
// MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0],INV_VIEW_MATRIX[1],INV_VIEW_MATRIX[2],MODEL_MATRIX[3]);
61-
mat_world *= mat4( vec4(cos(INSTANCE_CUSTOM.x),-sin(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(sin(INSTANCE_CUSTOM.x), cos(INSTANCE_CUSTOM.x), 0.0, 0.0),vec4(0.0, 0.0, 1.0, 0.0),vec4(0.0, 0.0, 0.0, 1.0));
62-
MODELVIEW_MATRIX = VIEW_MATRIX * mat_world;
63-
}
64-
65-
// POSITION = get_snapped_pos(PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(VERTEX, 1.0)); // snap position to grid
66-
// POSITION /= abs(POSITION.w); // discard depth for affine mapping
67-
68-
if (y_billboard)
69-
{
70-
MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0],INV_VIEW_MATRIX[1],INV_VIEW_MATRIX[2],MODEL_MATRIX[3]);
71-
MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0),vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0),vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0),vec4(0.0, 0.0, 0.0, 1.0));
72-
}
73-
74-
VERTEX = VERTEX; // it breaks without this
75-
}
76-
77-
void fragment()
78-
{
79-
// vec4 tex = texture(albedoTex, UV) * modulate_color * COLOR;
80-
vec4 tex = n64BilinearFilter(COLOR, UV) * modulate_color;
81-
ALPHA = tex.a;
82-
ALBEDO = tex.rgb;
83-
// ALPHA_SCISSOR_THRESHOLD = alpha_scissor;
84-
}
8+
#include "n64_base.gdshaderinc"

world/box/object_metal_mat.tres

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
[resource]
77
render_priority = 0
88
shader = ExtResource("1")
9-
shader_parameter/modulate_color = Color(0.94902, 0.6, 0, 1)
10-
shader_parameter/metal_modulate_color = Color(1, 1, 1, 1)
9+
shader_parameter/modulate_color = Color(1, 1, 1, 1)
1110
shader_parameter/albedoTex = ExtResource("2")

0 commit comments

Comments
 (0)