shadery a scripty

This commit is contained in:
det-fys 2023-12-25 18:47:55 +01:00
parent bb6115cfdf
commit 5f331dd548
20 changed files with 801 additions and 0 deletions

27
main/scripts/autoexec.lua Normal file
View File

@ -0,0 +1,27 @@
g_require("scripts/thread.lua")
g_require("scripts/input.lua")
g_require("scripts/player.lua")
print("autoexec")
inputmap["KEY_W"] = "forward"
inputmap["KEY_S"] = "backward"
inputmap["KEY_A"] = "left"
inputmap["KEY_D"] = "right"
local player = ent_spawn(-1, 1, 0, 0, 0, 0, 1, 1, 1)
mdl_set(player, "models/test_skeletal.mdl.json")
local playercamera = false
setupplayer(player)
onkeypress["KEY_T"] = function ()
if playercamera then
g_detachcamera()
else
g_attachcamera(player, 0, 1, 0)
end
playercamera = not playercamera
end

View File

@ -0,0 +1,140 @@
---@meta
--- Game
---@param interval number
---@param repeating boolean
---@param callback fun()
---@return number
function g_settimer(interval, repeating, callback) end
---@param timerid number
function g_cleartimer(timerid) end
---@return number
function g_getframe() end
---@return number
function g_gettps() end
---@param path string
---@return nil
function g_require(path) end
---@return number x
---@return number y
---@return number z
function g_getcameraforward() end
---@return number x
---@return number z
function g_getcameraforwardxz() end
---@return number x
---@return number z
function g_getcamerarightxz() end
---@param ent number
---@param x number
---@param y number
---@param z number
---@return nil
function g_attachcamera(ent, x, y, z) end
---@return nil
function g_detachcamera() end
--- Input
---@param callback fun(control: string, press: boolean)
---@return nil
function i_setcontrolcallback(callback) end
--- Entity
---@param x number
---@param y number
---@param z number
---@param pitch number
---@param yaw number
---@param roll number
---@param sx number
---@param sy number
---@param sz number
---@return number
function ent_spawn(x, y, z, pitch, yaw, roll, sx, sy, sz) end
--- Model
---@param ent number
---@param name string
---@return nil
function mdl_set(ent, name) end
---@param ent number
---@param name string
---@param t number
---@param fade number
---@return nil
function mdl_anim(ent, name, t, fade) end
---@param ent number
---@param name string
---@param fade number
---@return nil
function mdl_stopanim(ent, name, fade) end
---@param ent number
---@return nil
function mdl_remove(ent) end
-- CCT
---@param ent number
---@param height number
---@param radius number
---@return nil
function cct_add(ent, height, radius) end
---@param ent number
---@return nil
function cct_remove(ent) end
---@param ent number
---@param x number
---@param y number
---@param z number
---@return nil
function cct_setpos(ent, x, y, z) end
---@param ent number
---@return number x
---@return number y
---@return number z
function cct_getpos(ent) end
---@param ent number
---@return number x
---@return number z
function cct_getforwardxz(ent) end
---@param ent number
---@param x number
---@param y number
---@param z number
---@param speed number
---@return nil
function cct_turnto(ent, x, y, z, speed) end
---@param ent number
---@param angle number
---@param speed number
---@return nil
function cct_turntoangle(ent, angle, speed) end
---@param ent number
---@param x number
---@param y number
---@param z number
---@return nil
function cct_move(ent, x, y, z) end

29
main/scripts/input.lua Normal file
View File

@ -0,0 +1,29 @@
-- input utils
keyheld = {}
onkey = {}
onkeypress = {}
inputmap = {}
local function processcontrol(control, press)
if press then
keyheld[control] = true
else
keyheld[control] = nil
end
if onkey[control] then
onkey[control](press)
end
if press and onkeypress[control] then
onkeypress[control]()
end
end
i_setcontrolcallback(function (control, press)
processcontrol(control, press)
if (inputmap[control]) then
processcontrol(inputmap[control], press)
end
end)

76
main/scripts/player.lua Normal file
View File

