1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
local shader = [[
extern Image diffuse;
extern vec2 mouse;
extern number i ;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
vec3 light_vec = vec3(mouse,1);
vec3 light_direction = light_vec - vec3(pixel_coords, 0);
float distance = length(light_direction);
light_direction = normalize(light_direction);
vec3 normal = Texel(texture, texture_coords).xyz;
normal.y = 1 - normal.y;
normal = normalize(mix(vec3(-1), vec3(1), normal));
//float attenuation = 1/(7e-5*pow(distance, 2));
float attenuation = 5000/pow(distance, 2);
//float attenuation = 1;
float diffuse_term = clamp(attenuation * dot(normal, light_direction), 0.0, 1.0);
vec3 dark_color = vec3(0.0, 0.0, 1);
//vec3 light_color = vec3(0.6, 0.6, 0.0);
vec3 light_color = vec3(0.8, 0.8, 0.0);
vec3 ambient = mix(dark_color, light_color, diffuse_term) * 0.20;
// the shaded cel has a light value of 0.5, the light cel has a light value of 1
float cel_diffuse_term = smoothstep(0.49, 0.52, diffuse_term)/2 + 0.5;
//float cel_diffuse_term = step(0.5, diffuse_term)/2 + 0.5;
return vec4((cel_diffuse_term * Texel(diffuse, texture_coords).rgb) + ambient, Texel(texture, texture_coords).a);
//return vec4(ambient+Texel(diffuse, texture_coords).rgb/100, Texel(texture, texture_coords).a);
}
]]
local shader2 = [[
extern Image diffuse;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
return Texel(diffuse, texture_coords);
}
]]
local jg = jin.graphics
local effect = jg.Shader(shader)
local effect2 = jg.Shader(shader2)
local diffuse = jg.Image("treestump_diffuse.png")
local img = jg.Image("treestump.png")
local ww, wh = jg.size()
local scale = 2
ww = ww / scale
wh = wh / scale
jin.core.onEvent = function(e)
if e.type == "quit" then
jin.core.quit()
end
end
local mx, my
jin.core.onUpdate = function(dt)
mx, my = jin.mouse.position()
mx = mx / 2
my = my / 2
my = wh - my
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
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.unbind()
jg.draw(cvs, 0, 0, 2, 2)
end
|