aboutsummaryrefslogtreecommitdiff
path: root/bin/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'bin/main.lua')
-rw-r--r--bin/main.lua86
1 files changed, 75 insertions, 11 deletions
diff --git a/bin/main.lua b/bin/main.lua
index 67c6122..789dcc6 100644
--- a/bin/main.lua
+++ b/bin/main.lua
@@ -43,15 +43,81 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
}
]]
+local lighningShader = [[
+extern vec2 iResolution; // 分辨率, pixels
+extern number iTime; // 秒数, seconds
+// Lightning shader
+// rand,noise,fmb functions from https://www.shadertoy.com/view/Xsl3zN
+// jerome
+
+float rand(vec2 n) {
+ return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
+}
+
+float noise(vec2 n) {
+ const vec2 d = vec2(0.0, 1.0);
+ vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
+ return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
+}
+
+float fbm(vec2 n) {
+ float total = 0.0, amplitude = 1.0;
+ for (int i = 0; i < 7; i++) {
+ total += noise(n) * amplitude;
+ n += n;
+ amplitude *= 0.5;
+ }
+ return total;
+}
+
+vec4 effect(vec4 outcolor, Image img, vec2 texture_coords, vec2 pixel_coords)
+//void mainImage( out vec4 fragColor, in vec2 fragCoord )
+{
+ vec4 col = vec4(0,0,0,1);
+ vec2 uv = pixel_coords.xy * 1.0 / iResolution.xy;
+
+
+ // draw a line, left side is fixed
+ vec2 t = uv * vec2(2.0,1.0) - iTime*3.0;
+ vec2 t2 = (vec2(1,-1) + uv) * vec2(2.0,1.0) - iTime*3.0; // a second strand
+
+ // draw the lines,
+// this make the left side fixed, can be useful
+// float ycenter = mix( 0.5, 0.25 + 0.25*fbm( t ), uv.x*4.0);
+// float ycenter2 = mix( 0.5, 0.25 + 0.25*fbm( t2 ), uv.x*4.0);
+ float ycenter = fbm(t)*0.5;
+ float ycenter2= fbm(t2)*0.5;
+
+ // falloff
+ float diff = abs(uv.y - ycenter);
+ float c1 = 1.0 - mix(0.0,1.0,diff*20.0);
+
+ float diff2 = abs(uv.y - ycenter2);
+ float c2 = 1.0 - mix(0.0,1.0,diff2*20.0);
+
+ float c = max(c1,c2);
+ col = vec4(c*0.6,0.2*c2,c,1.0); // purple color
+ //fragColor = col;
+ return col;
+}
+]]
+
local jg = jin.graphics
local effect = jg.Shader(shader)
local effect2 = jg.Shader(shader2)
+local lightning = jg.Shader(lighningShader)
local diffuse = jg.Image("treestump_diffuse.png")
local img = jg.Image("treestump.png")
+local img2 = jg.Image("lightning.png")
local ww, wh = jg.size()
local scale = 2
ww = ww / scale
wh = wh / scale
+jin.core.onLoad = function()
+ jg.use(lightning)
+ lightning:send("vec2", "iResolution", ww, wh)
+ jg.unuse()
+end
jin.core.onEvent = function(e)
if e.type == "quit" then
jin.core.quit()
@@ -59,7 +125,9 @@ jin.core.onEvent = function(e)
end
local mx, my
local c = 0
+local t = 0;
jin.core.onUpdate = function(dt)
+ t = t + dt
mx, my = jin.mouse.position()
mx = mx / 2
my = my / 2
@@ -73,20 +141,16 @@ end
img:setAnchor(16, 16)
local cvs = jg.Canvas(320, 240)
local i = 0
+
jin.core.onDraw = function()
-- if true then return end
- i = i + 0.1
+ i = i + 0.001
jg.bind(cvs)
- jg.use(effect)
- effect:send("vec2", "mouse", mx, my)
- effect:send("number", "i", i)
- effect:send("Image", "diffuse", diffuse)
- jg.draw(img, ww / 2, wh / 2, 2, 2)
- jg.unuse()
- jg.use(effect)
- effect:send("Image", "diffuse", diffuse)
- jg.draw(img, ww / 3, wh / 2, 2, 2)
- jg.unuse()
+ jg.use(lightning)
+ lightning:send("number", "iTime", t)
+ lightning:send("Image", "img", img2)
+ jg.draw(img2, 20, 20)
+ jg.unuse()
jg.unbind()
jg.draw(cvs, 0, 0, 2, 2)
end