@ -0,0 +1,76 @@
g_require("scripts/input.lua")
local tick = 1 / g_gettps()
local player = nil
local playertimer = nil
local standanim = "stand"
local walkanim = "walk2"
function setupplayer(ent)
player = ent
mdl_anim(player, "init", 0, 0)
mdl_anim(player, standanim, 0, 0)
cct_add(player, 0.25, 1.2)
local walking = false
playertimer = g_settimer(0, true, function ()
local fb = 0
local rl = 0
if keyheld["forward"] then fb = fb + 1 end
if keyheld["backward"] then fb = fb - 1 end
if keyheld["right"] then rl = rl + 1 end
if keyheld["left"] then rl = rl - 1 end
local move_x = 0
local move_y = -3.0
local move_z = 0
local movespeed = 3.0
if fb ~= 0 or rl ~= 0 then
local foward_x, forward_z = g_getcameraforwardxz()
local right_x, right_z = g_getcamerarightxz()
local x = foward_x * fb + right_x * rl
local z = forward_z * fb + right_z * rl
local angle = math.atan(x, z)
move_x, move_z = cct_getforwardxz(player)
cct_turntoangle(player, angle, 6)
if not walking then
mdl_anim(player, walkanim, 6, 0.2)
walking = true
end
-- print(angle * 57.2957795)
else
if walking then
mdl_stopanim(player, walkanim, 0.1)
walking = false
end
end
cct_move(player, move_x * tick * movespeed, move_y * tick, move_z * tick * movespeed)
end)
end
function cleanupplayer()
if playertimer ~= nil then
g_cleartimer(playertimer)
playertimer = nil
end
if player ~= nil then
cct_remove(player)
player = nil
end
end

BIN
main/scripts/player.luac Normal file

Binary file not shown.

34
main/scripts/thread.lua Normal file
View File

@ -0,0 +1,34 @@
-- thread utils
function wait(t)
coroutine.yield(t)
end
function thread(cb)
local co = coroutine.create(cb)
local function resumefun()
if coroutine.status(co) == "dead" then
return
end
local suc, time = coroutine.resume(co)
if not suc then
error(debug.traceback(co))
end
if suc and time then
g_settimer(time, false, resumefun)
end
end
resumefun()
return co
end
function killthread(co)
coroutine.close(co)
end

98
main/shaders/basic.fs Normal file
View File

@ -0,0 +1,98 @@
#version 330 core
in vec2 v_texcoord;
// in vec3 v_fragpos;
in vec3 v_normal;
uniform sampler2D u_tex;
// uniform mat4 u_model;
// uniform mat4 u_view;
// uniform mat4 u_proj;
// uniform int u_no_light;
// uniform float u_alpha;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec4 object_color_4 = texture(u_tex, vec2(v_texcoord.x, 1.0 - v_texcoord.y));
if (object_color_4.a < 0.5) discard;
vec3 object_color = object_color_4.rgb;
o_color = vec4(object_color, 1.0);
o_normal = vec4(gl_FrontFacing ? v_normal : (v_normal * -1.0), 1.0);
// o_normal = vec4(v_normal, 1.0);
// o_normal = vec4(v_normal * -1.0, 1.0);
}
// //-----------------------------------
// #version 330 core
// //#define ALPHA_DITHER
// in vec2 Texcoord;
// in vec3 FragPosF;
// in vec3 Normal;
// // in float Dist;
// uniform sampler2D tex;
// uniform vec3 lightPos;
// uniform mat4 model;
// uniform mat4 view;
// uniform mat4 proj;
// uniform int NoLight;
// uniform float alpha;
// out vec4 FragColor;
// // out float FragPos;
// out vec4 FragNormal;
// #ifdef ALPHA_DITHER
// float bayer[64] = float[64] (
// 0.000000, 0.500000, 0.125000, 0.625000, 0.031250, 0.531250, 0.156250, 0.656250,
// 0.750000, 0.250000, 0.875000, 0.375000, 0.781250, 0.281250, 0.906250, 0.406250,
// 0.187500, 0.687500, 0.062500, 0.562500, 0.218750, 0.718750, 0.093750, 0.593750,
// 0.937500, 0.437500, 0.812500, 0.312500, 0.968750, 0.468750, 0.843750, 0.343750,
// 0.046875, 0.546875, 0.171875, 0.671875, 0.015625, 0.515625, 0.140625, 0.640625,
// 0.796875, 0.296875, 0.921875, 0.421875, 0.765625, 0.265625, 0.890625, 0.390625,
// 0.234375, 0.734375, 0.109375, 0.609375, 0.203125, 0.703125, 0.078125, 0.578125,
// 0.984375, 0.484375, 0.859375, 0.359375, 0.953125, 0.453125, 0.828125, 0.328125
// );
// int intmod(int a, int b) {
// return a - ((a / b) * b);
// }
// float map(float value, float min1, float max1, float min2, float max2) {
// return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
// }
// #endif
// void main() {
// vec4 objectColor4 = texture(tex, vec2(Texcoord.x, 1.0 - Texcoord.y));
// vec3 objectColor = objectColor4.rgb;
// #ifdef ALPHA_DITHER
// ivec2 iPoz = ivec2(gl_FragCoord.xy);
// int bx = intmod(iPoz.x, 8);
// int by = intmod(iPoz.y, 8);
// // if (objectColor4.a < 0.25 || alpha < bayer[bx * 8 + by]) discard;
// if (map(objectColor4.a, 0.0, 1.0, -0.05, 1.2) * alpha < bayer[bx * 8 + by]) discard;
// #else
// if (objectColor4.a < 0.25) discard;
// #endif
// FragColor = vec4(objectColor, 1.0);
// // FragPos = gl_FragCoord.z; //vec4(FragPosF, /*Dist*/ gl_FragCoord.z);
// FragNormal = vec4(Normal, NoLight == 1 ? 0.0 : 1.0);
// }

