Skip to content

Commit e129065

Browse files
committed
Added sprite samplers example
1 parent 788f188 commit e129065

15 files changed

+250
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ node_modules/
1818
.DS_Store
1919
/.editor_settings
2020
factory/bullets/.editor_settings
21+
sprite/samplers/.editor_settings

sprite/samplers/all.texture_profiles

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
path_settings {
2+
path: "**"
3+
profile: "Default"
4+
}
5+
profiles {
6+
name: "Default"
7+
platforms {
8+
os: OS_ID_GENERIC
9+
formats {
10+
format: TEXTURE_FORMAT_RGBA
11+
compression_level: BEST
12+
compression_type: COMPRESSION_TYPE_DEFAULT
13+
}
14+
mipmaps: false
15+
max_texture_size: 0
16+
premultiply_alpha: true
17+
}
18+
}

sprite/samplers/example.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
tags: sprite
3+
title: Multiple Sprite Samplers
4+
brief: This example shows how to sample from more than one image when drawing a sprite
5+
scripts: multi_sprite.script, multi_sprite.fp
6+
---
7+
8+
The example uses a sprite with a material with two samplers:
9+
10+
![](example/multi_sprite_material.png)
11+
12+
The samplers are assigned to two atlases, `one.atlas` and `two.atlas`:
13+
14+
![](example/multi_sample_collection.png)
15+
16+
Each atlas contains a Defold logo:
17+
18+
![](example/one_atlas.png)
19+
20+
![](example/two_atlas.png)
21+
22+
Note the rename pattern in `two.atlas`. The rename pattern is required so that it is possible to sample from the same location in both atlases.
23+
24+
The color data from the two samplers is mixed/interpolated in the fragment program to produce a final color. The amount of interpolation is controlled in the `mix_amount` fragment constant. The `mix_amount` is animated between 0.0 and 1.0 in the `multi_sprite.script`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: "multi_sample"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "logo"
5+
data: "components {\n"
6+
" id: \"multi_sample\"\n"
7+
" component: \"/example/multi_sample.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"sprite\"\n"
11+
" type: \"sprite\"\n"
12+
" data: \"default_animation: \\\"logo_256\\\"\\n"
13+
"material: \\\"/example/multi_sprite.material\\\"\\n"
14+
"textures {\\n"
15+
" sampler: \\\"texture1_sampler\\\"\\n"
16+
" texture: \\\"/example/one.atlas\\\"\\n"
17+
"}\\n"
18+
"textures {\\n"
19+
" sampler: \\\"texture2_sampler\\\"\\n"
20+
" texture: \\\"/example/two.atlas\\\"\\n"
21+
"}\\n"
22+
"\"\n"
23+
"}\n"
24+
""
25+
position {
26+
x: 360.0
27+
y: 360.0
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function init(self)
2+
go.animate("logo#sprite", "mix_amount.x", go.PLAYBACK_LOOP_PINGPONG, 1.0, go.EASING_INOUTQUAD, 2)
3+
end
Loading
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
varying mediump vec2 var_texcoord0;
2+
3+
uniform lowp sampler2D texture1_sampler;
4+
uniform lowp sampler2D texture2_sampler;
5+
uniform lowp vec4 tint;
6+
uniform lowp vec4 mix_amount;
7+
8+
void main()
9+
{
10+
// Pre-multiply alpha since all runtime textures already are
11+
lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
12+
// sample from both textures
13+
lowp vec4 color1 = texture2D(texture1_sampler, var_texcoord0.xy);
14+
lowp vec4 color2 = texture2D(texture2_sampler, var_texcoord0.xy);
15+
// mix (interpolate) the colors by the mix_amount
16+
lowp vec4 colormix = mix(color1, color2, mix_amount.x);
17+
// apply tint
18+
gl_FragColor = colormix * tint_pm;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "sprite"
2+
tags: "tile"
3+
vertex_program: "/builtins/materials/sprite.vp"
4+
fragment_program: "/example/multi_sprite.fp"
5+
vertex_constants {
6+
name: "view_proj"
7+
type: CONSTANT_TYPE_VIEWPROJ
8+
}
9+
fragment_constants {
10+
name: "tint"
11+
type: CONSTANT_TYPE_USER
12+
value {
13+
x: 1.0
14+
y: 1.0
15+
z: 1.0
16+
w: 1.0
17+
}
18+
}
19+
fragment_constants {
20+
name: "mix_amount"
21+
type: CONSTANT_TYPE_USER
22+
value {
23+
}
24+
}
25+
samplers {
26+
name: "texture1_sampler"
27+
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
28+
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
29+
filter_min: FILTER_MODE_MIN_DEFAULT
30+
filter_mag: FILTER_MODE_MAG_DEFAULT
31+
}
32+
samplers {
33+
name: "texture2_sampler"
34+
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
35+
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
36+
filter_min: FILTER_MODE_MIN_DEFAULT
37+
filter_mag: FILTER_MODE_MAG_DEFAULT
38+
}
Loading

sprite/samplers/example/one.atlas

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
images {
2+
image: "/builtins/assets/images/logo/logo_256.png"
3+
}
4+
extrude_borders: 2

sprite/samplers/example/one_atlas.png

215 KB
Loading

sprite/samplers/example/two.atlas

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
images {
2+
image: "/builtins/assets/images/logo/logo_blue_256.png"
3+
}
4+
extrude_borders: 2
5+
rename_patterns: "_blue="

sprite/samplers/example/two_atlas.png

186 KB
Loading

sprite/samplers/game.project

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[project]
2+
title = Defold-examples
3+
version = 0.1
4+
5+
[bootstrap]
6+
main_collection = /example/multi_sample.collectionc
7+
8+
[input]
9+
game_binding = /input/game.input_bindingc
10+
repeat_interval = 0.05
11+
12+
[display]
13+
width = 720
14+
height = 720
15+
high_dpi = 1
16+
17+
[physics]
18+
scale = 0.02
19+
gravity_y = -500.0
20+
21+
[script]
22+
shared_state = 1
23+
24+
[collection_proxy]
25+
max_count = 256
26+
27+
[label]
28+
subpixels = 1
29+
30+
[sprite]
31+
subpixels = 1
32+
max_count = 32765
33+
34+
[windows]
35+
iap_provider =
36+
37+
[android]
38+
package = com.defold.examples
39+
40+
[ios]
41+
bundle_identifier = com.defold.examples
42+
43+
[osx]
44+
bundle_identifier = com.defold.examples
45+
46+
[html5]
47+
show_fullscreen_button = 0
48+
show_made_with_defold = 0
49+
scale_mode = no_scale
50+
heap_size = 64
51+
52+
[graphics]
53+
texture_profiles = /all.texture_profiles
54+
55+
[collection]
56+
max_instances = 32765
57+
58+
[particle_fx]
59+
max_emitter_count = 1024
60+
61+
[render]
62+
clear_color_blue = 1.0
63+
clear_color_green = 1.0
64+
clear_color_red = 1.0
65+
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
key_trigger {
2+
input: KEY_UP
3+
action: "up"
4+
}
5+
key_trigger {
6+
input: KEY_DOWN
7+
action: "down"
8+
}
9+
key_trigger {
10+
input: KEY_LEFT
11+
action: "left"
12+
}
13+
key_trigger {
14+
input: KEY_RIGHT
15+
action: "right"
16+
}
17+
key_trigger {
18+
input: KEY_BACKSPACE
19+
action: "backspace"
20+
}
21+
key_trigger {
22+
input: KEY_SPACE
23+
action: "action"
24+
}
25+
mouse_trigger {
26+
input: MOUSE_BUTTON_LEFT
27+
action: "touch"
28+
}
29+
mouse_trigger {
30+
input: MOUSE_WHEEL_UP
31+
action: "wheel_up"
32+
}
33+
mouse_trigger {
34+
input: MOUSE_WHEEL_DOWN
35+
action: "wheel_down"
36+
}
37+
touch_trigger {
38+
input: TOUCH_MULTI
39+
action: "multitouch"
40+
}
41+
text_trigger {
42+
input: TEXT
43+
action: "type"
44+
}

0 commit comments

Comments
 (0)