Skip to content

Commit

Permalink
Added sprite material vertex color example
Browse files Browse the repository at this point in the history
  • Loading branch information
JCash committed Mar 1, 2024
1 parent e6fab5e commit e9a2196
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 11 deletions.
18 changes: 18 additions & 0 deletions examples/_main/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,21 @@ embedded_components {
w: 1.0
}
}
embedded_components {
id: "material/vertexcolor"
type: "collectionproxy"
data: "collection: \"/examples/material/vertexcolor/vertexcolor.collection\"\n"
"exclude: false\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
}
1 change: 1 addition & 0 deletions examples/_main/menu.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function init(self)
self.index["animation"] = { "spinner", "flipbook", "chained_tween", "basic_tween", "spine", "cursor", "easing" }
self.index["gui"] = { "button", "stencil", "load_texture", "progress", "pointer_over", "color", "slice9" }
self.index["input"] = { "move", "text", "down_duration", "mouse_and_touch" }
self.index["material"] = { "vertexcolor" }
self.index["particles"] = { "particlefx", "modifiers", "fire_and_smoke" }
self.index["sound"] = { "music", "fade_in_out", "panning" }
self.index["render"] = { "camera", "screen_to_world" }
Expand Down
23 changes: 23 additions & 0 deletions examples/material/vertexcolor/defold-logo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
embedded_components {
id: "sprite"
type: "sprite"
data: "default_animation: \"defold-logo\"\n"
"material: \"/examples/material/vertexcolor/vertexcolor.material\"\n"
"blend_mode: BLEND_MODE_ALPHA\n"
"textures {\n"
" sampler: \"texture_sampler\"\n"
" texture: \"/examples/material/vertexcolor/images.atlas\"\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
}
Binary file added examples/material/vertexcolor/defold-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions examples/material/vertexcolor/images.atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
images {
image: "/examples/material/vertexcolor/defold-logo.png"
sprite_trim_mode: SPRITE_TRIM_MODE_OFF
}
margin: 0
extrude_borders: 0
inner_padding: 0
max_page_width: 0
max_page_height: 0
rename_patterns: ""
58 changes: 58 additions & 0 deletions examples/material/vertexcolor/vertexcolor.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: "vertexcolor"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"main\"\n"
" component: \"/examples/material/vertexcolor/vertexcolor.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" property_decls {\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"factory\"\n"
" type: \"factory\"\n"
" data: \"prototype: \\\"/examples/material/vertexcolor/defold-logo.go\\\"\\n"
"load_dynamically: false\\n"
"dynamic_prototype: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
12 changes: 12 additions & 0 deletions examples/material/vertexcolor/vertexcolor.fp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
varying mediump vec2 var_texcoord0;
varying mediump vec4 var_mycolor;

uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;

void main()
{
// Pre-multiply alpha since all runtime textures already are
lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
gl_FragColor = texture2D(texture_sampler, var_texcoord0.xy) * tint_pm * var_mycolor;
}
42 changes: 42 additions & 0 deletions examples/material/vertexcolor/vertexcolor.material
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "sprite"
tags: "tile"
vertex_program: "/examples/material/vertexcolor/vertexcolor.vp"
fragment_program: "/examples/material/vertexcolor/vertexcolor.fp"
vertex_space: VERTEX_SPACE_WORLD
vertex_constants {
name: "view_proj"
type: CONSTANT_TYPE_VIEWPROJ
}
fragment_constants {
name: "tint"
type: CONSTANT_TYPE_USER
value {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
}
samplers {
name: "texture_sampler"
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
filter_min: FILTER_MODE_MIN_DEFAULT
filter_mag: FILTER_MODE_MAG_DEFAULT
max_anisotropy: 1.0
}
max_page_count: 0
attributes {
name: "mycolor"
semantic_type: SEMANTIC_TYPE_NONE
element_count: 4
normalize: false
data_type: TYPE_FLOAT
coordinate_space: COORDINATE_SPACE_LOCAL
double_values {
v: 1.0
v: 1.0
v: 1.0
v: 1.0
}
}
Empty file.
32 changes: 32 additions & 0 deletions examples/material/vertexcolor/vertexcolor.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function init(self)
msg.post(".", "acquire_input_focus")

local scale = 0.75
local spacingx = 160 * scale + 10
local spacingy = 190 * scale + 10
local startx = 40 + spacingx*0.5
local starty = 40 + spacingy*0.5

local maxy = 3
local maxx = 4

self.urls = {}
for y = 0, maxy do
for x = 0, maxx do
local p = vmath.vector3(startx + x*spacingx, starty + y*spacingy, 0.5)
local id = factory.create("#factory", p, nil, nil, vmath.vector3(0.8, 0.8, 1))
local url = msg.url(nil, id, "sprite")
table.insert(self.urls, url)

go.set(url, "mycolor", vmath.vector4(x/maxx, y/maxy, 0, 1))
end
end
end

function on_input(self, action_id, action)
if action_id == hash("touch") and action.released then
for _, url in ipairs(self.urls) do
-- crash: go.animate(url, "mycolor", go.PLAYBACK_ONCE_PINGPONG, vmath.vector4(0, 0, 1, 1), go.EASING_OUTELASTIC, 1)
end
end
end
16 changes: 16 additions & 0 deletions examples/material/vertexcolor/vertexcolor.vp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
uniform highp mat4 view_proj;

// positions are in world space
attribute highp vec4 position;
attribute mediump vec2 texcoord0;
attribute mediump vec4 mycolor;

varying mediump vec2 var_texcoord0;
varying mediump vec4 var_mycolor;

void main()
{
gl_Position = view_proj * vec4(position.xyz, 1.0);
var_texcoord0 = texcoord0;
var_mycolor = mycolor;
}
30 changes: 21 additions & 9 deletions examples/resource/modify_atlas/modify_atlas.collection
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ embedded_instances {
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/resource/modify_atlas/modify_atlas.atlas\\\"\\n"
"default_animation: \\\"shipGreen_manned\\\"\\n"
" data: \"default_animation: \\\"shipGreen_manned\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"textures {\\n"
" sampler: \\\"texture_sampler\\\"\\n"
" texture: \\\"/examples/resource/modify_atlas/modify_atlas.atlas\\\"\\n"
"}\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
Expand Down Expand Up @@ -45,10 +48,13 @@ embedded_instances {
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/resource/modify_atlas/modify_atlas.atlas\\\"\\n"
"default_animation: \\\"shipPink_manned\\\"\\n"
" data: \"default_animation: \\\"shipPink_manned\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"textures {\\n"
" sampler: \\\"texture_sampler\\\"\\n"
" texture: \\\"/examples/resource/modify_atlas/modify_atlas.atlas\\\"\\n"
"}\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
Expand Down Expand Up @@ -132,7 +138,7 @@ embedded_instances {
"blend_mode: BLEND_MODE_ALPHA\\n"
"line_break: false\\n"
"text: \\\"CLICK TO REPLACE PINK WITH YELLOW\\\"\\n"
"font: \\\"/builtins/fonts/system_font.font\\\"\\n"
"font: \\\"/builtins/fonts/debug/always_on_top.font\\\"\\n"
"material: \\\"/builtins/fonts/label.material\\\"\\n"
"\"\n"
" position {\n"
Expand Down Expand Up @@ -170,10 +176,13 @@ embedded_instances {
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/resource/modify_atlas/modify_atlas.atlas\\\"\\n"
"default_animation: \\\"shipGreen_manned\\\"\\n"
" data: \"default_animation: \\\"shipGreen_manned\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"textures {\\n"
" sampler: \\\"texture_sampler\\\"\\n"
" texture: \\\"/examples/resource/modify_atlas/modify_atlas.atlas\\\"\\n"
"}\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
Expand Down Expand Up @@ -210,10 +219,13 @@ embedded_instances {
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/resource/modify_atlas/modify_atlas.atlas\\\"\\n"
"default_animation: \\\"shipPink_manned\\\"\\n"
" data: \"default_animation: \\\"shipPink_manned\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"textures {\\n"
" sampler: \\\"texture_sampler\\\"\\n"
" texture: \\\"/examples/resource/modify_atlas/modify_atlas.atlas\\\"\\n"
"}\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
Expand Down
2 changes: 1 addition & 1 deletion examples/sprite/bunnymark/bunnymark.collection
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ embedded_instances {
"blend_mode: BLEND_MODE_ALPHA\\n"
"line_break: false\\n"
"text: \\\"Label\\\"\\n"
"font: \\\"/builtins/fonts/system_font.font\\\"\\n"
"font: \\\"/builtins/fonts/debug/always_on_top.font\\\"\\n"
"material: \\\"/builtins/fonts/label.material\\\"\\n"
"\"\n"
" position {\n"
Expand Down
2 changes: 1 addition & 1 deletion game.project
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title = Defold-examples
version = 0.1
custom_resources = examples/resource/modify_atlas/resources
dependencies#0 = https://github.com/defold/extension-spine/archive/refs/tags/2.13.0.zip
dependencies#0 = https://github.com/defold/extension-spine/archive/refs/tags/2.14.5.zip

[bootstrap]
main_collection = /examples/main.collectionc
Expand Down

0 comments on commit e9a2196

Please sign in to comment.