Skip to content

Commit b4dbf3e

Browse files
authored
Updated vertex color example to use new shaders. Fixed rapid clicking issue. (#123)
1 parent 5b53614 commit b4dbf3e

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
varying mediump vec2 var_texcoord0;
2-
varying mediump vec4 var_mycolor;
1+
#version 140
32

4-
uniform lowp sampler2D texture_sampler;
5-
uniform lowp vec4 tint;
3+
in mediump vec2 var_texcoord0;
4+
in mediump vec4 var_mycolor; // 4. Add var_mycolor definition
5+
6+
out vec4 out_fragColor;
7+
8+
uniform mediump sampler2D texture_sampler;
69

710
void main()
811
{
9-
// Pre-multiply alpha since all runtime textures already are
10-
lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
11-
gl_FragColor = texture2D(texture_sampler, var_texcoord0.xy) * tint_pm * var_mycolor;
12-
}
12+
// Pre-multiply color to match premultiplied textures
13+
mediump vec4 tint_pm = vec4(var_mycolor.rgb * var_mycolor.a, var_mycolor.a);
14+
out_fragColor = texture(texture_sampler, var_texcoord0.xy) * tint_pm;
15+
}

material/vertexcolor/example/vertexcolor.material

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,19 @@ name: "sprite"
22
tags: "tile"
33
vertex_program: "/example/vertexcolor.vp"
44
fragment_program: "/example/vertexcolor.fp"
5-
vertex_space: VERTEX_SPACE_WORLD
65
vertex_constants {
76
name: "view_proj"
87
type: CONSTANT_TYPE_VIEWPROJ
98
}
10-
fragment_constants {
11-
name: "tint"
12-
type: CONSTANT_TYPE_USER
13-
value {
14-
x: 1.0
15-
y: 1.0
16-
z: 1.0
17-
w: 1.0
18-
}
19-
}
209
samplers {
2110
name: "texture_sampler"
2211
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
2312
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
2413
filter_min: FILTER_MODE_MIN_DEFAULT
2514
filter_mag: FILTER_MODE_MAG_DEFAULT
26-
max_anisotropy: 1.0
2715
}
28-
max_page_count: 0
2916
attributes {
3017
name: "mycolor"
31-
semantic_type: SEMANTIC_TYPE_NONE
32-
element_count: 4
33-
normalize: false
34-
data_type: TYPE_FLOAT
35-
coordinate_space: COORDINATE_SPACE_LOCAL
3618
double_values {
3719
v: 1.0
3820
v: 1.0

material/vertexcolor/example/vertexcolor.script

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,39 @@ function init(self)
1111
local maxx = 4
1212

1313
self.urls = {}
14+
15+
-- 1. For all sprites in the example we set a slightly different `mycolor` vertex attribute:
1416
for y = 0, maxy do
1517
for x = 0, maxx do
1618
local p = vmath.vector3(startx + x*spacingx, starty + y*spacingy, 0.5)
1719
local id = factory.create("#factory", p, nil, nil, vmath.vector3(0.8, 0.8, 1))
1820
local url = msg.url(nil, id, "sprite")
1921
table.insert(self.urls, url)
20-
22+
23+
-- set vertex attribute:
2124
go.set(url, "mycolor", vmath.vector4(x/maxx, y/maxy, 0, 1))
2225
end
2326
end
2427

2528
self.updated = false
29+
self.animation_finished = true
2630
end
2731

2832
function update(self, dt)
2933
self.updated = true
3034
end
3135

3236
function on_input(self, action_id, action)
33-
if action_id == hash("touch") and action.pressed and self.updated then
37+
38+
-- 2. On click we animate the `mycolor` vertex attribute of each of the sprites to blue and back.
39+
if action_id == hash("touch") and action.pressed and self.updated and self.animation_finished then
3440
for _, url in ipairs(self.urls) do
35-
go.animate(url, "mycolor", go.PLAYBACK_ONCE_PINGPONG, vmath.vector4(0, 0, 1, 1), go.EASING_LINEAR, .2)
41+
self.animation_finished = false
42+
43+
-- animate vertex attribute:
44+
go.animate(url, "mycolor", go.PLAYBACK_ONCE_PINGPONG, vmath.vector4(0, 0, 1, 1), go.EASING_LINEAR, 1, 0, function()
45+
self.animation_finished = true
46+
end)
3647
end
3748
end
3849
end
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
uniform highp mat4 view_proj;
1+
#version 140
22

33
// positions are in world space
4-
attribute highp vec4 position;
5-
attribute mediump vec2 texcoord0;
6-
attribute mediump vec4 mycolor;
4+
in highp vec4 position;
5+
in mediump vec2 texcoord0;
6+
in mediump vec4 mycolor; // 1. Add attribute definition
77

8-
varying mediump vec2 var_texcoord0;
9-
varying mediump vec4 var_mycolor;
8+
out mediump vec2 var_texcoord0;
9+
out mediump vec4 var_mycolor; // 2. Add output variable to pass color to fp
10+
11+
uniform vs_uniforms
12+
{
13+
highp mat4 view_proj;
14+
};
1015

1116
void main()
1217
{
1318
gl_Position = view_proj * vec4(position.xyz, 1.0);
1419
var_texcoord0 = texcoord0;
15-
var_mycolor = mycolor;
16-
}
20+
var_mycolor = mycolor; // 3. Pass mycolor attribute value to fp.
21+
}
-7.27 KB
Loading

0 commit comments

Comments
 (0)