21
main/shaders/basic.vs Normal file
View File

@ -0,0 +1,21 @@
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_texcoord;
layout (location = 2) in vec3 a_normal;
uniform mat4 u_vp;
uniform mat4 u_model;
out vec3 v_fragpos;
out vec2 v_texcoord;
out vec3 v_normal;
void main() {
v_texcoord = a_texcoord;
v_normal = normalize(mat3(transpose(inverse(u_model))) * a_normal);
vec4 fragpos4 = u_model * vec4(a_position, 1.0);
v_fragpos = vec3(fragpos4);
gl_Position = u_vp * fragpos4;
}

View File

@ -0,0 +1,34 @@
#version 430 core
struct DrawCtx {
mat4 model;
vec4 color[4];
};
layout(std430, binding = 0) buffer InstanceBuffer {
DrawCtx instances[];
};
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_texcoord;
layout (location = 2) in vec3 a_normal;
uniform mat4 u_vp;
// uniform mat4 u_model;
out vec3 v_fragpos;
out vec2 v_texcoord;
out vec3 v_normal;
void main() {
// uint index = gl_GlobalInvocationID.x;
mat4 u_model = instances[gl_InstanceID].model;
v_texcoord = a_texcoord;
v_normal = normalize(mat3(transpose(inverse(u_model))) * a_normal);
vec4 fragpos4 = u_model * vec4(a_position, 1.0);
v_fragpos = vec3(fragpos4);
gl_Position = u_vp * fragpos4;
}

14
main/shaders/debug.fs Normal file
View File

@ -0,0 +1,14 @@
#version 330 core
in vec3 v_color;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
o_color = vec4(v_color, 0.0);
o_normal = vec4(0.0);
// o_normal = vec4(v_normal, 1.0);
// o_normal = vec4(v_normal * -1.0, 1.0);
}

13
main/shaders/debug.vs Normal file
View File

@ -0,0 +1,13 @@
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec3 a_color;
uniform mat4 u_vp;
out vec3 v_color;
void main() {
v_color = a_color;
gl_Position = u_vp * vec4(a_position, 1.0);
}

View File

