Skip to content

Commit ea19c17

Browse files
authored
Added custom easing example. (#169)
1 parent 55034b1 commit ea19c17

9 files changed

Lines changed: 151 additions & 0 deletions

File tree

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+
}
13.8 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
images {
2+
image: "/assets/images/logo-color.png"
3+
}
4+
extrude_borders: 2

animation/custom_easing/example.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
tags: animation, easing
3+
title: Custom easing - square wave
4+
brief: This example shows how to define a custom easing curve and use it when animating with `go.animate()` instead of a built-in easing constant.
5+
author: Defold Foundation
6+
scripts: custom_easing.script
7+
---
8+
9+
This example uses the square-wave easing curve. The logo alternates between its starting height and the target height, and the animation system interpolates the positions between the low and high positions when animating.
10+
11+
## Setup
12+
13+
The collection contains one game object with one sprite and one script. The script animates only the `position.y` property with the custom easing.
14+
15+
![setup](setup.png)
16+
17+
## How It Works
18+
19+
The script stores the custom easing samples in a plain Lua table and turns that table into a `vmath.vector`. Defold accepts that vector anywhere `go.animate()` (or `gui.animate()` in case of GUI animations) expects an easing value.
20+
21+
Because the vector is made of repeated blocks of `0` and `1`, the animation keeps snapping between the start value (start y position) and the target value (520 on Y axis, which is above the start value). That makes the easing behave like a square wave instead of a continuous curve. Note that the animation system still interpolates between the frames, so depending on time, the animation can be more trapezoidal or more square wave.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: "default"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "logo"
5+
data: "components {\n"
6+
" id: \"script\"\n"
7+
" component: \"/example/custom_easing.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"sprite\"\n"
11+
" type: \"sprite\"\n"
12+
" data: \"default_animation: \\\"logo-color\\\"\\n"
13+
"material: \\\"/builtins/materials/sprite.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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local VALUES = { -- <1>
2+
0, 0, 0, 0, 0, 0,
3+
1, 1, 1, 1, 1, 1,
4+
1, 1, 1, 1, 1, 1,
5+
0, 0, 0, 0, 0, 0,
6+
}
7+
local SQUARE_EASING = vmath.vector(VALUES) -- <2>
8+
9+
function init(self)
10+
go.animate(".", "position.y", go.PLAYBACK_LOOP_FORWARD, 520, SQUARE_EASING, 4.0) -- <3>
11+
end
12+
13+
--[[
14+
1. This table defines a custom easing curve with normalized samples between 0 and 1. Repeating blocks of zeroes and ones create a square-wave pattern.
15+
2. Convert the Lua table into a `vmath.vector`, which `go.animate()` accepts as a custom easing value.
16+
3. Animate the current game object's `position.y` sub-property. The custom curve makes the sprite move between its starting y position and 520 instead with interpolated in-between positions.
17+
]]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[project]
2+
title = Custom Easing - Square Wave
3+
version = 0.1
4+
developer = Defold Foundation
5+
6+
[bootstrap]
7+
main_collection = /example/custom_easing.collectionc
8+
9+
[input]
10+
game_binding = /builtins/input/all.input_bindingc
11+
repeat_interval = 0.05
12+
13+
[display]
14+
width = 720
15+
height = 720
16+
high_dpi = 1
17+
18+
[physics]
19+
scale = 0.02
20+
gravity_y = -500.0
21+
22+
[script]
23+
shared_state = 1
24+
25+
[collection_proxy]
26+
max_count = 256
27+
28+
[label]
29+
subpixels = 1
30+
31+
[sprite]
32+
subpixels = 1
33+
max_count = 32765
34+
35+
[windows]
36+
iap_provider =
37+
38+
[android]
39+
package = com.defold.examples
40+
41+
[ios]
42+
bundle_identifier = com.defold.examples
43+
44+
[osx]
45+
bundle_identifier = com.defold.examples
46+
47+
[html5]
48+
show_fullscreen_button = 0
49+
show_made_with_defold = 0
50+
scale_mode = no_scale
51+
heap_size = 64
52+
53+
[graphics]
54+
texture_profiles = /all.texture_profiles
55+
56+
[collection]
57+
max_instances = 32765
58+
59+
[particle_fx]
60+
max_emitter_count = 1024
61+
62+
[render]
63+
clear_color_blue = 0.183594
64+
clear_color_green = 0.164063
65+
clear_color_red = 0.160156
66+

animation/custom_easing/setup.png

83.9 KB
Loading
24.8 KB
Loading

0 commit comments

Comments
 (0)