Skip to content

Commit ae87837

Browse files
committed
Added Sprite Local UV example
1 parent a7897a3 commit ae87837

14 files changed

+261
-0
lines changed
Lines changed: 18 additions & 0 deletions
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+
}
Loading
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
images {
2+
image: "/assets/images/elementStone019.png"
3+
}
4+
images {
5+
image: "/assets/images/elementStone023.png"
6+
}
7+
extrude_borders: 2

material/sprite_local_uv/example.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
tags: sprite
3+
title: Sprite local UV
4+
brief: This example shows how to get local UV coordinates of a sprite regardless of sprite size
5+
author: Defold Foundation
6+
scripts: sprite_local_uv.script, sprite_local_uv.vp, sprite_local_uv.fp
7+
---
8+
9+
The example uses two game objects, each with a sprite component and a script.
10+
11+
![example](example.png)
12+
13+
The sprite component uses a custom sprite material `sprite_local_uv.material` with `local_position` and `sprite_size` as two vertex attributes. The `local_position` attribute is of semantic type "Position" and coordinate space "Local" while the `sprite_size` attribute is of semantic type "User" and will be set by the script.
14+
15+
![material](material.png)
16+
17+
The script gets the size of the sprite and sets it as the `sprite_size` vertex attribute.

material/sprite_local_uv/example.png

151 KB
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: "sprite_local_uv"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "square"
5+
data: "components {\n"
6+
" id: \"size\"\n"
7+
" component: \"/example/sprite_local_uv.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"sprite\"\n"
11+
" type: \"sprite\"\n"
12+
" data: \"default_animation: \\\"elementStone023\\\"\\n"
13+
"material: \\\"/example/sprite_local_uv.material\\\"\\n"
14+
"textures {\\n"
15+
" sampler: \\\"texture_sampler\\\"\\n"
16+
" texture: \\\"/assets/sprites.atlas\\\"\\n"
17+
"}\\n"
18+
"\"\n"
19+
"}\n"
20+
""
21+
position {
22+
x: 360.0
23+
y: 200.0
24+
}
25+
}
26+
embedded_instances {
27+
id: "rectangle"
28+
data: "components {\n"
29+
" id: \"size\"\n"
30+
" component: \"/example/sprite_local_uv.script\"\n"
31+
"}\n"
32+
"embedded_components {\n"
33+
" id: \"sprite\"\n"
34+
" type: \"sprite\"\n"
35+
" data: \"default_animation: \\\"elementStone019\\\"\\n"
36+
"material: \\\"/example/sprite_local_uv.material\\\"\\n"
37+
"size {\\n"
38+
" x: 140.0\\n"
39+
" y: 140.0\\n"
40+
"}\\n"
41+
"textures {\\n"
42+
" sampler: \\\"texture_sampler\\\"\\n"
43+
" texture: \\\"/assets/sprites.atlas\\\"\\n"
44+
"}\\n"
45+
"\"\n"
46+
"}\n"
47+
""
48+
position {
49+
x: 360.0
50+
y: 500.0
51+
}
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#version 140
2+
3+
// from sprite_local_uv.vp
4+
in mediump vec2 var_texcoord0;
5+
in highp vec2 var_position_local;
6+
7+
out vec4 out_fragColor;
8+
9+
uniform mediump sampler2D texture_sampler;
10+
uniform fs_uniforms
11+
{
12+
mediump vec4 tint;
13+
};
14+
15+
void main()
16+
{
17+
// Pre-multiply alpha since all runtime textures already are
18+
mediump vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
19+
20+
// sample color from sprite texture
21+
vec4 color = texture(texture_sampler, var_texcoord0.xy) * tint_pm;
22+
23+
// mix local position with red and green color of sprite to
24+
// create a gradient across the entire sprite
25+
out_fragColor.rg = mix(color.rg, var_position_local.xy, 0.3);
26+
// use blue and alpha from the sprite
27+
out_fragColor.b = color.b;
28+
out_fragColor.a = color.a;
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "sprite"
2+
tags: "tile"
3+
vertex_program: "/example/sprite_local_uv.vp"
4+
fragment_program: "/example/sprite_local_uv.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+
samplers {
20+
name: "texture_sampler"
21+
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
22+
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
23+
filter_min: FILTER_MODE_MIN_DEFAULT
24+
filter_mag: FILTER_MODE_MAG_DEFAULT
25+
}
26+
attributes {
27+
name: "position_local"
28+
semantic_type: SEMANTIC_TYPE_POSITION
29+
vector_type: VECTOR_TYPE_VEC2
30+
}
31+
attributes {
32+
name: "sprite_size"
33+
double_values {
34+
v: 64.0
35+
v: 64.0
36+
}
37+
vector_type: VECTOR_TYPE_VEC2
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function init(self)
2+
-- get the sprite size from the sprite component propertry 'size'
3+
local size = go.get("#sprite", "size")
4+
5+
-- set the size on the sprite material in the custom vertex attribute 'sprite_size'
6+
go.set("#sprite", "sprite_size", size)
7+
8+
-- rotate the sprite
9+
go.animate(".", "euler.z", go.PLAYBACK_LOOP_FORWARD, 360, go.EASING_LINEAR, 5)
10+
end

0 commit comments

Comments
 (0)