@ -0,0 +1,135 @@
#version 330 core
uniform sampler2D u_tex;
uniform sampler2D u_tex_pos;
uniform sampler2D u_tex_normal;
uniform sampler2D u_tex_light;
uniform vec3 u_sun_color;
uniform vec3 u_sun_direction;
uniform vec3 u_ambient_color;
// uniform float u_farplane;
// uniform vec3 u_farplane_color;
// uniform float u_farplane_strength;
layout (location = 0) out vec4 o_color;
vec3 GetAmbientLight() {
return u_ambient_color;
}
vec3 GetSunLight(vec3 normal) {
float sun_intensity = max(dot(normal, u_sun_direction), 0.0);
return sun_intensity * u_sun_color;
}
void main() {
ivec2 int_pos = ivec2(gl_FragCoord.xy);
vec4 local_rgba = texelFetch(u_tex, int_pos, 0);
vec4 local_normal_4 = texelFetch(u_tex_normal, int_pos, 0);
vec4 local_light_4 = texelFetch(u_tex_light, int_pos, 0);
float local_dist = texelFetch(u_tex_pos, int_pos, 0).r;
vec3 local_rgb = local_rgba.rgb;
vec3 local_normal = local_normal_4.xyz;
vec3 local_light = local_light_4.rgb;
vec3 rr = GetAmbientLight() + GetSunLight(local_normal) + local_light;
// vec3 rr = max(GetAmbientLight(), max(GetSunLight(local_normal), local_light));
vec3 result = local_rgb * mix(vec3(1.0), rr, local_rgba.a);
o_color = vec4(result, 1.0);
// o_color = vec4(1.0);
// o_color = vec4(local_rgb, 1.0);
}
/*
// in vec2 Poz;
layout (pixel_center_integer) in vec4 gl_FragCoord;
uniform sampler2D tex;
uniform sampler2D tex_pos;
uniform sampler2D tex_normal;
uniform sampler2D tex_light;
uniform sampler2D tex_transparent;
uniform float fadeAlpha;
uniform vec3 sunColor;
uniform vec3 sunDirection;
uniform vec3 ambientColor;
uniform float ambientStrength;
uniform float farplane;
uniform vec3 farplaneColor;
uniform float farplaneStrength;
uniform float time;
out vec4 FragColor;
float rand(vec2 co) {
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
}
vec3 noise(vec2 co, float t, float intensity) {
vec2 co_t = co * mod(t, 1.0);
return ((vec3(rand(co_t), rand(co_t * vec2(-1.0, 1.0)), rand(co_t * vec2(1.0, -1.0))) - 0.5) * intensity * 2.0);
}
float linearize_depth(float d, float zNear, float zFar) {
float z_n = 2.0 * d - 1.0;
return 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
}
void main() {
ivec2 iPoz = ivec2(gl_FragCoord.xy);
vec4 this_rgba = texelFetch(tex, iPoz, 0);
//vec4 this_posd = texelFetch(tex_pos, iPoz, 0);
vec4 this_normala = texelFetch(tex_normal, iPoz, 0);
vec4 this_lighta = texelFetch(tex_light, iPoz, 0);
// vec3 this_light = this_lighta.rgb / min(1.0, max(this_lighta.r, max(this_lighta.g, this_lighta.b)));
vec4 this_transparent = texelFetch(tex_transparent, iPoz, 0);
vec3 this_rgb = this_rgba.rgb;
//vec3 this_pos = this_posd.xyz;
vec3 this_normal = this_normala.xyz;
float Dist = texelFetch(tex_pos, iPoz, 0).r; //this_posd.w;
// vec3 this_pos = WorldPosFromDepth(Dist);
float LightAffected = this_normala.w;
// --- old fs
vec3 ambientResult = ambientStrength * ambientColor;
vec3 sunDir = normalize(sunDirection);
float sunIntensity = max(dot(this_normal, sunDir), 0.0);
vec3 sunResult = sunIntensity * sunColor;
// vec3 result = max(max(ambientResult, sunResult), this_lighta.rgb) * this_rgb;
vec3 result = (ambientResult + sunResult + this_lighta.rgb) * this_rgb;
vec3 litColor = mix(this_rgb, result, LightAffected);
//vec3 fragColor3 = litColor; //mix(litColor, farplaneColor, linearize_depth(Dist, 0.1, farplane) / farplane);
float fogIntensity = farplaneStrength * linearize_depth(Dist, 0.1, farplane) / farplane;
vec3 finalColor = mix(litColor, farplaneColor, fogIntensity);
vec3 fragColor3 = mix(finalColor, this_transparent.rgb, this_transparent.a);
FragColor = vec4(fragColor3 /*+ noise(gl_FragCoord.xy, time, 0.03)* /, 1.0);
}
*/

View File

@ -0,0 +1,7 @@
#version 330 core
layout (location = 0) in vec2 a_vert_pos;
void main() {
gl_Position = vec4(a_vert_pos, 0.0, 1.0);
}

19
main/shaders/skeletal.fs Normal file
View File

@ -0,0 +1,19 @@
#version 330 core
in vec2 v_texcoord;
in vec3 v_normal;
uniform sampler2D u_tex;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec4 object_color_4 = texture(u_tex, vec2(v_texcoord.x, 1.0 - v_texcoord.y));
if (object_color_4.a < 0.5) discard;
vec3 object_color = object_color_4.rgb;
o_color = vec4(object_color, 1.0);
o_normal = vec4(v_normal, 1.0);
}

40
main/shaders/skeletal.vs Normal file
View File

