From decb72f936060a65bff18e9cbf33642ea3a71cd0 Mon Sep 17 00:00:00 2001 From: Alexander M Pickering Date: Thu, 9 Jan 2025 18:11:46 -0600 Subject: Work --- src/shaders/stars.lua | 76 ++++++++++++++++++++++++++++++ src/shaders/world.frag | 19 ++++++++ src/shaders/world.moon | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ src/shaders/world.vert | 22 +++++++++ 4 files changed, 242 insertions(+) create mode 100644 src/shaders/stars.lua create mode 100644 src/shaders/world.frag create mode 100644 src/shaders/world.moon create mode 100644 src/shaders/world.vert (limited to 'src/shaders') diff --git a/src/shaders/stars.lua b/src/shaders/stars.lua new file mode 100644 index 0000000..111bef8 --- /dev/null +++ b/src/shaders/stars.lua @@ -0,0 +1,76 @@ +local win = require("window") +local color = require("color") +local world = require("world") +local numstars = 400 -- we might have as many as 4 over +local genned_stars = 0 +local period_x = 3 +local period_y = 3 +local stars = {} +while genned_stars < numstars do + local rngx = math.random() + local xpos = rngx * win.width --* (period_x - 1) + local rngy = math.random() + local ypos = rngy * win.height --* (period_y - 1) + local blinks = math.random() > 0.3 and (math.random() * 2 * math.pi) or 0 + stars[#stars+1] = vec3(xpos, ypos, blinks) + genned_stars = genned_stars + 1 + if xpos < win.width then + -- duplicate on the last screen + stars[#stars+1] = vec3(xpos + (win.width * (period_x-2)), ypos, blinks) + genned_stars = genned_stars + 1 + end + if ypos < win.height then + stars[#stars+1] = vec3(xpos, ypos + (win.height * (period_y-2)), blinks) + genned_stars = genned_stars + 1 + end + if xpos < win.width and ypos < win.height then + stars[#stars+1] = vec3(xpos + (win.width * (period_x-2)), ypos+(win.height * (period_y-2)),blinks) + genned_stars = genned_stars + 1 + end +end +local node = am.use_program(am.program([[ + precision highp float; + attribute vec3 stars; + uniform float time; + uniform float world_x; + uniform float world_y; + uniform float world_x_period; + uniform float world_y_period; + uniform mat4 MV; + uniform mat4 P; + void main() { + float world_x_off = mod(world_x, world_x_period); + float world_y_off = mod(world_y, world_y_period); + gl_Position = P * MV * vec4(stars.x - world_x_off, stars.y - world_y_off, 0.0, 1.0); + float intensity = sin(stars.z + time) * cos(time) + 1.; + gl_PointSize = pow(intensity, 2.) * stars.z * 0.3; + } + ]],[[ + precision mediump float; + uniform vec4 color; + void main() { + gl_FragColor = color; + } +]])) +^ am.bind({ + MV = mat4( + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + (-win.width / 2), (-win.height/2), 0, 1 + ), + color = color.am_color.highlight, + stars = am.vec3_array(stars), + world_x = am.current_time(), + world_x_period = (period_x - 2) * win.width, + world_y = am.current_time(), + world_y_period = (period_y - 2) * win.height, + time = am.current_time(), +}) +^ am.draw("points") +node:action(function(self) + self("bind").time = am.current_time() + self("bind").world_x = world.world_x + self("bind").world_y = world.world_y +end) +return node diff --git a/src/shaders/world.frag b/src/shaders/world.frag new file mode 100644 index 0000000..ded8abb --- /dev/null +++ b/src/shaders/world.frag @@ -0,0 +1,19 @@ +precision mediump float; +varying vec2 textureuv; // uv +uniform sampler2D textures; +uniform sampler2D emissives; +uniform sampler2D normals; +varying mat3 light1; // position, color, intensity-fadetime-? +uniform float time; +varying vec4 v_color; +void main() { + + vec2 uv = textureuv; + //vec2 uv = gl_FragCoord.xy; + //vec3 view_origin = vec3(0., 0., -3.); + //vec3 view_direction = vec3(uv, 3); + //vec3 screen_intersection = vec3(uv.x, uv.y, 0.); + gl_FragColor = texture2D(textures,uv);// + vec4(uv.xy / 4.,0.,1.); + //gl_FragColor = texture2D(textures,screen_intersection.xy); + +} diff --git a/src/shaders/world.moon b/src/shaders/world.moon new file mode 100644 index 0000000..ad40cb9 --- /dev/null +++ b/src/shaders/world.moon @@ -0,0 +1,125 @@ +win = require("window") +color = require("color") +world = require("world") +sprites = require("world.sprites") +shader_shim = require("shader_shim") +hc = require("party.hardoncollider.init") +-- Process the world into buffers to send to the shader + +error("Who is including world?") +print("sprites:",sprites,getmetatable(sprites)) +print("sprites.floor1_diffuse",sprites["floor1_diffuse"]) +view_angle = math.pi / 4 +near_plane = 1 +far_plane = 2 +aspect = win.width / win.height +s_mv = mat4( + 1, 0, 0, 0, + 0, aspect, 0, 0, + 0, 0, 1, 0, + -0.5, 0.5, 0, 4 + ) +p_mv = mat4( + 1 / ((win.width / win.height) * math.tan(view_angle / 2)), 0, 0, 0, + 0, 1/math.tan(view_angle/2), 0, 0, + 0, 0, far_plane / (far_plane - near_plane), 1, + 0, 0,(-far_plane * near_plane)/(far_plane - near_plane), 0 +) + +-- Each point needs: +-- vec3 position (x,y,z) +-- vec2 (u,v) +up_down_hall = (x,y) -> + r = { + --floor + vec3(x,y,0), + vec3(x+1,y,0), + vec3(x+1,y-1,0), + vec3(x+1,y-1,0), + vec3(x,y-1,0), + vec3(x,y,0), + + -- Left wall + vec3(x,y,1), + vec3(x,y,0), + vec3(x,y-1,0), + vec3(x,y-1,0), + vec3(x,y-1,1), + vec3(x,y,1), + + --Right wall + vec3(x+1,y,0), + vec3(x+1,y,1), + vec3(x+1,y-1,1), + vec3(x+1,y-1,1), + vec3(x+1,y-1,0), + vec3(x+1,y,0), + } + r + +sd = sprites.floor1_diffuse +w1 = sprites.wall1_diffuse + +-- uvs are s,t,smult, tmult +up_down_hall_uv = (x,y) -> + r = { + --floor + vec4(sd.s1,sd.t1,1,1), + vec4(sd.s2,sd.t1,1,1), + vec4(sd.s2,sd.t2,1,1), + vec4(sd.s2,sd.t2,1,1), + vec4(sd.s1,sd.t2,1,1), + vec4(sd.s1,sd.t1,1,1), + -- left wall + vec4(w1.s1,w1.t1,1,1), + vec4(w1.s1,w1.t2,1,1), + vec4(w1.s2,w1.t2,1,1), + vec4(w1.s2,w1.t2,1,1), + vec4(w1.s2,w1.t1,1,1), + vec4(w1.s1,w1.t1,1,1), + -- right wall + vec4(w1.s2,w1.t2,1,1), + vec4(w1.s2,w1.t1,1,1), + vec4(w1.s1,w1.t1,1,1), + vec4(w1.s1,w1.t1,1,1), + vec4(w1.s1,w1.t2,1,1), + vec4(w1.s2,w1.t2,1,1), + + } + r + +add_verts = (tbl, new) -> + for i = 1, #new + tbl[#tbl+1] = new[i] + +world_geom = {} +world_uv = {} +add_verts(world_geom, up_down_hall(0,0)) +add_verts(world_uv, up_down_hall_uv(0,0)) +add_verts(world_geom, up_down_hall(0,1)) +add_verts(world_uv, up_down_hall_uv(0,0)) +add_verts(world_geom, up_down_hall(0,-1)) +add_verts(world_uv, up_down_hall_uv(0,0)) +--sprites["diffuse"].texture.wrap = "repeat" +--sprites["normals"].texture.wrap = "repeat" +node = shader_shim.world\append(am.cull_face("front")\append(am.bind({ + MV: s_mv + P: mat4(1) + color: color.am_color.highlight, + world_x: 0, + world_y: 0, + world: am.vec3_array(world_geom) + time: am.current_time(), + textures: sprites.floor1_diffuse.texture + texuv: am.vec4_array(world_uv) +})\append(am.draw("triangles")))) +node\action(() => + bind = self("bind") + bind.time = am.current_time! + bind.world_x = math.sin(am.current_time!) * 2 + bind.world_y = math.cos(am.current_time!) * 2 +) +{ + node: node + bind: node("bind") +} diff --git a/src/shaders/world.vert b/src/shaders/world.vert new file mode 100644 index 0000000..0e6747a --- /dev/null +++ b/src/shaders/world.vert @@ -0,0 +1,22 @@ +precision highp float; +attribute vec3 world; // position +attribute vec2 texuv; +varying vec2 textureuv; +varying mat3 light1; +uniform vec4 color; +varying vec4 v_color; +uniform float world_x; +uniform float world_y; +uniform mat4 MV; +uniform mat4 P; +void main() { + v_color = vec4(world.xyz,1.); + vec2 vxy = vec2(world.x - world_x, world.y - world_y); + float z_scale = 0.5; + float xoff = world.z * vxy.x * z_scale; + float yoff = world.z * vxy.y * z_scale; + textureuv=texuv; + // if z > 0 then + // xoff = ceil(xoff, 0) + gl_Position = P * MV * vec4(vxy.x + xoff, vxy.y + yoff, 0., 1.0); +} -- cgit v1.2.3-70-g09d2