Skip to content

Commit 6b87009

Browse files
committed
Merge branch 'master' of https://github.com/defold/examples
2 parents ae9bfd1 + e8cb145 commit 6b87009

14 files changed

+376
-0
lines changed
638 Bytes
Loading
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
images {
2+
image: "/assets/Alchemy_vial.png"
3+
}
4+
extrude_borders: 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
font: "/assets/SourceSansPro-Semibold.ttf"
2+
material: "/builtins/fonts/font.material"
3+
size: 22
4+
characters: " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

material/custom_sprite/example.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
name: CustomSprite
3+
tags: material
4+
title: Custom Sprite
5+
brief: This example demonstrates a simple way to create and apply a custom sprite shader for changing colors and customizing an outline.
6+
scripts: set_color.script, recolor.fp, recolor.vp
7+
---
8+
9+
If your game requires a sprite that can be recolored and reused, a custom shader will be needed. Your sprite's artwork can be composed in such a way that will help achieve things you may want to do. For example an outline around your sprite that can be turned off/on and color changed. When creating your artwork if your sprite's green-channel is shifted slightly below 1.0 and you add an outline around your sprite with full green color equal to 1.0 then in the shader you can manage the green channel values that match 1.0 and change the color or completely hide these values thus removing the outline altogether. Recoloring sprites to be used throughout a game is pretty common. One way to achieve re-coloring with a range of values instead of a single color is to de-saturate a part of the sprite you want to recolor. When you de-saturate an image it will even out the red, green and blue channel values to a grey-scale. You can then check in the shader for these grey-scale values and change the colors. To check for these values you can add 2 or 3 channels together as a float value and then with another float multiply a single channel by 2 or 3, we then compare these values when valid use a new color.
10+
11+
In the example the custom sprite material has 2 vertex attributes each is a vector 4 of float values. The values are used for coloring the fluid and the outline from a script to the shader. The script has a function for creating a random color and also sets the color vertex properties
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
script: "/example/options.gui_script"
2+
fonts {
3+
name: "default"
4+
font: "/assets/text48.font"
5+
}
6+
nodes {
7+
position {
8+
x: 248.0
9+
y: 230.0
10+
}
11+
size {
12+
x: 100.0
13+
y: 60.0
14+
}
15+
color {
16+
x: 0.611
17+
y: 0.487
18+
z: 0.365
19+
}
20+
type: TYPE_BOX
21+
id: "io"
22+
inherit_alpha: true
23+
size_mode: SIZE_MODE_AUTO
24+
}
25+
nodes {
26+
position {
27+
x: -2.0
28+
}
29+
size {
30+
x: 40.0
31+
y: 30.0
32+
}
33+
type: TYPE_TEXT
34+
text: "Outline\n"
35+
"(Off)"
36+
font: "default"
37+
id: "text"
38+
line_break: true
39+
parent: "io"
40+
inherit_alpha: true
41+
}
42+
nodes {
43+
position {
44+
x: 360.0
45+
y: 230.0
46+
}
47+
size {
48+
x: 100.0
49+
y: 60.0
50+
}
51+
color {
52+
x: 0.611
53+
y: 0.487
54+
z: 0.365
55+
}
56+
type: TYPE_BOX
57+
id: "OutlineColor"
58+
inherit_alpha: true
59+
size_mode: SIZE_MODE_AUTO
60+
}
61+
nodes {
62+
position {
63+
x: -2.0
64+
}
65+
size {
66+
x: 40.0
67+
y: 20.0
68+
}
69+
type: TYPE_TEXT
70+
text: "Outline\n"
71+
"Color"
72+
font: "default"
73+
id: "text1"
74+
parent: "OutlineColor"
75+
inherit_alpha: true
76+
}
77+
nodes {
78+
position {
79+
x: 470.0
80+
y: 230.0
81+
}
82+
size {
83+
x: 100.0
84+
y: 60.0
85+
}
86+
color {
87+
x: 0.611
88+
y: 0.487
89+
z: 0.365
90+
}
91+
type: TYPE_BOX
92+
id: "Fluid"
93+
inherit_alpha: true
94+
size_mode: SIZE_MODE_AUTO
95+
}
96+
nodes {
97+
size {
98+
x: 40.0
99+
y: 20.0
100+
}
101+
type: TYPE_TEXT
102+
text: "Fluid\n"
103+
"Color"
104+
font: "default"
105+
id: "text2"
106+
parent: "Fluid"
107+
inherit_alpha: true
108+
}
109+
material: "/builtins/materials/gui.material"
110+
adjust_reference: ADJUST_REFERENCE_PARENT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: "main"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "new"
5+
data: "components {\n"
6+
" id: \"set_color\"\n"
7+
" component: \"/example/set_color.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"sprite\"\n"
11+
" type: \"sprite\"\n"
12+
" data: \"default_animation: \\\"Alchemy_vial\\\"\\n"
13+
"material: \\\"/example/recolor.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: 360.0
24+
}
25+
}
26+
embedded_instances {
27+
id: "ui"
28+
data: "components {\n"
29+
" id: \"buttons\"\n"
30+
" component: \"/example/buttons.gui\"\n"
31+
"}\n"
32+
""
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
local script_url = "/new#set_color"
2+
3+
function init(self)
4+
msg.post(".", "acquire_input_focus")
5+
6+
self.is_on = false
7+
self.io_text = gui.get_node("text")
8+
self.on_off = gui.get_node("io")
9+
self.oColor = gui.get_node("OutlineColor")
10+
self.fluid = gui.get_node("Fluid")
11+
12+
end
13+
14+
function on_input(self, action_id, action)
15+
if action_id == hash("touch") and action.pressed then
16+
17+
if gui.pick_node(self.on_off, action.x, action.y) then
18+
19+
msg.post(script_url, "outline_io")
20+
if self.is_on then
21+
gui.set_text(self.io_text,"Outline (Off)")
22+
self.is_on = not self.is_on
23+
else
24+
gui.set_text(self.io_text,"Outline (On)")
25+
self.is_on = not self.is_on
26+
end
27+
28+
elseif gui.pick_node(self.oColor, action.x, action.y) then
29+
30+
msg.post(script_url, "outline_color")
31+
32+
elseif gui.pick_node(self.fluid, action.x, action.y) then
33+
34+
msg.post(script_url, "fluid_color")
35+
36+
end
37+
end
38+
end
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#version 140
2+
3+
uniform sampler2D texture_sampler;
4+
uniform f_uniform
5+
{
6+
vec4 tint;
7+
};
8+
9+
in vec2 var_texcoord0;
10+
// custom vertex attributes
11+
in vec4 new_color;
12+
in vec4 new_outline;
13+
14+
out vec4 final_color;
15+
16+
void main()
17+
{
18+
lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
19+
lowp vec4 sprite = texture(texture_sampler, var_texcoord0.xy);
20+
21+
// float values used for comparing
22+
lowp float combine = (sprite.r + sprite.g);
23+
lowp float greenmul = sprite.g * 2;
24+
25+
// when 2 channels added together equal the same as a single channel multipled then we have desaturated values
26+
if(combine == greenmul){
27+
sprite = vec4(sprite.rgb*new_color.rgb,sprite.a);
28+
}
29+
30+
// when the green channel has a value of 1.0 and the w value is 1.0(on) then we color the outline
31+
if(new_outline.w >= 1.0 && sprite.g >= 1.0){
32+
sprite = vec4(new_outline.rgb,1.0);
33+
}
34+
else if (sprite.g >= 1.0){ //when the w value is not 1.0 we remove all values. turning the outline off
35+
sprite = vec4(0.0, 0.0, 0.0, 0.0);
36+
}
37+
38+
final_color = vec4(sprite * tint);
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: "sprite"
2+
tags: "tile"
3+
vertex_program: "/example/recolor.vp"
4+
fragment_program: "/example/recolor.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: "newcolor"
28+
semantic_type: SEMANTIC_TYPE_COLOR
29+
double_values {
30+
v: 0.3882
31+
v: 0.6078
32+
v: 1.0
33+
v: 1.0
34+
}
35+
}
36+
attributes {
37+
name: "outline"
38+
double_values {
39+
v: 0.5
40+
v: 0.5
41+
v: 0.5
42+
v: 0.0
43+
}
44+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#version 140
2+
3+
uniform v_inputs
4+
{
5+
mat4 view_proj;
6+
};
7+
// positions are in world space
8+
in vec4 position;
9+
in vec2 texcoord0;
10+
// custom vertex attributes from material
11+
in vec4 newcolor;
12+
in vec4 outline;
13+
14+
out vec2 var_texcoord0;
15+
// custom vertex attributes sent to fragment program
16+
out vec4 new_color;
17+
out vec4 new_outline;
18+
19+
void main()
20+
{
21+
gl_Position = view_proj * vec4(position.xyz, 1.0);
22+
var_texcoord0 = texcoord0;
23+
new_color = newcolor;
24+
new_outline = outline;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local sprite_to_color = "/new#sprite" local brightness = 0.3
2+
3+
local function random_color(self) -- create a new_color of random-ish float values (0.3 or 1.3)
4+
local random_number_r = math.random(0, 1)+brightness
5+
local random_number_b = math.random(0, 1)+brightness
6+
local random_number_g = math.random(0, 1)+brightness
7+
local new_color = vmath.vector4(random_number_r, random_number_g, random_number_b, self.outline_io)
8+
return new_color
9+
end
10+
11+
function init(self)
12+
msg.post("@render:", "clear_color", { color = vmath.vector4(0.25960784,0.2315686274509804,0.229607843, 1.0) } )
13+
14+
self.outline_io = 0.0 -- float is used when setting the w value of the material vertex attribute "outline" 0.0 = off 1.0 = on
15+
16+
math.randomseed(socket.gettime()*10000)
17+
18+
end
19+
20+
function on_message(self, message_id, message)
21+
22+
if message_id == hash("outline_io") then
23+
24+
if self.outline_io <= 0.0 then
25+
self.outline_io = 1.0
26+
else
27+
self.outline_io = 0.0
28+
end
29+
go.set(sprite_to_color, "outline.w", self.outline_io)
30+
31+
elseif message_id == hash("outline_color") then
32+
33+
go.set(sprite_to_color, "outline", random_color(self)) -- set color for outline
34+
35+
elseif message_id == hash("fluid_color") then
36+
37+
go.set(sprite_to_color, "newcolor", random_color(self)) -- set color for potion fluid
38+
39+
end
40+
41+
end

material/custom_sprite/game.project

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[bootstrap]
2+
main_collection = /example/custom_sprite.collectionc
3+
4+
[script]
5+
shared_state = 1
6+
7+
[display]
8+
width = 720
9+
height = 720
10+
11+
[android]
12+
input_method = HiddenInputField
13+
14+
[html5]
15+
scale_mode = stretch
16+
17+
[project]
18+
title = custom_sprite_shader
19+
20+
[graphics]
21+
default_texture_min_filter = nearest
22+
default_texture_mag_filter = nearest
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mouse_trigger {
2+
input: MOUSE_BUTTON_1
3+
action: "touch"
4+
}

0 commit comments

Comments
 (0)