@ -0,0 +1,40 @@
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_texcoord;
layout (location = 2) in vec3 a_normal;
layout (location = 3) in ivec4 a_bone_ids;
layout (location = 4) in vec4 a_bone_weights;
const int MAX_BONES = 240;
const int MAX_BONE_INFLUENCE = 4;
uniform mat4 u_bone_transforms[MAX_BONES];
uniform mat4 u_vp;
uniform mat4 u_model;
// out vec3 v_fragpos;
out vec2 v_texcoord;
out vec3 v_normal;
void main() {
mat4 bone_transform = mat4(0.0);
for (int i = 0; i < MAX_BONE_INFLUENCE; i++) {
if (a_bone_ids[i] == -1)
continue;
bone_transform += u_bone_transforms[a_bone_ids[i]] * a_bone_weights[i];
}
vec4 pos_l = bone_transform * vec4(a_position, 1.0);
vec4 fragpos4 = u_model * pos_l;
gl_Position = u_vp * fragpos4;
// v_fragpos = vec3(fragpos4);
v_texcoord = a_texcoord;
vec4 normal_l = bone_transform * vec4(a_normal, 0.0);
v_normal = normalize((u_model * normal_l).xyz);
}

13
main/shaders/skybox.fs Normal file
View File

@ -0,0 +1,13 @@
#version 330 core
in vec2 v_texcoord;
uniform sampler2D u_tex;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
o_color = vec4(texture(u_tex, vec2(v_texcoord.x, 1.0 - v_texcoord.y)).rgb, 0.0);
o_normal = vec4(0.0, 0.0, 0.0, 1.0);
}

14
main/shaders/skybox.vs Normal file
View File

@ -0,0 +1,14 @@
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_texcoord;
layout (location = 2) in vec3 a_normal;
uniform mat4 u_vp;
out vec2 v_texcoord;
void main() {
v_texcoord = a_texcoord;
gl_Position = u_vp * vec4(a_position, 1.0);
}

View File

@ -0,0 +1,25 @@
#version 330 core
in vec2 v_texcoord;
// in vec3 v_fragpos;
in vec3 v_normal;
uniform sampler2D u_tex;
uniform sampler2D u_tex1;
uniform float u_uv_mult;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec2 txc = vec2(v_texcoord.x, 1.0 - v_texcoord.y);
vec4 object_color_4 = texture(u_tex, txc) * texture(u_tex1, txc * u_uv_mult);
if (object_color_4.a < 0.5) discard;
vec3 object_color = object_color_4.rgb;
o_color = vec4(object_color, 1.0);
o_normal = vec4(v_normal, 1.0);
}

27
main/shaders/xz.fs Normal file
View File

@ -0,0 +1,27 @@
#version 330 core
in vec2 v_texcoord;
in vec3 v_fragpos;
in vec3 v_normal;
uniform sampler2D u_tex;
uniform vec2 u_xz_mult;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec2 tc = v_fragpos.xz * u_xz_mult.x;
vec4 color_xz = texture(u_tex, tc);
// vec4 object_color_4 = mix(color_xz1, color_xz2, color_mask.a);
// if (object_color_4.a < 0.5) discard;
vec3 object_color = color_xz.rgb;
o_color = vec4(object_color, 1.0);
o_normal = vec4(v_normal, 1.0);
}

View File

@ -0,0 +1,35 @@
#version 330 core
in vec2 v_texcoord;
in vec3 v_fragpos;
in vec3 v_normal;
uniform sampler2D u_tex;
uniform sampler2D u_tex1;
uniform sampler2D u_tex2;
uniform vec2 u_xz_mult;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec2 txc = vec2(v_texcoord.x, 1.0 - v_texcoord.y);
vec2 tc1 = v_fragpos.xz * u_xz_mult.x;
vec2 tc2 = v_fragpos.xz * u_xz_mult.y;
vec4 color_mask = texture(u_tex, txc);
vec4 color_xz1 = texture(u_tex1, tc1);
vec4 color_xz2 = texture(u_tex2, tc2);
// vec4 object_color_4 = mix(color_xz1, color_xz2, color_mask.a);
// if (object_color_4.a < 0.5) discard;
vec3 object_color = mix(color_xz1.rgb, color_xz2.rgb * color_mask.rgb, color_mask.a);
o_color = vec4(object_color, 1.0);
o_normal = vec4(v_normal, 1.0);
}